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.
 
 

92 lines
3.2 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. Json<SetPar>.Read();
  25. if (Enum.TryParse(Json<SetPar>.Data.ClientDeviceType, out DeviceClientType dct))
  26. {
  27. GeneralConfig.DeviceType = dct;
  28. }
  29. else { GeneralConfig.DeviceType = DeviceClientType.MORKS; }
  30. Json<MorkOrderPushPar>.Read();
  31. Json<BatchingInfoPar>.Read();
  32. Json<SimOrderConfig>.Read();
  33. MessageLog.GetInstance.Show($"启动【{GeneralConfig.DeviceType}】设备");
  34. }
  35. public void DataSave()
  36. {
  37. Json<MorkOrderPushPar>.Save();
  38. Json<BatchingInfoPar>.Save();
  39. Json<SetPar>.Save();
  40. Json<SimOrderConfig>.Save();
  41. TextHelper.GetInstance.SaveLogInfo(MessageLog.GetInstance.LogInfo, "LogInfo");
  42. }
  43. public void BusinessInit()
  44. {
  45. ThreadOperate.GetInstance.Start(new Action(() =>
  46. {
  47. InternetInfo.ConfigInit();//从 consul 获取配置数据
  48. Topics.Clear();
  49. Topics.Add(TOPIC.GetInstance.GetOrderPushTopic(GeneralConfig.DeviceType, InternetInfo.ClientId));
  50. Topics.Add(TOPIC.GetInstance.GetBusinessTopic(GeneralConfig.DeviceType, InternetInfo.ClientId));
  51. //MQTT 连接成功
  52. MqttHelper.GetInstance.ConnectOk = new Action(() =>
  53. {
  54. SimpleFactory.GetInstance.DeviceInit();//设备初始化
  55. MqttHelper.GetInstance.MqttSubscriptionAsync(Topics.ToArray());//主题订阅
  56. //IotReport.GetInstance.Init();//IOT 上报
  57. HeartbeatReport.GetInstance.Init();//心跳上报
  58. ServerData.GetInstance.Init();//数据处理初始化
  59. //接收MQTT消息
  60. MqttHelper.GetInstance.MqttReceive = new Action<MQTTnet.MqttApplicationMessageReceivedEventArgs>((receivce) =>
  61. {
  62. ServerData.GetInstance.ReceiveData(Encoding.UTF8.GetString(receivce.ApplicationMessage.Payload));
  63. });
  64. });
  65. //MQTT 重连成功
  66. MqttHelper.GetInstance.Reconnection = new Action(() => { MqttHelper.GetInstance.MqttSubscriptionAsync(Topics.ToArray()); });
  67. //MQTT 连接
  68. MqttHelper.GetInstance.MqttInitAsync(InternetInfo.MqttUserName, InternetInfo.MqttPassword,
  69. InternetInfo.MqttAddress, InternetInfo.MqttPort, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
  70. }), "业务逻辑初始化");
  71. }
  72. }
  73. }