HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2019-09-30 404cdc88627f942df7944af04ee05b9d527752d6
ZigbeeApp/Shared/Phone/ZigBee/Device/CommonDevice.cs
@@ -23,7 +23,7 @@
            {
                //if (msg.Contains("DeviceStatusReport") == false)
                //{
                    System.Console.WriteLine(msg);
                System.Console.WriteLine(msg);
                //}
            }
        }
@@ -76,7 +76,7 @@
                    gateWay.getGatewayBaseInfo.gwID = CurrentGateWayId;
                    gateWay.getGatewayBaseInfo.HomeId = Shared.Common.Config.Instance.HomeId;
                    ZbGateway.GateWayList.Add(gateWay);
                 }
                }
                return gateWay;
            }
@@ -110,13 +110,13 @@
                return null;
            }
            return CommonDeviceByByteString(v[1], System.Text.Encoding.UTF8.GetString(Shared.Common.Global.ReadFileByHomeId(deviceFilePath)));
        }
        /// <summary>
        /// 由设备字符串比特恢复设备对象
        /// </summary>
        /// <param name="strDeviceType">设备DeviceType的字符串类型</param>
        /// <param name="strDeviceByte">设备Json文件转为比特后再转为的字符串</param>
        }
        /// <summary>
        /// 由设备字符串比特恢复设备对象
        /// </summary>
        /// <param name="strDeviceType">设备DeviceType的字符串类型</param>
        /// <param name="strDeviceByte">设备Json文件转为比特后再转为的字符串</param>
        /// <returns></returns>
        public static CommonDevice CommonDeviceByByteString(string strDeviceType, string strDeviceByte)
        {
@@ -160,6 +160,10 @@
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<Transverter>(strDeviceByte);
            }
            else if (strDeviceType == ZigBee.Device.DeviceType.DoorLock.ToString())
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLock>(strDeviceByte);
            }
            return null;
        }
@@ -180,8 +184,87 @@
        /// </summary>
        public void ReSave()
        {
            //Console.WriteLine(FilePath);
            if (IconPath == string.Empty)
            {
                //保存设备图标(这里会保存一次,下面就不用保存了)
                this.SaveDeviceIcon();
                return;
            }
            Shared.Common.Global.WriteFileByBytesByHomeId(FilePath, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)));
        }
        /// <summary>
        /// 保存设备图标
        /// </summary>
        private void SaveDeviceIcon()
        {
            if (IconPath == string.Empty)
            {
                //干接点
                if (this.Type == DeviceType.OnOffSwitch)
                {
                    IconPath = "Device/DryContact.png";
                }
                else if (this.Type == DeviceType.ColorDimmableLight)
                {
                    //彩灯
                    IconPath = "Device/ColorDimmableLight.png";
                }
                else if (this.Type == DeviceType.DimmableLight)
                {
                    //调光器
                    IconPath = "Device/ColorDimmableLight.png";
                }
                else if (this.Type == DeviceType.OnOffOutput)
                {
                    //继电器
                    IconPath = "Device/RelayEpoint.png";
                }
                else if (this.Type == DeviceType.Thermostat)
                {
                    //空调
                    IconPath = "Device/AirConditionerEpoint.png";
                }
                else if (this.Type != DeviceType.UnKown)
                {
                    //其他的图标有点特殊
                    string unSelectPic = string.Empty;
                    string selectPic = string.Empty;
                    Shared.Common.LocalDevice.Current.GetDeviceBeloneIcon(new List<CommonDevice> { this }, ref unSelectPic, ref selectPic);
                    IconPath = unSelectPic;
                }
                Shared.Common.Global.WriteFileByBytesByHomeId(FilePath, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this)));
            }
        }
        /// <summary>
        /// 是否是自定义图片
        /// </summary>
        public bool IsCustomizeImage = false;
        /// <summary>
        /// 设备图片
        /// </summary>
        public string IconPath = string.Empty;
        /// <summary>
        /// 设备图片--在线或者选中状态
        /// </summary>
        /// <value>The online icon path.</value>
        [Newtonsoft.Json.JsonIgnore]
        public string OnlineIconPath
        {
            get
            {
                if (string.IsNullOrEmpty(IconPath))
                {
                    return string.Empty;
                }
                var pathArr = IconPath.Split('.');
                if (pathArr == null || string.IsNullOrEmpty(pathArr[0]))
                {
                    return string.Empty;
                }
                return $"{pathArr[0]}Selected.png";
            }
        }
        /// <summary>
