private void zDC1Received(int index, string message) { DeviceItemZDC1 device = (DeviceItemZDC1)listBox1.Items[index]; JObject jsonObject = JObject.Parse(message); if (!device.mac.Equals(jsonObject["mac"].ToString())) return; if (jsonObject.Property("name") != null) { device.name = jsonObject["name"].ToString(); if (index == listBox1.SelectedIndex) deviceControl1.zDC1RefreshName(); listBox1.Refresh(); //更新列表里的名称/mac地址 } if (jsonObject.Property("power") != null) { device.zDC1Power = jsonObject["power"].ToString(); if (index == listBox1.SelectedIndex) deviceControl1.zDC1RefreshPower(); } if (jsonObject.Property("voltage") != null) { device.zDC1Voltage = jsonObject["voltage"].ToString(); if (index == listBox1.SelectedIndex) deviceControl1.zDC1RefreshVoltage(); } if (jsonObject.Property("current") != null) { device.zDC1Current = jsonObject["current"].ToString(); if (index == listBox1.SelectedIndex) deviceControl1.zDC1RefreshCurrent(); } if (jsonObject.Property("total_time") != null) { device.zDC1TotalTime = (uint)jsonObject["total_time"]; if (index == listBox1.SelectedIndex) deviceControl1.zDC1RefreshTotalTime(); } #region 解析plug bool plugReturnFlag = false; for (int plug_id = 0; plug_id < 6; plug_id++) { if (jsonObject.Property("plug_" + plug_id) == null) continue; plugReturnFlag = true; JObject jsonPlug = (JObject)jsonObject["plug_" + plug_id]; if (jsonPlug.Property("on") != null) { int on = (int)jsonPlug["on"]; device.zDC1Switch[plug_id] = (on != 0); deviceControl1.zDC1RefreshSwitch(plug_id); } if (jsonPlug.Property("setting") == null) continue; JObject jsonPlugSetting = (JObject)jsonPlug["setting"]; if (jsonPlugSetting.Property("name") != null) { device.zDC1SwitchName[plug_id] = jsonPlugSetting["name"].ToString(); deviceControl1.zDC1RefreshSwitchName(plug_id); } } #endregion }
private void PublishReceivedCallBack(String topic, String message) { System.Console.WriteLine("Received topic [" + topic + "] :" + message); int index; try { JObject jObject = JObject.Parse(message); if (jObject.Property("mac") == null) return; if (jObject.Property("type") != null && jObject.Property("type_name") != null && jObject.Property("name") != null && jObject.Property("mac") != null) { for (index = 0; index < listBox1.Items.Count; index++) { if (jObject["mac"].ToString().Equals(((DeviceItem)listBox1.Items[index]).mac)) { break; } } if (index < listBox1.Items.Count) return; //设备重复,不增加 switch ((DEVICETYPE)(int)jObject["type"]) { case DEVICETYPE.TYPE_TC1: DeviceItemZTC1 deviceItemZTC1 = new DeviceItemZTC1(jObject["name"].ToString(), jObject["mac"].ToString()); deviceItemZTC1.typeName = jObject["type_name"].ToString(); listBox1.Items.Insert(0, deviceItemZTC1); break; case DEVICETYPE.TYPE_DC1: DeviceItemZDC1 deviceItemZDC1 = new DeviceItemZDC1(jObject["name"].ToString(), jObject["mac"].ToString()); deviceItemZDC1.typeName = jObject["type_name"].ToString(); listBox1.Items.Insert(0, deviceItemZDC1); break; } return; } String reMac = jObject["mac"].ToString(); for (index = 0; index < listBox1.Items.Count; index++) { //if(index) if (reMac.Equals(((DeviceItem)listBox1.Items[index]).mac)) { System.Console.WriteLine("设备:" + index); break; } } if (index >= listBox1.Items.Count) return; switch (((DeviceItem)listBox1.Items[index]).type) { case DEVICETYPE.TYPE_TC1: zTC1Received(index, message); break; case DEVICETYPE.TYPE_DC1: zDC1Received(index, message); break; } } catch (Exception) { //throw; } }