Skip to content

Commit

Permalink
prepping for initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
ngenovese11 committed Nov 18, 2020
1 parent 601470a commit 93a3971
Show file tree
Hide file tree
Showing 21 changed files with 9,320 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*.userosscache
*.sln.docstates
*.projectinfo
*.csproj.user

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
Expand Down Expand Up @@ -360,3 +361,5 @@ MigrationBackup/
# VS Code settings folders
.vscode/
*.projectinfo

SPlsWork/
42 changes: 22 additions & 20 deletions src/Builders/Ap89XxBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using ApcEpi.Abstractions;
using ApcEpi.Config;
using ApcEpi.Devices;
Expand All @@ -14,45 +13,48 @@ namespace ApcEpi.Builders
{
public class Ap89XxBuilder : IApDeviceBuilder
{
public static IApDeviceBuilder GetFromDeviceConfig(DeviceConfig dc)
{
var config = dc.Properties.ToObject<ApDeviceConfig>();
var coms = CommFactory.CreateCommForDevice(dc);

return new Ap89XxBuilder(dc.Key, dc.Name, coms, config);
}

private Ap89XxBuilder(string key, string name, IBasicCommunication coms, ApDeviceConfig config)
{
Coms = coms;
Name = name;
Key = key;
Outlets = BuildOutletsFromConfig(config, coms);
Outlets = BuildOutletsFromConfig(key, config, coms);
Monitor = new GenericCommunicationMonitoredDevice(
Key,
Name,
Coms,
"about\r");

Poll = new CTimer(_ => ApDevice.PollDevice(coms), null, Timeout.Infinite, 5000);
Poll = new CTimer(_ => ApDevice.PollDevice(coms), null, Timeout.Infinite);
}

public static ReadOnlyDictionary<uint, IApOutlet> BuildOutletsFromConfig(ApDeviceConfig config, IBasicCommunication coms)
public string Key { get; private set; }
public string Name { get; private set; }
public IBasicCommunication Coms { get; private set; }
public ICommunicationMonitor Monitor { get; private set; }
public ReadOnlyDictionary<uint, IApOutlet> Outlets { get; private set; }
public CTimer Poll { get; private set; }

public static ReadOnlyDictionary<uint, IApOutlet> BuildOutletsFromConfig(
string parentKey,
ApDeviceConfig config,
IBasicCommunication coms)
{
var outlets = config
.Outlets
.Select(x => new ApOutlet(x.Key, x.Value.Name, x.Value.OutletIndex, coms))
.Select(x => new ApOutlet(parentKey + "-" + x.Key, x.Value.Name, x.Value.OutletIndex, coms))
.ToDictionary<ApOutlet, uint, IApOutlet>(outlet => (uint) outlet.OutletIndex, outlet => outlet);

return new ReadOnlyDictionary<uint, IApOutlet>(outlets);
}

public string Key { get; private set; }
public string Name { get; private set; }
public ICommunicationMonitor Monitor { get; private set; }
public IBasicCommunication Coms { get; private set; }
public ReadOnlyDictionary<uint, IApOutlet> Outlets { get; private set; }
public CTimer Poll { get; private set; }
public static IApDeviceBuilder GetFromDeviceConfig(DeviceConfig dc)
{
var config = dc.Properties.ToObject<ApDeviceConfig>();
var coms = CommFactory.CreateCommForDevice(dc);

return new Ap89XxBuilder(dc.Key, dc.Name, coms, config);
}

public EssentialsDevice Build()
{
Expand Down
140 changes: 130 additions & 10 deletions src/Devices/ApDevice.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using ApcEpi.Abstractions;
using ApcEpi.JoinMaps;
using ApcEpi.Services.StatusCommands;
Expand All @@ -11,7 +12,7 @@

namespace ApcEpi.Devices
{
public class ApDevice: EssentialsDevice, IOutletName, IOutletPower, IOutletOnline
public class ApDevice: EssentialsDevice, IOutletName, IOutletPower, IOutletOnline, IBridgeAdvanced
{
private readonly ICommunicationMonitor _monitor;
private readonly CTimer _poll;
Expand All @@ -26,14 +27,34 @@ public ApDevice(IApDeviceBuilder builder)
_monitor = builder.Monitor;
_poll = builder.Poll;

var socket = builder.Coms as ISocketStatus;
if (socket != null)
{
socket.ConnectionChange +=
(sender, args) =>
{
if (args.Client.IsConnected)
_poll.Reset(2000, 30000);
else
{
_poll.Stop();
}
};
}

AddPostActivationAction(() => builder.Coms.Connect());

NameFeedback = new StringFeedback("DeviceNameFeedback", () => builder.Name);
Feedbacks.Add(IsOnline);
Feedbacks.Add(NameFeedback);
}

public StatusMonitorBase CommunicationMonitor
{
get { return _monitor.CommunicationMonitor; }
}

public StringFeedback NameFeedback { get; private set; }
public FeedbackCollection<Feedback> Feedbacks { get; private set; }

public BoolFeedback IsOnline
Expand All @@ -43,19 +64,39 @@ public BoolFeedback IsOnline

public override bool CustomActivate()
{
CommunicationMonitor.Start();
_poll.Reset(2000, 5000);
foreach (var apOutletFeedbacks in _outlets
.Values
.Select(x => new Feedback[]
{
x.IsOnline,
x.NameFeedback,
x.PowerIsOnFeedback
}))
{
Feedbacks.AddRange(apOutletFeedbacks);
}

foreach (var apOutlet in _outlets.Values)
foreach (var feedback in Feedbacks)
{
Feedbacks.AddRange(new Feedback[]
feedback.OutputChange += (sender, args) =>
{
apOutlet.IsOnline,
apOutlet.NameFeedback,
apOutlet.PowerIsOnFeedback
});
var fb = sender as Feedback;
if (fb != null && String.IsNullOrEmpty(fb.Key))
return;

if (fb is BoolFeedback)
Debug.Console(1, this, "Received update from {0} | {1}", fb.Key, fb.BoolValue);

if (fb is IntFeedback)
Debug.Console(1, this, "Received update from {0} | {1}", fb.Key, fb.IntValue);

if (fb is StringFeedback)
Debug.Console(1, this, "Received update from {0} | {1}", fb.Key, fb.StringValue);
};
}

CommunicationMonitor.Start();

return true;
}

Expand All @@ -72,7 +113,7 @@ public void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, E
joinMap.SetCustomJoinData(customJoins);

Debug.Console(1, "Linking to Trilist '{0}'", trilist.ID.ToString("X"));
Debug.Console(0, "Linking to Bridge Type {0}", GetType().Name);
Debug.Console(1, "Linking to Bridge Type {0}", GetType().Name);

trilist.OnlineStatusChange += (device, args) =>
{
Expand All @@ -82,6 +123,77 @@ public void LinkToApi(BasicTriList trilist, uint joinStart, string joinMapKey, E
foreach (var feedback in Feedbacks)
feedback.FireUpdate();
};

IsOnline.LinkInputSig(trilist.BooleanInput[joinMap.DeviceOnline.JoinNumber]);
NameFeedback.LinkInputSig(trilist.StringInput[joinMap.DeviceName.JoinNumber]);

for (uint x = 0; x < joinMap.OutletName.JoinSpan; x++)
{
var outletIndex = x + 1;
StringFeedback feedback;
if (!TryGetOutletNameFeedback(outletIndex, out feedback))
continue;

var joinActual = outletIndex + joinMap.OutletName.JoinNumber;

Debug.Console(2, this, "Linking Outlet Name Feedback | OutletIndex:{0}, Join:{1}", outletIndex, joinActual);
feedback.LinkInputSig(trilist.StringInput[joinActual]);
}

for (uint x = 0; x < joinMap.OutletOnline.JoinSpan; x++)
{
var outletIndex = x + 1;
BoolFeedback feedback;
if (!TryGetOutletOnlineFeedback(outletIndex, out feedback))
continue;

var joinActual = outletIndex + joinMap.OutletName.JoinNumber;

Debug.Console(2, this, "Linking Outlet Online Feedback | OutletIndex:{0}, Join:{1}", outletIndex, joinActual);
feedback.LinkInputSig(trilist.BooleanInput[joinActual]);
}

for (uint x = 0; x < joinMap.OutletPowerOn.JoinSpan; x++)
{
var outletIndex = x + 1;
BoolFeedback feedback;
if (!TryGetOutletPowerFeedback(outletIndex, out feedback))
continue;

var joinActual = outletIndex + joinMap.OutletPowerOn.JoinNumber;

Debug.Console(2, this, "Linking Outlet PowerIsOn Feedback | OutletIndex:{0}, Join:{1}", outletIndex, joinActual);
feedback.LinkInputSig(trilist.BooleanInput[joinActual]);

Debug.Console(2, this, "Linking Outlet PowerOn Method | OutletIndex:{0}, Join:{1}", outletIndex, joinActual);
trilist.SetSigTrueAction(joinActual, () => TurnOutletOn(outletIndex));
}

for (uint x = 0; x < joinMap.OutletPowerOff.JoinSpan; x++)
{
var outletIndex = x + 1;
BoolFeedback feedback;
if (!TryGetOutletPowerFeedback(outletIndex, out feedback))
continue;

var joinActual = outletIndex + joinMap.OutletPowerOff.JoinNumber;

Debug.Console(2, this, "Linking Outlet PowerOff Method | OutletIndex:{0}, Join:{1}", outletIndex, joinActual);
trilist.SetSigTrueAction(joinActual, () => TurnOutletOff(outletIndex));
}

for (uint x = 0; x < joinMap.OutletPowerToggle.JoinSpan; x++)
{
var outletIndex = x + 1;
BoolFeedback feedback;
if (!TryGetOutletPowerFeedback(outletIndex, out feedback))
continue;

var joinActual = outletIndex + joinMap.OutletPowerToggle.JoinNumber;

Debug.Console(2, this, "Linking Outlet PowerToggle Method | OutletIndex:{0}, Join:{1}", outletIndex, joinActual);
trilist.SetSigTrueAction(joinActual, () => ToggleOutletPower(outletIndex));
}
}

public void ToggleOutletPower(uint outletIndex)
Expand All @@ -90,6 +202,7 @@ public void ToggleOutletPower(uint outletIndex)
if (_outlets.TryGetValue(outletIndex, out outlet))
{
outlet.PowerToggle();
ResetPoll();
return;
}

Expand Down Expand Up @@ -135,6 +248,7 @@ public void TurnOutletOff(uint outletIndex)
if (_outlets.TryGetValue(outletIndex, out outlet))
{
outlet.PowerOff();
ResetPoll();
return;
}

Expand All @@ -147,6 +261,7 @@ public void TurnOutletOn(uint outletIndex)
if (_outlets.TryGetValue(outletIndex, out outlet))
{
outlet.PowerOn();
ResetPoll();
return;
}

Expand All @@ -161,5 +276,10 @@ public static void PollDevice(IBasicCommunication coms)
var command = ApOutletStatusCommands.GetAllOutletStatusCommand();
coms.SendText(command);
}

private void ResetPoll()
{
_poll.Reset(1000, 10000);
}
}
}
34 changes: 24 additions & 10 deletions src/Entities/Outlet/ApOutlet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ApcEpi.Abstractions;
using System;
using ApcEpi.Abstractions;
using ApcEpi.Services.NameCommands;
using PepperDash.Core;
using PepperDash.Essentials.Core;
Expand All @@ -15,19 +16,27 @@ public ApOutlet(string key, string name, int outletIndex, IBasicCommunication co
Key = key;
Name = name;
OutletIndex = outletIndex;
NameFeedback = new StringFeedback(Key + "-OutletName-" + name, () => Name);
NameFeedback = new StringFeedback(
Key + "-OutletName",
() => String.IsNullOrEmpty(Name) ? Key : Name);

_online = new ApOutletOnline(key, name, outletIndex, coms);
_power = new ApOutletPower(key, name, outletIndex, coms);

IsOnline.OutputChange += (sender, args) =>
{
if (!args.BoolValue)
return;
var socket = coms as ISocketStatus;
if (socket != null)
{
socket.ConnectionChange += (sender, args) =>
{
if (!args.Client.IsConnected)
return;

var outletNameCommand = ApOutletNameCommands.GetOutletNameCommand(outletIndex, name);
coms.SendText(outletNameCommand);
};
var outletNameCommand = ApOutletNameCommands
.GetOutletNameCommand(outletIndex, key);

coms.SendText(outletNameCommand);
};
}
}

public BoolFeedback IsOnline
Expand All @@ -37,14 +46,19 @@ public BoolFeedback IsOnline

public string Key { get; private set; }
public string Name { get; private set; }
public int OutletIndex { get; private set; }
public StringFeedback NameFeedback { get; private set; }
public int OutletIndex { get; private set; }

public BoolFeedback PowerIsOnFeedback
{
get { return _power.PowerIsOnFeedback; }
}

public static string GetMatchString(int outletIndex)
{
return Convert.ToString(outletIndex).PadLeft(2, ' ');
}

public void PowerOff()
{
_power.PowerOff();
Expand Down
Loading

0 comments on commit 93a3971

Please sign in to comment.