wxr
2020-06-08 b71dfb3ca100340005d56e1298292807da82322d
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Collections.Generic;
using HDL_ON.DAL;
using HDL_ON.DAL.Net;
using HDL_ON.Entity;
using Newtonsoft.Json.Linq;
using Shared;
 
namespace HDL_ON
{
    public partial class Control
    {
        public static void Send(Function function)
        {
            ControlBusScenes(function as Scene);
        }
 
        /// <summary>
        /// 发送命令
        /// 自动判断是否为A协议设备,
        /// 不是A协议,自动转化bus命令数据
        /// </summary>
        /// <param name="commandString"></param>
        /// <param name="function"></param>
        /// <returns></returns>
        public static void Send(CommandType_A commandType, Function function)
        {
            function.usageCount++;
            function.refreshTime = DateTime.Now;
            if (function.bus_Data!=null)
            {
                try
                {
                    ControlBusData(function, commandType);
                }
                catch (Exception ex)
                {
                    MainPage.Log($"发送数据异常: {ex.Message}");
                }
            }
            else
            {
                var commandObj = function.GetSendJObject(commandType);
                var commandString = Newtonsoft.Json.JsonConvert.SerializeObject(commandObj);
                var commandBytes = CommonPage.MyEncodingUTF8.GetBytes(commandString);
                MqttCommon.MqttRemoteSend(commandBytes);
            }
        }
 
        /// <summary>
        /// 控制bus场景
        /// </summary>
        static void ControlBusScenes(Scene scene)
        {
            foreach(var busSceneData in scene.bus_SceneData)
            {
                foreach(var function in DB_ResidenceData.residenceData.functionList.GetAllDeviceFunctionList())
                {
                    if(busSceneData.Key == function.sid)
                    {
                        switch (function.functionType)
                        {
                            case FunctionType.Curtain:
                                ControlBytesSend(Command.SetCurtainModelStutas, function.bus_Data.SubnetID, function.bus_Data.DeviceID, busSceneData.Value);
                                break;
                            case FunctionType.AC:
                                ControlBytesSend(Command.SetACMode, function.bus_Data.SubnetID, function.bus_Data.DeviceID, busSceneData.Value);
                                break;
                            case FunctionType.Relay:
                            case FunctionType.Dimmer:
                                ControlBytesSend(Command.SetSingleLight, function.bus_Data.SubnetID, function.bus_Data.DeviceID, busSceneData.Value);
                                break;
                            case FunctionType.RGB:
                                ControlBytesSend(Command.SetLogicLoopColor, function.bus_Data.SubnetID, function.bus_Data.DeviceID, busSceneData.Value);
                                break;
                            case FunctionType.FloorHeating:
                                var fh = function as FloorHeating;
                                if (fh.modeTemp.Count == 4)
                                {
                                    byte b1 = fh.on_off == "on" ? (byte)1 : (byte)0;
                                    if (fh.workMode > 0)
                                    {
                                        b1 += (byte)(16 + fh.workMode);
                                    }
                                    ControlBytesSend(Command.SetFloorHeat, function.bus_Data.SubnetID, function.bus_Data.DeviceID, new byte[] { fh.bus_Data.LoopID, b1, (byte)fh.curTempType, fh.curModeIndex, fh.modeTemp["ordinary"], fh.modeTemp["day"], fh.modeTemp["night"], fh.modeTemp["away"], 0, 0 });
                                }
                                break;
                            case FunctionType.Socket:
                                var s = function as SwitchSocket;
                                ControlBytesSend(Command.SetSingleLight, function.bus_Data.SubnetID, function.bus_Data.DeviceID, new byte[] { s.bus_Data.LoopID, s.on_off == "on" ? (byte)100 : (byte)0 });
                                break;
                        }
                    }
                }
            }
        }
 
