Updated C2NRthsController Class to build new devices in PreActivate Method

Addresses #292
This commit is contained in:
Trevor Payne
2020-06-30 14:48:31 -05:00
parent 495bf70d3a
commit f954043981

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using Crestron.SimplSharpPro; using Crestron.SimplSharpPro;
using Crestron.SimplSharpPro.DeviceSupport; using Crestron.SimplSharpPro.DeviceSupport;
using Crestron.SimplSharpPro.GeneralIO; using Crestron.SimplSharpPro.GeneralIO;
@@ -11,22 +12,38 @@ using PepperDash.Essentials.Core.Config;
namespace PepperDash.Essentials.Core.CrestronIO namespace PepperDash.Essentials.Core.CrestronIO
{ {
[Description("Wrapper class for the C2N-RTHS sensor")] [Description("Wrapper class for the C2N-RTHS sensor")]
public class C2nRthsController : CrestronGenericBridgeableBaseDevice public class C2nRthsController : CrestronGenericBridgeableBaseDevice, IOnline
{ {
private readonly C2nRths _device; private C2nRths _device;
public IntFeedback TemperatureFeedback { get; private set; } public IntFeedback TemperatureFeedback { get; private set; }
public IntFeedback HumidityFeedback { get; private set; } public IntFeedback HumidityFeedback { get; private set; }
public BoolFeedback IsOnline { get; private set; }
public C2nRthsController(string key, string name, GenericBase hardware) public C2nRthsController(string key, Func<DeviceConfig, C2nRths> preActivationFunc,
: base(key, name, hardware) DeviceConfig config)
: base(key, config.Name)
{ {
_device = hardware as C2nRths;
TemperatureFeedback = new IntFeedback(() => _device.TemperatureFeedback.UShortValue); AddPreActivationAction(() =>
HumidityFeedback = new IntFeedback(() => _device.HumidityFeedback.UShortValue); {
_device = preActivationFunc(config);
if (_device != null) _device.BaseEvent += DeviceOnBaseEvent; RegisterCrestronGenericBase(_device);
TemperatureFeedback = new IntFeedback(() => _device.TemperatureFeedback.UShortValue);
HumidityFeedback = new IntFeedback(() => _device.HumidityFeedback.UShortValue);
IsOnline = new BoolFeedback(() => _device.IsOnline);
if (_device != null) _device.BaseEvent += DeviceOnBaseEvent;
if (_device != null) _device.OnlineStatusChange += _device_OnlineStatusChange;
});
}
void _device_OnlineStatusChange(GenericBase currentDevice, OnlineOfflineEventArgs args)
{
IsOnline.FireUpdate();
} }
private void DeviceOnBaseEvent(GenericBase device, BaseEventArgs args) private void DeviceOnBaseEvent(GenericBase device, BaseEventArgs args)
@@ -77,24 +94,66 @@ namespace PepperDash.Essentials.Core.CrestronIO
HumidityFeedback.LinkInputSig(trilist.UShortInput[joinMap.Humidity.JoinNumber]); HumidityFeedback.LinkInputSig(trilist.UShortInput[joinMap.Humidity.JoinNumber]);
trilist.StringInput[joinMap.Name.JoinNumber].StringValue = Name; trilist.StringInput[joinMap.Name.JoinNumber].StringValue = Name;
}
}
public class C2nRthsControllerFactory : EssentialsDeviceFactory<C2nRthsController> trilist.OnlineStatusChange += (d, args) =>
{ {
public C2nRthsControllerFactory() if (!args.DeviceOnLine) return;
{
TypeNames = new List<string>() { "c2nrths" }; UpdateFeedbacksWhenOnline();
trilist.StringInput[joinMap.Name.JoinNumber].StringValue = Name;
};
} }
public override EssentialsDevice BuildDevice(DeviceConfig dc) private void UpdateFeedbacksWhenOnline()
{ {
Debug.Console(1, "Factory Attempting to create new C2N-RTHS Device"); IsOnline.FireUpdate();
TemperatureFeedback.FireUpdate();
HumidityFeedback.FireUpdate();
}
#region PreActivation
private static C2nRths GetC2nRthsDevice(DeviceConfig dc)
{
var control = CommFactory.GetControlPropertiesConfig(dc); var control = CommFactory.GetControlPropertiesConfig(dc);
var cresnetId = control.CresnetIdInt; var cresnetId = control.CresnetIdInt;
var branchId = control.ControlPortNumber;
var parentKey = string.IsNullOrEmpty(control.ControlPortDevKey) ? "processor" : control.ControlPortDevKey;
return new C2nRthsController(dc.Key, dc.Name, new C2nRths(cresnetId, Global.ControlSystem)); if (parentKey.Equals("processor", StringComparison.CurrentCultureIgnoreCase))
{
Debug.Console(0, "Device {0} is a valid cresnet master - creating new C2nRths");
return new C2nRths(cresnetId, Global.ControlSystem);
}
var cresnetBridge = DeviceManager.GetDeviceForKey(parentKey) as ICresnetBridge;
if (cresnetBridge != null)
{
Debug.Console(0, "Device {0} is a valid cresnet master - creating new C2nRths");
return new C2nRths(cresnetId, cresnetBridge.Branches[branchId]);
}
Debug.Console(0, "Device {0} is not a valid cresnet master", branchId);
return null;
}
#endregion
public class C2nRthsControllerFactory : EssentialsDeviceFactory<C2nRthsController>
{
public C2nRthsControllerFactory()
{
TypeNames = new List<string>() { "c2nrths" };
}
public override EssentialsDevice BuildDevice(DeviceConfig dc)
{
Debug.Console(1, "Factory Attempting to create new C2N-RTHS Device");
var control = CommFactory.GetControlPropertiesConfig(dc);
var cresnetId = control.CresnetIdInt;
return new C2nRthsController(dc.Key, GetC2nRthsDevice, dc);
}
} }
} }
} }