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.
 
 

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