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; /// /// 运行状态 /// private volatile bool _running; /// /// 服务地址 /// 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}"); } } } } /// /// 同步仓储信息 /// /// private void SyncWarehouse() { var storageGridCells = JGRManager.GetInstance().getStorageGridCells(); List> 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 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("仓储信息同步失败,服务未启动或者服务不可用"); } } /// /// 读取服务地址信息 /// private void readAddr() { // 获取 IP 地址和端口号 //var address = services.GetService().IsDevelopment() // ? "localhost" : services.GetService().ApplicationName; //var port = services.GetService().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}"); } /// /// 构建1分钟1次的心跳处理 /// async Task startHeartbeatAsync() { while (_running) { try { sendHeartBeart(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } await Task.Delay(TimeSpan.FromSeconds(60)); // 间隔60秒 } } /// /// 心跳发送接口 /// void sendHeartBeart() { string serverUri = _configuration["props:serverAddr"]; if (!string.IsNullOrEmpty(serverUri) && !string.IsNullOrWhiteSpace(serverUri)) { Dictionary 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 result = JsonConvert.DeserializeObject>(rs.data); if (!"200".Equals(result["code"])) { Console.WriteLine("心跳请求失败,与服务器的连接断开"); } } } } public void Stop() { //修改状态 _running = false; } } }