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

102 lines
3.4 KiB

  1. using BPA.ApolloClient;
  2. using BPA.Message;
  3. using BPASmartClient.Model;
  4. using Microsoft.Extensions.Configuration;
  5. using System.Xml.Linq;
  6. using System.Xml.XPath;
  7. namespace BPASmartClient.Business
  8. {
  9. /// <summary>
  10. /// 配置管理器
  11. /// </summary>
  12. public class ConfigMgr : IPlugin
  13. {
  14. /// <summary>
  15. /// 门店ID
  16. /// </summary>
  17. public int ClientId { get; set; }
  18. /// <summary>
  19. /// MQTT Broker
  20. /// </summary>
  21. public MQTT_Entity MQTT_Config { get; set; }
  22. //Apollo地址
  23. private string apolloUri;
  24. //设备集合
  25. private List<DeviceConfig> deviceConfigs;
  26. public void Dispose()
  27. {
  28. }
  29. public void Initialize()
  30. {
  31. ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]);
  32. apolloUri = System.Configuration.ConfigurationManager.AppSettings["ApolloUri"].ToString();
  33. InitDeviceModel();
  34. InitMQTT();
  35. }
  36. public List<DeviceConfig> GetDeviceConfigs()
  37. {
  38. if (null == deviceConfigs)
  39. InitDeviceModel();
  40. return deviceConfigs;
  41. }
  42. /// <summary>
  43. /// 加载设备模型
  44. /// </summary>
  45. private void InitDeviceModel()
  46. {
  47. deviceConfigs = new List<DeviceConfig>();
  48. var devicePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DeviceInfo.xml");
  49. var xdoc = XDocument.Load(devicePath);
  50. var devices = xdoc.XPathSelectElements("//Device");
  51. foreach (var device in devices)
  52. {
  53. DeviceConfig deviceConfig = new DeviceConfig();
  54. deviceConfig.Name = device.Attribute("Name").Value;
  55. deviceConfig.Module = device.Attribute("Module").Value;
  56. deviceConfig.DeviceId =int.Parse(device.Attribute("DeviceId").Value);
  57. foreach (var peripheralEl in device.XPathSelectElements("//Peripheral"))
  58. {
  59. BPASmartClient.Model.Peripheral peripheral = new BPASmartClient.Model.Peripheral();
  60. peripheral.Module = peripheralEl.Attribute("Module").Value;
  61. foreach (XElement parameter in peripheralEl.Element("Parameters").Elements())
  62. {
  63. peripheral.Parameters.Add(parameter.Name.LocalName, parameter.Value);
  64. }
  65. deviceConfig.Peripherals.Add(peripheral);
  66. }
  67. deviceConfigs.Add(deviceConfig);
  68. }
  69. }
  70. /// <summary>
  71. /// 加载MQTT配置
  72. /// </summary>
  73. private void InitMQTT()
  74. {
  75. IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
  76. configurationBuilder.AddApolloConfiguration(p =>
  77. {
  78. p.AppId = "dev1_order";
  79. p.MetaServer = apolloUri;
  80. p.Namespaces = new List<string>() { "DEV.Config" };
  81. });
  82. IConfiguration config = configurationBuilder.Build();
  83. var mqttBroker = config.GetSection("BrokerHostSettings");
  84. MQTT_Config = mqttBroker.Value.FromJSON<MQTT_Entity>();
  85. }
  86. public class MQTT_Entity
  87. {
  88. public string Host { get; set; }
  89. public int Port { get; set; }
  90. }
  91. }
  92. }