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.
 
 

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