private static void OnMqttDataPublished (object Sender, DataPublishedEventArgs e) { string Topic = e.Topic; if (!Topic.StartsWith ("Clayster/LearningIoT/Actuator/")) return; string s = System.Text.Encoding.UTF8.GetString (e.Data); Topic = Topic.Substring (30); switch (Topic) { case "do": int IntValue; if (int.TryParse (s, out IntValue) && IntValue >= 0 && IntValue <= 255) { int i; bool b; for (i = 0; i < 8; i++) { b = (IntValue & 1) != 0; digitalOutputs [i].Value = b; state.SetDO (i + 1, b); IntValue >>= 1; } state.UpdateIfModified (); } break; case "ao": bool BoolValue; if (XmlUtilities.TryParseBoolean (s, out BoolValue)) { if (BoolValue) { AlarmOn (); state.Alarm = true; } else { AlarmOff (); state.Alarm = false; } state.UpdateIfModified (); } break; default: if (Topic.StartsWith ("do") && int.TryParse (Topic.Substring (2), out IntValue) && IntValue >= 1 && IntValue <= 8 && XmlUtilities.TryParseBoolean (s, out BoolValue)) { digitalOutputs [IntValue - 1].Value = BoolValue; state.SetDO (IntValue, BoolValue); state.UpdateIfModified (); } break; } }
private static void OnMqttDataPublished(object Sender, DataPublishedEventArgs e) { string Topic = e.Topic; if (!Topic.StartsWith("Clayster/LearningIoT/Actuator/")) { return; } string s = System.Text.Encoding.UTF8.GetString(e.Data); Topic = Topic.Substring(30); switch (Topic) { case "do": int IntValue; if (int.TryParse(s, out IntValue) && IntValue >= 0 && IntValue <= 255) { int i; bool b; for (i = 0; i < 8; i++) { b = (IntValue & 1) != 0; digitalOutputs [i].Value = b; state.SetDO(i + 1, b); IntValue >>= 1; } state.UpdateIfModified(); } break; case "ao": bool BoolValue; if (XmlUtilities.TryParseBoolean(s, out BoolValue)) { if (BoolValue) { AlarmOn(); state.Alarm = true; } else { AlarmOff(); state.Alarm = false; } state.UpdateIfModified(); } break; default: if (Topic.StartsWith("do") && int.TryParse(Topic.Substring(2), out IntValue) && IntValue >= 1 && IntValue <= 8 && XmlUtilities.TryParseBoolean(s, out BoolValue)) { digitalOutputs [IntValue - 1].Value = BoolValue; state.SetDO(IntValue, BoolValue); state.UpdateIfModified(); } break; } }