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

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