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

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