Skip to content

Commit

Permalink
fix: fix broken legacy config
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor Payne committed May 4, 2023
1 parent d9c82d1 commit 58acaed
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 40 deletions.
1 change: 1 addition & 0 deletions Pdu-Wattbox-Epi/DeviceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PepperDash.Essentials.Core.Config;
using PepperDash.Core;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Wattbox.Lib;

namespace Pdu_Wattbox_Epi
Expand Down
1 change: 1 addition & 0 deletions Pdu-Wattbox-Epi/Pdu-Wattbox-Epi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WattboxCommunicationMonitor.cs" />
<Compile Include="WattboxController.cs" />
<Compile Include="WattboxJoinmapDynamic.cs" />
<Compile Include="WattboxOutlet.cs" />
<Compile Include="WattboxStatusMonitor.cs" />
<None Include="Properties\ControlSystem.cfg" />
Expand Down
17 changes: 10 additions & 7 deletions Pdu-Wattbox-Epi/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
using System.Collections.Generic;
using PepperDash.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Essentials.Core;


namespace Pdu_Wattbox_Epi {
public class Properties {
[JsonProperty("control")]

public WattboxControlProperties Control { get; set; }
public EssentialsControlPropertiesConfig Control { get; set; }
[JsonProperty("authType")]
public string AuthType { get; set; }
[JsonProperty("outlets")]
public List<Outlet> Outlets { get; set; }
}

public class Outlet {
[JsonProperty("key")]
public string Key { get; set; }
[JsonProperty("outletNumber")]
public int OutletNumber { get; set; }
[JsonProperty("enabled")]
public bool Enabled { get; set; }
[JsonProperty("name")]
public string Name { get; set; }

public Outlet(string key, OutletDict outlet)
Expand All @@ -27,13 +34,9 @@ public Outlet(string key, OutletDict outlet)
Enabled = !outlet.IsInvisible;
Name = outlet.Name;
}
public Outlet() {}
}

public class WattboxControlProperties
{
[JsonProperty("tcpSshProperties")]
public TcpSshPropertiesConfig TcpSshProperties { get; set; }
}

