You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

95 lines
3.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using HBLConsole.Model;
  7. using HBLConsole.Service;
  8. using HBLConsole.GVL;
  9. using HBLConsole.Communication;
  10. using BPA.Message;
  11. using HBLConsole.Business;
  12. using HBLConsole.Factory;
  13. using BPA.Message.Enum;
  14. namespace HBLConsole.MainConsole
  15. {
  16. public class Main
  17. {
  18. private volatile static Main _Instance;
  19. public static Main GetInstance => _Instance ?? (_Instance = new Main());
  20. private Main() { }
  21. List<string> Topics = new List<string>();
  22. public void DataInit()
  23. {
  24. ThreadOperate.GetInstance.Start(new Action(() => { Sqlite<Alarm>.GetInstance.GetData(); }), "GetAlarm");
  25. Json<SetPar>.Read();
  26. if (Enum.TryParse(Json<SetPar>.Data.ClientDeviceType, out DeviceClientType dct))
  27. {
  28. GeneralConfig.DeviceType = dct;
  29. }
  30. else { GeneralConfig.DeviceType = DeviceClientType.MORKS; }
  31. Json<MorkOrderPushPar>.Read();
  32. Json<BatchingInfoPar>.Read();
  33. Json<SimOrderConfig>.Read();
  34. Json<KeepDataBase>.Read();
  35. MessageLog.GetInstance.Show($"启动【{GeneralConfig.DeviceType}】设备");
  36. }
  37. public void DataSave()
  38. {
  39. Json<MorkOrderPushPar>.Save();
  40. Json<BatchingInfoPar>.Save();
  41. Json<SetPar>.Save();
  42. Json<SimOrderConfig>.Save();
  43. Json<KeepDataBase>.Save();
  44. Sqlite<Alarm>.GetInstance.Save();
  45. TextHelper.GetInstance.SaveLogInfo(MessageLog.GetInstance.LogInfo, "LogInfo");
  46. }
  47. public void BusinessInit()
  48. {
  49. ThreadOperate.GetInstance.Start(new Action(() =>
  50. {
  51. InternetInfo.ConfigInit();//从 consul 获取配置数据
  52. Topics.Clear();
  53. Topics.Add(TOPIC.GetInstance.GetOrderPushTopic(GeneralConfig.DeviceType, InternetInfo.ClientId));
  54. Topics.Add(TOPIC.GetInstance.GetBusinessTopic(GeneralConfig.DeviceType, InternetInfo.ClientId));
  55. //MQTT 连接成功
  56. MqttHelper.GetInstance.ConnectOk = new Action(() =>
  57. {
  58. SimpleFactory.GetInstance.DeviceInit();//设备初始化
  59. MqttHelper.GetInstance.MqttSubscriptionAsync(Topics.ToArray());//主题订阅
  60. IotReport.GetInstance.Init();//IOT 上报
  61. HeartbeatReport.GetInstance.Init();//心跳上报
  62. ServerData.GetInstance.Init();//数据处理初始化
  63. //接收MQTT消息
  64. MqttHelper.GetInstance.MqttReceive = new Action<MQTTnet.MqttApplicationMessageReceivedEventArgs>((receivce) =>
  65. {
  66. ServerData.GetInstance.ReceiveData(Encoding.UTF8.GetString(receivce.ApplicationMessage.Payload));
  67. });
  68. });
  69. //MQTT 重连成功
  70. MqttHelper.GetInstance.Reconnection = new Action(() => { MqttHelper.GetInstance.MqttSubscriptionAsync(Topics.ToArray()); });
  71. //MQTT 连接
  72. MqttHelper.GetInstance.MqttInitAsync(InternetInfo.MqttUserName, InternetInfo.MqttPassword,
  73. InternetInfo.MqttAddress, InternetInfo.MqttPort, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
  74. }), "业务逻辑初始化");
  75. }
  76. }
  77. }