private static void OnUdpData(IAsyncResult result) { UdpClient client = result.AsyncState as UdpClient; IPEndPoint source = new IPEndPoint(0, 0); byte[] message = client.EndReceive(result, ref source); string messageData = Encoding.ASCII.GetString(message); try { if (messageData.StartsWith("*!")) { //JSON Message messageData = messageData.Substring(2); JToken jsonData = JToken.Parse(messageData); int trans = Convert.ToInt32(jsonData.SelectToken("trans")); if (trans > lastTrans) { lastTrans = trans; logger.Debug(messageData); //Insert JSON into System Table if (jsonData.SelectToken("pkt").ToString() == "system") { logger.Info("OnUdpData|System JSON message received"); SQLite.InsertSystemTable(jsonData); } //Insert JSON into Energy Table if (jsonData.SelectToken("fn").ToString() == "meterData") { logger.Info("OnUdpData|Energy meter JSON message received"); SQLite.InsertEnergyTable(jsonData); Notify.SendUpdate(jsonData); } //Insert JSON into Heating Table if (jsonData.SelectToken("fn").ToString() == "statusPush") { logger.Info("OnUdpData|Heating JSON message received"); SQLite.InsertHeatingTable(jsonData); Notify.SendUpdate(jsonData); } //Insert JSON into Device Table if (jsonData.SelectToken("pkt").ToString() == "room" && jsonData.SelectToken("fn").ToString() == "read") { SQLite.InsertDeviceTable(jsonData); } //Set Target Confirmation - Confirms LWRF hub has accepted send request and records packet ID and send parameters if (jsonData.SelectToken("fn").ToString() == "setTarget") { logger.Info("OnUdpData|setTarget JSON message received"); int room = int.Parse(jsonData.SelectToken("room").ToString()); var temp = int.Parse(jsonData.SelectToken("temp").ToString()); int packet = int.Parse(jsonData.SelectToken("packet").ToString()); foreach (var m in LWRF.messageList) { if (m.Value.room == room && m.Value.temp == temp) { logger.Debug("OnUdpData|messageList: add msgId {0} packetId {1}", m.Value.msgId, packet); var stTemp = LWRF.messageList[m.Value.msgId]; stTemp.packet = packet; LWRF.messageList[m.Value.msgId] = stTemp; break; } } } //Set Target Acknowledgement - Confirms LWRF device has accepted send request via hub, if failed will attempt to resend request if (jsonData.SelectToken("fn").ToString() == "ack") { int packet = int.Parse(jsonData.SelectToken("packet").ToString()); string status = jsonData.SelectToken("status").ToString(); logger.Info("OnUdpData|Device ACK JSON message received packetId {0}", packet); foreach (var m in LWRF.messageList) { if (m.Value.packet == packet) { if (status.ToLower() == "success") { logger.Info("OnUdpData|Device Acknowledged {0}", m.Value.messageText); logger.Debug("OnUdpData|messageList: removing msgId {0} packetId {1}", m.Value.msgId, packet); LWRF.messageList.Remove(m.Value.msgId); if (LWRF.messageList.Count == 0) { logger.Debug("OnUdpData|messageList: Empty"); } else { foreach (var list in LWRF.messageList) { logger.Debug("OnUdpData|messageList: {0} {1}", list.Key, list.Value.msgId); } } break; } else { logger.Info("OnUdpData|Device Unsuccessful {0}", m.Value.messageText); if (m.Value.attenpt < LWRF.lwrfHubRetries) { logger.Debug("OnUdpData|msgID {0} failure will requeue", m.Value.msgId); var aTemp = LWRF.messageList[m.Value.msgId]; aTemp.msgId = 0; aTemp.packet = 0; aTemp.attenpt += 1; UDPSender.QueueUdpMessage(aTemp); } else { logger.Debug("OnUdpData|msgID {0} exceeds maximum requeue count, will cancel", m.Value.msgId); } LWRF.messageList.Remove(m.Value.msgId); break; } } } } } } else { //ACK Message messageData = messageData.TrimEnd('\r', '\n'); logger.Debug("OnUdpData|messageAck: {0}", messageData); string[] messageParts = messageData.Split(','); logger.Info("OnUdpData|LWRF Hub ACK received msgID {0}", messageParts[0]); LWRF.messageAck.Add(Convert.ToInt32(messageParts[0]), messageParts[1]); logger.Debug("OnUdpData|messageAck: add msgID {0}", messageParts[0]); foreach (var ack in LWRF.messageAck) { logger.Debug("OnUdpData|messageAck: {0} {1}", ack.Key, ack.Value); } } } catch (Exception e) { logger.Error(e); } finally { client.BeginReceive(new AsyncCallback(OnUdpData), client); } }
private static void SendRequest(string action, Dictionary <string, string> parameters = null) { string room = null; string device = null; string level = null; if (parameters != null) { parameters.TryGetValue("room", out room); parameters.TryGetValue("device", out device); parameters.TryGetValue("level", out level); } //Check that levels are within the correct range for lighting devices if (action.ToLower().StartsWith("light") && level != null) { if (int.Parse(level) < 0) { level = "0"; } if (int.Parse(level) > 100) { level = "100"; } if (int.Parse(level) == 0) { action = "light_off"; } } //Check that levels are within the correct range for heating devices if (action.ToLower().StartsWith("heat")) { if (int.Parse(level) < 0) { level = "0"; } if (int.Parse(level) > 40) { level = "40"; } } Message m = new Message(); switch (action.ToLower()) { case "system_subscribe": logger.Info("SmartThings Subscribe Request"); break; case "system_register": m.messageText = "!F*p"; m.system = true; break; case "system_deregister": m.messageText = "!F*xp"; m.system = true; break; case "system_info": m.messageText = "@H"; m.system = true; break; case "system_ledon": m.messageText = "@L1"; m.system = true; break; case "system_ledoff": m.messageText = "@L0"; m.system = true; break; case "switch_on": m.messageText = "!R" + room + "D" + device + "F1|\0"; m.delay = 1; break; case "switch_off": m.messageText = "!R" + room + "D" + device + "F0|\0"; m.delay = 1; break; case "switch_dim": m.messageText = "!R" + room + "D" + device + "FdP" + Math.Round((Convert.ToInt32(level) * 0.32)) + "|\0"; m.delay = 1; break; case "switch_plock": m.messageText = "!R" + room + "D" + device + "Fl|\0"; break; case "switch_flock": m.messageText = "!R" + room + "D" + device + "Fk|\0"; break; case "switch_unlock": m.messageText = "!R" + room + "D" + device + "Fu|\0"; break; case "heat_temp": m.messageText = "!R" + device + "F*tP" + level + "|\0"; m.delay = 5; m.room = int.Parse(device); m.temp = int.Parse(level); break; case "heat_on": m.messageText = "!R" + device + "F*tP60|\0"; m.delay = 5; m.room = int.Parse(device); m.temp = 60; break; case "heat_off": m.messageText = "!R" + device + "F*tP50|\0"; m.delay = 5; m.room = int.Parse(device); m.temp = 50; break; case "device_info": m.messageText = "@?R" + device; break; case "device_link": m.messageText = "!R" + device + "F*L|\0"; break; case "device_unlink": m.messageText = "!R" + device + "F*xU|\0"; break; default: m.messageText = "@H"; break; } if (m.messageText != null) { logger.Info("SendRequest|Queuing UDP Message {0}", m.messageText); UDPSender.QueueUdpMessage(m); } return; }