终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

ConfigMgr.cs 3.7 KiB

2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. InternetInfo.OrderServer= System.Configuration.ConfigurationManager.AppSettings["OrderServiceUri"].ToString();
  34. InternetInfo.StockServer= System.Configuration.ConfigurationManager.AppSettings["StockServiceUri"].ToString();
  35. InitDeviceModel();
  36. InitMQTT();
  37. }
  38. public List<DeviceConfig> GetDeviceConfigs()
  39. {
  40. if (null == deviceConfigs)
  41. InitDeviceModel();
  42. return deviceConfigs;
  43. }
  44. /// <summary>
  45. /// 加载设备模型
  46. /// </summary>
  47. private void InitDeviceModel()
  48. {
  49. deviceConfigs = new List<DeviceConfig>();
  50. var devicePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DeviceInfo.xml");
  51. var xdoc = XDocument.Load(devicePath);
  52. var devices = xdoc.XPathSelectElements("//Device");
  53. foreach (var device in devices)
  54. {
  55. DeviceConfig deviceConfig = new DeviceConfig();
  56. deviceConfig.Name = device.Attribute("Name").Value;
  57. deviceConfig.Module = device.Attribute("Module").Value;
  58. deviceConfig.DeviceId =int.Parse(device.Attribute("DeviceId").Value);
  59. foreach (var peripheralEl in device.XPathSelectElements("//Peripheral"))
  60. {
  61. BPASmartClient.Model.Peripheral peripheral = new BPASmartClient.Model.Peripheral();
  62. peripheral.Module = peripheralEl.Attribute("Module").Value;
  63. foreach (XElement parameter in peripheralEl.Element("Parameters").Elements())
  64. {
  65. peripheral.Parameters.Add(parameter.Name.LocalName, parameter.Value);
  66. }
  67. deviceConfig.Peripherals.Add(peripheral);
  68. }
  69. deviceConfigs.Add(deviceConfig);
  70. }
  71. }
  72. /// <summary>
  73. /// 加载MQTT配置
  74. /// </summary>
  75. private void InitMQTT()
  76. {
  77. IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
  78. configurationBuilder.AddApolloConfiguration(p =>
  79. {
  80. p.AppId = "dev1_order";
  81. p.MetaServer = apolloUri;
  82. p.Namespaces = new List<string>() { "DEV.Config" };
  83. });
  84. IConfiguration config = configurationBuilder.Build();
  85. var mqttBroker = config.GetSection("BrokerHostSettings");
  86. MQTT_Config = mqttBroker.Value.FromJSON<MQTT_Entity>();
  87. }
  88. public class MQTT_Entity
  89. {
  90. public string Host { get; set; }
  91. public int Port { get; set; }
  92. }
  93. }
  94. }