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

133 lines
4.7 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. private string apoid;
  26. private string namespa;
  27. //设备集合
  28. private List<DeviceConfig> deviceConfigs;
  29. public void Dispose()
  30. {
  31. }
  32. public void Initialize()
  33. {
  34. ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]);
  35. apolloUri = System.Configuration.ConfigurationManager.AppSettings["ApolloUri"].ToString();
  36. apoid = System.Configuration.ConfigurationManager.AppSettings["AppId"].ToString();
  37. namespa = System.Configuration.ConfigurationManager.AppSettings["Namespaces"].ToString();
  38. InternetInfo.OrderServer = System.Configuration.ConfigurationManager.AppSettings["OrderServiceUri"].ToString();
  39. InternetInfo.StockServer = System.Configuration.ConfigurationManager.AppSettings["StockServiceUri"].ToString();
  40. InitDeviceModel();
  41. InitMQTT();
  42. }
  43. public List<DeviceConfig> GetDeviceConfigs()
  44. {
  45. if (null == deviceConfigs)
  46. InitDeviceModel();
  47. return deviceConfigs;
  48. }
  49. /// <summary>
  50. /// 加载设备模型
  51. /// </summary>
  52. private void InitDeviceModel()
  53. {
  54. deviceConfigs = new List<DeviceConfig>();
  55. var devicePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DeviceInfo.xml");
  56. var xdoc = XDocument.Load(devicePath);
  57. var devices = xdoc.XPathSelectElements("//Device");
  58. foreach (var device in devices)
  59. {
  60. DeviceConfig deviceConfig = new DeviceConfig();
  61. deviceConfig.Name = device.Attribute("Name").Value;
  62. deviceConfig.Module = device.Attribute("Module").Value;
  63. deviceConfig.DeviceId = int.Parse(device.Attribute("DeviceId").Value);
  64. var Peripherals = device.XPathSelectElements("Peripherals/Peripheral");
  65. if (Peripherals != null)
  66. {
  67. foreach (var Per in Peripherals)
  68. {
  69. BPASmartClient.Model.Peripheral peripheral = new BPASmartClient.Model.Peripheral();
  70. peripheral.Module = Per.Attribute("Module").Value;
  71. var Parameters = Per.Element("Parameters").Elements();
  72. if (Parameters != null)
  73. {
  74. foreach (var item in Parameters)
  75. {
  76. peripheral.Parameters.Add(item.Name.LocalName, item.Value);
  77. }
  78. }
  79. deviceConfig.Peripherals.Add(peripheral);
  80. }
  81. }
  82. deviceConfigs.Add(deviceConfig);
  83. }
  84. }
  85. /// <summary>
  86. /// 加载MQTT配置
  87. /// </summary>
  88. private void InitMQTT()
  89. {
  90. IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
  91. //测试版本
  92. //configurationBuilder.AddApolloConfiguration(p =>
  93. // {
  94. // p.AppId = "dev1_order";
  95. // p.MetaServer = apolloUri;
  96. // p.Namespaces = new List<string>() { "DEV.Config" };
  97. // });
  98. //正式版本
  99. configurationBuilder.AddApolloConfiguration(p =>
  100. {
  101. p.AppId = apoid;
  102. p.MetaServer = apolloUri;
  103. p.Namespaces = new List<string>() { namespa };
  104. });
  105. IConfiguration config = configurationBuilder.Build();
  106. var mqttBroker = config.GetSection("BrokerHostSettings");
  107. MQTT_Config = mqttBroker.Value.FromJSON<MQTT_Entity>();
  108. if (MQTT_Config == null) MessageLog.GetInstance.Show("获取配置数据为空!");
  109. }
  110. public void Start()
  111. {
  112. }
  113. public class MQTT_Entity
  114. {
  115. public string Host { get; set; }
  116. public int Port { get; set; }
  117. }
  118. }
  119. }