wei
2020-12-24 d1af1b8337591363e60c286348501a841138dbb3
HDL_ON/DAL/DriverLayer/Control_Udp.cs
@@ -29,6 +29,7 @@
        /// 所有对一端口的控制都会放到这个集合里
        /// </summary>
        static List<Control_Udp> controlList = new List<Control_Udp>(50);
        public System.Net.IPEndPoint EndPoint
        {
@@ -65,7 +66,7 @@
        /// <summary>
        /// 重发验证
        /// </summary>
        public void ReceiveRepeatManager(string receiveFlag)
        public static void ReceiveRepeatManager(string receiveFlag,byte []usefulBytes)
        {
            for (int i = 0; i < controlList.Count; i++)
            {
@@ -157,6 +158,10 @@
        {
            foreach (var f in scene.functions)
            {
                if(f.localFunction == null)
                {
                    continue;
                }
                switch (f.localFunction.functionType)
                {
                    case FunctionType.Curtain:
@@ -248,7 +253,7 @@
                            }
                        }
                        ControlBytesSend(Command.SetSingleLight, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID,
                            new byte[] { f.localFunction.bus.LoopId, brightness });
                            new byte[] { f.localFunction.bus.LoopId, brightness, (byte)(f.localFunction.delay / 255), (byte)(f.localFunction.delay % 255) });
                        break;
                    case FunctionType.RGB:
                        break;
@@ -623,7 +628,7 @@
                {
                    for (int i = 0; i < controlList.Count;)
                    {
                        if (controlList[i] == null || 3 <= controlList[i].Packet.HaveSendCount)
                        if (controlList[i] == null || 3 <= controlList[i].packet.HaveSendCount)
                        {
                            controlList.RemoveAt(i);
                        }
@@ -692,7 +697,7 @@
        void run()
        {
            allDone.Set();
            Packet.HaveSendCount = 4;
            packet.HaveSendCount = 4;
        }
        //数据发送处理
@@ -706,7 +711,7 @@
                new System.Threading.Thread(() =>
                {
                    System.Threading.Thread.Sleep(1000);
                    UdpSocket._BusSocket.Start(6000);
                    UdpSocket._BusSocket.Start(UdpSocket._BusSocket.Port);
                    controlLostCount = 0;
                })
                { IsBackground = true }.Start();
@@ -714,20 +719,20 @@
            try
            {
                MainPage.Log("发送数据:" + SendFlag);
                UdpSocket._BusSocket.AsyncBeginSend(Packet);
                Packet.HaveSendCount--;
                //MainPage.Log("发送数据:" + SendFlag);
                UdpSocket._BusSocket.AsyncBeginSend(packet);
                packet.HaveSendCount--;
                //这里是重发两次
                while (Packet.HaveSendCount < 3)
                while (packet.HaveSendCount < 3)
                {
                    if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks)
                    if (packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks)
                    {
                        MainPage.Log("重发数据:" + SendFlag);
                        UdpSocket._BusSocket.AsyncBeginSend(Packet);
                        UdpSocket._BusSocket.AsyncBeginSend(packet);
                        controlLostCount++;
                    }
                    System.Threading.Thread.Sleep(100);
                    System.Threading.Thread.Sleep(300);
                }
            }
            catch (Exception ex)
@@ -743,7 +748,7 @@
        /// <summary>
        /// 当前数据包
        /// </summary>
        Packet Packet;
        Packet packet;
        /// <summary>
        /// 记录发送数据包
        /// </summary>
@@ -800,14 +805,14 @@
        {
            try
            {
                Packet = new Packet(target.SendBytes, target.IPEndPoint);
                Packet.HaveSendCount = 3 - sendCount;
                packet = new Packet(target.SendBytes, target.IPEndPoint);
                packet.HaveSendCount = 3 - sendCount;
                signPacket(target);
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
                thread.IsBackground = true;
                thread.Start(Packet);
                thread.Start(packet);
                if (isWait)
                {
@@ -832,11 +837,11 @@
                
                var sendBytes = Control.Ins.ConvertSendBodyData(CommunicationTopic.SearchLoaclGateway, bodyString);
                //组播发送
                Packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585));
                Packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断
                packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585));
                packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断
                System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
                thread.IsBackground = true;
                thread.Start(Packet);
                thread.Start(packet);
                //wait();不需要等待
            }
            catch (Exception ex)
@@ -844,24 +849,24 @@
                MainPage.Log($"Send bus data error {ex.Message}");
            }
        }
        /// <summary>
        /// 发送udp A协议数据
        /// </summary>
        public void SendLocalHdlLinkData(byte[] sendBytes)
        public void SendLocalHdlLinkData(byte[] sendBytes,string id, bool isWait=false)
        {
            if (controlLostCount > 3)
            {
                UdpSocket._BusSocket.Stop();
                UdpSocket._BusSocket.Start(8585);
                controlLostCount = 0;
            }
            Packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Control.Ins.reportIp), 8585));
            Packet.HaveSendCount = 2;//ps:没有重发,后期有时间可以增加重发判断
            packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Control.Ins.reportIp), 8585));
            sendFlag = id;
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
            thread.IsBackground = true;
            thread.Start(Packet);
            controlLostCount++;
            thread.Start(packet);
            if (isWait)
            {
                wait();
            }
            MainPage.Log($"发送Hdl-Link数据,IP:{Control.Ins.reportIp}:8585");
        }
    }
}