Skip to content

Commit

Permalink
Merge pull request #13 from PepperDash/feature/socket-connection
Browse files Browse the repository at this point in the history
Feature/socket connection
  • Loading branch information
ngenovese11 authored Jun 7, 2023
2 parents 13bc3fa + 52a9b68 commit 32d8314
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 42 deletions.
2 changes: 2 additions & 0 deletions GetPackages.BAT
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@Echo ON
nuget install .\packages.config -OutputDirectory .\packages -excludeVersion
4 changes: 2 additions & 2 deletions epi-display-panasonic-projectors/IpCommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public string Delimiter

public string GetCommand(string cmd, string parameter)
{
return String.Format(CommandWithParameterOnly, cmd, parameter);
return String.Format("00{0}:{1}{2}", cmd, parameter, Delimiter);
}

public string GetCommand(string cmd)
{
return String.Format(CommandWithParameterOnly, cmd);
return String.Format("00{0}{1}", cmd, Delimiter);
}
}
}
28 changes: 28 additions & 0 deletions epi-display-panasonic-projectors/PanasonicIpStatusMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using PepperDash.Core;
using PepperDash.Essentials.Core;

namespace PepperDash.Essentials
{
public class PanasonicStatusMonitor : StatusMonitorBase
{
public PanasonicStatusMonitor(IKeyed parent, ICommunicationReceiver coms, long warningTime, long errorTime)
: base(parent, warningTime, errorTime)
{
coms.TextReceived += (sender, args) =>
{
Status = MonitorStatus.IsOk;
ResetErrorTimers();
};
}

public override void Start()
{
StartErrorTimers();
}

public override void Stop()
{
StopErrorTimers();
}
}
}
101 changes: 63 additions & 38 deletions epi-display-panasonic-projectors/PanasonicProjectorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
using PepperDash.Core;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Core.Bridges;
using PepperDash.Essentials.Core.Queues;
using PepperDash.Essentials.Core.Routing;
using PepperDash_Essentials_Core.Queues;

