mac
2023-09-01 0a11352dffeb8d5f31dd40b9728fe1abf4537f04
HDL_ON/UI/UI2/FuntionControlView/HisenseTV /Send.cs
@@ -1,5 +1,8 @@
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;
@@ -26,6 +29,46 @@
                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);
        }
@@ -65,6 +108,46 @@
            { 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)