using Furion; using Furion.TaskScheduler; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using WIDESEA_Common; using WIDESEA_Common.DBNames; using WIDESEA_Core; using WIDESEA_Core.SqlSuger; using WIDESEA_Core.Utilities; using WIDESEA_Entity; using WIDESEA_Entity.DomainModels; using WIDESEA_WCS.WCSClient; namespace WIDESEA_WCS { public class JobWorker : ISpareTimeWorker { private bool _taskScheduler = true; public static List equipmentinfos = new List(); public static List caches = new List(); /// /// 服务重启 /// /// /// [SpareTime(1000, "StartServer", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public async Task StartServer(SpareTimer timer, long count) { if (_taskScheduler) { //SpareTime.Cancel("StartServer"); WebResponseContent responseContent = WCSService.ConnectServer(); if (responseContent.Status) { if (WCSService.Clients.Count > 0) { _taskScheduler = false; await Task.CompletedTask; WriteLogHelper.WriteLog("服务开启", $"开启结果:{responseContent.Message} 开启时间:{DateTime.Now.ToString("F")},服务开启"); SpareTime.Cancel("StartServer"); } } else { WriteLogHelper.WriteLog("服务开启", $"开启结果:{responseContent.Message} 开启时间:{DateTime.Now.ToString("F")},服务开启"); } } } /// /// 每五秒执行 /// /// /// [SpareTime(5000, "jobName", StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void DoSomething(SpareTimer timer, long count) { PLCClient client = null; caches = new List(); List cachess = new List(); if (WCSService.Clients != null) { if (WCSService.Clients.Count > 0) { client = WCSService.Clients[0]; if (client.ConnectionMessage == "拆包间") { int x = client.PLCDBItems.Select(x => x.EquipNum).Select(int.Parse).ToList().Max(); for (int i = 1; i < x + 1; i++) { string state = client.ReadValue(AGVDBName.R_AGV_CacheState.ToString() + i, i.ToString()).ToString(); Cache cache = new Cache() { cacheNo = i, cacheState = state == "1" ? "有货" : state == "2" ? "无货" : state == "3" ? "待料中" : state == "4" ? "上架中" : state == "5" ? "异常" : "无此缓存架", cacheType = client.ReadValue(AGVDBName.R_AGV_CacheType.ToString() + i, i.ToString()).ToString(), }; if (cache.cacheState != "无此缓存架") { caches.Add(cache); } if (cache.cacheState == "无货") { cachess.Add(cache); } } //ChatHub chatHub = new ChatHub(); ChatHub chatHub = App.GetService(); chatHub.SendMessage("ReciveMessage", (caches)); chatHub.SendMessage("SendError", (cachess)); } else { SpareTime.Cancel("jobName"); } } } } /// /// 每五秒执行 库存 /// /// /// [SpareTime(5000, "jobName1", StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void SendContainer(SpareTimer timer, long count) { PLCClient client = null; List containers = new List(); if (WCSService.Clients != null) { if (WCSService.Clients.Count > 0) { client = WCSService.Clients[0]; if (client.ConnectionMessage == "拆包间") { containers = SqlSugarHelper.WCSDB.Queryable().ToList(); ChatHub chatHub = App.GetService(); chatHub.SendMessage("SendContainer", (containers)); } else { SpareTime.Cancel("jobName1"); } } } } /// /// 每五秒执行 投料间 /// /// /// [SpareTime(5000, "SendFeedingData", StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)] public void SendFeedingData(SpareTimer timer, long count) { PLCClient client = null; if (WCSService.Clients != null) { if (WCSService.Clients.Count > 0) { client = WCSService.Clients[0]; List Zcachess = new List(); List Zcaches = new List(); List Fcachess = new List(); List Fcaches = new List(); if (client.ConnectionMessage == "投料间") { ChatHub chatHub = App.GetService(); int xz = client.PLCDBItems.Where(x => x.ItemName.Contains(AGVDBName.R_AGV_CacheStateZ.ToString())).Select(x => x.EquipNum).Select(int.Parse).ToList().Max(); int xf = client.PLCDBItems.Where(x => x.ItemName.Contains(AGVDBName.R_AGV_CacheStateF.ToString())).Select(x => x.EquipNum).Select(int.Parse).ToList().Max(); for (int i = 1; i < xz + 1; i++) { string state = client.ReadValue(AGVDBName.R_AGV_CacheStateZ.ToString() + i, i.ToString()).ToString(); Cache cache = new Cache() { cacheNo = i, cacheState = state == "1" ? "有货" : state == "2" ? "无货" : state == "3" ? "待料中" : state == "4" ? "上架中" : state == "5" ? "异常" : "无此缓存架", cacheType = client.ReadValue(AGVDBName.R_AGV_CacheTypeZ.ToString() + i, i.ToString()).ToString(), }; if (cache.cacheState != "无此缓存架") { Zcaches.Add(cache); } if (cache.cacheState == "无货") { Zcachess.Add(cache); } } chatHub.SendMessage("SendZFeedingData", (Zcaches)); chatHub.SendMessage("SendZError", (Zcachess)); for (int i = 1; i < xf + 1; i++) { string state = client.ReadValue(AGVDBName.R_AGV_CacheStateF.ToString() + i, i.ToString()).ToString(); Cache cache = new Cache() { cacheNo = i, cacheState = state == "1" ? "有货" : state == "2" ? "无货" : state == "3" ? "待料中" : state == "4" ? "上架中" : state == "5" ? "异常" : "无此缓存架", cacheType = client.ReadValue(AGVDBName.R_AGV_CacheTypeF.ToString() + i, i.ToString()).ToString(), }; if (cache.cacheState != "无此缓存架") { Fcaches.Add(cache); } if (cache.cacheState == "无货") { Fcachess.Add(cache); } } chatHub.SendMessage("SendFFeedingData", (Fcaches)); chatHub.SendMessage("SendFError", (Fcachess)); } else { SpareTime.Cancel("SendFeedingData"); } } } } } }