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

  1. using BPA.ApolloClient;
  2. using BPA.Helper;
  3. using BPASmartClient.Model;
  4. using Microsoft.Extensions.Configuration;
  5. using Newtonsoft.Json;
  6. using System.Collections.ObjectModel;
  7. using System.Xml.Linq;
  8. using System.Xml.XPath;
  9. namespace BPASmartClient.Business
  10. {
  11. /// <summary>
  12. /// 配置管理器
  13. /// </summary>
  14. public class ConfigMgr : IPlugin
  15. {
  16. /// <summary>
  17. /// 门店ID
  18. /// </summary>
  19. public int ClientId { get; set; }
  20. /// <summary>
  21. /// MQTT Broker
  22. /// </summary>
  23. public MQTT_Entity MQTT_Config { get; set; }
  24. public MQTT_Account Mqtt_Account { get; set; }
  25. public UriConfig uriConfig { get; set; }
  26. public ObservableCollection<DeviceConfigModelJson> deviceConfigModelJsons { get; set; }
  27. //Apollo地址
  28. private string apolloUri;
  29. private string apoid;
  30. private string namespa;
  31. //设备集合
  32. private List<DeviceConfig> deviceConfigs;
  33. public void Dispose()
  34. {
  35. }
  36. public void Initialize()
  37. {
  38. var text = TextHelper.GetInstance.ReadTextInfo("StartShop", "DeviceConfig");
  39. string path = $"{LocaPath.GetDeviceConfigPath}{text}.json";
  40. if (File.Exists(path))
  41. {
  42. string JsonString = File.ReadAllText(path);
  43. deviceConfigModelJsons = JsonConvert.DeserializeObject<ObservableCollection<DeviceConfigModelJson>>(JsonString);
  44. }
  45. else deviceConfigModelJsons = new ObservableCollection<DeviceConfigModelJson>();
  46. if (deviceConfigModelJsons?.Count > 0)
  47. {
  48. ClientId = int.Parse(deviceConfigModelJsons.ElementAt(0).ShopId);
  49. }
  50. //ClientId = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ClientId"]);
  51. apolloUri = System.Configuration.ConfigurationManager.AppSettings["ApolloUri"].ToString();
  52. apoid = System.Configuration.ConfigurationManager.AppSettings["AppId"].ToString();
  53. namespa = System.Configuration.ConfigurationManager.AppSettings["Namespaces"].ToString();
  54. InternetInfo.IsEnableTest = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["IsEnableTest"].ToString());
  55. InitDeviceModel();
  56. InitConfig();
  57. }
  58. public List<DeviceConfig> GetDeviceConfigs()
  59. {
  60. if (null == deviceConfigs)
  61. InitDeviceModel();
  62. return deviceConfigs;
  63. }
  64. /// <summary>
  65. /// 加载设备模型
  66. /// </summary>
  67. private void InitDeviceModel()
  68. {
  69. }
  70. public void InitConfig()
  71. {
  72. IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory());
  73. configurationBuilder.AddApolloConfiguration(p =>
  74. {
  75. p.AppId = apoid;
  76. p.MetaServer = apolloUri;
  77. p.Namespaces = new List<string>() { "application", namespa };
  78. });
  79. IConfiguration config = configurationBuilder.Build();
  80. MQTT_Config = config.GetSection("BrokerHostSettings").Value.FromJSON<MQTT_Entity>();
  81. Mqtt_Account = config.GetSection("MqttAccount").Value.FromJSON<MQTT_Account>();
  82. uriConfig = config.GetSection("URL").Value.FromJSON<UriConfig>();
  83. if (uriConfig != null)
  84. {
  85. InternetInfo.OrderServer = uriConfig.OrderServiceUrl;
  86. InternetInfo.StockServer = uriConfig.StockServiceUrl;
  87. InternetInfo.BroadcastPubTopic = uriConfig.BroadcastPubTopic;
  88. InternetInfo.DataVApiAddress = uriConfig.DataVServiceUrl;
  89. InternetInfo.WebServiceUri = uriConfig.WebServiceUrl;
  90. ActionManage.GetInstance.Send("配置初始化完成Iot启动");
  91. }
  92. }
  93. public void Start()
  94. {
  95. }
  96. public class MQTT_Entity
  97. {
  98. public string Host { get; set; }
  99. public int Port { get; set; }
  100. }
  101. public class MQTT_Account
  102. {
  103. public string UserName { get; set; }
  104. public string Password { get; set; }
  105. }
  106. public class UriConfig
  107. {
  108. public string OrderServiceUrl { get; set; }
  109. public string StockServiceUrl { get; set; }
  110. public string DataVServiceUrl { get; set; }
  111. public string WebServiceUrl { get; set; }
  112. public string BroadcastPubTopic { get; set; }
  113. }
  114. }
  115. }