123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using AmrControl.Common.HttpClients;
- using AmrControl.JGR;
- using Newtonsoft.Json;
- using System.Net.NetworkInformation;
- namespace AmrControl.services
- {
- public class RegistryService : IRegistryService
- {
- private readonly IConfiguration _configuration;
- /// <summary>
- /// 运行状态
- /// </summary>
- private volatile bool _running;
- /// <summary>
- /// 服务地址
- /// </summary>
- private string? serviceAddr;
- public RegistryService(IConfiguration configuration) {
- this._configuration = configuration;
- }
- public void Start()
- {
- if(!_running)
- {
- _running = true;
- readAddr();
- //发送心跳及同步仓储信息
- if (!string.IsNullOrEmpty(_configuration["props:serverAddr"]) && !string.IsNullOrWhiteSpace(_configuration["props:serverAddr"]))
- {
- //启动异步任务构建心跳处理
- startHeartbeatAsync();
- try
- {
- //构建仓储结构
- SyncWarehouse();
- }
- catch (Exception ex)
- {
- Console.WriteLine($"仓储同步失败,失败原因:{ex.Message}");
- }
- }
- }
- }
- /// <summary>
- /// 同步仓储信息
- /// </summary>
- /// <exception cref="Exception"></exception>
- private void SyncWarehouse()
- {
- var storageGridCells = JGRManager.GetInstance().getStorageGridCells();
- List<Dictionary<string, string>> locations = new();
- foreach (var v in storageGridCells.Values)
- {
- locations.Add(new() {
- { "coordinate", v.positionX + "," + v.positionY},
- { "enable", v.enableUse?"1":"0"}});
- }
- //获取当前产线编号
- string lineNo = _configuration["props:lineNo"];
- string serverUri = _configuration["props:serverAddr"];
- //发送消息到服务端进行数据同步
- Dictionary<string, object> data = new()
- {
- { "lineNo", lineNo},
- { "posList", locations}
- };
- HttpRespResult rs = DefaultHttpClient.getClient(serverUri).httpPostClient().sendRequest("/wcs/dispather-service/sync-house-location", data);
- if (rs.code != 200)
- {
- Console.WriteLine("仓储同步失败");
- throw new Exception("仓储信息同步失败,服务未启动或者服务不可用");
- }
- }
- /// <summary>
- /// 读取服务地址信息
- /// </summary>
- private void readAddr()
- {
- // 获取 IP 地址和端口号
- //var address = services.GetService<IWebHostEnvironment>().IsDevelopment()
- // ? "localhost" : services.GetService<IWebHostEnvironment>().ApplicationName;
- //var port = services.GetService<IWebHostEnvironment>().WebRootPath;
- var interfaces = NetworkInterface.GetAllNetworkInterfaces()
- .Where(x => x.OperationalStatus == OperationalStatus.Up && x.NetworkInterfaceType != NetworkInterfaceType.Loopback);
- var host = "127.0.0.1";
- foreach (var networkInterface in interfaces)
- {
- var properties = networkInterface.GetIPProperties();
- var ipv4Addresses = properties.UnicastAddresses
- .Where(x => x.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
- .Select(x => x.Address);
- if (ipv4Addresses != null)
- {
- foreach (var ip in ipv4Addresses)
- {
- if (ip.ToString().Split('.')[2] == "1")
- {
- host = ip.ToString();
- }
- }
- }
- }
- string port = "5089";
- var addr = _configuration["urls"];
- if (!string.IsNullOrEmpty(addr) && !string.IsNullOrWhiteSpace(addr))
- {
- foreach (var address in addr.Split(';'))
- {
- if (address.StartsWith("http://"))
- {
- port = address.Substring(address.LastIndexOf(":") + 1);
- }
- }
- }
- this.serviceAddr = "http://" + host + ":" + port;
- Console.WriteLine($"服务地址:{serviceAddr}");
- }
- /// <summary>
- /// 构建1分钟1次的心跳处理
- /// </summary>
- async Task startHeartbeatAsync()
- {
- while (_running)
- {
- try
- {
- sendHeartBeart();
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.ToString());
- }
- await Task.Delay(TimeSpan.FromSeconds(60)); // 间隔60秒
- }
- }
- /// <summary>
- /// 心跳发送接口
- /// </summary>
- void sendHeartBeart()
- {
- string serverUri = _configuration["props:serverAddr"];
- if (!string.IsNullOrEmpty(serverUri) && !string.IsNullOrWhiteSpace(serverUri))
- {
- Dictionary<string, string> data = new()
- {
- { "lineNo", _configuration["props:lineNo"] },
- { "addr", serviceAddr}
- };
- HttpRespResult rs = DefaultHttpClient.getClient(serverUri).httpPostClient().sendRequest("/wcs/dispather-service", data);
- if (rs.code != 200)
- {
- Console.WriteLine("心跳请求失败,与服务器的连接断开");
- } else
- {
- Dictionary<string, string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>(rs.data);
- if (!"200".Equals(result["code"]))
- {
- Console.WriteLine("心跳请求失败,与服务器的连接断开");
- }
- }
- }
- }
- public void Stop()
- {
- //修改状态
- _running = false;
- }
- }
- }
|