        /// <summary>
        /// 将A协议控制数据转换程bus协议的控制数据
        /// </summary>
        /// <param name="commandString"></param>
        /// <returns></returns>
        static void ControlBusData(Function function, CommandType_A command)
        {
            try
            {
                var subnetId = function.bus_Data.SubnetID;
                var deviceId = function.bus_Data.DeviceID;
                var loopId = function.bus_Data.LoopID;
 
                switch (command)
                {
                    case CommandType_A.write:
                        switch (function.functionCategory)
                        {
                            case FunctionCategory.Light:
                                var light = function as Light;
                                switch (light.functionType)
                                {
                                    case FunctionType.RGB:
                                        MainPage.Log($"rgb level : {light.brightness}");
                                        byte b = 100;//开关操作依据on_off字段,实际开关值依据brightness,当on_off为打开,brightness不能为0
                                        if (light.on_off == "off")
                                        {
                                            b = 0;
                                        }
                                        else
                                        {
                                            b = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness;
                                        }
                                        var bytes = new byte[] { function.bus_Data.LoopID,
                                            b,
                                            254, 0,0,3,
                                            (byte)light.redColor,(byte)light.greenColor,
                                            (byte)light.blueColor,
                                            0,0};
                                        MainPage.Log($"rgb level1 : {light.brightness}");
                                        ControlBytesSend(Command.SetLogicLoopColor, subnetId, deviceId, bytes,1);
                                        MainPage.Log($"rgb level2 : {light.brightness}");
                                        break;
                                    case FunctionType.Dimmer:
                                        byte b1 = 100;
                                        if (light.on_off == "off")
                                        {
                                            b1 = 0;
                                        }
                                        else
                                        {
                                            b1 = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness;
                                        }
                                        ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] {
                                            function.bus_Data.LoopID,
                                            b1,
                                            0, 0 });
                                        break;
                                    case FunctionType.Relay:
                                        ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { function.bus_Data.LoopID, light.on_off == "on" ? (byte)100 : (byte)0, 0, 0 });
                                        break;
                                }
                                break;
                            case FunctionCategory.Curtain:
                                var curtain = function as Curtain;
                                switch (curtain.functionType)
                                {
                                    case FunctionType.Curtain:
                                        byte b1 = 0;
                                        if (curtain.on_off == "stop")
                                        {
                                            b1 = 0;
                                        }else if(curtain.on_off == "on")
                                        {
                                            b1 = 1;
                                        }
                                        else
                                        {
                                            b1 = 2;
                                        }
                                        ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, b1 });
                                        break;
                                    case FunctionType.MotorCurtain:
                                    case FunctionType.RollingShutter:
                                        if (curtain.on_off == "stop")
                                        {
                                            ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, 0 });
                                        }
                                        ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, (byte)curtain.openLevel });
                                        break;
                                }
                                break;
                            case FunctionCategory.Thermostat:
                                switch (function.functionType)
                                {
                                    case FunctionType.AC:
                                        var aC = function as AC;
                                        ControlBytesSend(Command.SetACMode, subnetId, deviceId, new byte[] { aC.bus_Data.LoopID, (byte)aC.curTempType, 32, 32, 32, 32, 32, 0, aC.on_off == "on" ? (byte)1 : (byte)0, aC.curModeIndex, aC.curFanIndex, (byte)aC.curTemp, 0 });
                                        break;
                                    case FunctionType.FloorHeating:
                                        var fh = function as FloorHeating;
                                        if (fh.modeTemp.Count == 4)
                                        {
                                            byte b1 = fh.on_off == "on" ? (byte)1 : (byte)0;
                                            if (fh.workMode > 0)
                                            {
                                                b1 += (byte)(16 + fh.workMode);
                                            }
                                            ControlBytesSend(Command.SetFloorHeat, subnetId, deviceId, new byte[] { fh.bus_Data.LoopID, b1,(byte)fh.curTempType,fh.curModeIndex, fh.modeTemp["ordinary"], fh.modeTemp["day"], fh.modeTemp["night"], fh.modeTemp["away"], 0, 0 });
                                        }
                                        //else
                                        //{
                                        //    ControlBytesSend(Command.ReadFloorHeat, subnetId,deviceId,new byte[] { fh.bus_Data.LoopID});
                                        //}
                                        break;
                                }
                                break;
                            case FunctionCategory.Electrical:
                                switch (function.functionType)
                                {
                                    case FunctionType.Fan:
                                        var fan = function as Fan;
                                        if(fan.on_off == "on")
                                        {
                                            ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID, (byte)fan.openLevel });
                                        }
                                        {
                                            ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID, 0 });
                                        }
                                        break;
                                }
                                break;
                            case FunctionCategory.SwitchDevice:
                                switch(function.functionType)
                                {
                                    case FunctionType.Socket:
                                        var s = function as SwitchSocket;
                                        ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { s.bus_Data.LoopID, s.on_off == "on" ? (byte)100 : (byte)0 });
                                        break;
                                }
                                break;
                        }
                        break;
                    case CommandType_A.read:
                        switch (function.functionCategory)
                        {
                            case FunctionCategory.Light:
                                switch (function.functionType)
                                {
                                    case FunctionType.Relay:
                                    case FunctionType.Dimmer:
                                        ControlBytesSend(Command.ReadLightSingleLoopBrightness, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
                                        break;
                                    case FunctionType.RGB:
                                        ControlBytesSend(Command.ReadLogicLoopColor, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
                                        break;
                                }
                                break;
                            case FunctionCategory.Curtain:
                                switch (function.functionType)
                                {
                                    case FunctionType.Curtain:
                                    case FunctionType.MotorCurtain:
                                    case FunctionType.RollingShutter:
                                        ControlBytesSend(Command.ReadCurtainStatus, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
                                        break;
                                }
                                break;
                            case FunctionCategory.Thermostat:
                                switch (function.functionType)
                                {
                                    case FunctionType.AC:
                                        ControlBytesSend(Command.ReadACMode, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
                                        break;
                                    case FunctionType.FloorHeating:
                                        ControlBytesSend(Command.ReadFloorHeat, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
                                        break;
                                }
                                break;
                            case FunctionCategory.Electrical:
                                switch (function.functionType)
                                {
                                    case FunctionType.Fan:
                                        var fan = function as Fan;
                                        ControlBytesSend(Command.SetSingleLightACK, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID });
                                        break;
                                }
                                break;
                            case FunctionCategory.SwitchDevice:
                                switch (function.functionType)
                                {
                                    case FunctionType.Socket:
                                        var s = function as SwitchSocket;
                                        ControlBytesSend(Command.SetSingleLightACK, subnetId, deviceId, new byte[] { s.bus_Data.LoopID });
                                        break;
                                }
                                break;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                MainPage.Log($"DataConversion_Bus Erorr : {ex.Message}");
            }
        }
    }
}