@@ -192,7 +275,10 @@
        /// 当前设备类型
        /// </summary>
        public DeviceType Type = DeviceType.UnKown;
        /// <summary>
        /// 设备的功能类型(此类型目前只针对继电器回路有效,默认为灯光)
        /// </summary>
        public DeviceFunctionType DfunctionType = DeviceFunctionType.A灯光;
        /// <summary>
        /// MAC地址
        /// </summary>
@@ -312,7 +398,7 @@
        /// <summary>
        /// 序列号
        /// </summary>
        public string SerialNumber = string.Empty;
        public string SerialNumber = string.Empty;
        /// <summary>
        /// 所有指定cluster是否都已经成功绑定协调器
        ///<para>0:未完全绑定</para>
@@ -559,6 +645,18 @@
            /// 驱动代码。为0时,表示zigbee协调器设备。其他值表示为虚拟驱动设备
            /// </summary>
            public int DriveCode;
            /// <summary>
            /// 厂商名称
            /// </summary>
            public string ManufacturerName = string.Empty;
            /// <summary>
            /// 模块ID
            /// </summary>
            public string ModelIdentifier = string.Empty;
            /// <summary>
            /// 好像是序列号
            /// </summary>
            public string ProductCode = string.Empty;
            /// <summary>
            /// 输入簇列表
            /// </summary>
@@ -798,7 +896,7 @@
            /// 1:路由器设备
            /// <para>2:终端设备</para>
            /// </summary>
            public int ZigbeeType;
            public int ZigbeeType;
        }
        /// <summary>
@@ -931,7 +1029,7 @@
            /// <summary>
            /// 序列号
            /// </summary>
            public string SerialNumber = string.Empty;
            public string SerialNumber = string.Empty;
            /// <summary>
            /// 输入簇列表
            /// </summary>
@@ -1208,6 +1306,24 @@
                                    }
                                }
                                break;
                            case DeviceType.DoorLock:
                                var doorLock = new DoorLock() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
                                doorLock.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLock.DeviceRenameResponseData>(jobject["Data"].ToString());
                                if (doorLock.renameDeviceData == null)
                                {
                                    d = new DeviceRenameAllData { errorMessageBase = "网关返回的数据为空" };
                                }
                                else
                                {
                                    d = new DeviceRenameAllData { deviceRenameData = doorLock.renameDeviceData };
                                    System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                                    var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == doorLock.DeviceID && obj.DeviceAddr == doorLock.DeviceAddr && obj.DeviceEpoint == doorLock.DeviceEpoint);
                                    if (info != null)
                                    {
                                        info.DeviceEpointName = doorLock.renameDeviceData.DeviceName;
                                    }
                                }
                                break;
                        }
                    }
                };
@@ -1223,7 +1339,7 @@
                    var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 96 } };
                    var data = new JObject { { "DeviceName", deviceName } };
                    jObject.Add("Data", data);
                     Gateway?.Send(("DeviceRename"), jObject.ToString());
                    Gateway?.Send(("DeviceRename"), jObject.ToString());
                }
                catch { }
@@ -1349,7 +1465,7 @@
                                    if (infoToggleLight != null)
                                    {
                                        infoToggleLight.DeviceName = toggleLight.renameDeviceMacNameData.MacName;
                                     }
                                    }
                                }
                                break;
                            case DeviceType.AirSwitch:
