Skip to content

Commit

Permalink
feat: adds ability for attributes to interact directly with devices
Browse files Browse the repository at this point in the history
  • Loading branch information
aknous committed Dec 4, 2024
1 parent 36a504d commit 0215b41
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 15 deletions.
125 changes: 125 additions & 0 deletions src/DynFusionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Crestron.SimplSharpPro;
using PepperDash.Core;
using Crestron.SimplSharp.Reflection;
using Newtonsoft.Json;

namespace DynFusion
{
Expand All @@ -31,8 +32,15 @@ public DynFusionDigitalAttribute(string name, UInt32 joinNumber, string deviceKe
BoolValueFeedback = new BoolFeedback(() => { return BoolValue; });
Debug.Console(2, "Creating DigitalAttribute {0} {1} {2}", this.JoinNumber, this.Name, this.RwType);


if (deviceKey != null)
{
_devicekey = deviceKey;
if (boolAction != null)
{
_action = boolAction;
}

if (boolFeedback != null)
{
try
Expand All @@ -52,6 +60,9 @@ public DynFusionDigitalAttribute(string name, UInt32 joinNumber, string deviceKe
}

}

private string _devicekey { get; set; }
private string _action { get; set; }
public BoolFeedback BoolValueFeedback { get; set; }

private bool _BoolValue { get; set; }
Expand All @@ -71,6 +82,21 @@ public bool BoolValue

}
}

public void CallAction(bool value)
{
if (_devicekey != null && _action != null)
{
var payload = new
{
deviceKey = _devicekey,
methodName = _action,
@params = new object[] { value }
};
string jsonString = JsonConvert.SerializeObject(payload);
DeviceJsonApi.DoDeviceActionWithJson(jsonString);
}
}
}
public class DynFusionAnalogAttribute : DynFusionAttributeBase
{
Expand All @@ -82,6 +108,40 @@ public DynFusionAnalogAttribute(string name, UInt32 joinNumber)
Debug.Console(2, "Creating AnalogAttribute {0} {1} {2}", this.JoinNumber, this.Name, this.RwType);
}

public DynFusionAnalogAttribute(string name, UInt32 joinNumber, string deviceKey, string intAction, string intFeedback)
: base(name, eSigType.UShort, joinNumber)
{
Debug.Console(2, "Creating AnalogAttribute {0} {1} {2}", this.JoinNumber, this.Name, this.RwType);

if (deviceKey != null)
{
_devicekey = deviceKey;
if (intAction != null)
{
_action = intAction;
}

if (intFeedback != null)
{
try
{
var fb = DeviceJsonApi.GetPropertyByName(deviceKey, intFeedback) as IntFeedback;
fb.OutputChange += ((sender, args) =>
{
this.UShortValue = args.UShortValue;
});
}
catch (Exception ex)
{
Debug.Console(0, Debug.ErrorLogLevel.Error, "DynFuison Issue linking Device {0} BoolFB {1}\n{2}", deviceKey, intFeedback, ex);
}

}
}
}
private string _devicekey { get; set; }
private string _action { get; set; }

public IntFeedback UShortValueFeedback { get; set; }
private UInt32 _UShortValue { get; set; }
public UInt32 UShortValue
Expand All @@ -98,6 +158,21 @@ public UInt32 UShortValue

}
}

public void CallAction(uint value)
{
if (_devicekey != null && _action != null)
{
var payload = new
{
deviceKey = _devicekey,
methodName = _action,
@params = new object[] { value }
};
string jsonString = JsonConvert.SerializeObject(payload);
DeviceJsonApi.DoDeviceActionWithJson(jsonString);
}
}
}
public class DynFusionSerialAttribute : DynFusionAttributeBase
{
Expand All @@ -108,6 +183,41 @@ public DynFusionSerialAttribute(string name, UInt32 joinNumber)

Debug.Console(2, "Creating StringAttribute {0} {1} {2}", this.JoinNumber, this.Name, this.RwType);
}

public DynFusionSerialAttribute(string name, UInt32 joinNumber, string deviceKey, string stringAction, string stringFeedback)
: base(name, eSigType.String, joinNumber)
{
Debug.Console(2, "Creating StringAttribute {0} {1} {2}", this.JoinNumber, this.Name, this.RwType);

if (deviceKey != null)
{
_devicekey = deviceKey;
if (stringAction != null)
{
_action = stringAction;
}

if (stringFeedback != null)
{
try
{
var fb = DeviceJsonApi.GetPropertyByName(deviceKey, stringFeedback) as StringFeedback;
fb.OutputChange += ((sender, args) =>
{
this.StringValue = args.StringValue;
});
}
catch (Exception ex)
{
Debug.Console(0, Debug.ErrorLogLevel.Error, "DynFuison Issue linking Device {0} BoolFB {1}\n{2}", deviceKey, stringFeedback, ex);
}

}
}
}
private string _devicekey { get; set; }
private string _action { get; set; }

public StringFeedback StringValueFeedback { get; set; }
private String _StringValue { get; set; }
public String StringValue
Expand All @@ -125,6 +235,21 @@ public String StringValue
}

}

