Merge branch 'development' into release/1.5.7

This commit is contained in:
Andrew Welker
2020-07-10 08:35:26 -07:00
committed by GitHub

View File

@@ -1,310 +1,317 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Crestron.SimplSharp; using Crestron.SimplSharp;
using Crestron.SimplSharpPro; using Crestron.SimplSharpPro;
using Crestron.SimplSharpPro.Gateways; using Crestron.SimplSharpPro.Gateways;
using Crestron.SimplSharpPro.Remotes; using Crestron.SimplSharpPro.Remotes;
using Crestron.SimplSharpPro.DeviceSupport; using Crestron.SimplSharpPro.DeviceSupport;
using Crestron.SimplSharp.Reflection; using Crestron.SimplSharp.Reflection;
using Newtonsoft.Json; using Newtonsoft.Json;
using PepperDash.Core; using PepperDash.Core;
using PepperDash.Essentials.Core; using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Config; using PepperDash.Essentials.Core.Config;
using PepperDash.Essentials.Core.Bridges; using PepperDash.Essentials.Core.Bridges;
namespace PepperDash.Essentials.Core namespace PepperDash.Essentials.Core
{ {
[Description("Wrapper class for all HR-Series remotes")] [Description("Wrapper class for all HR-Series remotes")]
public class Hrxx0WirelessRemoteController : EssentialsBridgeableDevice, IHasFeedback public class Hrxx0WirelessRemoteController : EssentialsBridgeableDevice, IHasFeedback
{ {
private CenRfgwController _gateway; private CenRfgwController _gateway;
private Hr1x0WirelessRemoteBase _remote; private GatewayBase _gatewayBase;
public FeedbackCollection<Feedback> Feedbacks { get; set; }
private Hr1x0WirelessRemoteBase _remote;
public CrestronCollection<Button> Buttons { get { return _remote.Button; } }
public FeedbackCollection<Feedback> Feedbacks { get; set; }
private DeviceConfig _config;
public CrestronCollection<Button> Buttons { get { return _remote.Button; } }
public Hrxx0WirelessRemoteController(string key, Func<DeviceConfig, Hr1x0WirelessRemoteBase> preActivationFunc,
DeviceConfig config) private DeviceConfig _config;
: base(key, config.Name)
{ public Hrxx0WirelessRemoteController(string key, Func<DeviceConfig, Hr1x0WirelessRemoteBase> preActivationFunc,
Feedbacks = new FeedbackCollection<Feedback>(); DeviceConfig config)
: base(key, config.Name)
var props = JsonConvert.DeserializeObject<CrestronRemotePropertiesConfig>(config.Properties.ToString()); {
Feedbacks = new FeedbackCollection<Feedback>();
var type = config.Type;
var rfId = (uint)props.Control.InfinetIdInt; var props = JsonConvert.DeserializeObject<CrestronRemotePropertiesConfig>(config.Properties.ToString());
_config = config;
var type = config.Type;
GatewayBase gateway; var rfId = (uint)props.Control.InfinetIdInt;
_config = config;
if (props.GatewayDeviceKey == "processor")
{ if (props.GatewayDeviceKey == "processor")
gateway = Global.ControlSystem.ControllerRFGatewayDevice; {
} {
AddPreActivationAction(() =>
else {
{ _remote = preActivationFunc(config);
var gatewayDev = DeviceManager.GetDeviceForKey(props.GatewayDeviceKey) as CenRfgwController; RegisterEvents();
if (gatewayDev == null) });
{
Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is not a valid device", props.GatewayDeviceKey); return;
} }
if (gatewayDev != null) }
{
Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is a valid device", props.GatewayDeviceKey);
gateway = gatewayDev.GateWay; var gatewayDev = DeviceManager.GetDeviceForKey(props.GatewayDeviceKey) as CenRfgwController;
_gateway = gatewayDev; if (gatewayDev == null)
} {
} Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is not a valid device", props.GatewayDeviceKey);
}
if (_gateway == null) return; if (gatewayDev != null)
{
_gateway.IsReadyEvent += _gateway_IsReadyEvent; Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is a valid device", props.GatewayDeviceKey);
if (_gateway.IsReady) _gateway = gatewayDev;
{ }
AddPreActivationAction(() =>
{
_remote = preActivationFunc(config); if (_gateway == null) return;
RegisterEvents(); _gateway.IsReadyEvent += _gateway_IsReadyEvent;
}); if (_gateway.IsReady)
} {
} AddPreActivationAction(() =>
{
void _gateway_IsReadyEvent(object sender, PepperDash_Essentials_Core.IsReadyEventArgs e) _remote = preActivationFunc(config);
{
if (e.IsReady != true) return; RegisterEvents();
_remote = GetHr1x0WirelessRemote(_config); });
}
RegisterEvents(); }
}
void _gateway_IsReadyEvent(object sender, PepperDash_Essentials_Core.IsReadyEventArgs e)
void _remote_BaseEvent(GenericBase device, BaseEventArgs args) {
{ if (e.IsReady != true) return;
if(args.EventId == Hr1x0EventIds.BatteryCriticalFeedbackEventId) _remote = GetHr1x0WirelessRemote(_config);
Feedbacks["BatteryCritical"].FireUpdate();
if(args.EventId == Hr1x0EventIds.BatteryLowFeedbackEventId) RegisterEvents();
Feedbacks["BatteryLow"].FireUpdate(); }
if(args.EventId == Hr1x0EventIds.BatteryVoltageFeedbackEventId)
Feedbacks["BatteryVoltage"].FireUpdate(); void _remote_BaseEvent(GenericBase device, BaseEventArgs args)
} {
if(args.EventId == Hr1x0EventIds.BatteryCriticalFeedbackEventId)
private void RegisterEvents() Feedbacks["BatteryCritical"].FireUpdate();
{ if(args.EventId == Hr1x0EventIds.BatteryLowFeedbackEventId)
_remote.ButtonStateChange += _remote_ButtonStateChange; Feedbacks["BatteryLow"].FireUpdate();
if(args.EventId == Hr1x0EventIds.BatteryVoltageFeedbackEventId)
Feedbacks.Add(new BoolFeedback("BatteryCritical", () => _remote.BatteryCriticalFeedback.BoolValue)); Feedbacks["BatteryVoltage"].FireUpdate();
Feedbacks.Add(new BoolFeedback("BatteryLow", () => _remote.BatteryLowFeedback.BoolValue)); }
Feedbacks.Add(new IntFeedback("BatteryVoltage", () => _remote.BatteryVoltageFeedback.UShortValue));
private void RegisterEvents()
_remote.BaseEvent += _remote_BaseEvent; {
} _remote.ButtonStateChange += _remote_ButtonStateChange;
void _remote_ButtonStateChange(GenericBase device, ButtonEventArgs args) Feedbacks.Add(new BoolFeedback("BatteryCritical", () => _remote.BatteryCriticalFeedback.BoolValue));
{ Feedbacks.Add(new BoolFeedback("BatteryLow", () => _remote.BatteryLowFeedback.BoolValue));
try Feedbacks.Add(new IntFeedback("BatteryVoltage", () => _remote.BatteryVoltageFeedback.UShortValue));
{
var handler = args.Button.UserObject; _remote.BaseEvent += _remote_BaseEvent;
}
if (handler == null) return;
void _remote_ButtonStateChange(GenericBase device, ButtonEventArgs args)
Debug.Console(1, this, "Executing Action: {0}", handler.ToString()); {
try
if (handler is Action<bool>) {
{ var handler = args.Button.UserObject;
(handler as Action<bool>)(args.Button.State == eButtonState.Pressed ? true : false);
} if (handler == null) return;
}
catch (Exception e) Debug.Console(1, this, "Executing Action: {0}", handler.ToString());
{
Debug.Console(2, this, "Error in ButtonStateChange handler: {0}", e); if (handler is Action<bool>)
} {
} (handler as Action<bool>)(args.Button.State == eButtonState.Pressed ? true : false);
}
}
#region Preactivation catch (Exception e)
{
private static Hr1x0WirelessRemoteBase GetHr1x0WirelessRemote(DeviceConfig config) Debug.Console(2, this, "Error in ButtonStateChange handler: {0}", e);
{ }
var props = JsonConvert.DeserializeObject<CrestronRemotePropertiesConfig>(config.Properties.ToString()); }
var type = config.Type;
var rfId = (uint)props.Control.InfinetIdInt; #region Preactivation
GatewayBase gateway; private static Hr1x0WirelessRemoteBase GetHr1x0WirelessRemote(DeviceConfig config)
{
if (props.GatewayDeviceKey == "processor") var props = JsonConvert.DeserializeObject<CrestronRemotePropertiesConfig>(config.Properties.ToString());
{
gateway = Global.ControlSystem.ControllerRFGatewayDevice; var type = config.Type;
} var rfId = (uint)props.Control.InfinetIdInt;
else
{ GatewayBase gateway;
var gatewayDev = DeviceManager.GetDeviceForKey(props.GatewayDeviceKey) as CenRfgwController;
if (gatewayDev == null) if (props.GatewayDeviceKey == "processor")
{ {
Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is not a valid device", props.GatewayDeviceKey); gateway = Global.ControlSystem.ControllerRFGatewayDevice;
return null; }
} else
Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is a valid device", props.GatewayDeviceKey); {
gateway = gatewayDev.GateWay; var gatewayDev = DeviceManager.GetDeviceForKey(props.GatewayDeviceKey) as CenRfgwController;
} if (gatewayDev == null)
{
if (gateway == null) Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is not a valid device", props.GatewayDeviceKey);
{ return null;
Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is not a valid gateway", props.GatewayDeviceKey); }
return null; Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is a valid device", props.GatewayDeviceKey);
} gateway = gatewayDev.GateWay;
}
Hr1x0WirelessRemoteBase remoteBase;
switch (type) if (gateway == null)
{ {
case ("hr100"): Debug.Console(0, "GetHr1x0WirelessRemote: Device '{0}' is not a valid gateway", props.GatewayDeviceKey);
remoteBase = new Hr100(rfId, gateway); return null;
break; }
case ("hr150"):
remoteBase = new Hr150(rfId, gateway); Hr1x0WirelessRemoteBase remoteBase;
break; switch (type)
case ("hr310"): {
remoteBase = new Hr310(rfId, gateway); case ("hr100"):
break; remoteBase = new Hr100(rfId, gateway);
default: break;
return null; case ("hr150"):
} remoteBase = new Hr150(rfId, gateway);
break;
// register the device when using an internal RF gateway case ("hr310"):
if (props.GatewayDeviceKey == "processor") remoteBase = new Hr310(rfId, gateway);
{ break;
remoteBase.RegisterWithLogging(config.Key); default:
} return null;
}
return remoteBase;
} // register the device when using an internal RF gateway
if (props.GatewayDeviceKey == "processor")
#endregion {
remoteBase.RegisterWithLogging(config.Key);
#region Factory }
public class Hrxx0WirelessRemoteControllerFactory : EssentialsDeviceFactory<Hrxx0WirelessRemoteController>
{ return remoteBase;
public Hrxx0WirelessRemoteControllerFactory() }
{
TypeNames = new List<string>() { "hr100", "hr150", "hr310" }; #endregion
}
#region Factory
public override EssentialsDevice BuildDevice(DeviceConfig dc) public class Hrxx0WirelessRemoteControllerFactory : EssentialsDeviceFactory<Hrxx0WirelessRemoteController>
{ {
Debug.Console(1, "Factory Attempting to create new HR-x00 Remote Device"); public Hrxx0WirelessRemoteControllerFactory()
{
return new Hrxx0WirelessRemoteController(dc.Key, GetHr1x0WirelessRemote, dc); TypeNames = new List<string>() { "hr100", "hr150", "hr310" };
} }
}
#endregion public override EssentialsDevice BuildDevice(DeviceConfig dc)
{
public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge) Debug.Console(1, "Factory Attempting to create new HR-x00 Remote Device");
{
var joinMap = new Hrxxx0WirelessRemoteControllerJoinMap(joinStart); return new Hrxx0WirelessRemoteController(dc.Key, GetHr1x0WirelessRemote, dc);
}
var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey); }
#endregion
if (!string.IsNullOrEmpty(joinMapSerialized))
joinMap = JsonConvert.DeserializeObject<Hrxxx0WirelessRemoteControllerJoinMap>(joinMapSerialized); public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge)
{
if (bridge != null) var joinMap = new Hrxxx0WirelessRemoteControllerJoinMap(joinStart);
{
bridge.AddJoinMap(Key, joinMap); var joinMapSerialized = JoinMapHelper.GetSerializedJoinMapForDevice(joinMapKey);
}
else if (!string.IsNullOrEmpty(joinMapSerialized))
{ joinMap = JsonConvert.DeserializeObject<Hrxxx0WirelessRemoteControllerJoinMap>(joinMapSerialized);
Debug.Console(0, this, "Please update config to use 'eiscapiadvanced' to get all join map features for this device.");
} if (bridge != null)
{
//List<string> ExcludedKeys = new List<string>(); bridge.AddJoinMap(Key, joinMap);
foreach (var feedback in Feedbacks) }
{ else
var myFeedback = feedback; {
Debug.Console(0, this, "Please update config to use 'eiscapiadvanced' to get all join map features for this device.");
var joinData = }
joinMap.Joins.FirstOrDefault(
x => //List<string> ExcludedKeys = new List<string>();
x.Key.Equals(myFeedback.Key, StringComparison.InvariantCultureIgnoreCase)); foreach (var feedback in Feedbacks)
{
if (string.IsNullOrEmpty((joinData.Key))) continue; var myFeedback = feedback;
var name = joinData.Key; var joinData =
var join = joinData.Value; joinMap.Joins.FirstOrDefault(
x =>
if (join.Metadata.JoinType == eJoinType.Digital) x.Key.Equals(myFeedback.Key, StringComparison.InvariantCultureIgnoreCase));
{
Debug.Console(0, this, "Linking Bool Feedback '{0}' to join {1}", name, join.JoinNumber); if (string.IsNullOrEmpty((joinData.Key))) continue;
var someFeedback = myFeedback as BoolFeedback;
if(someFeedback == null) continue; var name = joinData.Key;
someFeedback.LinkInputSig(trilist.BooleanInput[join.JoinNumber]); var join = joinData.Value;
}
if (join.Metadata.JoinType == eJoinType.Analog) if (join.Metadata.JoinType == eJoinType.Digital)
{ {
Debug.Console(0, this, "Linking Analog Feedback '{0}' to join {1}", name, join.JoinNumber); Debug.Console(0, this, "Linking Bool Feedback '{0}' to join {1}", name, join.JoinNumber);
var someFeedback = myFeedback as IntFeedback; var someFeedback = myFeedback as BoolFeedback;
if (someFeedback == null) continue; if(someFeedback == null) continue;
someFeedback.LinkInputSig(trilist.UShortInput[join.JoinNumber]); someFeedback.LinkInputSig(trilist.BooleanInput[join.JoinNumber]);
} }
if (join.Metadata.JoinType == eJoinType.Serial) if (join.Metadata.JoinType == eJoinType.Analog)
{ {
Debug.Console(0, this, "Linking Serial Feedback '{0}' to join {1}", name, join.JoinNumber); Debug.Console(0, this, "Linking Analog Feedback '{0}' to join {1}", name, join.JoinNumber);
var someFeedback = myFeedback as StringFeedback; var someFeedback = myFeedback as IntFeedback;
if (someFeedback == null) continue; if (someFeedback == null) continue;
someFeedback.LinkInputSig(trilist.StringInput[join.JoinNumber]); someFeedback.LinkInputSig(trilist.UShortInput[join.JoinNumber]);
} }
} if (join.Metadata.JoinType == eJoinType.Serial)
{
//var newJoinKeys = joinMap.Joins.Keys.Except(ExcludedKeys).ToList(); Debug.Console(0, this, "Linking Serial Feedback '{0}' to join {1}", name, join.JoinNumber);
var someFeedback = myFeedback as StringFeedback;
//var newJoinMap = newJoinKeys.Where(k => joinMap.Joins.ContainsKey(k)).Select(k => joinMap.Joins[k]); if (someFeedback == null) continue;
someFeedback.LinkInputSig(trilist.StringInput[join.JoinNumber]);
}
Debug.Console(2, this, "There are {0} remote buttons", _remote.Button.Count); }
for (uint i = 1; i <= _remote.Button.Count; i++)
{ //var newJoinKeys = joinMap.Joins.Keys.Except(ExcludedKeys).ToList();
Debug.Console(2, this, "Attempting to link join index {0}", i);
var index = i; //var newJoinMap = newJoinKeys.Where(k => joinMap.Joins.ContainsKey(k)).Select(k => joinMap.Joins[k]);
var joinData =
joinMap.Joins.FirstOrDefault(
o => Debug.Console(2, this, "There are {0} remote buttons", _remote.Button.Count);
o.Key.Equals(_remote.Button[index].Name.ToString(), for (uint i = 1; i <= _remote.Button.Count; i++)
StringComparison.InvariantCultureIgnoreCase)); {
Debug.Console(2, this, "Attempting to link join index {0}", i);
if (string.IsNullOrEmpty((joinData.Key))) continue; var index = i;
var joinData =
var join = joinData.Value; joinMap.Joins.FirstOrDefault(
var name = joinData.Key; o =>
o.Key.Equals(_remote.Button[index].Name.ToString(),
Debug.Console(2, this, "Setting User Object for '{0}'", name); StringComparison.InvariantCultureIgnoreCase));
if (join.Metadata.JoinType == eJoinType.Digital)
{ if (string.IsNullOrEmpty((joinData.Key))) continue;
_remote.Button[i].SetButtonAction((b) => trilist.BooleanInput[join.JoinNumber].BoolValue = b);
} var join = joinData.Value;
} var name = joinData.Key;
trilist.OnlineStatusChange += (d, args) => Debug.Console(2, this, "Setting User Object for '{0}'", name);
{ if (join.Metadata.JoinType == eJoinType.Digital)
if (!args.DeviceOnLine) return; {
_remote.Button[i].SetButtonAction((b) => trilist.BooleanInput[join.JoinNumber].BoolValue = b);
foreach (var feedback in Feedbacks) }
{ }
feedback.FireUpdate();
} trilist.OnlineStatusChange += (d, args) =>
{
}; if (!args.DeviceOnLine) return;
}
public void SetTrilistBool(BasicTriList trilist, uint join, bool b) foreach (var feedback in Feedbacks)
{ {
trilist.BooleanInput[join].BoolValue = b; feedback.FireUpdate();
} }
}
} };
}
public void SetTrilistBool(BasicTriList trilist, uint join, bool b)
{
trilist.BooleanInput[join].BoolValue = b;
}
}
}