mac
2024-07-25 16bea1d248f0010049bceaa562939297fa26b130
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI.UI2.FuntionControlView.VideoDoorLock;
using Shared;
using static HDL_ON.UI.UI2.FuntionControlView.VideoDoorLock.CommonMethod;
 
namespace HDL_ON.UI.UI2.FuntionControlView.HisenseTV
{
    public class Send
    {
        private static Send sendMethod = null;
        /// <summary>
        /// 获取对象
        /// </summary>
        public static Send Current
        {
            get
            {
                if (sendMethod == null)
                {
                    sendMethod = new Send();
                }
 
                return sendMethod;
 
            }
 
        }
        /// <summary>
        /// 打开海信电视
        /// </summary>
        /// <param name="deviceMac">电视mac</param>
        public void Open(string deviceMac)
        {
            CommonMethod.Current.SunThread(() =>
            {
                // 创建UdpClient实例
                UdpClient udpClient = new UdpClient();
                try
                {
                    // 设置发送和接收数据的IP和端口
                    IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 9);
                    // 发送数据
                    //byte[] data = System.Text.Encoding.ASCII.GetBytes("Hello, UDP Server!");
                    byte[] data = StrToToHexByte(deviceMac);
                    for (int i = 0; i < 16; i++)
                    {
                        udpClient.Send(data, data.Length, iPEndPoint);
                        //Console.WriteLine("数据已发送");
                    }
                    //// 接收数据
                    //IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
                    //byte[] receivedData = udpClient.Receive(ref remoteEP);
                    //string receivedMessage = System.Text.Encoding.ASCII.GetString(receivedData);
                    //Console.WriteLine("接收到的数据:" + receivedMessage);
                }
                catch (Exception e)
                {
                    Console.WriteLine("发生错误:" + e.Message);
                }
                finally
                {
                    // 关闭UdpClient连接
                    udpClient.Close();
                }
            }, TipType.none);
 
        }
 
        /// <summary>
        /// 刷新设备状态
        /// </summary>
        /// <param name="tipType">是否需要提示,默认提示</param>
        /// <returns></returns>
        public bool RefreshDeviceStatus(List<string> functionIds, TipType tipType = TipType.flicker)
        {
            try
            {
                Dictionary<string, object> d = new Dictionary<string, object>();
                d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id);
                d.Add("deviceIds", functionIds);
                var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_RefreshDeviceStatus, "刷新设备状态");
                if (!this.DataChecking(responsePackNew, tipType))
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
 
