终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ConfigMgr.cs 4.8 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }