终端一体化运控平台
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.
 
 
 

171 lines
6.4 KiB

  1. using BPA.ApolloClient;
  2. using BPA.Message;
  3. using BPASmartClient.Helper;
  4. using BPASmartClient.Message;
  5. using BPASmartClient.Model;
  6. using Microsoft.Extensions.Configuration;
  7. using Newtonsoft.Json;
  8. using System.Collections.ObjectModel;
  9. using System.Xml.Linq;
  10. using System.Xml.XPath;
  11. namespace BPASmartClient.Business
  12. {
  13. /// <summary>
  14. /// 配置管理器
  15. /// </summary>
  16. public class ConfigMgr : IPlugin
  17. {
  18. /// <summary>
  19. /// 门店ID
  20. /// </summary>
  21. public int ClientId { get; set; }
  22. /// <summary>
  23. /// MQTT Broker
  24. /// </summary>
  25. public MQTT_Entity MQTT_Config { get; set; }
  26. //Apollo地址
  27. private string apolloUri;
  28. private string apoid;
  29. private string namespa;
  30. //设备集合
  31. private List<DeviceConfig> deviceConfigs;
  32. public void Dispose()
  33. {
  34. }
  35. public void Initialize()
  36. {
  37. ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]);
  38. apolloUri = System.Configuration.ConfigurationManager.AppSettings["ApolloUri"].ToString();
  39. apoid = System.Configuration.ConfigurationManager.AppSettings["AppId"].ToString();
  40. namespa = System.Configuration.ConfigurationManager.AppSettings["Namespaces"].ToString();
  41. InternetInfo.OrderServer = System.Configuration.ConfigurationManager.AppSettings["OrderServiceUri"].ToString();
  42. InternetInfo.StockServer = System.Configuration.ConfigurationManager.AppSettings["StockServiceUri"].ToString();
  43. InternetInfo.IsEnableTest = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["IsEnableTest"].ToString());
  44. InitDeviceModel();
  45. InitMQTT();
  46. }
  47. public List<DeviceConfig> GetDeviceConfigs()
  48. {
  49. if (null == deviceConfigs)
  50. InitDeviceModel();
  51. return deviceConfigs;
  52. }
  53. /// <summary>
  54. /// 加载设备模型
  55. /// </summary>
  56. private void InitDeviceModel()
  57. {
  58. //var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig");
  59. //string path = $"{LocaPath.GetInstance().GetDeviceConfigPath}{text}.json";
  60. //deviceConfigs = new List<DeviceConfig>();
  61. //if (File.Exists(path))
  62. //{
  63. // string JsonString = File.ReadAllText(path);
  64. // var result = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
  65. // if (result != null)
  66. // {
  67. // foreach (var shop in result)
  68. // {
  69. // foreach (var device in shop.deviceModels)
  70. // {
  71. // DeviceConfig deviceConfig = new DeviceConfig();
  72. // deviceConfig.Name = device.DeviceName;
  73. // deviceConfig.Module = device.DeviceNamespace;
  74. // deviceConfig.DeviceId = int.Parse(device.DeviceId);
  75. // foreach (var comms in device.communicationDevcies)
  76. // {
  77. // BPASmartClient.Model.Peripheral peripheral = new BPASmartClient.Model.Peripheral();
  78. // peripheral.Module = comms.CommunicationNamespace;
  79. // }
  80. // deviceConfigs.Add(deviceConfig);
  81. // }
  82. // }
  83. // }
  84. //}
  85. //deviceConfigs = new List<DeviceConfig>();
  86. //var devicePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DeviceInfo.xml");
  87. //var xdoc = XDocument.Load(devicePath);
  88. //var devices = xdoc.XPathSelectElements("//Device");
  89. //foreach (var device in devices)
  90. //{
  91. // DeviceConfig deviceConfig = new DeviceConfig();
  92. // deviceConfig.Name = device.Attribute("Name").Value;
  93. // deviceConfig.Module = device.Attribute("Module").Value;
  94. // deviceConfig.DeviceId = int.Parse(device.Attribute("DeviceId").Value);
  95. // var Peripherals = device.XPathSelectElements("Peripherals/Peripheral");
  96. // if (Peripherals != null)
  97. // {
  98. // foreach (var Per in Peripherals)
  99. // {
  100. // BPASmartClient.Model.Peripheral peripheral = new BPASmartClient.Model.Peripheral();
  101. // peripheral.Module = Per.Attribute("Module").Value;
  102. // var Parameters = Per.Element("Parameters").Elements();
  103. // if (Parameters != null)
  104. // {
  105. // foreach (var item in Parameters)
  106. // {
  107. // peripheral.Parameters.Add(item.Name.LocalName, item.Value);
  108. // }
  109. // }
  110. // deviceConfig.Peripherals.Add(peripheral);
  111. // }
  112. // }
  113. // deviceConfigs.Add(deviceConfig);
  114. //}
  115. }
  116. /// <summary>
  117. /// 加载MQTT配置
  118. /// </summary>
  119. private void InitMQTT()
  120. {
  121. IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
  122. //测试版本
  123. //configurationBuilder.AddApolloConfiguration(p =>
  124. // {
  125. // p.AppId = "dev1_order";
  126. // p.MetaServer = apolloUri;
  127. // p.Namespaces = new List<string>() { "DEV.Config" };
  128. // });
  129. //正式版本
  130. configurationBuilder.AddApolloConfiguration(p =>
  131. {
  132. p.AppId = apoid;
  133. p.MetaServer = apolloUri;
  134. p.Namespaces = new List<string>() { namespa };
  135. });
  136. IConfiguration config = configurationBuilder.Build();
  137. var mqttBroker = config.GetSection("BrokerHostSettings");
  138. MQTT_Config = mqttBroker.Value.FromJSON<MQTT_Entity>();
  139. if (MQTT_Config == null) MessageLog.GetInstance.Show("获取配置数据为空!");
  140. }
  141. public void Start()
  142. {
  143. }
  144. public class MQTT_Entity
  145. {
  146. public string Host { get; set; }
  147. public int Port { get; set; }
  148. }
  149. }
  150. }