public class OutletDict
{
Expand Down
43 changes: 21 additions & 22 deletions Pdu-Wattbox-Epi/WattboxController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
using System.Text.RegularExpressions;
using Crestron.SimplSharp;
using Crestron.SimplSharpPro.DeviceSupport;
using Crestron.SimplSharpPro.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using Newtonsoft.Json;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Bridges;
using PepperDash.Essentials.Core.Config;
Expand All @@ -35,10 +34,6 @@ public class WattboxController : EssentialsBridgeableDevice, IHasControlledPower

public StatusMonitorBase CommunicationMonitor { get; set; }






public readonly int OutletCount;

Expand All @@ -56,7 +51,6 @@ public WattboxController(string key, string name, WattboxCommunicationMonitor co

Comms = comms;


Comms.UpdateOutletStatus = UpdateOutletStatus;
Comms.UpdateOnlineStatus = UpdateOnlineStatus;
Comms.UpdateLoggedInStatus = UpdateLoggedInStatus;
Expand All @@ -76,15 +70,29 @@ public WattboxController(string key, string name, WattboxCommunicationMonitor co

var outlets = new List<Outlet>();

_props = dc.Properties.ToObject<Properties>();
var outletsToken = dc.Properties.SelectToken("outlets");
if (outletsToken == null)
{
Debug.Console(0, this, "OutletsToken is null");
return;
}
if (outletsToken is JArray)
{
outlets = outletsToken.ToObject<List<Outlet>>();
_props = dc.Properties.ToObject<Properties>();

Debug.Console(0, this, "Found an Array");
outlets = _props.Outlets;
if (outlets == null)
{
Debug.Console(0, this,"That Array is Null");
return;
}
}

else if (outletsToken is JObject)
{
Debug.Console(0, this, "Found an Object");

outlets = ListConvert(outletsToken.ToObject<Dictionary<string, OutletDict>>());
}
Outlets = outlets;
Expand Down Expand Up @@ -200,12 +208,6 @@ private void UpdateSerial(string serial)
UpdateDeviceInfo();
}







public void GetStatus()
{
Comms.GetStatus();
Expand All @@ -223,7 +225,7 @@ public void SetOutlet(int index, EWattboxOutletSet action)

public override void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, EiscApiAdvanced bridge)
{
var joinMap = new PduJoinMapBase(joinStart);
var joinMap = new WattboxJoinmapDynamic(joinStart, PduOutlets);

var customJoins = JoinMapHelper.TryGetJoinMapAdvancedForDevice(joinMapKey);

Expand All @@ -239,12 +241,12 @@ public override void LinkToApi(BasicTriList trilist, uint joinStart, string join

Debug.Console(2, this, "There are {0} Outlets", Outlets.Count());

IsOnlineFeedback.LinkInputSig(trilist.BooleanInput[joinMap.Online.JoinNumber]);
IsOnlineFeedback.LinkInputSig(trilist.BooleanInput[joinMap.BaseJoinMap.Online.JoinNumber]);

NameFeedback.LinkInputSig(trilist.StringInput[joinMap.Name.JoinNumber]);
NameFeedback.LinkInputSig(trilist.StringInput[joinMap.BaseJoinMap.Name.JoinNumber]);


if ((int)joinMap.OutletName.JoinNumber - (int)joinStart > 0)
if ((int)joinMap.BaseJoinMap.OutletName.JoinNumber - (int)joinStart > 0)
{
foreach (var o in PduOutlets.Select(outlet => outlet.Value).OfType<WattboxOutlet>())
{
Expand Down Expand Up @@ -317,14 +319,11 @@ private string GetMacAddress(string ipAddress)

}



#region ICommunicationMonitor Members


#endregion


#region IDeviceInfoProvider Members


Expand Down
147 changes: 147 additions & 0 deletions Pdu-Wattbox-Epi/WattboxJoinmapDynamic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System.Collections.Generic;
using System.Linq;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Bridges;
using PepperDash_Essentials_Core.Devices;

namespace Pdu_Wattbox_Epi
{
public class WattboxJoinmapDynamic : JoinMapBaseAdvanced
{
public readonly PduJoinMapBase BaseJoinMap;

public WattboxJoinmapDynamic(uint joinStart, IEnumerable<KeyValuePair<int, IHasPowerCycle>> pduOutlets)
: base(joinStart, typeof (WattboxJoinmapDynamic))
{
BaseJoinMap = new PduJoinMapBase(joinStart);


Joins.Add("Name", BaseJoinMap.Joins["Name"]);
Joins.Add("Online", BaseJoinMap.Joins["Online"]);
Joins.Add("OutletCount", BaseJoinMap.Joins["OutletCount"]);

foreach (var index in pduOutlets.Select(outlet => outlet.Key))
{
SetOutletNameJoinData(index);
SetOutletEnabledJoinData(index);
SetOutletPowerCycleJoinData(index);
SetOutletPowerOffJoinData(index);
SetOutletPowerOnJoinData(index);
}

}

private void SetOutletNameJoinData(int index)
{

var joinData = new JoinData
{

JoinNumber = (uint)((index - 1)*4 + BaseJoinMap.OutletName.JoinNumber),
JoinSpan = 1
};

var joinMetaData = new JoinMetadata
{
Description = string.Format("Outlet {0} Name", index),
JoinCapabilities = eJoinCapabilities.ToSIMPL,
JoinType = eJoinType.Serial
};

var joinDataComplete = new JoinDataComplete(joinData, joinMetaData);

Joins.Add(string.Format("Outlet {0} Name", index), joinDataComplete);

}

private void SetOutletEnabledJoinData(int index)
{

var joinData = new JoinData
{

JoinNumber = (uint)(((index - 1) * 4) + BaseJoinMap.OutletEnabled.JoinNumber),
JoinSpan = 1
};

var joinMetaData = new JoinMetadata
{
Description = string.Format("Outlet {0} Enabled", index),
JoinCapabilities = eJoinCapabilities.ToSIMPL,
JoinType = eJoinType.Digital
};

var joinDataComplete = new JoinDataComplete(joinData, joinMetaData);

Joins.Add(string.Format("Outlet {0} Enabled", index), joinDataComplete);

}
private void SetOutletPowerOffJoinData(int index)
{

var joinData = new JoinData
{

JoinNumber = (uint)(((index - 1) * 4) + BaseJoinMap.OutletPowerOff.JoinNumber),
JoinSpan = 1
};

var joinMetaData = new JoinMetadata
{
Description = string.Format("Outlet {0} Power Off", index),
JoinCapabilities = eJoinCapabilities.ToFromSIMPL,
JoinType = eJoinType.Digital
};

var joinDataComplete = new JoinDataComplete(joinData, joinMetaData);

Joins.Add(string.Format("Outlet {0} Power Off", index), joinDataComplete);

}
private void SetOutletPowerOnJoinData(int index)
{

var joinData = new JoinData
{

JoinNumber = (uint)(((index - 1) * 4) + BaseJoinMap.OutletPowerOn.JoinNumber),
JoinSpan = 1
};

var joinMetaData = new JoinMetadata
{
Description = string.Format("Outlet {0} Power On", index),
JoinCapabilities = eJoinCapabilities.ToFromSIMPL,
JoinType = eJoinType.Digital
};

var joinDataComplete = new JoinDataComplete(joinData, joinMetaData);

Joins.Add(string.Format("Outlet {0} Power On", index), joinDataComplete);

}
private void SetOutletPowerCycleJoinData(int index)
{

var joinData = new JoinData
{

JoinNumber = (uint)(((index - 1) * 4) + BaseJoinMap.OutletPowerCycle.JoinNumber),
JoinSpan = 1
};

var joinMetaData = new JoinMetadata
{
Description = string.Format("Outlet {0} Power Cycle", index),
JoinCapabilities = eJoinCapabilities.FromSIMPL,
JoinType = eJoinType.Digital
};

var joinDataComplete = new JoinDataComplete(joinData, joinMetaData);

Joins.Add(string.Format("Outlet {0} Power Cycle", index), joinDataComplete);

}

}
}
20 changes: 9 additions & 11 deletions Pdu-Wattbox-Epi/WattboxOutlet.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Crestron.SimplSharpPro.DeviceSupport;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Bridges;
using PepperDash_Essentials_Core.Devices;
using Feedback = PepperDash.Essentials.Core.Feedback;

Expand Down Expand Up @@ -79,17 +78,16 @@ public void PowerToggle()
_parent.SetOutlet(_index, PowerStatus ? EWattboxOutletSet.PowerOff : EWattboxOutletSet.PowerOn);
}

public void LinkOutlet(BasicTriList trilist, PduJoinMapBase joinMap)
public void LinkOutlet(BasicTriList trilist, WattboxJoinmapDynamic joinMap)
{
var offset = (ushort) ((_index - 1)*4);
EnabledFeedback.LinkInputSig(trilist.BooleanInput[joinMap.OutletEnabled.JoinNumber + offset]);
NameFeedback.LinkInputSig(trilist.StringInput[joinMap.OutletName.JoinNumber + offset]);
PowerIsOnFeedback.LinkInputSig(trilist.BooleanInput[joinMap.OutletPowerOn.JoinNumber + offset]);
PowerIsOnFeedback.LinkComplementInputSig(trilist.BooleanInput[joinMap.OutletPowerOff.JoinNumber + offset]);

trilist.SetSigTrueAction((joinMap.OutletPowerOn.JoinNumber + offset), PowerOn);
trilist.SetSigTrueAction((joinMap.OutletPowerOff.JoinNumber + offset), PowerOff);
trilist.SetSigTrueAction((joinMap.OutletPowerCycle.JoinNumber + offset), PowerCycle);
EnabledFeedback.LinkInputSig(trilist.BooleanInput[joinMap.Joins[string.Format("Outlet {0} Enabled", _index)].JoinNumber]);
NameFeedback.LinkInputSig(trilist.StringInput[joinMap.Joins[string.Format("Outlet {0} Name", _index)].JoinNumber]);
PowerIsOnFeedback.LinkInputSig(trilist.BooleanInput[joinMap.Joins[string.Format("Outlet {0} Power On", _index)].JoinNumber]);
PowerIsOnFeedback.LinkComplementInputSig(trilist.BooleanInput[joinMap.Joins[string.Format("Outlet {0} Power Off", _index)].JoinNumber]);

trilist.SetSigTrueAction(joinMap.Joins[string.Format("Outlet {0} Power On", _index)].JoinNumber, PowerOn);
trilist.SetSigTrueAction(joinMap.Joins[string.Format("Outlet {0} Power Off", _index)].JoinNumber, PowerOff);
trilist.SetSigTrueAction(joinMap.Joins[string.Format("Outlet {0} Power Cycle", _index)].JoinNumber, PowerCycle);

trilist.OnlineStatusChange += (d, args) =>
{
Expand Down

0 comments on commit 58acaed

Please sign in to comment.