黄学彪
2020-03-23 21923381bdac04d1633b168c97accc81f0898d84
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
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using Shared.Common;
using Shared.Phone.Device.DeviceLogic;
using Shared.Phone.UserCenter;
using Shared.Phone.UserView;
using ZigBee.Device;
 
namespace Shared.Phone.Device.CommonForm
{
    public class RoomView:FrameLayout, ZigBee.Common.IStatus
    {
        /// <summary>
        /// Room
        /// </summary>
        private Common.Room room;
 
        FrameLayout roomNameBackground;
 
        FrameLayout roomTemperatureBackground;
 
        Button roomListBtn;
 
        Button temperatureText;
 
        Button humidityText;
 
        CommonDevice temperDevice;
 
        CommonDevice humidDevice;
 
        #region ◆ 接口__________________________
      
        /// <summary>
        /// 设备状态更新接口
        /// <para>type:如果为 DeviceInComingRespon:设备新上报</para>
        /// <para>type:如果为 IASInfoReport:RemoveDeviceRespon</para>
        /// <para>type:如果为 DeviceStatusReport:设备上报</para>
        /// <para>type:如果为 IASInfoReport:IAS安防信息上报</para>
        /// <para>type:如果为 OnlineStatusChange: 设备在线状态更新</para>
        /// </summary>
        /// <param name="common">Common.</param>
        /// <param name="typeTag">Type tag.</param>
        public void DeviceInfoChange(CommonDevice common, string typeTag)
        {
            if (typeTag == "DeviceStatusReport")
            {
                Application.RunOnMainThread(() =>
                {
                    try
                    {
                        if (common.DeviceStatusReport.AttriBute == null || common.DeviceStatusReport.AttriBute.Count == 0)
                        {
                            return;
                        }
                        //是否为当前设备
                        if ((temperDevice?.DeviceEpoint != common.DeviceEpoint || temperDevice?.DeviceAddr != common.DeviceAddr) && (humidDevice?.DeviceEpoint != common.DeviceEpoint || humidDevice?.DeviceAddr != common.DeviceAddr) )
                        {
                            return;
                        }
 
                        //if (common.Type == DeviceType.TemperatureSensor)
                        //{
                        if (common.DeviceStatusReport.CluterID == 1026)
                        {
                            foreach (var data in common.DeviceStatusReport.AttriBute)
                            {
                                if (data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
                                {
                                    if (data.AttriButeData == 0)
                                    {
                                        //0℃
                                        temperatureText.Text = "0.0℃";
                                        room.Temperatrue = 0;
                                    }
                                    else if (data.AttriButeData > 32767)
                                    {
                                        //负数(特殊处理)
                                        string strValue = (data.AttriButeData - 65536).ToString();
                                        //小数点需要一位
                                        strValue = strValue.Substring(0, strValue.Length - 1);
                                        temperatureText.Text = strValue.Insert(strValue.Length - 1, ".") + "℃";
                                        room.Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                    }
                                    else
                                    {
                                        //小数点需要一位
                                        string strValue = data.AttriButeData.ToString();
                                        strValue = strValue.Substring(0, strValue.Length - 1);
                                        temperatureText.Text = strValue.Insert(strValue.Length - 1, ".") + "℃";
                                        room.Temperatrue = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                    }
                                }
                            }
                        }
                        else if (common.DeviceStatusReport.CluterID == 1029)
                        {
                            foreach (var data in common.DeviceStatusReport.AttriBute)
                            {
                                if (data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
                                {
                                    if (data.AttriButeData == 0)
                                    {
                                        //0
                                        humidityText.Text = "--%";
                                        room.Humidity = 0;
                                    }
                                    else
                                    {
                                        //小数点需要一位(湿度没有负数)
                                        string strValue = data.AttriButeData.ToString();
                                        strValue = strValue.Substring(0, strValue.Length - 1);
                                        humidityText.Text = strValue.Insert(strValue.Length - 1, ".") + "%";
                                        room.Humidity = Convert.ToDecimal(strValue.Insert(strValue.Length - 1, "."));
                                    }
                                }
                            }
                        }
                        //}
                    }
                    catch (Exception ex)
                    {
                        System.Console.WriteLine($"Error:{ex.Message}");
                    }
                });
            }
        }
        #endregion
 
        public override void RemoveFromParent()
        {
            ZbGateway.StatusList.Remove(this);
            base.RemoveFromParent();
        }
 
        /// <summary>
        /// RoomView
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public RoomView(int x, int y)
        {
            X = Application.GetRealWidth(x);
            Y = Application.GetRealHeight(y);
            Width = Application.GetRealWidth(717);
            Height = Application.GetRealHeight(478);
            ZbGateway.StatusList.Add(this);
        }
 
        /// <summary>
        /// Init
        /// </summary>
        /// <param name="r"></param>
        public void Init(Common.Room r)
        {
            this.RemoveAll();
 
            this.room = r;
 
            this.temperDevice = Common.LocalDevice.Current.GetDevice(room.TemperatrueDevice);
 
            this.humidDevice = Common.LocalDevice.Current.GetDevice(room.HumidityDevice);
 
            var roomBackView = new FrameLayout()
            {
                Width = Application.GetRealWidth(717),
                Height = Application.GetRealHeight(478),
                Radius = (uint)Application.GetRealHeight(17)
            };
            AddChidren(roomBackView);
 
            var roomImg = new ImageView()
            {
                ImagePath = room.BackgroundImageType == 0 ? room.BackgroundImage : System.IO.Path.Combine(Config.Instance.FullPath, room.BackgroundImage),
                Radius = (uint)Application.GetRealHeight(17)
            };
            roomBackView.AddChidren(roomImg);
 
            //加个特殊的遮罩
            var frameBackGroudTemp = new FrameLayout();
            frameBackGroudTemp.Width = roomBackView.Width;
            frameBackGroudTemp.Height = roomBackView.Height;
            frameBackGroudTemp.Radius = roomBackView.Radius;
            frameBackGroudTemp.BackgroundColor = 0x12000000;
            roomBackView.AddChidren(frameBackGroudTemp);
 
            roomNameBackground = new FrameLayout
            {
                X = Application.GetRealWidth(29),
                Y = Application.GetRealHeight(282),
                Width = Application.GetRealWidth(200),
                Height = Application.GetRealHeight(80),
                BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor2
            };
            roomBackView.AddChidren(roomNameBackground);
            roomNameBackground.SetCornerWithSameRadius(Application.GetRealHeight(40), HDLUtils.RectCornerTopRight | HDLUtils.RectCornerBottomRight);
 
            var roomName = new Button()
            {
                X = Application.GetRealWidth(29),
                Width = Application.GetRealWidth(190),
                Text = room.Name,
                TextColor = ZigbeeColor.Current.GXCTextWhiteColor,
                TextSize = 10,
                TextAlignment=TextAlignment.CenterLeft,
                IsBold = true
            };
            roomNameBackground.AddChidren(roomName);
            roomNameBackground.Width = (roomName.GetTextWidth() + Application.GetRealWidth(100)) > Application.GetRealWidth(600) ? Application.GetRealWidth(600) : roomName.GetTextWidth() + Application.GetRealWidth(100);
            roomName.Width = roomNameBackground.Width - Application.GetRealWidth(40);
 
            roomTemperatureBackground = new FrameLayout
            {
                X = Application.GetRealWidth(29),
                Y = Application.GetRealHeight(374),
                Width = Application.GetRealWidth(400),
                Height = Application.GetRealHeight(80),
                BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor2
            };
            roomBackView.AddChidren(roomTemperatureBackground);
            roomTemperatureBackground.SetCornerWithSameRadius(Application.GetRealHeight(40), HDLUtils.RectCornerTopRight | HDLUtils.RectCornerBottomRight);
 
            var temperatureIcon = new Button
            {
                X = Application.GetRealWidth(12),
                Width = Application.GetMinRealAverage(58),
                Height = Application.GetMinRealAverage(58),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Room/Temperature.png"
            };
            roomTemperatureBackground.AddChidren(temperatureIcon);
 
            temperatureText = new Button
            {
                X = Application.GetRealWidth(69),
                Width = Application.GetRealWidth(120),
                Text = "--℃",
                TextSize = 14,
                TextAlignment=TextAlignment.CenterLeft
            };
            if (string.IsNullOrEmpty(room.TemperatrueDevice) == false)
            {
                temperatureText.Text = room.Temperatrue == 0 ? "0.0℃" : room.Temperatrue.ToString() + "℃";
            }
            roomTemperatureBackground.AddChidren(temperatureText);
            temperatureText.Width = temperatureText.GetTextWidth() + Application.GetRealWidth(60);
 
            var humidityIcon = new Button
            {
                X = temperatureText.Right,
                Width = Application.GetMinRealAverage(58),
                Height = Application.GetMinRealAverage(58),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Room/Humidity.png"
            };
            roomTemperatureBackground.AddChidren(humidityIcon);
 
            humidityText = new Button
            {
                X = humidityIcon.Right,
                Width = Application.GetRealWidth(120),
                Text = "--%",
                TextSize = 14,
                TextAlignment = TextAlignment.CenterLeft
            };
            if (string.IsNullOrEmpty(room.HumidityDevice) == false)
            {
                humidityText.Text = room.Humidity == 0 ? "0.0%" : room.Humidity.ToString() + "%";
            }
            roomTemperatureBackground.AddChidren(humidityText);
            humidityText.Width = humidityText.GetTextWidth() + Application.GetRealWidth(60);
            roomTemperatureBackground.Width = (humidityText.Width + temperatureText.Width + Application.GetRealWidth(150)) > Application.GetRealWidth(600) ? Application.GetRealWidth(600) : (humidityText.Width + temperatureText.Width + Application.GetRealWidth(150));
 
            if (string.IsNullOrEmpty(room.TemperatrueDevice) == false)
            {
                //发送获取温度的命令
                var dev = Common.LocalDevice.Current.GetDevice(room.TemperatrueDevice);
                ReadDeviceAttributeLogic.Instance.SendTemperatureStatuComand(dev);
            }
 
            if (string.IsNullOrEmpty(room.HumidityDevice) == false)
            {
                var dev = Common.LocalDevice.Current.GetDevice(room.HumidityDevice);
                ReadDeviceAttributeLogic.Instance.SendHumidityStatuComand(dev);
            }
 
            roomListBtn = new Button()
            {
                X = roomBackView.Width - Application.GetRealWidth(100 + 20),
                Y = Application.GetRealHeight(20),
                Width = Application.GetMinRealAverage(100),
                Height = Application.GetMinRealAverage(100),
                UnSelectedImagePath = "Room/List.png",
            };
            roomBackView.AddChidren(roomListBtn);
 
            roomListBtn.MouseUpEventHandler += (send, e) =>
            {
                CommonPage.Instance.IsDrawerLockMode = true;
 
                var form = new UserCenter.Residence.EditorRoomInforForm();
                form.AddForm(room);
                form.FinishEditorEvent += (roomName2) =>
                {
                    //重新刷新控件
                    this.Init(this.room);
                };
            };
        }
 
        /// <summary>
        /// HideName
        /// </summary>
        public void HideName(bool statu)
        {
            roomNameBackground.Visible = roomTemperatureBackground.Visible = roomListBtn.Visible = !statu;
        }
    }
}