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

183 lines
6.8 KiB

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