陈嘉乐
2020-07-10 48ba446936b51fffafa7c3600c0dadc6ac0e8c20
ZigbeeApp/Shared/Phone/ZigBee/Device/Rollershade.cs
@@ -16,81 +16,44 @@
        /// WcdType=4:开合帘
        /// WcdType=0:卷帘
        /// </summary>
        public int WcdType = 0;
        /// <summary>
        ///窗帘当前高度,单位:cm.
        /// </summary>
        public int WcdCurrentPositionLift = 0;
        public int WcdType = -1;
        /// <summary>
        /// 当前窗帘所在的位置(百分比).
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int WcdCurrentPositionLiftPercentage = 0;
        //{
        //    get
        //    {
        //        if (DeviceStatusReport.CluterID == 258)
        //        {
        //            var attriButeList = DeviceStatusReport.AttriBute;
        //            foreach (var attriBute in attriButeList)
        //            {
        //                if (attriBute.AttributeId == 8)
        //                {
        //                    return attriBute.AttriButeData;
        //                }
        //            }
        //        }
        //        return 0;
        //    }
        //}
        /// <summary>
        /// 窗帘全开所在的角度,单位:0.1°
        /// </summary>
        public int WcdInstalledOpenLimitTilt = 0;
        /// <summary>
        /// 窗帘全关所在的角度,单位:0.1°
        /// </summary>
        public int WcdInstalledClosedLimitTilt = 0;
        /// <summary>
        /// 当前开合帘电机方向
        /// <para> (false:电机方向正向;true:电机方向反向;)</para> .
        /// </summary>
        public bool WcdCurrentDirection = false;
        /// <summary>
        /// 当前开合帘维护模式
        /// <para>fasle:电机运转正常;true:电机在维修模式下运行;</para>
        /// </summary>
        public bool WcdCurrentMaintenance = false;
        /// <summary>
        /// 当前窗帘开合帘限位开(上)限位
        /// 注意这里值是网关返回的实际值,UI上显示的值要实际处理
        /// </summary>
        public int WcdCurrentWcdInstalOpenLimit = 0;
        /// <summary>
        /// 当前开合帘限位关(下)限位
        /// 注意这里值是网关返回的实际值,UI上显示的值要实际处理
        /// </summary>
        public int WcdCurrentInstalCloseLimit = 0;
        /// <summary>
        /// 当前开合帘总的长度
        /// </summary>
        public int WcdCurrentLength = 0;
        /// <summary>
        /// 读取当前窗帘的模式
        /// <para>AttriButeId=0 (先调用发现属性接口,查看设备是否支持该接口的属性)</para>
        /// </summary>
        public void ReadWcdType()
        public new void ReadWcdType()
        {
            ReadAttri(Device.Cluster_ID.WindowCovering, AttriButeId.WindowCoveringType);
        }
        /// <summary>
        /// 读取当前窗帘模式
        /// </summary>
        /// <param name="commonDevice"></param>
        /// <param name="action"></param>
        public static void ReadWcdTypeAction(CommonDevice commonDevice, Action action)
        {
            new System.Threading.Thread(() =>
            {
                (commonDevice as Rollershade).ReadWcdType();
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalSeconds < 10)
                {
                    System.Threading.Thread.Sleep(100);
                    if ((commonDevice as Rollershade).WcdType != -1)
                    {
                        break;
                    }
                }
                action?.Invoke();
            })
            { IsBackground = true }.Start();
        }
        /// <summary>
