void Send(string s) { if (Debug.Level == 2) { Debug.Console(2, this, "Send: '{0}'", ComTextHelper.GetEscapedText(s)); } Communication.SendText(s); }
void Port_LineReceived(object dev, GenericCommMethodReceiveTextArgs args) { if (Debug.Level == 2) { Debug.Console(2, this, "RX: '{0}'", ShowHexResponse ? ComTextHelper.GetEscapedText(args.Text) : args.Text); } }
void SendBytes(byte[] b) { if (Debug.Level == 2) // This check is here to prevent following string format from building unnecessarily on level 0 or 1 { Debug.Console(2, this, "Sending:{0}", ComTextHelper.GetEscapedText(b)); } Communication.SendBytes(b); }
/// <summary> /// Formats an outgoing message. Replaces third byte with ID and replaces last byte with checksum /// </summary> /// <param name="b"></param> void SendBytes(byte[] b) { b[1] = ID; var command = b.ToString(); Debug.Console(1, this, "Sending: '{0}'", ComTextHelper.GetEscapedText(b)); Communication.SendBytes(b); }
void Port_LineReceived(object dev, GenericCommMethodReceiveTextArgs args) { if (Debug.Level == 2) { Debug.Console(2, this, "Received: '{0}'", ComTextHelper.GetEscapedText(args.Text)); } if (args.Text == "DO SOMETHING HERE EVENTUALLY") { _IsMuted = true; MuteFeedback.FireUpdate(); } }
void Communication_BytesReceived(object sender, GenericCommMethodReceiveBytesArgs e) { // This is probably not thread-safe buffering // Append the incoming bytes with whatever is in the buffer var newBytes = new byte[IncomingBuffer.Length + e.Bytes.Length]; IncomingBuffer.CopyTo(newBytes, 0); e.Bytes.CopyTo(newBytes, IncomingBuffer.Length); if (Debug.Level == 2) // This check is here to prevent following string format from building unnecessarily on level 0 or 1 { Debug.Console(2, this, "Received:{0}", ComTextHelper.GetEscapedText(newBytes)); } }
/// <summary> /// Formats an outgoing message. Replaces third byte with ID and replaces last byte with checksum /// </summary> /// <param name="b"></param> void SendBytes(byte[] b) { if (LastCommandSentWasVolume) // If the last command sent was volume { if (b[1] != 0x12) // Check if this command is volume, and if not, delay this command { CrestronEnvironment.Sleep(100); } } b[2] = ID; // append checksum by adding all bytes, except last which should be 00 int checksum = 0; for (var i = 1; i < b.Length - 1; i++) // add 2nd through 2nd-to-last bytes { checksum += b[i]; } checksum = checksum & 0x000000FF; // mask off MSBs b[b.Length - 1] = (byte)checksum; if (Debug.Level == 2) // This check is here to prevent following string format from building unnecessarily on level 0 or 1 { Debug.Console(2, this, "Sending:{0}", ComTextHelper.GetEscapedText(b)); } if (b[1] == 0x12) { LastCommandSentWasVolume = true; } else { LastCommandSentWasVolume = false; } Communication.SendBytes(b); }
/// <summary> /// Handles a response message from the DSP /// </summary> /// <param name="dev"></param> /// <param name="args"></param> void Port_LineReceived(object dev, GenericCommMethodReceiveTextArgs args) { if (Debug.Level == 2) { Debug.Console(2, this, "RX: '{0}'", ShowHexResponse ? ComTextHelper.GetEscapedText(args.Text) : args.Text); } Debug.Console(1, this, "RX: '{0}'", args.Text); try { if (args.Text.IndexOf("Welcome to the Tesira Text Protocol Server...") > -1) { // Indicates a new TTP session SubscribeToAttributes(); } else if (args.Text.IndexOf("publishToken") > -1) { // response is from a subscribed attribute string pattern = "! \"publishToken\":[\"](.*)[\"] \"value\":(.*)"; Match match = Regex.Match(args.Text, pattern); if (match.Success) { string key; string customName; string value; customName = match.Groups[1].Value; // Finds the key (everything before the '~' character key = customName.Substring(0, customName.IndexOf("~", 0) - 1); value = match.Groups[2].Value; foreach (KeyValuePair <string, TesiraForteLevelControl> controlPoint in LevelControlPoints) { if (customName == controlPoint.Value.LevelCustomName || customName == controlPoint.Value.MuteCustomName) { controlPoint.Value.ParseSubscriptionMessage(customName, value); return; } } } /// same for dialers /// 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 { return; } // response is not from a subscribed attribute. From a get/set/toggle/increment/decrement command if (!CommandQueue.IsEmpty) { if (CommandQueue.Peek() is QueuedCommand) { // Expected response belongs to a child class QueuedCommand tempCommand = (QueuedCommand)CommandQueue.TryToDequeue(); //Debug.Console(1, this, "Command Dequeued. CommandQueue Size: {0}", CommandQueue.Count); tempCommand.ControlPoint.ParseGetMessage(tempCommand.AttributeCode, args.Text); } else { // Expected response belongs to this class string temp = (string)CommandQueue.TryToDequeue(); //Debug.Console(1, this, "Command Dequeued. CommandQueue Size: {0}", CommandQueue.Count); } if (CommandQueue.IsEmpty) { CommandQueueInProgress = false; } else { SendNextQueuedCommand(); } } } else if (args.Text.IndexOf("-ERR") > -1) { // Error response switch (args.Text) { case "-ERR ALREADY_SUBSCRIBED": { ResetSubscriptionTimer(); break; } default: { Debug.Console(0, this, "Error From DSP: '{0}'", args.Text); break; } } } } catch (Exception e) { if (Debug.Level == 2) { Debug.Console(2, this, "Error parsing response: '{0}'\n{1}", args.Text, e); } } }
private void Port_LineReceived(object dev, GenericCommMethodReceiveTextArgs args) { if (Debug.Level == 2) { Debug.Console(2, this, "RX: '{0}'", ShowHexResponse ? ComTextHelper.GetEscapedText(args.Text) : args.Text); } //Debug.Console(1, this, "RX: '{0}'", args.Text); try { DeviceRx = args.Text; CommandPassthruFeedback.FireUpdate(); if (args.Text.IndexOf("Welcome to the Tesira Text Protocol Server...", StringComparison.Ordinal) > -1) { // Indicates a new TTP session // moved to CustomActivate() method CommunicationMonitor.Start(); CrestronInvoke.BeginInvoke((o) => HandleAttributeSubscriptions()); } else if (args.Text.IndexOf("! ", StringComparison.Ordinal) > -1) { // response is from a subscribed attribute //(if(args.Text const string pattern = "! [\\\"](.*?[^\\\\])[\\\"] (.*)"; var match = Regex.Match(args.Text, pattern); if (!match.Success) { return; } var customName = match.Groups[1].Value; var value = match.Groups[2].Value; AdvanceQueue(args.Text); foreach (var controlPoint in Faders.Where(controlPoint => customName == controlPoint.Value.LevelCustomName || customName == controlPoint.Value.MuteCustomName)) { controlPoint.Value.ParseSubscriptionMessage(customName, value); return; } foreach (var controlPoint in Dialers.Where(controlPoint => customName == controlPoint.Value.AutoAnswerCustomName || customName == controlPoint.Value.ControlStatusCustomName || customName == controlPoint.Value.DialerCustomName)) { controlPoint.Value.ParseSubscriptionMessage(customName, value); return; } foreach (var controlPoint in States.Where(controlPoint => customName == controlPoint.Value.StateCustomName)) { controlPoint.Value.ParseSubscriptionMessage(customName, value); return; } foreach (var controlPoint in Switchers.Where(controlPoint => customName == controlPoint.Value.SelectorCustomName)) { controlPoint.Value.ParseSubscriptionMessage(customName, value); return; } foreach (var controlPoint in Meters.Where(controlPoint => customName == controlPoint.Value.MeterCustomName)) { controlPoint.Value.ParseSubscriptionMessage(customName, value); return; } // same for dialers // same for switchers } else if (args.Text.IndexOf("+OK", StringComparison.Ordinal) > -1) { if (args.Text == "+OK") // Check for a simple "+OK" only 'ack' repsonse or a list response and ignore { return; } // response is not from a subscribed attribute. From a get/set/toggle/increment/decrement command //string pattern = "(?<=\" )(.*?)(?=\\+)"; //string data = Regex.Replace(args.Text, pattern, ""); AdvanceQueue(args.Text); } else if (args.Text.IndexOf("-ERR", StringComparison.Ordinal) > -1) { // Error response Debug.Console(2, this, "Error From DSP: '{0}'", args.Text); switch (args.Text) { case "-ERR ALREADY_SUBSCRIBED": { WatchDogSniffer = false; AdvanceQueue(args.Text); break; } default: { WatchDogSniffer = false; AdvanceQueue(args.Text); break; } } } } catch (Exception e) { if (Debug.Level == 2) { Debug.Console(2, this, "Error parsing response: '{0}'\n{1}", args.Text, e); } } }
/// <summary> /// / /// </summary> /// <param name="sender"></param> void Communication_BytesReceived(object sender, GenericCommMethodReceiveBytesArgs e) { try { // This is probably not thread-safe buffering // Append the incoming bytes with whatever is in the buffer var newBytes = new byte[IncomingBuffer.Length + e.Bytes.Length]; IncomingBuffer.CopyTo(newBytes, 0); e.Bytes.CopyTo(newBytes, IncomingBuffer.Length); if (Debug.Level == 2) // This check is here to prevent following string format from building unnecessarily on level 0 or 1 { Debug.Console(2, this, "Received:{0}", ComTextHelper.GetEscapedText(newBytes)); } // Need to find AA FF and have for (int i = 0; i < newBytes.Length; i++) { if (newBytes[i] == 0xAA && newBytes[i + 1] == 0xFF) { newBytes = newBytes.Skip(i).ToArray(); // Trim off junk if there's "dirt" in the buffer // parse it // If it's at least got the header, then process it, while (newBytes.Length > 4 && newBytes[0] == 0xAA && newBytes[1] == 0xFF) { var msgLen = newBytes[3]; // if the buffer is shorter than the header (3) + message (msgLen) + checksum (1), // give and save it for next time if (newBytes.Length < msgLen + 4) { break; } // Good length, grab the message var message = newBytes.Skip(4).Take(msgLen).ToArray(); // At this point, the ack/nak is the first byte if (message[0] == 0x41) { switch (message[1]) // type byte { case 0x00: // General status //UpdatePowerFB(message[2], message[5]); // "power" can be misrepresented when the display sleeps // Handle the first power on fb when waiting for it. if (IsPoweringOnIgnorePowerFb && message[2] == 0x01) { IsPoweringOnIgnorePowerFb = false; } // Ignore general-status power off messages when powering up if (!(IsPoweringOnIgnorePowerFb && message[2] == 0x00)) { UpdatePowerFB(message[2]); } UpdateVolumeFB(message[3]); UpdateMuteFb(message[4]); UpdateInputFb(message[5]); break; case 0x11: UpdatePowerFB(message[2]); break; case 0x12: UpdateVolumeFB(message[2]); break; case 0x13: UpdateMuteFb(message[2]); break; case 0x14: UpdateInputFb(message[2]); break; default: break; } } // Skip over what we've used and save the rest for next time newBytes = newBytes.Skip(5 + msgLen).ToArray(); } break; // parsing will mean we can stop looking for header in loop } } // Save whatever partial message is here IncomingBuffer = newBytes; } catch (Exception err) { Debug.Console(2, this, "Error parsing feedback: {0}", err); } }
/// <summary> /// This method will run when the PortGather is satisfied. Parse responses here. /// </summary> /// <param name="dev"></param> /// <param name="args"></param> void Port_LineReceived(object dev, GenericCommMethodReceiveTextArgs args) { if (Debug.Level == 2) { Debug.Console(2, this, "Received: '{0}'", ComTextHelper.GetEscapedText(args.Text)); } char[] trimChars = { '\x02', '\x03' }; var FB = args.Text.Trim(trimChars); Debug.Console(2, this, "Received cmd: '{0}'", FB); switch (FB) { case "PON": { _PowerIsOn = true; PowerIsOnFeedback.FireUpdate(); InputNumberFeedback.FireUpdate(); break; } case "POF": { _PowerIsOn = false; PowerIsOnFeedback.FireUpdate(); InputNumber = 102; InputNumberFeedback.FireUpdate(); break; } case "QPW:1": { _PowerIsOn = true; PowerIsOnFeedback.FireUpdate(); InputNumberFeedback.FireUpdate(); break; } case "QPW:0": { _PowerIsOn = false; PowerIsOnFeedback.FireUpdate(); InputNumber = 102; InputNumberFeedback.FireUpdate(); break; } case "QMI:HM1": { if (_PowerIsOn) { InputNumber = 1; InputNumberFeedback.FireUpdate(); } break; } case "QMI:HM2": { if (_PowerIsOn) { InputNumber = 2; InputNumberFeedback.FireUpdate(); } break; } case "QMI:DV1": { if (_PowerIsOn) { InputNumber = 3; InputNumberFeedback.FireUpdate(); } break; } case "QMI:PC1": { if (_PowerIsOn) { InputNumber = 4; InputNumberFeedback.FireUpdate(); } break; } } }
///// <summary> ///// / ///// </summary> ///// <param name="sender"></param> //void Communication_BytesReceived(object sender, GenericCommMethodReceiveBytesArgs e) //{ // // This is probably not thread-safe buffering // // Append the incoming bytes with whatever is in the buffer // var newBytes = new byte[IncomingBuffer.Length + e.Bytes.Length]; // IncomingBuffer.CopyTo(newBytes, 0); // e.Bytes.CopyTo(newBytes, IncomingBuffer.Length); // if (Debug.Level == 2) // This check is here to prevent following string format from building unnecessarily on level 0 or 1 // Debug.Console(2, this, "Received:{0}", ComTextHelper.GetEscapedText(newBytes)); // // Need to find AA FF and have // for (int i = 0; i < newBytes.Length; i++) // { // if (newBytes[i] == 0xAA && newBytes[i + 1] == 0xFF) // { // newBytes = newBytes.Skip(i).ToArray(); // Trim off junk if there's "dirt" in the buffer // // parse it // // If it's at least got the header, then process it, // while (newBytes.Length > 4 && newBytes[0] == 0xAA && newBytes[1] == 0xFF) // { // var msgLen = newBytes[3]; // // if the buffer is shorter than the header (3) + message (msgLen) + checksum (1), // // give and save it for next time // if (newBytes.Length < msgLen + 4) // break; // // Good length, grab the message // var message = newBytes.Skip(4).Take(msgLen).ToArray(); // // At this point, the ack/nak is the first byte // if (message[0] == 0x41) // { // switch (message[1]) // type byte // { // case 0x00: // General status // UpdatePowerFB(message[2], message[5]); // "power" can be misrepresented when the display sleeps // UpdateInputFb(message[5]); // UpdateVolumeFB(message[3]); // UpdateMuteFb(message[4]); // UpdateInputFb(message[5]); // break; // case 0x11: // UpdatePowerFB(message[2]); // break; // case 0x12: // UpdateVolumeFB(message[2]); // break; // case 0x13: // UpdateMuteFb(message[2]); // break; // case 0x14: // UpdateInputFb(message[2]); // break; // default: // break; // } // } // // Skip over what we've used and save the rest for next time // newBytes = newBytes.Skip(5 + msgLen).ToArray(); // } // break; // parsing will mean we can stop looking for header in loop // } // } // // Save whatever partial message is here // IncomingBuffer = newBytes; //} void PortGather_LineReceived(object sender, GenericCommMethodReceiveTextArgs e) { Debug.Console(1, this, "Receivied: '{0}'", ComTextHelper.GetEscapedText(e.Text)); if (e.Text.IndexOf("\x50\x4F\x57") > -1) { // Power Status Response var value = e.Text.ToCharArray(); switch (value[6]) { case '\x00': { _PowerIsOn = false; break; } case '\x01': { _PowerIsOn = true; break; } } PowerIsOnFeedback.FireUpdate(); Debug.Console(1, this, "PowerIsOn State: {0}", PowerIsOnFeedback.BoolValue); } else if (e.Text.IndexOf("\x4D\x49\x4E") > -1) { var value = e.Text.ToCharArray(); var b = value[6]; var newInput = InputPorts.FirstOrDefault(i => i.FeedbackMatchObject.Equals(b)); if (newInput != null && newInput != _CurrentInputPort) { _CurrentInputPort = newInput; CurrentInputFeedback.FireUpdate(); Debug.Console(1, this, "Current Input: {0}", CurrentInputFeedback.StringValue); } } else if (e.Text.IndexOf("\x56\x4F\x4C") > -1) { // Volume Status Response var value = e.Text.ToCharArray(); var b = value[6]; var newVol = (ushort)NumericalHelpers.Scale((double)b, 0, 100, 0, 65535); if (!VolumeIsRamping) { _LastVolumeSent = newVol; } if (newVol != _VolumeLevelForSig) { _VolumeLevelForSig = newVol; VolumeLevelFeedback.FireUpdate(); if (_VolumeLevelForSig > 0) { _IsMuted = false; } else { _IsMuted = true; } MuteFeedback.FireUpdate(); Debug.Console(1, this, "Volume Level: {0}", VolumeLevelFeedback.IntValue); } } }