Merge pull request #458 from PepperDash/feature/IDspPreset

Add IDspPreset Interface
This commit is contained in:
Andrew Welker
2020-10-21 16:14:54 -06:00
committed by GitHub
5 changed files with 433 additions and 394 deletions

View File

@@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace PepperDash.Essentials.Core
{
public interface IHasDspPresets
{
List<IDspPreset> Presets { get; }
void RecallPreset(IDspPreset preset);
}
public interface IDspPreset
{
string Name { get; }
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp;
namespace PepperDash_Essentials_Core.Gateways
{
public class CenCn2Controller
{
}
}

View File

@@ -190,6 +190,7 @@
<Compile Include="Devices\EssentialsBridgeableDevice.cs" /> <Compile Include="Devices\EssentialsBridgeableDevice.cs" />
<Compile Include="Devices\EssentialsDevice.cs" /> <Compile Include="Devices\EssentialsDevice.cs" />
<Compile Include="Devices\GenericIRController.cs" /> <Compile Include="Devices\GenericIRController.cs" />
<Compile Include="Devices\IDspPreset.cs" />
<Compile Include="Devices\IProjectorInterfaces.cs" /> <Compile Include="Devices\IProjectorInterfaces.cs" />
<Compile Include="Devices\PC\InRoomPc.cs" /> <Compile Include="Devices\PC\InRoomPc.cs" />
<Compile Include="Devices\PC\Laptop.cs" /> <Compile Include="Devices\PC\Laptop.cs" />

View File

@@ -6,390 +6,398 @@ using Crestron.SimplSharp;
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 System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace PepperDash.Essentials.Devices.Common.DSP namespace PepperDash.Essentials.Devices.Common.DSP
{ {
// QUESTIONS: // QUESTIONS:
// //
// When subscribing, just use the Instance ID for Custom Name? // When subscribing, just use the Instance ID for Custom Name?
// Verbose on subscriptions? // Verbose on subscriptions?
// Example subscription feedback responses // Example subscription feedback responses
// ! "publishToken":"name" "value":-77.0 // ! "publishToken":"name" "value":-77.0
// ! "myLevelName" -77 // ! "myLevelName" -77
public class BiampTesiraForteDsp : DspBase public class BiampTesiraForteDsp : DspBase
{ {
public IBasicCommunication Communication { get; private set; } public IBasicCommunication Communication { get; private set; }
public CommunicationGather PortGather { get; private set; } public CommunicationGather PortGather { get; private set; }
public StatusMonitorBase CommunicationMonitor { get; private set; } public StatusMonitorBase CommunicationMonitor { get; private set; }
new public Dictionary<string, TesiraForteLevelControl> LevelControlPoints { get; private set; } public new Dictionary<string, TesiraForteLevelControl> LevelControlPoints { get; private set; }
public bool isSubscribed; public bool isSubscribed;
private CTimer SubscriptionTimer; private CTimer SubscriptionTimer;
CrestronQueue CommandQueue; private CrestronQueue CommandQueue;
bool CommandQueueInProgress = false; private bool CommandQueueInProgress = false;
//new public Dictionary<string, DspControlPoint> DialerControlPoints { get; private set; } //new public Dictionary<string, DspControlPoint> DialerControlPoints { get; private set; }
//new public Dictionary<string, DspControlPoint> SwitcherControlPoints { get; private set; } //new public Dictionary<string, DspControlPoint> SwitcherControlPoints { get; private set; }
/// <summary> /// <summary>
/// Shows received lines as hex /// Shows received lines as hex
/// </summary> /// </summary>
public bool ShowHexResponse { get; set; } public bool ShowHexResponse { get; set; }
public BiampTesiraForteDsp(string key, string name, IBasicCommunication comm, BiampTesiraFortePropertiesConfig props) : public BiampTesiraForteDsp(string key, string name, IBasicCommunication comm,
base(key, name) BiampTesiraFortePropertiesConfig props) :
{ base(key, name)
CommandQueue = new CrestronQueue(100); {
CommandQueue = new CrestronQueue(100);
Communication = comm;
var socket = comm as ISocketStatus; Communication = comm;
if (socket != null) var socket = comm as ISocketStatus;
{ if (socket != null)
// This instance uses IP control {
// This instance uses IP control
socket.ConnectionChange += new EventHandler<GenericSocketStatusChageEventArgs>(socket_ConnectionChange);
} socket.ConnectionChange += new EventHandler<GenericSocketStatusChageEventArgs>(socket_ConnectionChange);
else }
{ else
// This instance uses RS-232 control {
} // This instance uses RS-232 control
PortGather = new CommunicationGather(Communication, "\x0d\x0a"); }
PortGather.LineReceived += this.Port_LineReceived; PortGather = new CommunicationGather(Communication, "\x0d\x0a");
if (props.CommunicationMonitorProperties != null) PortGather.LineReceived += this.Port_LineReceived;
{ if (props.CommunicationMonitorProperties != null)
CommunicationMonitor = new GenericCommunicationMonitor(this, Communication, props.CommunicationMonitorProperties); {
} CommunicationMonitor = new GenericCommunicationMonitor(this, Communication,
else props.CommunicationMonitorProperties);
{ }
//#warning Need to deal with this poll string else
CommunicationMonitor = new GenericCommunicationMonitor(this, Communication, 120000, 120000, 300000, "SESSION get aliases\x0d\x0a"); {
} //#warning Need to deal with this poll string
CommunicationMonitor = new GenericCommunicationMonitor(this, Communication, 120000, 120000, 300000,
LevelControlPoints = new Dictionary<string, TesiraForteLevelControl>(); "SESSION get aliases\x0d\x0a");
}
foreach (KeyValuePair<string, BiampTesiraForteLevelControlBlockConfig> block in props.LevelControlBlocks)
{ LevelControlPoints = new Dictionary<string, TesiraForteLevelControl>();
this.LevelControlPoints.Add(block.Key, new TesiraForteLevelControl(block.Key, block.Value, this));
} foreach (KeyValuePair<string, BiampTesiraForteLevelControlBlockConfig> block in props.LevelControlBlocks)
{
} this.LevelControlPoints.Add(block.Key, new TesiraForteLevelControl(block.Key, block.Value, this));
}
public override bool CustomActivate()
{ }
Communication.Connect();
CommunicationMonitor.StatusChange += (o, a) => { Debug.Console(2, this, "Communication monitor state: {0}", CommunicationMonitor.Status); }; public override bool CustomActivate()
CommunicationMonitor.Start(); {
Communication.Connect();
CrestronConsole.AddNewConsoleCommand(SendLine, "send" + Key, "", ConsoleAccessLevelEnum.AccessOperator); CommunicationMonitor.StatusChange +=
CrestronConsole.AddNewConsoleCommand(s => Communication.Connect(), "con" + Key, "", ConsoleAccessLevelEnum.AccessOperator); (o, a) => { Debug.Console(2, this, "Communication monitor state: {0}", CommunicationMonitor.Status); };
return true; CommunicationMonitor.Start();
}
CrestronConsole.AddNewConsoleCommand(SendLine, "send" + Key, "", ConsoleAccessLevelEnum.AccessOperator);
void socket_ConnectionChange(object sender, GenericSocketStatusChageEventArgs e) CrestronConsole.AddNewConsoleCommand(s => Communication.Connect(), "con" + Key, "",
{ ConsoleAccessLevelEnum.AccessOperator);
Debug.Console(2, this, "Socket Status Change: {0}", e.Client.ClientStatus.ToString()); return true;
}
if (e.Client.IsConnected)
{ private void socket_ConnectionChange(object sender, GenericSocketStatusChageEventArgs e)
// Tasks on connect {
} Debug.Console(2, this, "Socket Status Change: {0}", e.Client.ClientStatus.ToString());
else
{ if (e.Client.IsConnected)
// Cleanup items from this session {
// Tasks on connect
if (SubscriptionTimer != null) }
{ else
SubscriptionTimer.Stop(); {
SubscriptionTimer = null; // Cleanup items from this session
}
if (SubscriptionTimer != null)
isSubscribed = false; {
CommandQueue.Clear(); SubscriptionTimer.Stop();
CommandQueueInProgress = false; SubscriptionTimer = null;
} }
}
isSubscribed = false;
/// <summary> CommandQueue.Clear();
/// Initiates the subscription process to the DSP CommandQueueInProgress = false;
/// </summary> }
void SubscribeToAttributes() }
{
SendLine("SESSION set verbose true"); /// <summary>
/// Initiates the subscription process to the DSP
foreach (KeyValuePair<string, TesiraForteLevelControl> level in LevelControlPoints) /// </summary>
{ private void SubscribeToAttributes()
level.Value.Subscribe(); {
} SendLine("SESSION set verbose true");
if (!CommandQueueInProgress) foreach (KeyValuePair<string, TesiraForteLevelControl> level in LevelControlPoints)
SendNextQueuedCommand(); {
level.Value.Subscribe();
ResetSubscriptionTimer(); }
}
if (!CommandQueueInProgress)
/// <summary> SendNextQueuedCommand();
/// Resets or Sets the subscription timer
/// </summary> ResetSubscriptionTimer();
void ResetSubscriptionTimer() }
{
isSubscribed = true; /// <summary>
/// Resets or Sets the subscription timer
if (SubscriptionTimer != null) /// </summary>
{ private void ResetSubscriptionTimer()
SubscriptionTimer = new CTimer(o => SubscribeToAttributes(), 30000); {
SubscriptionTimer.Reset(); isSubscribed = true;
} if (SubscriptionTimer != null)
} {
SubscriptionTimer = new CTimer(o => SubscribeToAttributes(), 30000);
/// <summary> SubscriptionTimer.Reset();
/// Handles a response message from the DSP
/// </summary> }
/// <param name="dev"></param> }
/// <param name="args"></param>
void Port_LineReceived(object dev, GenericCommMethodReceiveTextArgs args) /// <summary>
{ /// Handles a response message from the DSP
if (Debug.Level == 2) /// </summary>
Debug.Console(2, this, "RX: '{0}'", /// <param name="dev"></param>
ShowHexResponse ? ComTextHelper.GetEscapedText(args.Text) : args.Text); /// <param name="args"></param>
private void Port_LineReceived(object dev, GenericCommMethodReceiveTextArgs args)
Debug.Console(1, this, "RX: '{0}'", args.Text); {
if (Debug.Level == 2)
try Debug.Console(2, this, "RX: '{0}'",
{ ShowHexResponse ? ComTextHelper.GetEscapedText(args.Text) : args.Text);
if (args.Text.IndexOf("Welcome to the Tesira Text Protocol Server...") > -1)
{ Debug.Console(1, this, "RX: '{0}'", args.Text);
// Indicates a new TTP session
try
SubscribeToAttributes(); {
} if (args.Text.IndexOf("Welcome to the Tesira Text Protocol Server...") > -1)
else if (args.Text.IndexOf("publishToken") > -1) {
{ // Indicates a new TTP session
// response is from a subscribed attribute
SubscribeToAttributes();
string pattern = "! \"publishToken\":[\"](.*)[\"] \"value\":(.*)"; }
else if (args.Text.IndexOf("publishToken") > -1)
Match match = Regex.Match(args.Text, pattern); {
// response is from a subscribed attribute
if (match.Success)
{ string pattern = "! \"publishToken\":[\"](.*)[\"] \"value\":(.*)";
string key; Match match = Regex.Match(args.Text, pattern);
string customName; if (match.Success)
{
string value;
string key;
customName = match.Groups[1].Value;
string customName;
// Finds the key (everything before the '~' character
key = customName.Substring(0, customName.IndexOf("~", 0) - 1); string value;
value = match.Groups[2].Value; customName = match.Groups[1].Value;
foreach (KeyValuePair<string, TesiraForteLevelControl> controlPoint in LevelControlPoints) // Finds the key (everything before the '~' character
{ key = customName.Substring(0, customName.IndexOf("~", 0) - 1);
if (customName == controlPoint.Value.LevelCustomName || customName == controlPoint.Value.MuteCustomName)
{ value = match.Groups[2].Value;
controlPoint.Value.ParseSubscriptionMessage(customName, value);
return; foreach (KeyValuePair<string, TesiraForteLevelControl> controlPoint in LevelControlPoints)
} {
if (customName == controlPoint.Value.LevelCustomName ||
} customName == controlPoint.Value.MuteCustomName)
} {
controlPoint.Value.ParseSubscriptionMessage(customName, value);
/// same for dialers return;
/// same for switchers }
} }
else if (args.Text.IndexOf("+OK") > -1) }
{
if (args.Text == "+OK" || args.Text.IndexOf("list\":") > -1 ) // Check for a simple "+OK" only 'ack' repsonse or a list response and ignore /// same for dialers
return; /// same for switchers
// response is not from a subscribed attribute. From a get/set/toggle/increment/decrement command }
else if (args.Text.IndexOf("+OK") > -1)
if (!CommandQueue.IsEmpty) {
{ if (args.Text == "+OK" || args.Text.IndexOf("list\":") > -1)
if (CommandQueue.Peek() is QueuedCommand) // Check for a simple "+OK" only 'ack' repsonse or a list response and ignore
{ return;
// Expected response belongs to a child class
QueuedCommand tempCommand = (QueuedCommand)CommandQueue.TryToDequeue(); // response is not from a subscribed attribute. From a get/set/toggle/increment/decrement command
//Debug.Console(1, this, "Command Dequeued. CommandQueue Size: {0}", CommandQueue.Count);
if (!CommandQueue.IsEmpty)
tempCommand.ControlPoint.ParseGetMessage(tempCommand.AttributeCode, args.Text); {
} if (CommandQueue.Peek() is QueuedCommand)
else {
{ // Expected response belongs to a child class
// Expected response belongs to this class QueuedCommand tempCommand = (QueuedCommand) CommandQueue.TryToDequeue();
string temp = (string)CommandQueue.TryToDequeue(); //Debug.Console(1, this, "Command Dequeued. CommandQueue Size: {0}", CommandQueue.Count);
//Debug.Console(1, this, "Command Dequeued. CommandQueue Size: {0}", CommandQueue.Count);
tempCommand.ControlPoint.ParseGetMessage(tempCommand.AttributeCode, args.Text);
} }
else
if (CommandQueue.IsEmpty) {
CommandQueueInProgress = false; // Expected response belongs to this class
else string temp = (string) CommandQueue.TryToDequeue();
SendNextQueuedCommand(); //Debug.Console(1, this, "Command Dequeued. CommandQueue Size: {0}", CommandQueue.Count);
} }
if (CommandQueue.IsEmpty)
} CommandQueueInProgress = false;
else if (args.Text.IndexOf("-ERR") > -1) else
{ SendNextQueuedCommand();
// Error response
}
switch (args.Text)
{
case "-ERR ALREADY_SUBSCRIBED": }
{ else if (args.Text.IndexOf("-ERR") > -1)
ResetSubscriptionTimer(); {
break; // Error response
}
default: switch (args.Text)
{ {
Debug.Console(0, this, "Error From DSP: '{0}'", args.Text); case "-ERR ALREADY_SUBSCRIBED":
break; {
} ResetSubscriptionTimer();
} break;
}
} default:
} {
catch (Exception e) Debug.Console(0, this, "Error From DSP: '{0}'", args.Text);
{ break;
if (Debug.Level == 2) }
Debug.Console(2, this, "Error parsing response: '{0}'\n{1}", args.Text, e); }
}
}
} }
catch (Exception e)
/// <summary> {
/// Sends a command to the DSP (with delimiter appended) if (Debug.Level == 2)
/// </summary> Debug.Console(2, this, "Error parsing response: '{0}'\n{1}", args.Text, e);
/// <param name="s">Command to send</param> }
public void SendLine(string s)
{ }
Debug.Console(1, this, "TX: '{0}'", s);
Communication.SendText(s + "\x0a"); /// <summary>
} /// Sends a command to the DSP (with delimiter appended)
/// </summary>
/// <summary> /// <param name="s">Command to send</param>
/// Adds a command from a child module to the queue public void SendLine(string s)
/// </summary> {
/// <param name="command">Command object from child module</param> Debug.Console(1, this, "TX: '{0}'", s);
public void EnqueueCommand(QueuedCommand commandToEnqueue) Communication.SendText(s + "\x0a");
{ }
CommandQueue.Enqueue(commandToEnqueue);
//Debug.Console(1, this, "Command (QueuedCommand) Enqueued '{0}'. CommandQueue has '{1}' Elements.", commandToEnqueue.Command, CommandQueue.Count); /// <summary>
/// Adds a command from a child module to the queue
if(!CommandQueueInProgress) /// </summary>
SendNextQueuedCommand(); /// <param name="command">Command object from child module</param>
} public void EnqueueCommand(QueuedCommand commandToEnqueue)
{
/// <summary> CommandQueue.Enqueue(commandToEnqueue);
/// Adds a raw string command to the queue //Debug.Console(1, this, "Command (QueuedCommand) Enqueued '{0}'. CommandQueue has '{1}' Elements.", commandToEnqueue.Command, CommandQueue.Count);
/// </summary>
/// <param name="command"></param> if (!CommandQueueInProgress)
public void EnqueueCommand(string command) SendNextQueuedCommand();
{ }
CommandQueue.Enqueue(command);
//Debug.Console(1, this, "Command (string) Enqueued '{0}'. CommandQueue has '{1}' Elements.", command, CommandQueue.Count);
/// <summary>
if (!CommandQueueInProgress) /// Adds a raw string command to the queue
SendNextQueuedCommand(); /// </summary>
} /// <param name="command"></param>
public void EnqueueCommand(string command)
/// <summary> {
/// Sends the next queued command to the DSP CommandQueue.Enqueue(command);
/// </summary> //Debug.Console(1, this, "Command (string) Enqueued '{0}'. CommandQueue has '{1}' Elements.", command, CommandQueue.Count);
void SendNextQueuedCommand()
{ if (!CommandQueueInProgress)
//Debug.Console(2, this, "Attempting to send next queued command. CommandQueueInProgress: {0} Communication isConnected: {1}", CommandQueueInProgress, Communication.IsConnected); SendNextQueuedCommand();
}
//if (CommandQueue.IsEmpty)
// CommandQueueInProgress = false; /// <summary>
/// Sends the next queued command to the DSP
//Debug.Console(1, this, "CommandQueue has {0} Elements:\n", CommandQueue.Count); /// </summary>
private void SendNextQueuedCommand()
//foreach (object o in CommandQueue) {
//{ //Debug.Console(2, this, "Attempting to send next queued command. CommandQueueInProgress: {0} Communication isConnected: {1}", CommandQueueInProgress, Communication.IsConnected);
// if (o is string)
// Debug.Console(1, this, "{0}", o); //if (CommandQueue.IsEmpty)
// else if(o is QueuedCommand) // CommandQueueInProgress = false;
// {
// var item = (QueuedCommand)o; //Debug.Console(1, this, "CommandQueue has {0} Elements:\n", CommandQueue.Count);
// Debug.Console(1, this, "{0}", item.Command);
// } //foreach (object o in CommandQueue)
//} //{
// if (o is string)
//Debug.Console(1, this, "End of CommandQueue"); // Debug.Console(1, this, "{0}", o);
// else if(o is QueuedCommand)
if (Communication.IsConnected && !CommandQueue.IsEmpty) // {
{ // var item = (QueuedCommand)o;
CommandQueueInProgress = true; // Debug.Console(1, this, "{0}", item.Command);
// }
if (CommandQueue.Peek() is QueuedCommand) //}
{
QueuedCommand nextCommand = new QueuedCommand(); //Debug.Console(1, this, "End of CommandQueue");
nextCommand = (QueuedCommand)CommandQueue.Peek(); if (Communication.IsConnected && !CommandQueue.IsEmpty)
{
SendLine(nextCommand.Command); CommandQueueInProgress = true;
}
else if (CommandQueue.Peek() is QueuedCommand)
{ {
string nextCommand = (string)CommandQueue.Peek(); QueuedCommand nextCommand = new QueuedCommand();
SendLine(nextCommand); nextCommand = (QueuedCommand) CommandQueue.Peek();
}
} SendLine(nextCommand.Command);
}
} else
{
/// <summary> string nextCommand = (string) CommandQueue.Peek();
/// Sends a command to execute a preset
/// </summary> SendLine(nextCommand);
/// <param name="name">Preset Name</param> }
public override void RunPreset(string name) }
{
SendLine(string.Format("DEVICE recallPreset {0}", name)); }
}
/// <summary>
public class QueuedCommand /// Sends a command to execute a preset
{ /// </summary>
public string Command { get; set; } /// <param name="name">Preset Name</param>
public string AttributeCode { get; set; } public void RunPreset(string name)
public TesiraForteControlPoint ControlPoint { get; set; } {
} SendLine(string.Format("DEVICE recallPreset {0}", name));
} }
public class BiampTesiraForteDspFactory : EssentialsDeviceFactory<BiampTesiraForteDsp> public class QueuedCommand
{ {
public BiampTesiraForteDspFactory() public string Command { get; set; }
{ public string AttributeCode { get; set; }
TypeNames = new List<string>() { "biamptesira" }; public TesiraForteControlPoint ControlPoint { get; set; }
} }
}
public override EssentialsDevice BuildDevice(DeviceConfig dc)
{ public class BiampTesiraForteDspFactory : EssentialsDeviceFactory<BiampTesiraForteDsp>
Debug.Console(1, "Factory Attempting to create new BiampTesira Device"); {
var comm = CommFactory.CreateCommForDevice(dc); public BiampTesiraForteDspFactory()
var props = Newtonsoft.Json.JsonConvert.DeserializeObject<BiampTesiraFortePropertiesConfig>( {
dc.Properties.ToString()); TypeNames = new List<string>() {"biamptesira"};
return new BiampTesiraForteDsp(dc.Key, dc.Name, comm, props); }
}
} public override EssentialsDevice BuildDevice(DeviceConfig dc)
{
} Debug.Console(1, "Factory Attempting to create new BiampTesira Device");
var comm = CommFactory.CreateCommForDevice(dc);
var props = Newtonsoft.Json.JsonConvert.DeserializeObject<BiampTesiraFortePropertiesConfig>(
dc.Properties.ToString());
return new BiampTesiraForteDsp(dc.Key, dc.Name, comm, props);
}
}
}

View File

@@ -15,18 +15,19 @@ namespace PepperDash.Essentials.Devices.Common.DSP
public Dictionary<string, DspControlPoint> DialerControlPoints { get; private set; } public Dictionary<string, DspControlPoint> DialerControlPoints { get; private set; }
public Dictionary<string, DspControlPoint> SwitcherControlPoints { get; private set; } public Dictionary<string, DspControlPoint> SwitcherControlPoints { get; private set; }
public abstract void RunPreset(string name); public DspBase(string key, string name) :
base(key, name)
public DspBase(string key, string name) : {
base(key, name) { } }
// in audio call feedback // in audio call feedback
// VOIP // VOIP
// Phone dialer // Phone dialer
} }
// Fusion // Fusion