RegistryService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using AmrControl.Common.HttpClients;
  2. using AmrControl.JGR;
  3. using Newtonsoft.Json;
  4. using System.Net.NetworkInformation;
  5. namespace AmrControl.services
  6. {
  7. public class RegistryService : IRegistryService
  8. {
  9. private readonly IConfiguration _configuration;
  10. /// <summary>
  11. /// 运行状态
  12. /// </summary>
  13. private volatile bool _running;
  14. /// <summary>
  15. /// 服务地址
  16. /// </summary>
  17. private string? serviceAddr;
  18. public RegistryService(IConfiguration configuration) {
  19. this._configuration = configuration;
  20. }
  21. public void Start()
  22. {
  23. if(!_running)
  24. {
  25. _running = true;
  26. readAddr();
  27. //发送心跳及同步仓储信息
  28. if (!string.IsNullOrEmpty(_configuration["props:serverAddr"]) && !string.IsNullOrWhiteSpace(_configuration["props:serverAddr"]))
  29. {
  30. //启动异步任务构建心跳处理
  31. startHeartbeatAsync();
  32. try
  33. {
  34. //构建仓储结构
  35. SyncWarehouse();
  36. }
  37. catch (Exception ex)
  38. {
  39. Console.WriteLine($"仓储同步失败,失败原因:{ex.Message}");
  40. }
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// 同步仓储信息
  46. /// </summary>
  47. /// <exception cref="Exception"></exception>
  48. private void SyncWarehouse()
  49. {
  50. var storageGridCells = JGRManager.GetInstance().getStorageGridCells();
  51. List<Dictionary<string, string>> locations = new();
  52. foreach (var v in storageGridCells.Values)
  53. {
  54. locations.Add(new() {
  55. { "coordinate", v.positionX + "," + v.positionY},
  56. { "enable", v.enableUse?"1":"0"}});
  57. }
  58. //获取当前产线编号
  59. string lineNo = _configuration["props:lineNo"];
  60. string serverUri = _configuration["props:serverAddr"];
  61. //发送消息到服务端进行数据同步
  62. Dictionary<string, object> data = new()
  63. {
  64. { "lineNo", lineNo},
  65. { "posList", locations}
  66. };
  67. HttpRespResult rs = DefaultHttpClient.getClient(serverUri).httpPostClient().sendRequest("/wcs/dispather-service/sync-house-location", data);
  68. if (rs.code != 200)
  69. {
  70. Console.WriteLine("仓储同步失败");
  71. throw new Exception("仓储信息同步失败,服务未启动或者服务不可用");
  72. }
  73. }
  74. /// <summary>
  75. /// 读取服务地址信息
  76. /// </summary>
  77. private void readAddr()
  78. {
  79. // 获取 IP 地址和端口号
  80. //var address = services.GetService<IWebHostEnvironment>().IsDevelopment()
  81. // ? "localhost" : services.GetService<IWebHostEnvironment>().ApplicationName;
  82. //var port = services.GetService<IWebHostEnvironment>().WebRootPath;
  83. var interfaces = NetworkInterface.GetAllNetworkInterfaces()
  84. .Where(x => x.OperationalStatus == OperationalStatus.Up && x.NetworkInterfaceType != NetworkInterfaceType.Loopback);
  85. var host = "127.0.0.1";
  86. foreach (var networkInterface in interfaces)
  87. {
  88. var properties = networkInterface.GetIPProperties();
  89. var ipv4Addresses = properties.UnicastAddresses
  90. .Where(x => x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
  91. .Select(x => x.Address);
  92. if (ipv4Addresses != null)
  93. {
  94. foreach (var ip in ipv4Addresses)
  95. {
  96. if (ip.ToString().Split('.')[2] == "1")
  97. {
  98. host = ip.ToString();
  99. }
  100. }
  101. }
  102. }
  103. string port = "5089";
  104. var addr = _configuration["urls"];
  105. if (!string.IsNullOrEmpty(addr) && !string.IsNullOrWhiteSpace(addr))
  106. {
  107. foreach (var address in addr.Split(';'))
  108. {
  109. if (address.StartsWith("http://"))
  110. {
  111. port = address.Substring(address.LastIndexOf(":") + 1);
  112. }
  113. }
  114. }
  115. this.serviceAddr = "http://" + host + ":" + port;
  116. Console.WriteLine($"服务地址:{serviceAddr}");
  117. }
  118. /// <summary>
  119. /// 构建1分钟1次的心跳处理
  120. /// </summary>
  121. async Task startHeartbeatAsync()
  122. {
  123. while (_running)
  124. {
  125. try
  126. {
  127. sendHeartBeart();
  128. }
  129. catch (Exception ex)
  130. {
  131. Console.WriteLine(ex.ToString());
  132. }
  133. await Task.Delay(TimeSpan.FromSeconds(60)); // 间隔60秒
  134. }
  135. }
  136. /// <summary>
  137. /// 心跳发送接口
  138. /// </summary>
  139. void sendHeartBeart()
  140. {
  141. string serverUri = _configuration["props:serverAddr"];
  142. if (!string.IsNullOrEmpty(serverUri) && !string.IsNullOrWhiteSpace(serverUri))
  143. {
  144. Dictionary<string, string> data = new()
  145. {
  146. { "lineNo", _configuration["props:lineNo"] },
  147. { "addr", serviceAddr}
  148. };
  149. HttpRespResult rs = DefaultHttpClient.getClient(serverUri).httpPostClient().sendRequest("/wcs/dispather-service", data);
  150. if (rs.code != 200)
  151. {
  152. Console.WriteLine("心跳请求失败,与服务器的连接断开");
  153. } else
  154. {
  155. Dictionary<string, string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>(rs.data);
  156. if (!"200".Equals(result["code"]))
  157. {
  158. Console.WriteLine("心跳请求失败,与服务器的连接断开");
  159. }
  160. }
  161. }
  162. }
  163. public void Stop()
  164. {
  165. //修改状态
  166. _running = false;
  167. }
  168. }
  169. }