From 8ad3d686f463fd8ff449ee92e4da9bd8cc2e7268 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Sun, 26 Jan 2025 19:14:18 -0600 Subject: [PATCH] fix up SLIP decoding and data-less qlab replies --- qController/Classes/Communication/QParser.cs | 44 +++++++++++++++---- .../Communication/SharpOSC/OscMessage.cs | 11 +++-- .../Communication/SharpOSC/OscPacket.cs | 5 +++ .../Communication/SharpOSC/SlipFrame.cs | 2 +- .../Communication/SharpOSC/TCPClient.cs | 43 +++++++----------- qController/Pages/ControlPage.xaml.cs | 3 ++ 6 files changed, 68 insertions(+), 40 deletions(-) diff --git a/qController/Classes/Communication/QParser.cs b/qController/Classes/Communication/QParser.cs index 7941aa9..1433849 100644 --- a/qController/Classes/Communication/QParser.cs +++ b/qController/Classes/Communication/QParser.cs @@ -81,21 +81,41 @@ namespace qController.Communication public void ParseCueUpdateInfo(OscMessage msg) { JToken cueUpdate = OSC2JSON(msg); - QCue cue = JsonConvert.DeserializeObject(cueUpdate.ToString()); - OnCueInfoUpdated(cue); + if(cueUpdate != null) + { + QCue cue = JsonConvert.DeserializeObject(cueUpdate.ToString()); + OnCueInfoUpdated(cue); + }else + { + Log.Debug("QPARSER - cue update info does not have any data"); + } } public void ParseSelectedCueInfo(OscMessage msg){ JToken selectedCue = OSC2JSON(msg); - QCue cue = JsonConvert.DeserializeObject(selectedCue.ToString()); - OnSelectedCueUpdated(cue); + if (selectedCue != null) + { + QCue cue = JsonConvert.DeserializeObject(selectedCue.ToString()); + OnSelectedCueUpdated(cue); + } + else + { + Log.Debug("QPARSER - selected does not have any data"); + } } public void ParseQInfo(OscMessage msg) { JToken qInfo = OSC2JSON(msg); - List qWorkspaceInfo = JsonConvert.DeserializeObject>(qInfo.ToString()); - OnWorkspaceInfoReceived(qWorkspaceInfo); + if (qInfo != null) + { + List qWorkspaceInfo = JsonConvert.DeserializeObject>(qInfo.ToString()); + OnWorkspaceInfoReceived(qWorkspaceInfo); + } + else + { + Log.Debug("QPARSER - selected does not have any data"); + } } public void ParseWorkspaceInfo(OscMessage msg) @@ -131,8 +151,15 @@ namespace qController.Communication { string cue_id = msg.Address.Split('/')[3]; JToken children_obj = OSC2JSON(msg); - List children = JsonConvert.DeserializeObject>(children_obj.ToString()); - OnChildrenUpdated(cue_id, children); + if(children_obj != null) + { + List children = JsonConvert.DeserializeObject>(children_obj.ToString()); + OnChildrenUpdated(cue_id, children); + }else + { + Log.Debug("QPARSER - children info message does not have any data"); + + } } } @@ -157,6 +184,7 @@ namespace qController.Communication protected virtual void OnPlaybackPositionUpdated(string id) { + Log.Error(id); PlaybackPositionUpdated?.Invoke(this, new PlaybackPositionArgs() { PlaybackPosition = id }); } protected virtual void OnConnectionStatusChanged(string status, string workspace_id) diff --git a/qController/Classes/Communication/SharpOSC/OscMessage.cs b/qController/Classes/Communication/SharpOSC/OscMessage.cs index f34eced..bb3fcd9 100644 --- a/qController/Classes/Communication/SharpOSC/OscMessage.cs +++ b/qController/Classes/Communication/SharpOSC/OscMessage.cs @@ -15,9 +15,14 @@ namespace SharpOSC this.Address = address; Arguments = new List(); Arguments.AddRange(args); - } - - public override byte[] GetBytes() + } + + public override string ToString() + { + return "Address: " + Address + " Args: " + string.Join(",", Arguments); + } + + public override byte[] GetBytes() { List parts = new List(); diff --git a/qController/Classes/Communication/SharpOSC/OscPacket.cs b/qController/Classes/Communication/SharpOSC/OscPacket.cs index 52f8cad..441b51e 100644 --- a/qController/Classes/Communication/SharpOSC/OscPacket.cs +++ b/qController/Classes/Communication/SharpOSC/OscPacket.cs @@ -15,6 +15,11 @@ namespace SharpOSC return parseMessage(OscData); } + public static OscMessage GetMessage(byte[] OscData) + { + return parseMessage(OscData); + } + public abstract byte[] GetBytes(); #region Parse OSC packages diff --git a/qController/Classes/Communication/SharpOSC/SlipFrame.cs b/qController/Classes/Communication/SharpOSC/SlipFrame.cs index eae4819..60d050d 100644 --- a/qController/Classes/Communication/SharpOSC/SlipFrame.cs +++ b/qController/Classes/Communication/SharpOSC/SlipFrame.cs @@ -8,12 +8,12 @@ namespace SharpOSC public static readonly byte ESC = 0xdb; public static readonly byte ESC_END = 0xDC; public static readonly byte ESC_ESC = 0xDD; + private static List buffer = new List(); public static List Decode(byte[] data) { List messages = new List(); - List buffer = new List(); bool escapeNext = false; for (int i = 0; i < data.Length; i++) { diff --git a/qController/Classes/Communication/SharpOSC/TCPClient.cs b/qController/Classes/Communication/SharpOSC/TCPClient.cs index ab37ca0..27743a9 100644 --- a/qController/Classes/Communication/SharpOSC/TCPClient.cs +++ b/qController/Classes/Communication/SharpOSC/TCPClient.cs @@ -36,8 +36,6 @@ namespace SharpOSC TcpSharpSocketClient? tcpClient; - private List frameStream = new List(); - public TCPClient(string address, int port) { _port = port; @@ -78,38 +76,27 @@ namespace SharpOSC private void DataReceived(object sender, OnClientDataReceivedEventArgs e) { - frameStream.AddRange(e.Data); - - int i = 0; - int frameEnd = frameStream.FindIndex(1, frameByte => frameByte.Equals(SlipFrame.END)); - while (frameEnd > 0) + List messages = SlipFrame.Decode(e.Data); + _log.Debug("tcpclient: " + messages.Count + " messages pulled from slip"); + foreach (var message in messages) { - List frame = frameStream.GetRange(0, frameEnd + 1); - frameStream.RemoveRange(0, frameEnd + 1); - List messages = SlipFrame.Decode(frame.ToArray()); - foreach (var message in messages) + try { - try - { - OscPacket packet = OscPacket.GetPacket(message); - OscMessage responseMessage = (OscMessage)packet; - if (packet == null) - { - _log.Error("packet is null"); - } - if (responseMessage == null) - { - _log.Error("responeMessage is null"); - } + OscMessage responseMessage = OscPacket.GetMessage(message); + if (responseMessage == null) + { + _log.Error("responseMessage is null"); + } else + { OnMessageReceived(responseMessage); } - catch (Exception ex) - { - _log.Error($"Exception parsing OSC message: {ex.ToString()}"); - } + + } + catch (Exception ex) + { + _log.Error($"Exception parsing OSC message: {ex.ToString()}"); } - frameEnd = frameStream.FindIndex(0, frameByte => frameByte.Equals(SlipFrame.END)); } } diff --git a/qController/Pages/ControlPage.xaml.cs b/qController/Pages/ControlPage.xaml.cs index 12d754b..d2311ca 100644 --- a/qController/Pages/ControlPage.xaml.cs +++ b/qController/Pages/ControlPage.xaml.cs @@ -289,6 +289,9 @@ namespace qController } qCell.UpdateSelectedCue(cue); }); + } else + { + Log.Error("Playback Position has been updated but cue is null?"); } }