@@ -1368,7 +1484,7 @@
                                    if (infoAirSwitch != null)
                                    {
                                        infoAirSwitch.DeviceName = airSwitch.renameDeviceMacNameData.MacName;
                                     }
                                    }
                                }
                                break;
                            case DeviceType.OnOffSwitch:
@@ -1386,7 +1502,7 @@
                                    if (infoPanel != null)
                                    {
                                        infoPanel.DeviceName = panelObj.renameDeviceMacNameData.MacName;
                                     }
                                    }
                                }
                                break;
                            case DeviceType.WindowCoveringDevice:
@@ -1404,7 +1520,7 @@
                                    if (wc != null)
                                    {
                                        wc.DeviceName = windowCovering.renameDeviceMacNameData.MacName;
                                     }
                                    }
                                }
                                break;
                            case DeviceType.IASZone:
@@ -1423,7 +1539,7 @@
                                    if (info != null)
                                    {
                                        info.DeviceName = ias.renameDeviceMacNameData.MacName;
                                     }
                                    }
                                }
                                break;
                            case DeviceType.DimmableLight:
@@ -1442,7 +1558,7 @@
                                    if (info != null)
                                    {
                                        info.DeviceName = dimmableLight.renameDeviceMacNameData.MacName;
                                     }
                                    }
                                }
                                break;
                            case DeviceType.Repeater:
@@ -1461,7 +1577,7 @@
                                    if (info != null)
                                    {
                                        info.DeviceName = repeater.renameDeviceMacNameData.MacName;
                                     }
                                    }
                                }
                                break;
                            case DeviceType.Thermostat:
@@ -1502,6 +1618,25 @@
                                    }
                                }
                                break;
                            case DeviceType.DoorLock:
                                var doorLock = new DoorLock() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
                                doorLock.renameDeviceMacNameData = Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLock.RenameDeviceMacNameData>(jobject["Data"].ToString());
                                if (doorLock.renameDeviceMacNameData == null)
                                {
                                    d = new RenameDeviceMacNameAllData { errorMessageBase = "网关返回的数据为空" };
                                }
                                else
                                {
                                    d = new RenameDeviceMacNameAllData { renameDeviceMacNameData = doorLock.renameDeviceMacNameData };
                                    System.Console.WriteLine($"UI收到通知后的主题_{ topic}_收到通知后的数据_{ d.renameDeviceMacNameData.ToString()}");
                                    var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == doorLock.DeviceID && obj.DeviceAddr == doorLock.DeviceAddr && obj.DeviceEpoint == doorLock.DeviceEpoint);
                                    if (info != null)
                                    {
                                        info.DeviceName = doorLock.renameDeviceMacNameData.MacName;
                                    }
                                }
                                break;
                                //case DeviceType.TemperatureSensor:
                                //    var temperatureSensor = new TemperatureSensor() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.CurrentGateWayId };
                                //    temperatureSensor.renameDeviceData = Newtonsoft.Json.JsonConvert.DeserializeObject<TemperatureSensor.DeviceRenameResponseData>(jobject["Data"].ToString());
@@ -1536,7 +1671,7 @@
                    var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 100 } };
                    var data = new JObject { { "MacName", macName } };
                    jObject.Add("Data", data);
                     Gateway?.Send(("MacRename"), jObject.ToString());
                    Gateway?.Send(("MacRename"), jObject.ToString());
                }
                catch { }
@@ -1625,7 +1760,7 @@
            try
            {
                var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 0 }, { "Command", 97 } };
                 Gateway.Send("FactoryReset", jObject.ToString());
                Gateway.Send("FactoryReset", jObject.ToString());
            }
            catch { }
