黄学彪
2019-12-20 23fb45dd846ed8b62304c408c6bbe64265d4ac8b
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
using System;
using Shared.Common;
using Shared.Phone.UserCenter;
using Shared.Phone.UserView;
 
namespace Shared.Phone.Device.CommonForm
{
    public class RoomView:FrameLayout
    {
        /// <summary>
        /// Room
        /// </summary>
        private Common.Room room;
        /// <summary>
        /// action
        /// </summary>
        public Action action;
 
        public override void RemoveFromParent()
        {
            HdlGatewayReceiveLogic.Current.RemoveEvent("TemperatrueDevice");
            HdlGatewayReceiveLogic.Current.RemoveEvent("HumidityDevice");
            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);
        }
 
        /// <summary>
        /// Init
        /// </summary>
        /// <param name="r"></param>
        public void Init(Common.Room r)
        {
            this.room = r;
 
            var roomBackView = new FrameLayout()
            {
                Width = Application.GetRealWidth(717),
                Height = Application.GetRealHeight(478)
            };
            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 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(5),
                Width = Application.GetRealWidth(190),
                Text = room.Name,
                TextColor = ZigbeeColor.Current.GXCTextWhiteColor,
            };
            roomNameBackground.AddChidren(roomName);
 
 
            var 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);
 
            var temperatureText = new Button
            {
                X = Application.GetRealWidth(69),
                Width = Application.GetRealWidth(120),
                Height = Application.GetRealHeight(58),
                Gravity = Gravity.CenterVertical,
                Text = $"{room.Temperatrue}℃"
            };
            roomTemperatureBackground.AddChidren(temperatureText);
 
            var humidityIcon = new Button
            {
                X = Application.GetRealWidth(200),
                Width = Application.GetMinRealAverage(58),
                Height = Application.GetMinRealAverage(58),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Room/Humidity.png"
            };
            roomTemperatureBackground.AddChidren(humidityIcon);
 
            var humidityText = new Button
            {
                X = Application.GetRealWidth(260),
                Width = Application.GetRealWidth(120),
                Height = Application.GetRealHeight(58),
                Gravity = Gravity.CenterVertical,
                Text = $"{room.Humidity}%"
            };
            roomTemperatureBackground.AddChidren(humidityText);
 
 
            if (string.IsNullOrEmpty(room.TemperatrueDevice) == false)
            {
                HdlGatewayReceiveLogic.Current.RemoveEvent("TemperatrueDevice");
                HdlGatewayReceiveLogic.Current.AddAttributeEvent("TemperatrueDevice", ReceiveComandDiv.A设备属性上报, (Action<ZigBee.Device.CommonDevice>)((report) =>
                {
                    string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(report);
                    if (room.TemperatrueDevice != mainKeys)
                    {
                        return;
                    }
                    //移除掉事件
                    HdlGatewayReceiveLogic.Current.RemoveEvent("TemperatrueDevice");
 
                    foreach (var data in report.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 = int.Parse(strValue) * 0.1;
                            }
                            else
                            {
                                //小数点需要一位
                                string strValue = data.AttriButeData.ToString();
                                strValue = strValue.Substring(0, strValue.Length - 1);
                                temperatureText.Text = strValue.Insert(strValue.Length - 1, ".") + "℃";
                                room.Temperatrue = int.Parse(strValue) * 0.1;
                            }
                        }
                    }
                }));
                //发送获取温度的命令
                var dev = Common.LocalDevice.Current.GetDevice(room.TemperatrueDevice);
                if (dev != null)
                {
                    (dev as ZigBee.Device.TemperatureSensor).ReadTemperatureOrHumidity();
                }
            }
 
            if (string.IsNullOrEmpty(room.HumidityDevice) == false)
            {
                HdlGatewayReceiveLogic.Current.RemoveEvent("HumidityDevice");
                HdlGatewayReceiveLogic.Current.AddAttributeEvent("HumidityDevice",  ReceiveComandDiv.A设备属性上报, (report) =>
                {
                    string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(report);
                    if (room.HumidityDevice != mainKeys)
                    {
                        return;
                    }
                    //移除掉事件
                    HdlGatewayReceiveLogic.Current.RemoveEvent("HumidityDevice");
                    foreach (var data in report.DeviceStatusReport.AttriBute)
                    {
                        if (data.AttributeId == (int)ZigBee.Device.AttriButeId.MeasuredValue)
                        {
                            if (data.AttriButeData == 0)
                            {
                                //0
                                humidityText.Text = "0.0%";
                                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 = int.Parse(strValue) * 0.1;
                            }
                        }
                    }
                });
                //发送获取湿度的命令
                var dev = Common.LocalDevice.Current.GetDevice(room.HumidityDevice);
                if (dev != null)
                {
                    (dev as ZigBee.Device.TemperatureSensor).ReadTemperatureOrHumidity();
                }
            }
 
 
            var 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 editRoom = new Device.Room.EditRoom();
                HomePage.Instance.AddChidren(editRoom);
                HomePage.Instance.PageIndex += 1;
                editRoom.Show(room);
                editRoom.action += () =>
                {
                    action?.Invoke();
                };
            };
        }
    }
}