public void CallAction(string value)
{
if (this._devicekey != null && this._action != null)
{
var payload = new
{
deviceKey = _devicekey,
methodName = _action,
@params = new object[] { value }
};
string jsonString = JsonConvert.SerializeObject(payload);
DeviceJsonApi.DoDeviceActionWithJson(jsonString);
}
}
}
public class DynFusionAttributeBase
{
Expand Down
38 changes: 23 additions & 15 deletions src/DynFusionDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ public override bool CustomActivate()
}
if (att.RwType == eReadWrite.ReadWrite || att.RwType == eReadWrite.Write)
{
DigitalAttributesFromFusion.Add(att.JoinNumber,
new DynFusionDigitalAttribute(att.Name, att.JoinNumber));
}
DigitalAttributesToFusion.Add(att.JoinNumber,
new DynFusionDigitalAttribute(att.Name, att.JoinNumber, att.LinkDeviceKey,
att.LinkDeviceMethod, att.LinkDeviceFeedback));
}
}

foreach (var att in _Config.CustomAttributes.AnalogAttributes)
Expand All @@ -113,35 +114,39 @@ public override bool CustomActivate()

if (att.RwType == eReadWrite.ReadWrite || att.RwType == eReadWrite.Read)
{
AnalogAttributesToFusion.Add(att.JoinNumber,
new DynFusionAnalogAttribute(att.Name, att.JoinNumber));
AnalogAttributesToFusion.Add(att.JoinNumber,
new DynFusionAnalogAttribute(att.Name, att.JoinNumber, att.LinkDeviceKey,
att.LinkDeviceMethod, att.LinkDeviceFeedback));

AnalogAttributesToFusion[att.JoinNumber].UShortValueFeedback.LinkInputSig(
AnalogAttributesToFusion[att.JoinNumber].UShortValueFeedback.LinkInputSig(
FusionSymbol.UserDefinedUShortSigDetails[att.JoinNumber - FusionJoinOffset].InputSig);
}
if (att.RwType == eReadWrite.ReadWrite || att.RwType == eReadWrite.Write)
{
AnalogAttributesFromFusion.Add(att.JoinNumber,
new DynFusionAnalogAttribute(att.Name, att.JoinNumber));
}
AnalogAttributesToFusion.Add(att.JoinNumber,
new DynFusionAnalogAttribute(att.Name, att.JoinNumber, att.LinkDeviceKey,
att.LinkDeviceMethod, att.LinkDeviceFeedback));
}
}
foreach (var att in _Config.CustomAttributes.SerialAttributes)
{
FusionSymbol.AddSig(eSigType.String, att.JoinNumber - FusionJoinOffset, att.Name,
GetIOMask(att.RwType));
if (att.RwType == eReadWrite.ReadWrite || att.RwType == eReadWrite.Read)
{
SerialAttributesToFusion.Add(att.JoinNumber,
new DynFusionSerialAttribute(att.Name, att.JoinNumber));
SerialAttributesToFusion.Add(att.JoinNumber,
new DynFusionSerialAttribute(att.Name, att.JoinNumber, att.LinkDeviceKey,
att.LinkDeviceMethod, att.LinkDeviceFeedback));

SerialAttributesToFusion[att.JoinNumber].StringValueFeedback.LinkInputSig(
SerialAttributesToFusion[att.JoinNumber].StringValueFeedback.LinkInputSig(
FusionSymbol.UserDefinedStringSigDetails[att.JoinNumber - FusionJoinOffset].InputSig);
}
if (att.RwType == eReadWrite.ReadWrite || att.RwType == eReadWrite.Write)
{
SerialAttributesFromFusion.Add(att.JoinNumber,
new DynFusionSerialAttribute(att.Name, att.JoinNumber));
}
SerialAttributesToFusion.Add(att.JoinNumber,
new DynFusionSerialAttribute(att.Name, att.JoinNumber, att.LinkDeviceKey,
att.LinkDeviceMethod, att.LinkDeviceFeedback));
}
}

// Create Links for Standard joins
Expand Down Expand Up @@ -597,6 +602,7 @@ private void FusionSymbol_FusionStateChange(FusionBase device, FusionStateEventA
if (DigitalAttributesFromFusion.TryGetValue(joinNumber, out output))
{
output.BoolValue = sigDetails.OutputSig.BoolValue;
output.CallAction(output.BoolValue);
}
break;
}
Expand All @@ -613,6 +619,7 @@ private void FusionSymbol_FusionStateChange(FusionBase device, FusionStateEventA
if (AnalogAttributesFromFusion.TryGetValue(joinNumber, out output))
{
output.UShortValue = sigDetails.OutputSig.UShortValue;
output.CallAction(output.UShortValue);
}
break;
}
Expand All @@ -628,6 +635,7 @@ private void FusionSymbol_FusionStateChange(FusionBase device, FusionStateEventA
if (SerialAttributesFromFusion.TryGetValue(joinNumber, out output))
{
output.StringValue = sigDetails.OutputSig.StringValue;
output.CallAction(output.StringValue);
}
break;
}
Expand Down

0 comments on commit 0215b41

Please sign in to comment.