@@ -1862,6 +1997,25 @@
                                                    }
                                                }
                                                break;
                                            case DeviceType.DoorLock:
                                                var doorLock = new DoorLock() { DeviceAddr = jobject.Value<string>("DeviceAddr"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                                                doorLock.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLock.RemoveDeviceResponseData>(jobject["Data"].ToString());
                                                if (doorLock.removeDeviceResponseData == null)
                                                {
                                                    return;
                                                }
                                                else
                                                {
                                                    if (doorLock.removeDeviceResponseData.Result == 0)
                                                    {
                                                        var info = Gateway.DeviceList.Find((CommonDevice obj) => obj.DeviceID == doorLock.DeviceID && obj.DeviceAddr == doorLock.DeviceAddr && obj.DeviceEpoint == doorLock.DeviceEpoint);
                                                        if (info != null)
                                                        {
                                                            Gateway.DeviceList.Remove(info);
                                                        }
                                                    }
                                                }
                                                break;
                                        }
                                    }
                                }
@@ -1890,7 +2044,7 @@
                    { "DeviceAddrList", deviceAddrList }
                        };
                        jObject.Add("Data", data);
                         Gateway.Send(("RemoveDevice"), jObject.ToString());
                        Gateway.Send(("RemoveDevice"), jObject.ToString());
                    }
                }
                catch { }
@@ -2086,7 +2240,7 @@
                    var jObject = new JObject { { "Cluster_ID", 0 }, { "Command", 81 } };
                    var data = new JObject { { "GwId", gwId } };
                    jObject.Add("Data", data);
                     Gateway.Send(("RemoveEqOfGw"), jObject.ToString());
                    Gateway.Send(("RemoveEqOfGw"), jObject.ToString());
                }
                catch { }
@@ -2179,7 +2333,7 @@
                var jObject = new JObject { { "DeviceAddr", deviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 3 }, { "Command", 0 }, { "SendMode", 2 } };
                var data = new JObject { { "Time", time } };
                jObject.Add("Data", data);
                 Gateway.Send(("Identify"), jObject.ToString());
                Gateway.Send(("Identify"), jObject.ToString());
            }
            catch { }
@@ -2715,11 +2869,11 @@
                    { "AttriButeId", (int)attriButeId}
                   }
           };
                   var data = new JObject { { "AttriBute", attriBute } };
                   JObject.Add("Data", data);
                    var data = new JObject { { "AttriBute", attriBute } };
                    JObject.Add("Data", data);
                    Gateway?.Send(("GetDeviceStatus"), JObject.ToString());
               }
               catch { }
                }
                catch { }
                // Gateway.Actions -= action;
                System.Console.WriteLine("GetDeviceStatus_Actions 退出" + System.DateTime.Now.ToString());
@@ -2831,7 +2985,7 @@
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", clusterID }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", setWritableValue.Undivided }, { "AttributeId", setWritableValue.AttributeId }, { "AttributeDataType", setWritableValue.AttributeDataType }, { "AttributeData", setWritableValue.AttributeData } };
                    jObject.Add("Data", data);
                     gateway.Send("SetWritableValue", jObject.ToString());
                    gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
@@ -2951,7 +3105,7 @@
            try
            {
                var jobject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 6 }, { "Command", command }, { "SendMode", 2 } };
                 Gateway?.Send(("DeviceControl"), jobject.ToString());
                Gateway?.Send(("DeviceControl"), jobject.ToString());
                System.Console.WriteLine("SwitchControl_发送数据" + "_" + jobject.ToString() + "_" + System.DateTime.Now.ToString());
            }
@@ -3026,7 +3180,7 @@
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", clusterID }, { "Command", 301 } };
                     gateway.Send("Cluster/OwnAttributes", jObject.ToString());
                    gateway.Send("Cluster/OwnAttributes", jObject.ToString());
                }
                catch { }