namespace PepperDash.Essentials.Displays
{
Expand Down Expand Up @@ -46,13 +46,8 @@ public class PanasonicProjectorController : TwoWayDisplayBase, IBridgeAdvanced,

private readonly IBasicCommunication _comms;

/// <summary>
/// Set this value to that of the delimiter used by the API (if applicable)
/// </summary>
private readonly string _commsDelimiter;

private readonly CommunicationGather _commsGather;
private readonly GenericCommunicationMonitor _commsMonitor;
private readonly StatusMonitorBase _commsMonitor;

private string _currentCommand;

Expand All @@ -75,6 +70,7 @@ public PanasonicProjectorController(string key, string name, PanasonicProjectorC

_txQueue = new CrestronQueue<string>(50);


_config = config;

if (_config.WarmupTimeInSeconds == 0)
Expand Down Expand Up @@ -108,9 +104,9 @@ public PanasonicProjectorController(string key, string name, PanasonicProjectorC
return;
}

_commsDelimiter = _commandBuilder.Delimiter;
var commsDelimiter = _commandBuilder.Delimiter;

_commsMonitor = new GenericCommunicationMonitor(this, _comms, 10000, 15000, 30000, Poll);
_commsMonitor = new PanasonicStatusMonitor(this, _comms, 60000, 120000);

var socket = _comms as ISocketStatus;
if (socket != null)
Expand All @@ -125,7 +121,7 @@ public PanasonicProjectorController(string key, string name, PanasonicProjectorC
// Only one of the below handlers should be necessary.

// _comms gather for any API that has a defined delimiter
_commsGather = new CommunicationGather(_comms, _commsDelimiter);
_commsGather = new CommunicationGather(_comms, commsDelimiter);
_commsGather.LineReceived += Handle_LineRecieved;

SetUpInputPorts();
Expand Down Expand Up @@ -195,6 +191,30 @@ public BoolFeedback OnlineFeedback
public override bool CustomActivate()
{
_commsMonitor.Start();

var pollTimer = new CTimer(_ =>
{
try
{
SendText(_commandBuilder.GetCommand("QPW"));
}
catch (Exception ex)
{
Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Caught an exception in the poll:{0}", ex.Message);
throw;
}
}, null, 50000, 50000);

CrestronEnvironment.ProgramStatusEventHandler += type =>
{
if (type != eProgramStatusEventType.Stopping)
return;

pollTimer.Stop();
pollTimer.Dispose();
_commsMonitor.Stop();
};

return base.CustomActivate();
}

Expand Down Expand Up @@ -227,12 +247,13 @@ private void SetUpInputPorts()
var digitalLink = new RoutingInputPort(RoutingPortNames.DmIn, eRoutingSignalType.Video,
eRoutingPortConnectionType.DmCat, new Action(() => SetInput(eInputTypes.Dl1)), this);


InputPorts.Add(hdmi1);
InputPorts.Add(dvi);
InputPorts.Add(computer1);
InputPorts.Add(computer2);
InputPorts.Add(video);
InputPorts.Add(sVideo);
InputPorts.Add(dvi);
InputPorts.Add(hdmi1);
InputPorts.Add(hdmi2);
InputPorts.Add(sdi);
InputPorts.Add(digitalLink);
Expand Down Expand Up @@ -267,10 +288,9 @@ private void socket_ConnectionChange(object sender, GenericSocketStatusChageEven
{
StatusFeedback.FireUpdate();
}

if (!args.Client.IsConnected && !_txQueue.IsEmpty)
{
_comms.Connect();
CrestronInvoke.BeginInvoke(_ => _comms.Connect());
}
}

Expand Down Expand Up @@ -298,36 +318,37 @@ public void SendText(string text)
return;
}

_txQueue.Enqueue(text);

if (!_comms.IsConnected)
if (_comms.IsConnected)
{
_comms.Connect();
_currentCommand = text;
_comms.SendText(String.IsNullOrEmpty(_hash) ? text : String.Format("{0}{1}", _hash, text));
}
else
{

_txQueue.Enqueue(text);
Debug.Console(1, this, "Queue isn't empty and client isn't connected, connecting...");
CrestronInvoke.BeginInvoke(_ => _comms.Connect());
}
}

/// <summary>
/// Polls the device
/// </summary>
/// <remarks>
/// Poll method is used by the communication monitor. Update the poll method as needed for the plugin being developed
/// </remarks>
public void Poll()
{
Debug.Console(1, this, "Sending poll...");
SendText(_commandBuilder.GetCommand("QPW"));
}

private void ParseResponse(string response)
{
//need to calculate hash
if (response.Contains("ntcontrol 1"))
if (response.ToLower().Contains("ntcontrol 1"))
{
_hash = GetHash(response);
DequeueAndSend(null);
return;
}

if (response.Contains("ntcontrol 0"))
if (response.ToLower().Contains("ntcontrol 0"))
{
DequeueAndSend(null);
return;
Expand Down Expand Up @@ -361,26 +382,30 @@ private void ParseResponse(string response)
{
CurrentInput = response.Replace("iis:", "").Trim();
}

}


private object DequeueAndSend(object notUsed)
{
if (_txQueue.IsEmpty)
{
return null;
}

var cmdToSend = _txQueue.Dequeue(10);
if (_txQueue.IsEmpty)
{
Debug.Console(1, this, "Queue is empty we're out!");
return null;
}

if (String.IsNullOrEmpty(cmdToSend))
{
Debug.Console(1, this, "Unable to get command to send");
return null;
}
var cmdToSend = _txQueue.Dequeue(10);

_currentCommand = cmdToSend;
if (String.IsNullOrEmpty(cmdToSend))
{
Debug.Console(1, this, "Unable to get command to send");

}

_comms.SendText(String.IsNullOrEmpty(_hash) ? cmdToSend : String.Format("{0}{1}", _hash, cmdToSend));
_currentCommand = cmdToSend;
_comms.SendText(String.IsNullOrEmpty(_hash) ? cmdToSend : String.Format("{0}{1}", _hash, cmdToSend));


return null;
}
Expand Down
2 changes: 1 addition & 1 deletion epi-display-panasonic-projectors/QueueMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using PepperDash_Essentials_Core.Queues;
using PepperDash.Essentials.Core.Queues;

namespace PepperDash.Essentials.Displays
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="eInputTypes.cs" />
<Compile Include="ICommandBuilder.cs" />
<Compile Include="IpCommandBuilder.cs" />
<Compile Include="PanasonicIpStatusMonitor.cs" />
<Compile Include="PanasonicProjectorControllerConfig.cs" />
<Compile Include="PanasonicProjectorControllerFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
2 changes: 1 addition & 1 deletion packages.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<packages>
<package id="PepperDashEssentials" version="1.7.6" targetFramework="net35" allowedVersions="[1.0,2.0)"/>
<package id="PepperDashEssentials" version="1.10.5" targetFramework="net35" allowedVersions="[1.0,2.0)"/>
</packages>

0 comments on commit 32d8314

Please sign in to comment.