From 21e393ed6c0de6bcd56d8b7be7df15f631bea0ed Mon Sep 17 00:00:00 2001 From: Andrew Welker Date: Tue, 14 May 2024 08:50:37 -0500 Subject: [PATCH] fix: update parsing/response handling for rs232 --- src/Rs232Commands.cs | 26 ++ src/Rs232ParsingUtils.cs | 7 +- src/SonyBraviaDevice.cs | 501 ++++++++++++++++++++++----------------- src/SonyBraviaInputs.cs | 20 +- 4 files changed, 331 insertions(+), 223 deletions(-) diff --git a/src/Rs232Commands.cs b/src/Rs232Commands.cs index 788c372..1f2d544 100644 --- a/src/Rs232Commands.cs +++ b/src/Rs232Commands.cs @@ -311,6 +311,29 @@ public static IQueueMessage GetInputQuery(IBasicCommunication coms, Action action; + private readonly byte[] _message; + + public Rs232Response(byte[] message, Action action) + { + _message = message; + + this.action = action; + } + + public void Dispatch() + { + if(action == null || _message.Length == 0) + { + return; + } + + action(_message); + } + } + public class Rs232Command:IQueueMessage { private readonly Action _action; @@ -321,6 +344,8 @@ public class Rs232Command:IQueueMessage private readonly eCommandType _commandType; + public eCommandType CommandType => _commandType; + public Rs232Command(IBasicCommunication coms, byte[] message, Action updateCommandAction, eCommandType commandType) { if(coms == null) @@ -360,6 +385,7 @@ public void Dispatch() { _action(_commandType); + Debug.Console(DebugLevels.DebugLevel, "Sending command {0}", _message.ToReadableString()); _comm.SendBytes(_message); } diff --git a/src/Rs232ParsingUtils.cs b/src/Rs232ParsingUtils.cs index 5e3f106..8830d8b 100644 --- a/src/Rs232ParsingUtils.cs +++ b/src/Rs232ParsingUtils.cs @@ -26,10 +26,11 @@ public static class Rs232ParsingUtils public static bool ParsePowerResponse(this byte[] response) { - Debug.Console(DebugLevels.DebugLevel, "ParsePowerResponse response: {0}", response.ToReadableString()); + Debug.Console(DebugLevels.DebugLevel, "ParsePowerResponse response: {0}", ComTextHelper.GetEscapedText(response)); if (response[2] == 0x00) { + return response[3] == 0x01; } @@ -44,7 +45,7 @@ public static bool ParsePowerResponse(this byte[] response) public static string ParseInputResponse(this byte[] response) { // TODO [ ] actually add in parsing - Debug.Console(DebugLevels.DebugLevel, "ParseInputResponse response: {0}", response.ToReadableString()); + Debug.Console(DebugLevels.DebugLevel, "ParseInputResponse response: {0}", ComTextHelper.GetEscapedText(response)); //add together the input type byte & the input number byte var inputNumber = response[3] << 8 | response[4]; @@ -62,6 +63,7 @@ public static string ParseInputResponse(this byte[] response) public static int ParseVolumeResponse(this byte[] response) { + Debug.Console(DebugLevels.DebugLevel, "ParseVolumeResponse response: {0}", ComTextHelper.GetEscapedText(response)); //not a direct volume response if (response[3] != 0x01) { @@ -78,6 +80,7 @@ public static int ParseVolumeResponse(this byte[] response) /// public static bool ParseMuteResponse(this byte[] response) { + Debug.Console(DebugLevels.DebugLevel, "ParseMuteResponse response: {0}", ComTextHelper.GetEscapedText(response)); //not a direct mute response if (response[3] != 0x01) { return false; } diff --git a/src/SonyBraviaDevice.cs b/src/SonyBraviaDevice.cs index 0f88eca..feb66d6 100644 --- a/src/SonyBraviaDevice.cs +++ b/src/SonyBraviaDevice.cs @@ -38,9 +38,11 @@ public class SonyBraviaDevice : TwoWayDisplayBase, ICommunicationMonitor, IBridg private readonly CTimer _pollTimer; private readonly IQueueMessage _powerOffCommand; - private readonly IQueueMessage _powerOnCommand; + private readonly IQueueMessage _powerOnCommand; - private readonly CrestronQueue _queueRs232; + private byte[] _incomingBuffer = { }; + + private readonly GenericQueue _queueRs232; private readonly CrestronQueue _queueSimpleIp; private string _currentInput; private bool _powerIsOn; @@ -49,6 +51,8 @@ public class SonyBraviaDevice : TwoWayDisplayBase, ICommunicationMonitor, IBridg private readonly long _coolingTimeMs; private readonly long _warmingtimeMs; + private int pollIndex = 0; + private Dictionary _ackStringFormats = new Dictionary { {0x00, "Control complete ({0})"}, {0x01, "Abnormal End: over maximum value ({0})" }, @@ -81,8 +85,15 @@ public SonyBraviaDevice(DeviceConfig config, IBasicCommunication comms) _comsIsRs232 = socket == null || props.ForceRs232; if (_comsIsRs232) { - _queueRs232 = new CrestronQueue(50); - _coms.BytesReceived += (sender, args) => _queueRs232.Enqueue(args.Bytes); + _queueRs232 = new GenericQueue(string.Format("{0}-r232queue", Key), 50); + + _coms.BytesReceived += (sender, args) => { + Debug.Console(DebugLevels.DebugLevel, this, "received response: {0}", ComTextHelper.GetEscapedText(args.Bytes)); + _queueRs232.Enqueue(new Rs232Response(args.Bytes, ProcessRs232Response)); + }; + + //_coms.BytesReceived += (sender, args) => ProcessRs232Response(args.Bytes); + _powerOnCommand = Rs232Commands.GetPowerOn(_coms, UpdateLastSentCommandType); _powerOffCommand = Rs232Commands.GetPowerOff(_coms, UpdateLastSentCommandType); @@ -106,7 +117,7 @@ public SonyBraviaDevice(DeviceConfig config, IBasicCommunication comms) } if (CommandQueue == null) - CommandQueue = new GenericQueue(string.Format("{0}-commandQueue", config.Key),100, 50); + CommandQueue = new GenericQueue(string.Format("{0}-commandQueue", config.Key),500, 50); var monitorConfig = props.CommunicationMonitorProperties ?? DefaultMonitorConfig; CommunicationMonitor = new GenericCommunicationMonitor( @@ -119,10 +130,12 @@ public SonyBraviaDevice(DeviceConfig config, IBasicCommunication comms) var worker = _comsIsRs232 - ? new Thread(ProcessRs232Response, null) + ? null //new Thread(ProcessRs232Response, null) : new Thread(ProcessSimpleIpResponse, null); - _pollTimer = new CTimer((o) => Poll(new List { powerQuery, inputQuery, muteQuery, volumeQuery }),Timeout.Infinite); + _pollTimer = _comsIsRs232 + ? new CTimer((o) => PollRs232(new List { Rs232Commands.PowerQuery.WithChecksum(), Rs232Commands.InputQuery.WithChecksum(), Rs232Commands.VolumeQuery.WithChecksum(), Rs232Commands.MuteQuery.WithChecksum()}),Timeout.Infinite) + : new CTimer((o) => Poll(new List { powerQuery, inputQuery, muteQuery, volumeQuery }),Timeout.Infinite); MuteFeedback = new BoolFeedback(() => _muted); VolumeLevelFeedback = new IntFeedback(() => CrestronEnvironment.ScaleWithLimits(_rawVolume, 255, 0, 65535, 0)); @@ -153,7 +166,7 @@ public override void Initialize() { _coms.Connect(); CommunicationMonitor.Start(); - _pollTimer.Reset(5000, 15000); + _pollTimer.Reset(0, 1000); } catch (Exception ex) { @@ -162,10 +175,30 @@ public override void Initialize() } } + private void PollRs232(List pollCommands) + { + if (pollIndex >= pollCommands.Count) + { + pollIndex = 0; + } + + var command = pollCommands[pollIndex]; + + Debug.Console(2, this, "Sending command {0}", ComTextHelper.GetEscapedText(command)); + + _lastCommand = command; + _coms.SendBytes(command); + + pollIndex += 1; + } + + private byte[] _lastCommand; + private eCommandType _lastCommandType; private void UpdateLastSentCommandType(eCommandType commandType) { + Debug.Console(DebugLevels.TraceLevel, this, "Setting last command type to {0}", commandType); _lastCommandType = commandType; } @@ -289,8 +322,14 @@ public static void Poll(List commands) /// public override void PowerOn() { + if (_comsIsRs232) { + var command = Rs232Commands.PowerOn.WithChecksum(); + _lastCommand = command; + _coms.SendBytes(command); + return; + } CommandQueue.Enqueue(_powerOnCommand); - _pollTimer.Reset(1000, 15000); + _pollTimer.Reset(1000, 1000); } /// @@ -298,6 +337,13 @@ public override void PowerOn() /// public override void PowerOff() { + if (_comsIsRs232) + { + var command = Rs232Commands.PowerOff.WithChecksum(); + _lastCommand = command; + _coms.SendBytes(command); + return; + } CommandQueue.Enqueue(_powerOffCommand); _pollTimer.Reset(1000, 15000); } @@ -411,64 +457,64 @@ private void SetupInputs() Items = new Dictionary { { - "hdmi1", new SonyBraviaInput("Hdmi1", "HDMI 1", this, - _comsIsRs232 ? Rs232Commands.GetHdmi1(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 1))}, + "hdmi1", _comsIsRs232 + ? new SonyBraviaInput("Hdmi1", "HDMI 1", _coms, Rs232Commands.InputHdmi1.WithChecksum()) + : new SonyBraviaInput("Hdmi1", "HDMI 1", this, SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 1)) + }, { - "hdmi2", new SonyBraviaInput("hdmi2", "HDMI 2", this, - _comsIsRs232 ? Rs232Commands.GetHdmi2(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 2)) + "hdmi2", _comsIsRs232 + ? new SonyBraviaInput("Hdmi2", "HDMI 2", _coms, Rs232Commands.InputHdmi2.WithChecksum()) + : new SonyBraviaInput("Hdmi2", "HDMI 2", this, SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 2)) }, { - "hdmi3", new SonyBraviaInput("hdmi3", "HDMI 3", this, - _comsIsRs232 ? Rs232Commands.GetHdmi3(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 3)) + "hdmi3", _comsIsRs232 + ? new SonyBraviaInput("Hdmi3", "HDMI 3", _coms, Rs232Commands.InputHdmi3.WithChecksum()) + : new SonyBraviaInput("Hdmi3", "HDMI 3", this, SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 3)) }, { - "hdmi4", new SonyBraviaInput("hdmi4", "HDMI 4", this, - _comsIsRs232 ? Rs232Commands.GetHdmi4(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 4)) + "hdmi4", _comsIsRs232 + ? new SonyBraviaInput("Hdmi4", "HDMI 4", _coms, Rs232Commands.InputHdmi4.WithChecksum()) + : new SonyBraviaInput("Hdmi4", "HDMI 4", this, SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 4)) }, { - "hdmi5", new SonyBraviaInput("hdmi5", "HDMI 5", this, - _comsIsRs232 ? Rs232Commands.GetHdmi5(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 5)) + "hdmi5", _comsIsRs232 + ? new SonyBraviaInput("Hdmi5", "HDMI 5", _coms, Rs232Commands.InputHdmi5.WithChecksum()) + : new SonyBraviaInput("Hdmi5", "HDMI 5", this, SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 5)) }, { - "video1", new SonyBraviaInput("video1", "Video 1", this, - _comsIsRs232 ? Rs232Commands.GetVideo1(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 1)) + "video1",_comsIsRs232 + ? new SonyBraviaInput("video1", "Video 1", _coms, Rs232Commands.InputVideo1.WithChecksum()) + : new SonyBraviaInput("video1", "Video 1", this, SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 1)) }, { - "video2", new SonyBraviaInput("video2", "Video 2", this, - _comsIsRs232 ? Rs232Commands.GetVideo2(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 2)) + "video2",_comsIsRs232 + ? new SonyBraviaInput("video2", "Video 2", _coms, Rs232Commands.InputVideo2.WithChecksum()) + : new SonyBraviaInput("video2", "Video 2", this, SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 2)) }, { - "video3", new SonyBraviaInput("video3", "Video 3", this, - _comsIsRs232 ? Rs232Commands.GetVideo3(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 3)) + "video3", _comsIsRs232 + ? new SonyBraviaInput("video3", "Video 3", _coms, Rs232Commands.InputVideo3.WithChecksum()) + : new SonyBraviaInput("video3", "Video 3", this, SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 3)) }, { - "component1", new SonyBraviaInput("component1", "Component 1", this, - _comsIsRs232 ? Rs232Commands.GetComponent1(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 1)) + "component1", _comsIsRs232 + ? new SonyBraviaInput("component1", "Component 1", _coms, Rs232Commands.InputComponent1.WithChecksum()) + : new SonyBraviaInput("component1", "Component 1", this,SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 1)) }, { - "component2", new SonyBraviaInput("component2", "Component 2", this, - _comsIsRs232 ? Rs232Commands.GetComponent2(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 2)) + "component2", _comsIsRs232 + ? new SonyBraviaInput("component2", "Component 2", _coms, Rs232Commands.InputComponent2.WithChecksum()) + : new SonyBraviaInput("component2", "Component 2", this,SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 2)) }, { - "component3", new SonyBraviaInput("component3", "Component 3", this, - _comsIsRs232 ? Rs232Commands.GetComponent3(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 3)) + "component3", _comsIsRs232 + ? new SonyBraviaInput("component3", "Component 3", _coms, Rs232Commands.InputComponent3.WithChecksum()) + : new SonyBraviaInput("component3", "Component 3", this,SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 3)) }, { - "vga1", new SonyBraviaInput("vga1", "VGA 1", this, - _comsIsRs232 ? Rs232Commands.GetComponent1(_coms, UpdateLastSentCommandType) - : null) + "vga1",_comsIsRs232 ? new SonyBraviaInput("vga1", "VGA 1", _coms, Rs232Commands.InputComponent1.WithChecksum()) + : new SonyBraviaInput("vga1", "VGA 1", this, null) } } }; @@ -480,9 +526,15 @@ private void SetupInputs() /// public void InputHdmi1() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetHdmi1(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 1)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputHdmi1.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 1)); } /// @@ -490,9 +542,15 @@ public void InputHdmi1() /// public void InputHdmi2() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetHdmi2(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 2)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputHdmi2.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 2)); } /// @@ -500,9 +558,15 @@ public void InputHdmi2() /// public void InputHdmi3() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetHdmi3(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 3)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputHdmi3.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 3)); } /// @@ -510,9 +574,15 @@ public void InputHdmi3() /// public void InputHdmi4() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetHdmi4(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 4)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputHdmi4.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 4)); } /// @@ -520,9 +590,15 @@ public void InputHdmi4() /// public void InputHdmi5() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetHdmi5(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 5)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputHdmi5.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Hdmi, 5)); } /// @@ -530,9 +606,15 @@ public void InputHdmi5() /// public void InputVideo1() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetVideo1(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 1)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputVideo1.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 1)); } /// @@ -540,9 +622,15 @@ public void InputVideo1() /// public void InputVideo2() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetVideo2(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 2)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputVideo2.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 2)); } /// @@ -550,9 +638,15 @@ public void InputVideo2() /// public void InputVideo3() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetVideo3(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 3)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputVideo3.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Composite, 3)); } /// @@ -560,9 +654,15 @@ public void InputVideo3() /// public void InputComponent1() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetComponent1(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 1)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputComponent1.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 1)); } /// @@ -570,9 +670,15 @@ public void InputComponent1() /// public void InputComponent2() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetComponent2(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 2)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputComponent2.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 2)); } /// @@ -580,9 +686,15 @@ public void InputComponent2() /// public void InputComponent3() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetComponent3(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 3)); + if (_comsIsRs232) + { + var command = Rs232Commands.InputComponent3.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; + } + + CommandQueue.Enqueue(SimpleIpCommands.GetInputCommand(_coms, SimpleIpCommands.InputTypes.Component, 3)); } /// @@ -590,22 +702,14 @@ public void InputComponent3() /// public void InputVga1() { - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetComponent1(_coms, UpdateLastSentCommandType) - : null); - } + if (!_comsIsRs232) return; - /// - /// Poll device for input state - /// - public void InputPoll() - { - // byte[] poll = { 0x83, 0x00, 0x02, 0xFF, 0xFF, 0x83 }; - //CommandQueue.Enqueue(Rs232Commands.GetInputQuery(_coms)); - CommandQueue.Enqueue(_comsIsRs232 - ? Rs232Commands.GetInputQuery(_coms, UpdateLastSentCommandType) - : SimpleIpCommands.GetQueryCommand(_coms, "INPT")); + var command = Rs232Commands.InputComponent1.WithChecksum(); + _coms.SendBytes(command); + _lastCommand = command; + return; } + /// /// Execute switch @@ -641,150 +745,100 @@ public override void ExecuteSwitch(object selector) } } - private object ProcessRs232Response(object _) + private void ProcessRs232Response(byte[] response) { - var seperator = new string('-', 50); - - byte[] buffer = null; - while (true) + try { - try + var buffer = new byte[_incomingBuffer.Length + response.Length]; + _incomingBuffer.CopyTo(buffer, 0); + response.CopyTo(buffer, _incomingBuffer.Length); + + Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: {0}", ComTextHelper.GetEscapedText(buffer)); + + //it starts a valid response + if (buffer.Length >= 3) { - var bytes = _queueRs232.Dequeue(); - if (bytes == null) - { - Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: _queueRs232.Dequeue failed, object was null"); - return null; - } - - Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: bytes-'{0}' (len-'{1}')", bytes.ToReadableString(), bytes.Length); - - if (buffer == null) - buffer = bytes; - else + //If the message is an ack, byte3 will be the sum of the first 2 bytes + if (buffer[0] == 0x70 & buffer[2] == buffer[0] + buffer[1]) { - var newBuffer = new byte[buffer.Length + bytes.Length]; - buffer.CopyTo(newBuffer, 0); - bytes.CopyTo(newBuffer, buffer.Length); - buffer = newBuffer; + var message = new byte[3]; + buffer.CopyTo(message, 0); + HandleAck(message); + byte[] clear = { }; + _incomingBuffer = clear; + return; } - Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: bytes-'{0}' (len-'{1}') | buffer-'{2}' (len-'{3}')", bytes.ToReadableString(), bytes.Length, buffer.ToReadableString(), buffer.Length); + //header length is 3. + var messageLength = 3 + buffer[2]; - if (!buffer.ContainsHeader()) + if(buffer[0] == 0x70 && buffer.Length >= messageLength) { - Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: buffer-'{0}' (len-'{1}') did not contain a header", - buffer.ToReadableString(), buffer.Length); - - continue; + var message = new byte[messageLength]; + buffer.CopyTo(message, 0); + ParseMessage(message); + byte[] clear = { }; + _incomingBuffer = clear; + return; } + } - if (buffer.ElementAtOrDefault(0) != 0x70) - buffer = buffer.CleanToFirstHeader(); - - Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: bytes-'{0}' (len-'{1}') | buffer-'{2}' (len-'{3}')", - bytes.ToReadableString(), bytes.Length, buffer.ToReadableString(), buffer.Length); + if (buffer[0] == 0x70) + { + _incomingBuffer = buffer; + } else + { + byte[] clear = { }; + _incomingBuffer = clear; + } + + } + catch (Exception ex) + { + Debug.Console(DebugLevels.TraceLevel, this, Debug.ErrorLogLevel.Error, "ProcessRs232Response Exception: {0}", ex.Message); + Debug.Console(DebugLevels.DebugLevel, this, Debug.ErrorLogLevel.Error, "ProcessRs232Response Exception Stack Trace: {0}", ex.StackTrace); + if (ex.InnerException != null) + Debug.Console(DebugLevels.ErrorLevel, this, Debug.ErrorLogLevel.Error, "ProcessRs232Response Inner Exception: {0}", ex.InnerException); + } + } - const int safety = 10; - var numberOfSpins = 0; - while (buffer.Length >= 3 && numberOfSpins <= safety) + private void ParseMessage(byte[] message) + { + // 3rd byte is the command type + switch (_lastCommand[2]) + { + case 0x00: //power + PowerIsOn = Rs232ParsingUtils.ParsePowerResponse(message); + PowerIsOnFeedback.FireUpdate(); + break; + case 0x02: //input + _currentInput = Rs232ParsingUtils.ParseInputResponse(message); + CurrentInputFeedback.FireUpdate(); +#if SERIES4 + if (Inputs.Items.ContainsKey(_currentInput)) { - ++numberOfSpins; - if (numberOfSpins == safety) - Debug.Console(0, - this, - Debug.ErrorLogLevel.Notice, - "We hit our safety limit, something is wrong... Buffer:{0}, Bytes:{1}", - buffer.ToReadableString(), - bytes.ToReadableString()); - - var message = buffer.GetFirstMessage(); - Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: bytes-'{0}' (len-'{1}') | buffer-'{2}' (len-'{3}') | message-'{4}' (len-'{5}')", - bytes.ToReadableString(), bytes.Length, - buffer.ToReadableString(), buffer.Length, - message.ToReadableString(), message.Length); - switch (_lastCommandType) + foreach(var input in Inputs.Items) { - case eCommandType.Command: - { - HandleAck(message); - buffer = buffer.CleanOutFirstMessage(); - continue; - } - - } - - // we have a full message, lets check it out - Debug.Console(DebugLevels.ErrorLevel, this, "ProcessRs232Response: message-'{0}' (len-'{1}')", - message.ToReadableString(), message.Length); - - var dataSize = message[2]; - var totalDataSize = dataSize + 3; - var isComplete = totalDataSize == message.Length; - Debug.Console( - DebugLevels.ErrorLevel, this, "ProcessRs232Response: dataSize-'{0}' | totalDataSize-'{1}' | message.Length-'{2}'", - dataSize, totalDataSize, message.Length); - - if (!isComplete) - { - Debug.Console(DebugLevels.DebugLevel, this, "Message is incomplete... spinning around"); - break; + input.Value.IsSelected = input.Key.Equals(_currentInput); } + } - switch (_lastCommandType) - { - case eCommandType.PowerQuery: - { - PowerIsOn = buffer.ParsePowerResponse(); - - PowerIsOnFeedback.FireUpdate(); - break; - } - case eCommandType.InputQuery: - { - _currentInput = buffer.ParseInputResponse(); - CurrentInputFeedback.FireUpdate(); - -#if SERIES4 - if (Inputs.Items.ContainsKey(_currentInput)) - { - foreach (var item in Inputs.Items) - { - item.Value.IsSelected = item.Key.Equals(_currentInput); - } - } - - Inputs.CurrentItem = _currentInput; + Inputs.CurrentItem = _currentInput; #endif - break; - } - case eCommandType.VolumeQuery: - { - _rawVolume = buffer.ParseVolumeResponse(); - - VolumeLevelFeedback.FireUpdate(); - break; - } - case eCommandType.MuteQuery: - { - _muted = buffer.ParseMuteResponse(); - MuteFeedback.FireUpdate(); - break; - } - } - buffer = buffer.NumberOfHeaders() > 1 ? buffer.CleanOutFirstMessage() : new byte[0]; - } - } - catch (Exception ex) - { - Debug.Console(DebugLevels.TraceLevel, this, Debug.ErrorLogLevel.Error, "ProcessRs232Response Exception: {0}", ex.Message); - Debug.Console(DebugLevels.DebugLevel, this, Debug.ErrorLogLevel.Error, "ProcessRs232Response Exception Stack Trace: {0}", ex.StackTrace); - if (ex.InnerException != null) - Debug.Console(DebugLevels.ErrorLevel, this, Debug.ErrorLogLevel.Error, "ProcessRs232Response Inner Exception: {0}", ex.InnerException); - Debug.Console(DebugLevels.DebugLevel, this, seperator); - } + break; + case 0x05: //volume + _rawVolume = Rs232ParsingUtils.ParseVolumeResponse(message); + VolumeLevelFeedback.FireUpdate(); + break; + case 0x06: //mute + _muted = Rs232ParsingUtils.ParseMuteResponse(message); + MuteFeedback.FireUpdate(); + break; + default: + Debug.Console(0, this, "Unknown response received: {0}", ComTextHelper.GetEscapedText(message)); + break; } } @@ -794,11 +848,11 @@ private void HandleAck(byte[] message) if (!_ackStringFormats.TryGetValue(message[1], out consoleMessageFormat)) { - Debug.Console(DebugLevels.DebugLevel, this, "Unknown Response: {0}", message.ToReadableString()); + Debug.Console(DebugLevels.DebugLevel, this, "Unknown Response: {0}", ComTextHelper.GetEscapedText(message)); return; } - Debug.Console(DebugLevels.DebugLevel, this, consoleMessageFormat, message.ToReadableString()); + Debug.Console(DebugLevels.DebugLevel, this, consoleMessageFormat, ComTextHelper.GetEscapedText(message)); } private object ProcessSimpleIpResponse(object _) @@ -949,6 +1003,8 @@ public void SetInput(string selector) { input.Select(); } + + _pollTimer.Reset(1000, 15000); } #endif public void MuteOn() @@ -956,6 +1012,7 @@ public void MuteOn() CommandQueue.Enqueue(_comsIsRs232 ? Rs232Commands.GetMuteOn(_coms, UpdateLastSentCommandType) : null); + _pollTimer.Reset(1000, 15000); } public void MuteOff() @@ -963,6 +1020,7 @@ public void MuteOff() CommandQueue.Enqueue(_comsIsRs232 ? Rs232Commands.GetMuteOff(_coms, UpdateLastSentCommandType) : null); + _pollTimer.Reset(1000, 15000); } public void MuteToggle() { @@ -982,6 +1040,7 @@ public void SetVolume(ushort level) CommandQueue.Enqueue(_comsIsRs232 ? Rs232Commands.GetVolumeDirect(_coms, UpdateLastSentCommandType, scaledVolume) : null); + _pollTimer.Reset(1000, 15000); } public void VolumeUp(bool pressRelease) @@ -989,6 +1048,7 @@ public void VolumeUp(bool pressRelease) CommandQueue.Enqueue(_comsIsRs232 ? Rs232Commands.GetVolumeUp(_coms, UpdateLastSentCommandType) : null); + _pollTimer.Reset(1000, 15000); } public void VolumeDown(bool pressRelease) @@ -996,6 +1056,7 @@ public void VolumeDown(bool pressRelease) CommandQueue.Enqueue(_comsIsRs232 ? Rs232Commands.GetVolumeDown(_coms, UpdateLastSentCommandType) : null); + _pollTimer.Reset(1000, 15000); } diff --git a/src/SonyBraviaInputs.cs b/src/SonyBraviaInputs.cs index 0c24c88..f2859c6 100644 --- a/src/SonyBraviaInputs.cs +++ b/src/SonyBraviaInputs.cs @@ -1,4 +1,5 @@  +using PepperDash.Core; using PepperDash.Essentials.Core.DeviceTypeInterfaces; using PepperDash.Essentials.Core.Queues; using SonyBraviaEpi; @@ -60,8 +61,11 @@ public class SonyBraviaInput : ISelectableItem { private bool _isSelected; + private readonly byte[] _command; + private readonly IBasicCommunication _coms; + private readonly IQueueMessage _inputCommand; - private readonly SonyBraviaDevice _parent; + private readonly SonyBraviaDevice _parent; public SonyBraviaInput(string key, string name, SonyBraviaDevice parent, IQueueMessage inputCommand) { @@ -71,6 +75,14 @@ public SonyBraviaInput(string key, string name, SonyBraviaDevice parent, IQueueM _inputCommand = inputCommand; } + public SonyBraviaInput(string key, string name, IBasicCommunication coms, byte[] command) + { + Key = key; + Name = name; + _coms = coms; + _command = command; + } + public string Key { get; private set; } public string Name { get; private set; } @@ -93,6 +105,12 @@ public bool IsSelected public void Select() { + if(_coms != null && _command != null) + { + _coms.SendBytes(_command); + return; + } + _parent.EnqueueCommand(_inputCommand); } }