@@ -3283,7 +3437,7 @@
                    var jObject = new JObject { { "Cluster_ID", 25 }, { "Command", 30 } };
                    var data = new JObject { { "OTAImageName", oTAImageName }, { "OTAImagePath", "/tmp" } };
                    jObject.Add("Data", data);
                     gateway.Send(("OTA/SetImage"), jObject.ToString());
                    gateway.Send(("OTA/SetImage"), jObject.ToString());
                }
                catch
                { }
@@ -3457,7 +3611,7 @@
                        }
                        var data = new JObject { { "DeviceList", deviceList } };
                        jObject.Add("Data", data);
                         gateway.Send(("OTA/StartUpdate"), jObject.ToString());
                        gateway.Send(("OTA/StartUpdate"), jObject.ToString());
                    }
                }
                catch
@@ -3594,28 +3748,28 @@
        /// <para>isOn 0:关闭透传数据上传</para>
        /// <para>isOn 1:开启透传数据上传</para>
        /// </summary>
        public void OpenPassthroughControl(ZigBee.Device.ZbGateway gateway, int isOn = 1)
        {
            if (Gateway == null)
            {
                return;
            }
            Action<string, string> action = (topic, message) => { };
            Gateway.Actions += action;
            System.Console.WriteLine("OnZbDataPassthrough_Actions 启动" + "_" + System.DateTime.Now.ToString());
        //public void OpenPassthroughControl(ZigBee.Device.ZbGateway gateway, int isOn = 1)
        //{
        //    if (Gateway == null)
        //    {
        //        return;
        //    }
        //    Action<string, string> action = (topic, message) => { };
        //    Gateway.Actions += action;
        //    System.Console.WriteLine("OnZbDataPassthrough_Actions 启动" + "_" + System.DateTime.Now.ToString());
            try
            {
                var jObject = new JObject { { "Cluster_ID", 64513 }, { "Command", 1 } };
                var data = new JObject { { "IsOn", isOn } };
                jObject.Add("Data", data);
                gateway.Send(("OnZbDataPassthrough"), jObject.ToString());
            }
            catch { }
        //    try
        //    {
        //        var jObject = new JObject { { "Cluster_ID", 64513 }, { "Command", 1 } };
        //        var data = new JObject { { "IsOn", isOn } };
        //        jObject.Add("Data", data);
        //        gateway.Send(("OnZbDataPassthrough"), jObject.ToString());
        //    }
        //    catch { }
            Gateway.Actions -= action;
            System.Console.WriteLine("OnZbDataPassthrough_Actions 退出" + System.DateTime.Now.ToString());
        }
        //    Gateway.Actions -= action;
        //    System.Console.WriteLine("OnZbDataPassthrough_Actions 退出" + System.DateTime.Now.ToString());
        //}
        #endregion
        #region 客户端向节点设备透传数据.
@@ -3671,7 +3825,6 @@
                try
                {
                    OpenPassthroughControl(gateway);
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", 200 }, { "Cluster_ID", 64513 }, { "Command", 0 } };
                    var data = new JObject { { "PassData", passData } };
                    jObject.Add("Data", data);
@@ -3837,7 +3990,7 @@
                        { "FileName", imageName }
                     };
                    jObject.Add("Data", data);
                     gateway.Send("DownloadFile", jObject.ToString());
                    gateway.Send("DownloadFile", jObject.ToString());
                }
                catch
                {
@@ -4003,7 +4156,7 @@
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", deviceEpoint }, { "Cluster_ID", 25 }, { "Command", 5 } };
                     gateway.Send("OTA/KillUpdate", jObject.ToString());
                    gateway.Send("OTA/KillUpdate", jObject.ToString());
                }
                catch
                { }
@@ -4098,12 +4251,12 @@
            /// <summary>
            ///响应操作码(0-ffff)
            /// </summary>
            public int command=-1;
            public int command = -1;
            /// <summary>
            /// 状态值
            /// <para>0--成功 1--失败 ff--无效</para>
            /// </summary>
            public int status=-1;
            public int status = -1;
        }
        #endregion
    }