        /// <summary>
        /// 获取设备详情
        /// </summary>
        /// <param name="tipType">是否需要提示,默认提示</param>
        /// <returns></returns>
        public Function GetDeviceInfo(string functionId, TipType tipType = TipType.flicker)
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id);
            d.Add("deviceIds", new List<string>() { functionId });
 
            var responsePackNew = RequestServerhomeId(d, NewAPI.Api_Post_GetDevcieInfoList, "获取设备详情");
            if (!this.DataChecking(responsePackNew, tipType))
            {
                return null;
            }
            var functionList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Function>>(responsePackNew.Data.ToString());
            if (functionList != null && functionList.Count > 0)
            {
                return functionList[0];
            }
            return null;
 
        }
 
        /// <summary>
        ///删除设备
        /// </summary>
        /// <param name="deviceId">设备id</param>
        /// <param name="action">成功返回true</param>
        /// <param name="tipType">是否需要提示,默认提示</param>
        public void DelDevice(string deviceId, Action<bool> action, TipType tipType = TipType.flicker)
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            d.Add("homeId", DB_ResidenceData.Instance.CurrentRegion.id);
            d.Add("deviceIds", new List<string> { deviceId });//设备id
            var responsePackNew = UI.Music.SendMethod.Current.RequestServerhomeId(d, NewAPI.Api_Post_UnbindDeviceToHome, "删除海信电视");
            if (!this.DataChecking(responsePackNew, tipType))
            {
                //action?.Invoke(false);
                return;
            }
            action?.Invoke(true);
        }
 
        /// <summary>
        /// 发送控制命令
        /// </summary>
        /// <param name="device">当前设备</param>
        /// <param name="dic">发送控制数据</param>
        public void SendControlCommand(Function device, Dictionary<string, string> dic,Action<bool> action) 
        {
            new System.Threading.Thread(() =>
            {
                var result = DriverLayer.Control.Ins.SendWriteCommand(device, dic, false, 0);
                action?.Invoke(result);
 
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 将16进制的字符串转为byte[]
        /// </summary>
        /// <param name="hexDeviceMacString"></param>
        /// <returns></returns>
        private byte[] StrToToHexByte(string hexDeviceMacString)
        {
            if (string.IsNullOrEmpty(hexDeviceMacString))
            {
                return new byte[] { };
            }
            hexDeviceMacString = hexDeviceMacString.Replace(" ", "").Replace("_","");
            if ((hexDeviceMacString.Length % 2) != 0) {
                return new byte[] { };
            }
            byte[] returnBytes = new byte[hexDeviceMacString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexDeviceMacString.Substring(i * 2, 2), 16);
            return returnBytes;
        } 
 
        /// <summary>
        /// 二进制数组转十六进制字符串
        /// </summary>
        /// <param name="btyes"></param>
        /// <returns></returns>
        private string Byte2hex(byte[] btyes) 
        {
            StringBuilder sb = new StringBuilder(btyes.Length * 2);
            for (int i = 0; i < btyes.Length; i++)
            {
                int hight = ((btyes[i] >> 4) & 0x0f);
                int low = btyes[i] & 0x0f;
                sb.Append(hight > 9 ? (char)((hight - 10) + 'a') : (char)(hight + '0'));
                sb.Append(low > 9 ? (char)((low - 10) + 'a') : (char)(low + '0'));
            }
            return sb.ToString();
        }
 
 
 
        /// <summary>
        ///请求服务器(与住宅有关:例如;homeId) 
        /// </summary>
        /// <param name="o">发送数据</param>
        /// <param name="api_Url">请求地址(不是绝对地址)</param>
        /// <param name="tag">标记->描述接口(自定义)</param>
        /// <returns></returns>
        public ResponsePackNew RequestServerhomeId(object o, string api_Url, string tag, int mTimeout = 3)
        {
            Log($"{DateTime.Now}->发送->{tag}", api_Url, o.ToString());
            var requestJson = HttpUtil.GetSignRequestJson(o);
            var r = HttpUtil.RequestHttpsPostFroHome(api_Url, requestJson, mTimeout);
            Log($"{DateTime.Now}->回复->{tag}", "", Newtonsoft.Json.JsonConvert.SerializeObject(r));
            return r;
 
        }
 
 
        /// <summary>
        /// <summary>
        /// 检验数据回复成功或者失败
        /// </summary>
        /// <param name="responsePackNew">回复数据对象</param>
        /// <param name="tipType">是否需要提示</param>
        /// <returns></returns>
        private bool DataChecking(ResponsePackNew responsePackNew, TipType tipType)
        {
            if (responsePackNew.Data == null || responsePackNew.Code != "0" || responsePackNew.Data.ToString() == "")
            {
                if (TipType.flicker == tipType)
                {
                    if (responsePackNew == null || responsePackNew.Code == "-1")
                    {
                        responsePackNew = new ResponsePackNew { message = "没回复,请确认网络是否正常.", Code = "-1", };
                    }
                    Application.RunOnMainThread(() =>
                    {
                        //new Tip()
                        //{
                        //    CloseTime = 1,
                        //    Text = responsePackNew.message + "(" + responsePackNew.Code + ")",
                        //    Direction = AMPopTipDirection.None,
                        //}.Show(MainPage.BasePageView.GetChildren(MainPage.BasePageView.ChildrenCount - 1));
                        CommonMethod.Current.ShowTip(responsePackNew.message + "(" + responsePackNew.Code + ")", 3);
                    });
                }
                return false;
            }
            return true;
        }
        /// <summary>
        /// 打印日志
        /// </summary>
        /// <param name="tag">打印标记</param>
        /// <param name="content">打印内容</param>
        public void Log(string tag, string url, string content)
        {
#if DEBUG
            Console.WriteLine(tag + $"\r\n{url}\r\n{content}");
#endif
        }
    }
}