Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/PepperDash.Essentials.Core/Comm and IR/ComPortController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class ComPortController : Device, IBasicCommunicationWithStreamDebugging

ComPort Port;
ComPort.ComPortSpec Spec;
private readonly object _deactivateLock = new object();
private bool _isDeactivated;

/// <summary>
/// Constructor
Expand All @@ -59,6 +61,8 @@ public ComPortController(string key, Func<EssentialsControlPropertiesConfig, Com

RegisterAndConfigureComPort();
});

CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler;
}
Comment on lines 62 to 66

/// <summary>
Expand All @@ -81,6 +85,7 @@ public ComPortController(string key, ComPort port, ComPort.ComPortSpec spec)
//IsConnected = new BoolFeedback(CommonBoolCue.IsConnected, () => true);

RegisterAndConfigureComPort();
CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler;
}

private void RegisterAndConfigureComPort()
Expand Down Expand Up @@ -114,14 +119,6 @@ private void RegisterAndConfigureComPort()
Port.SerialDataReceived += Port_SerialDataReceived;
}

/// <summary>
/// Destructor
/// </summary>
~ComPortController()
{
Port.SerialDataReceived -= Port_SerialDataReceived;
}

void Port_SerialDataReceived(ComPort ReceivingComPort, ComPortSerialDataEventArgs args)
{
OnDataReceived(args.SerialData);
Expand Down Expand Up @@ -150,12 +147,34 @@ void OnDataReceived(string s)
if (!eventSubscribed) Debug.LogMessage(LogEventLevel.Warning, this, "Received data but no handler is registered");
}

void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
{
if (programEventType == eProgramStatusEventType.Stopping)
{
Deactivate();
}
}

/// <summary>
/// Deactivate method
/// </summary>
/// <inheritdoc />
public override bool Deactivate()
{
lock (_deactivateLock)
{
if (_isDeactivated)
return true;

Comment on lines +164 to +168
_isDeactivated = true;
}

CrestronEnvironment.ProgramStatusEventHandler -= CrestronEnvironment_ProgramStatusEventHandler;

if (Port == null)
return true;

Port.SerialDataReceived -= Port_SerialDataReceived;
return Port.UnRegister() == eDeviceRegistrationUnRegistrationResponse.Success;
}

Expand Down
Loading