wxr
2020-03-19 b69d7735274b8d0f741da8a6bb8b8e1347477a5a
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
 
namespace HDL_ON.Entity
{
    public class Light : Function
    {
        /*
        灯光类:trait: [switch,brightness,color,cct,delay,fadeTime]
        属性    描述
        switch    on/off;
        brightness    0-100;
        color    int (red (0-255) green (0-255) blue (0-255))
        cct    int (warm light(0-255) cold light (0-255) )
        delay    0-3600s
        fadetime    0-3600s
        */
        public Light()
        {
        }
        /// <summary>
        /// 获取功能类型
        /// </summary>
        /// <returns></returns>
        protected override string GetFunctionType()
        {
            string type = "Relay";
            if (PropertyArray.Contains("brightness"))
            {
                type = "Dimmer";
                if (PropertyArray.Contains("color"))
                {
                    type = "RGB";
                }
            }
            return type;
        }
 
        /// <summary>
        /// 开关状态
        /// 0:关
        /// 1:开
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int on_off
        {
            get
            {
                try
                {
                    string o = "0";
                    dicPropert.TryGetValue("on_off", out o);
                    return Convert.ToInt32(o == "" ? "0" : o);
                }
                catch
                {
                    MainPage.Log("on_off 数据获取失败.");
                    dicPropert.TryAdd("on_off", "0");
                    return 0;
                }
            }
            set
            {
                try
                {
                    dicPropert["on_off"] = value.ToString();
                }
                catch
                {
                    MainPage.Log("on_off 数据刷新失败.");
                    dicPropert.TryAdd("on_off", value.ToString());
                }
            }
        }
        /// <summary>
        /// 亮度值
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public int brightness
        {
            get
            {
                string b = "0";
                dicPropert.TryGetValue("brightness", out b);
                return Convert.ToInt32(b == "" ? "0" : b);
            }
            set
            {
                try
                {
                    dicPropert["brightness"] = value.ToString();
                }
                catch
                {
                    MainPage.Log("brightness 数据刷新失败.");
                }
            }
        }
 
        /// <summary>
        /// RGB颜色
        /// 255255255
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public string color
        {
            get
            {
                string c = "255255255";
                dicPropert.TryGetValue("color", out c);
                if (c.Length != 9)
                {
                    dicPropert["color"] = "255255255";
                }
                return c.Length == 9 ? c : "255255255";
            }
            set
            {
                try
                {
                    dicPropert["color"] = value.ToString();
                }
                catch
                {
                    MainPage.Log("color 数据刷新失败.");
                }
            }
        }
        [Newtonsoft.Json.JsonIgnore]
        public byte redColor
        {
            get {
                try
                {
                    return Convert.ToByte(color.Substring(0, 3));
                }
                catch (Exception ex)
                {
                    MainPage.Log($"Get red color error : {ex.Message}");
                    return 0;
                }
            }
            set
            {
                try
                {
                    dicPropert["color"] = dicPropert["color"].ToString().Remove(0, 3).Insert(0, value.ToString().PadLeft(3, '0'));
                }
                catch (Exception ex)
                {
                    MainPage.Log($"set red color error : {ex.Message}");
                }
            }
        }
        [Newtonsoft.Json.JsonIgnore]
        public byte greenColor
        {
            get
            {
                try
                {
                    return Convert.ToByte(color.Substring(3, 3));
                }
                catch (Exception ex)
                {
                    MainPage.Log($"Get green color error : {ex.Message}");
                    return 0;
                }
            }
            set
            {
                try
                {
                    dicPropert["color"] = dicPropert["color"].ToString().Remove(3, 3).Insert(3, value.ToString().PadLeft(3, '0'));
                }
                catch (Exception ex)
                {
                    MainPage.Log($"set green color error : {ex.Message}");
                }
            }
        }
        [Newtonsoft.Json.JsonIgnore]
        public byte blueColor
        {
            get
            {
                try
                {
                    return Convert.ToByte(color.Substring(6, 3));
                }
                catch (Exception ex)
                {
                    MainPage.Log($"Get blue color error : {ex.Message}");
                    return 0;
                }
            }
            set
            {
                try
                {
                    dicPropert["color"] = dicPropert["color"].ToString().Remove(6, 3).Insert(6, value.ToString().PadLeft(3, '0'));
                }
                catch (Exception ex)
                {
                    MainPage.Log($"set blue color error : {ex.Message}");
                }
            }
        }
 
 
        /// <summary>
        /// 拼接、获取A协议操作数据
        /// </summary>
        public override JObject GetSendJObject(string command )
        {
            var sendJob = new JObject();
            if (command == "write")
            {
 
                sendJob = new JObject { { "vendor_code", a_Protocol_Namespace }, { "Command", command }, { "Type", "device" } };
                JObject data = null;
                switch (functionType)
                {
                    case "Relay"://继电器控制
                        data = new JObject { { "sid", sid }, { "switch", on_off } };
                        break;
                    case "Dimmer":
                        data = new JObject { { "sid", sid }, { "brightness", brightness } };
                        break;
                    case "RGB":
                        data = new JObject { { "sid", sid }, { "brightness", brightness }, { "color", color } };
                        break;
                }
                sendJob.Add("objects", data);
            }
            else if(command == "read")
            {
                sendJob = new JObject { { "vendor_code", a_Protocol_Namespace }, { "Command", command }, { "Type", "device" } };
                var data = new JObject {{ "sid", sid } };
                sendJob.Add("objects", data);
            }
            return sendJob;
        }
    }
}