| | |
| | | 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; |
| | |
| | | } |
| | | |
| | | } |
| | | /// <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> |
| | | ///删除设备 |
| | |
| | | var responsePackNew = UI.Music.SendMethod.Current.RequestServerhomeId(d, NewAPI.Api_Post_UnbindDeviceToHome, "删除海信电视"); |
| | | if (!this.DataChecking(responsePackNew, tipType)) |
| | | { |
| | | action?.Invoke(false); |
| | | //action?.Invoke(false); |
| | | return; |
| | | } |
| | | action?.Invoke(true); |
| | | } |
| | |
| | | /// <summary> |
| | | /// 发送控制命令 |
| | | /// </summary> |
| | | /// <param name="function">当前设备</param> |
| | | /// <param name="device">当前设备</param> |
| | | /// <param name="dic">发送控制数据</param> |
| | | public void SendControlCommand(Function function, Dictionary<string, string> dic,Action<bool> action) |
| | | public void SendControlCommand(Function device, Dictionary<string, string> dic,Action<bool> action) |
| | | { |
| | | new System.Threading.Thread(() => |
| | | { |
| | | var result = DriverLayer.Control.Ins.SendWriteCommand(function, dic, false, 0); |
| | | 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> |
| | |
| | | { |
| | | if (TipType.flicker == tipType) |
| | | { |
| | | if (responsePackNew == null) |
| | | if (responsePackNew == null || responsePackNew.Code == "-1") |
| | | { |
| | | responsePackNew = new ResponsePackNew { message = "没回复,请确认网络是否正常.", Code = "-1", }; |
| | | } |