windows服务,配置程序随系统启动
高胜
2023-03-27 cf310edb65a9e124b2d0da24077fcc9ffbcde8cd
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestartService.Alarm;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace RestartService.Monitor
{
    public class EmqxClientMonitor
    {
        private static int DeviceCacheExCount = 0;
 
        public static void monitor()
        {
            new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    try
                    {
                        var result = HttpHp.Get<ResponseData<List<JObject>>>(ConfigurationManager.AppSettings["EmqClientUrl"]);
 
                        if (result.code != 0)
                        {
                            throw new Exception("EMQX服务响应异常");
                        }
 
                        if (result.data.Count == 0)
                        {
                            throw new Exception("MQTT重连失败,服务不可用!");
                        }
                        DeviceCacheExCount = 0;
                    }
                    catch (Exception exc)
                    {
                        DeviceCacheExCount++;
                        if (DeviceCacheExCount > 2 && DeviceCacheExCount % 3 == 0)
                        {
                            try
                            {
                                AlarmHp.ApplicationAlarm("SupportCtrlOldUdpGate", "warn-system", exc.StackTrace, "157.175.231.123", "国外-透传服务" + exc.Message);
                            }
                            catch (Exception ex)
                            {
                                LogHp.WriteLog("透传服务异常", JsonConvert.SerializeObject(ex));
                            }
 
                        }
                    }
                    Thread.Sleep(1000 * 10);
                }
            }))
            { IsBackground = true }.Start();
            
 
        }
    }
}