using System.Collections.Generic;
using Crestron.SimplSharp;
using PepperDash.Core;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Config;
using PepperDash.Essentials.Core.CrestronIO;
using PepperDash.Essentials.Core.Shades;
using Serilog.Events;
namespace PepperDash.Essentials.Devices.Common.Shades
{
///
/// Controls a single shade using three relays
///
public class RelayControlledShade : ShadeBase, IShadesOpenCloseStop
{
RelayControlledShadeConfigProperties Config;
ISwitchedOutput OpenRelay;
ISwitchedOutput StopOrPresetRelay;
ISwitchedOutput CloseRelay;
int RelayPulseTime;
///
/// Gets or sets the StopOrPresetButtonLabel
///
public string StopOrPresetButtonLabel { get; set; }
///
/// Initializes a new instance of the RelayControlledShade class
///
/// The device key
/// The device name
/// The relay controlled shade configuration
public RelayControlledShade(string key, string name, RelayControlledShadeConfigProperties config)
: base(key, name)
{
Config = config;
RelayPulseTime = Config.RelayPulseTime;
StopOrPresetButtonLabel = Config.StopOrPresetLabel;
}
///
/// CustomActivate method
///
///
public override bool CustomActivate()
{
//Create ISwitchedOutput objects based on props
OpenRelay = GetSwitchedOutputFromDevice(Config.Relays.Open);
StopOrPresetRelay = GetSwitchedOutputFromDevice(Config.Relays.StopOrPreset);
CloseRelay = GetSwitchedOutputFromDevice(Config.Relays.Close);
return base.CustomActivate();
}
///
/// Open method
///
///
public override void Open()
{
Debug.LogMessage(LogEventLevel.Debug, this, "Opening Shade: '{0}'", this.Name);
PulseOutput(OpenRelay, RelayPulseTime);
}
///
/// Stop method
///
///
public override void Stop()
{
Debug.LogMessage(LogEventLevel.Debug, this, "Stopping Shade: '{0}'", this.Name);
PulseOutput(StopOrPresetRelay, RelayPulseTime);
}
///
/// Close method
///
///
public override void Close()
{
Debug.LogMessage(LogEventLevel.Debug, this, "Closing Shade: '{0}'", this.Name);
PulseOutput(CloseRelay, RelayPulseTime);
}
void PulseOutput(ISwitchedOutput output, int pulseTime)
{
output.On();
CTimer pulseTimer = new CTimer(new CTimerCallbackFunction((o) => output.Off()), pulseTime);
}
///
/// Attempts to get the port on teh specified device from config
///
///
///
ISwitchedOutput GetSwitchedOutputFromDevice(IOPortConfig relayConfig)
{
var portDevice = DeviceManager.GetDeviceForKey(relayConfig.PortDeviceKey);
if (portDevice != null)
{
return (portDevice as ISwitchedOutputCollection).SwitchedOutputs[relayConfig.PortNumber];
}
else
{
Debug.LogMessage(LogEventLevel.Debug, this, "Error: Unable to get relay on port '{0}' from device with key '{1}'", relayConfig.PortNumber, relayConfig.PortDeviceKey);
return null;
}
}
}
///
/// Represents a RelayControlledShadeConfigProperties
///
public class RelayControlledShadeConfigProperties
{
///
/// Gets or sets the RelayPulseTime
///
public int RelayPulseTime { get; set; }
///
/// Gets or sets the Relays
///
public ShadeRelaysConfig Relays { get; set; }
///
/// Gets or sets the StopOrPresetLabel
///
public string StopOrPresetLabel { get; set; }
///
/// Represents a ShadeRelaysConfig
///
public class ShadeRelaysConfig
{
///
/// Gets or sets the Open
///
public IOPortConfig Open { get; set; }
///
/// Gets or sets the StopOrPreset
///
public IOPortConfig StopOrPreset { get; set; }
///
/// Gets or sets the Close
///
public IOPortConfig Close { get; set; }
}
}
///
/// Represents a RelayControlledShadeFactory
///
public class RelayControlledShadeFactory : EssentialsDeviceFactory
{
///
/// Initializes a new instance of the RelayControlledShadeFactory class
///
public RelayControlledShadeFactory()
{
TypeNames = new List() { "relaycontrolledshade" };
}
///
/// BuildDevice method
///
///
public override EssentialsDevice BuildDevice(DeviceConfig dc)
{
Debug.LogMessage(LogEventLevel.Debug, "Factory Attempting to create new Generic Comm Device");
var props = Newtonsoft.Json.JsonConvert.DeserializeObject(dc.Properties.ToString());
return new RelayControlledShade(dc.Key, dc.Name, props);
}
}
}