hxb
2020-09-23 2921da3e4ba99f0df28a172d47daa69e375431bc
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
 
using Common;
using Common.MqttNets;
using HDLCloudMonitorSupportCtrlOldUdpGate.Logs;
using HDLCloudMonitorSupportCtrlOldUdpGate.Context;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
 
namespace HDLCloudMonitorSupportCtrlOldUdpGate
{
    public class MockEmqCtrlBusproUdpGateway
    {
        private static int CtrlTimerCount = 10;
 
        public static async Task StartCtrlBusproUdpGateway()
        {
            var option = new MqttClientEngineOption
            {
                ClientId = Config.ClientId,
                MqttDomainOrIp = Config.MqttDomainOrIp,
                MqttPort = 1883,
                UserName = Config.ConnUsername,
                Password = Config.ConnPassword,
            };
            option.SubscribeList.Add(new SubscribeTopicInfo { Topic = $"/BusGateWayToClient/{Config.MacMark}/Common/#" });        
 
            option.UseApplicationMessageReceivedHandler += async (mqttMessage) =>
            {
                await Task.Run(async () =>
                {
                    try
                    {
                        var resStr = Encoding.UTF8.GetString(mqttMessage.ApplicationMessage.Payload);
                        if (resStr.Contains(GlobalRecordContext.RequestGuid))
                        {
                            DateTime tim = DateTime.Now;
                            GlobalRecordContext.CtrlRecordDto.ComebackMilliseconds= (tim - GlobalRecordContext.ReceiveResponseTime).TotalMilliseconds;
 
                           // Console.WriteLine("控制端-Udp网关接收时间:" + GlobalRecordContext.CtrlRecordDto.ComebackMilliseconds+" ms");
 
                            Console.WriteLine($"控制端收到:{ tim.ToString(Consts.TimeFormat1)}");
 
                            // Console.WriteLine(GlobalRecordContext.CtrlRecordDto);
 
                            MqttLogHp.Save();
 
                            GlobalRecordContext.ResetCtrlRecordDto();                          
                        }
                        else  
                        {
                           //Console.WriteLine($"其他:Topic={ mqttMessage.ApplicationMessage.Topic }");
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                });
            };
            
            await MqttClientEngine.ServerMqttEngine(option);
            bool isMenu = true;
            while (true)
            {        
                try
                {
                    if (isMenu)
                    {
                        OutputMenu();
                    }                 
                    String input = Console.ReadLine().ToLower();
                    Console.WriteLine(input);
 
                    switch (input)
                    {
                        case "c":
                            {
                                isMenu = true;
                                Console.Clear();
                            }; break;
                        case "a":
                            {
                                isMenu = true;
                                while (true)
                                {
                                    Console.WriteLine();
                                    await FactoryCtrl();
                                    Thread.Sleep(TimeSpan.FromSeconds(3));
                                }
                            }; break;
                        default:
                            {
                                await FactoryCtrl();
                                isMenu = false;
                            }; break;
                    }
                }
                catch 
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }
 
        private static void OutputMenu()
        {
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("[             c:清屏                   ]");
            Console.WriteLine("[        enter:执行一次控制            ]");
            Console.WriteLine("[        a:自动控制(间隔3s)            ]");
            Console.WriteLine("----------------------------------------");
            Console.WriteLine("请输入命令:");
        }
 
        private static async Task FactoryCtrl()
        {
            GlobalRecordContext.RequestGuid = Guid.NewGuid().ToString();
            GlobalRecordContext.CtrlStartTime = DateTime.Now;
 
            #region 控制端连接服务器获取一端口的信息
 
            var data = new Data();
            data.SubnetID = 0xFB;
            data.DeviceID = 0xFB;
            data.Command = 0x000E;
            data.AddData = Encoding.UTF8.GetBytes(GlobalRecordContext.RequestGuid);
 
 
            Console.WriteLine($"  开始控制:{GlobalRecordContext.CtrlStartTime.ToString(Consts.TimeFormat1)}");
            string pubTopic = $"/ClientToBusGateWay/{Config.MacMark}/Common/MonitorFilterProcessor";
            await MqttClientEngine.PublishTopic(pubTopic, null, data.SendBytes);          
 
            #endregion
        }
    }
}