黄学彪
2020-04-13 3793a9a38ac6c4c4111c2bba3a35a71c30601e82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System;
using Newtonsoft.Json.Linq;
 
namespace ZigBee.Device
{
    [System.Serializable]
    public class FreshAir : CommonDevice
    {
        public FreshAir()
        {
            this.Type = DeviceType.FreshAir;
        }
 
        /// <summary>
        /// 恒温设备当前模式
        /// <para>恒温设备具备功能,SystemMode Attribute Values如下</para>
        /// <para>0:Off </para>
        /// <para>1:Auto </para>
        /// <para>3:Cool </para>
        /// <para>4:Heat </para>
        /// <para>5:Emergency heating </para>
        /// <para>6:Precooling</para>
        /// <para>7:Fan only </para>
        /// <para>8:Dry </para>
        /// <para>9:Sleep</para>
        /// </summary>
        public int currentSystemMode = 0;
 
        ///<summary >
        ///设置恒温器设备当前工作模式.
        /// </summary>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> SetSystemModeAsync(AcMode acMode)
        {
            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 };
                            DebugPrintLog($"UI收到通知后的主题_{ topic}");
                        }
                    }
                };
                Gateway.Actions += action;
                DebugPrintLog("SetWritableValue_Actions 启动" + "_" + System.DateTime.Now.ToString());
                try
                {
                    var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 513 }, { "Command", 120 } };
                    var data = new JObject { { "Undivided", 0 }, { "AttributeId", 28 }, { "AttributeDataType", 48 }, { "AttributeData", (int)acMode } };
                    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;
                DebugPrintLog("SetWritableValue_Actions 退出" + System.DateTime.Now.ToString());
                return d;
            });
        }
 
        public enum AcMode
        {
            /// <summary>
            /// 关闭模式(测试恒温面板时发现:2,5,6,7,8,9都是可以打开的)
            /// </summary>
            Off = 0,
            /// <summary>
            /// 自动模式
            /// </summary>
            Auto = 1,
            /// <summary>
            /// 制冷模式
            /// </summary>
            Cool = 3,
            /// <summary>
            /// 制热模式
            /// </summary>
            Heat = 4,
            /// <summary>
            /// 紧急制热模式
            /// </summary>
            EmergencyHeating = 5,
            /// <summary>
            /// 预冷模式
            /// </summary>
            Precooling = 6,
            /// <summary>
            /// 只有风速模式
            /// </summary>
            FanOnly = 7,
            /// <summary>
            /// 干燥模式
            /// </summary>
            Dry = 8,
            /// <summary>
            /// 睡眠模式
            /// </summary>
            Sleep = 9
        }
 
        /// <summary>
        /// 关闭
        /// </summary>
        /// <returns>The close.</returns>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> Close()
        {
            return await SetSystemModeAsync(AcMode.Off);
        }
 
        /// <summary>
        /// 开启
        /// </summary>
        /// <returns>The open.</returns>
        /// <param name="acMode">Ac mode.</param>
        public async System.Threading.Tasks.Task<SetWritableValueResponAllData> Open(AcMode acMode = AcMode.Cool)
        {
            return await SetSystemModeAsync(acMode);
        }
    }
}