@@ -209,7 +172,7 @@
            var data = new JObject {
                    { "Value", value }
                 };
            jObject.Add("Data", data);
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
@@ -231,7 +194,7 @@
            var data = new JObject {
                    { "Value", value }
                 };
            jObject.Add("Data", data);
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
@@ -253,7 +216,7 @@
            var data = new JObject {
                    { "Value", value }
                };
            jObject.Add("Data", data);
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
@@ -276,190 +239,9 @@
            var data = new JObject {
                    { "Value", value }
                 };
            jObject.Add("Data", data);
            jObject.Add("Data", data);
            Gateway?.Send("DeviceControl", jObject.ToString());
        }
        #region 设置开合帘电机方向.
        /// <summary>
        /// 设置开合帘电机方向
        /// <para>isDirectionReversed (false:电机方向正向;true:电机方向反向;)</para> .
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetCurtainDirectionAsync(bool isDirectionReversed)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SetWritableValueResponAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
                        if (tempData == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                System.Console.WriteLine("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    int attributeData = 0;
                    int bit0 = 0;
                    if (isDirectionReversed)
                    {
                        bit0 = 1;
                    }
                    attributeData = bit0 * 1;
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 23 }, { "AttributeDataType", 24 }, { "AttributeData", attributeData } };
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                System.Console.WriteLine("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
        #endregion
        #region 设置开合帘维护模式
        /// <summary>
        /// 设置开合帘维护模式
        /// <para>isMotorMaintenance (fasle:电机运转正常;true:电机在维修模式下运行;)</para>
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetCurtianMaintenanceAsync(bool isMotorMaintenance)
        {
            if (Gateway == null)
            {
                return null;
            }
            return await System.Threading.Tasks.Task.Run(async () =>
            {
                SetWritableValueResponAllData d = null;
                Action<string, string> action = (topic, message) =>
                {
                    var gatewayID = topic.Split('/')[0];
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关错误回复,且数据是空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { errorResponData = temp, errorMessageBase = ErrorMess(temp.Error) };
                        }
                    }
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
                        if (tempData == null)
                        {
                            d = new SetWritableValueResponAllData { errorMessageBase = "网关返回的数据为空" };
                        }
                        else
                        {
                            d = new SetWritableValueResponAllData { setWritableValueResponData = tempData };
                            System.Console.WriteLine($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                System.Console.WriteLine("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    int attributeData = 0;
                    int bit2 = 0;
                    if (isMotorMaintenance)
                    {
                        bit2 = 1;
                    }
                    attributeData = bit2 * 4;
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 23 }, { "AttributeDataType", 24 }, { "AttributeData", attributeData } };
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
                var dateTime = DateTime.Now;
                while ((DateTime.Now - dateTime).TotalMilliseconds < WaitReceiveDataTime)
                {
                    await System.Threading.Tasks.Task.Delay(10);
                    if (d != null)
                    {
                        break;
                    }
                }
                if ((DateTime.Now - dateTime).TotalMilliseconds > WaitReceiveDataTime)
                {
                    d = new SetWritableValueResponAllData { errorMessageBase = " 回复超时,请重新操作" };
                }
                Gateway.Actions -= action;
                System.Console.WriteLine("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
        #endregion
        #region 设置开合帘限位参数.
        /// <summary>
@@ -481,8 +263,7 @@
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
@@ -496,7 +277,6 @@
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
                        if (tempData == null)
@@ -519,15 +299,15 @@
                    switch (installedLimit)
                    {
                        case CurtainPrivateInstalledLimi.UpLimit:
                            tempLimit = 1;
                            tempLimit = 2;
                            break;
                        case CurtainPrivateInstalledLimi.DownLimit:
                            tempLimit = 2;
                            tempLimit = 1;
                            break;
                    }
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 0xea01 }, { "AttributeDataType", 32 }, { "AttributeData", tempLimit } };
                    jObject.Add("Data", data);
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
@@ -560,11 +340,11 @@
            /// <summary>
            /// 窗帘全开所在的位置(上限位)
            /// </summary>
            UpLimit = 1,
            UpLimit = 2,
            /// <summary>
            /// 窗帘全关所在的位置(下限位)
            /// </summary>
            DownLimit = 2,
            DownLimit = 1,
        }
        #endregion
@@ -589,8 +369,7 @@
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
@@ -604,7 +383,6 @@
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
                        if (tempData == null)
@@ -622,11 +400,11 @@
                System.Console.WriteLine("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 0xea02 }, { "AttributeDataType", 32 }, { "AttributeData", 1 } };
                    jObject.Add("Data", data);
                    jObject.Add("Data", data);
                    Gateway.Send("SetWritableValue", jObject.ToString());
                }
                catch { }
@@ -688,8 +466,7 @@
                    if (topic == gatewayID + "/" + "Error_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { Time = jobject.Value<int>("Time"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<ZbGateway.ErrorResponData>(jobject["Data"].ToString());
                        var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.ErrorResponData>(jobject["Data"].ToString());
                        if (temp == null)
                        {
@@ -703,7 +480,6 @@
                    if (topic == gatewayID + "/" + "SetWritableValue_Respon")
                    {
                        var gatewayTemp = new ZbGateway() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = Gateway.getGatewayBaseInfo.gwID };
                        var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<SetWritableValueResponData>(jobject["Data"].ToString());
                        if (tempData == null)
@@ -726,10 +502,10 @@
                    switch (limiType)
                    {
                        case LimiType.DownLimit:
                            tempLimit = 8;
                            tempLimit = 2;
                            break;
                        case LimiType.UpLimit:
                            tempLimit = 2;
                            tempLimit = 8;
                            break;
                    }
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
@@ -767,13 +543,13 @@
            /// <summary>
            /// 删除窗帘上限位
            /// </summary>
            UpLimit = 0x02,
            UpLimit = 0x08,
            /// <summary>
            /// 删除窗帘下限位
            /// </summary>
            DownLimit = 0x08,
            DownLimit = 0x02,
        }
        }
        #endregion
    }
}