using Microsoft.AspNetCore.Mvc; using SqlSugar; using FreeRedis; using WMS.Util; using WMS.Info.Models.WCSDeviceMonitor.WireRods; using WMS.Info.Models.WCSDeviceMonitor; using MessagePack; using MessagePack.Resolvers; using WMS.BZWeb.Extensions; using WMS.BZModels; using ServiceStack; using WCS.Entity.Protocol.DataStructure; using WCS.Entity.Protocol.Station; using WCS.Entity.Protocol.Protocol.DataStructure; using WMS.BZServices.PT; namespace WMS.BZWeb.Areas.DeviceMonitorManager.Controllers { [Area("DeviceMonitorManager")] public class PTDeviceMonitorController : MvcControllerBase { private readonly RedisClient _PTRedis; private readonly WareCellService _wareCellService; private readonly WCSTaskOldService _wCSTaskOldService; private readonly AgvTaskInfoService _agvtaskInfoService; private readonly RedisClient _FJRedis; private readonly Dictionary> _srmStations = new Dictionary>() { { "SRM1",new List{ "1001", "1002" , "1003", "1004" } }, { "SRM2",new List{ "1005", "1006" , "1007", "1008" } }, { "SRM3",new List{ "1009", "1010" , "1011", "1012" } }, { "SRM4",new List{ "1013", "1014" , "1015", "1016" } } }; public PTDeviceMonitorController(IServiceProvider serviceProvider, WareCellService wareCellService, WCSTaskOldService wCSTaskOldService, AgvTaskInfoService agvtaskInfoService) { var redisDict = serviceProvider.GetRequiredService>(); if (redisDict.Any()) { _PTRedis = redisDict["PTRedis"]; _PTRedis.Serialize = obj => MessagePackSerializer.Serialize(obj);// JsonConvert.SerializeObject(obj); _PTRedis.DeserializeRaw = (bytes, type) => MessagePackSerializer.Deserialize(type, bytes);// JsonConvert.DeserializeObject(json, type); } _wCSTaskOldService = wCSTaskOldService; _wareCellService = wareCellService; _agvtaskInfoService = agvtaskInfoService; _FJRedis = redisDict["FJRedis"]; _FJRedis.Serialize = obj => MessagePackSerializer.Serialize(obj);// JsonConvert.SerializeObject(obj); _FJRedis.DeserializeRaw = (bytes, type) => MessagePackSerializer.Deserialize(type, bytes);// JsonConvert.DeserializeObject(json, type); } #region 视图功能 // GET: DeviceMonitorManager/PTDeviceMonitor public ActionResult Index() { var StartFrame = _PTRedis.LIndex("Packs", 0).Frame.ToLocalTime(); var Len = _PTRedis.LLen("Packs"); var LastFrame = _PTRedis.LIndex("Packs", Len - 1).Frame.ToLocalTime(); ViewBag.Max = LastFrame.Ticks; ViewBag.Min = StartFrame.Ticks; ViewBag.Len = Len; ViewBag.Frame = LastFrame; return View(); } /// /// 表单页 /// /// [HttpGet] public ActionResult DDJDetail() { return View(); } [HttpGet] public ActionResult InteractionPointIndex() { var keylists = _FJRedis.Keys("*WCSLog**"); // keylist.Split(':').Last() foreach (var keylist in keylists) { //keylist.Replace(_FJRedis.Prefix); var value = _FJRedis.Get(keylist); } return View(); } #endregion public ActionResult GetLastDatas() { var len = _PTRedis.LLen("Packs"); var device = _PTRedis.Get("DeviceDataPack"); var wCSDevicePacks = ConvertData(device); wCSDevicePacks.Frame = device.Frame.ToLocalTime(); wCSDevicePacks.Index = len.ToString(); return Success(wCSDevicePacks); } /// /// /// /// /// /// public ActionResult GetStartDatas(string timestamp, int len) { if (timestamp == "undefined") { return Fail(""); } var frame = new DateTime(Convert.ToInt64(timestamp)); var Len = Convert.ToInt64(len); var temp = Len; var i = Len - 1; var isFind = true; WCSDevicePacks wCSDevicePacks = new WCSDevicePacks(); try { while (isFind) { temp /= 2; if (temp == 0) temp = 1; var ps = _PTRedis.LRange("Packs", i - 1, i); if (ps.Any(v => v.Frame.ToLocalTime() <= frame) && ps.Any(v => v.Frame.ToLocalTime() >= frame)) { isFind = false; var DataPack = ps.Last(); if (DataPack != null) { wCSDevicePacks = ConvertData(DataPack); wCSDevicePacks.Frame = DataPack.Frame.ToLocalTime(); wCSDevicePacks.Index = i.ToString(); } } else if (ps.All(v => v.Frame.ToLocalTime() < frame)) { i += temp; } else { i -= temp; } } } catch (Exception ex) { return Fail("出错了" + ex.Message); } return Success(wCSDevicePacks); } /// /// /// /// public ActionResult GetLastFrame() { var len = _PTRedis.LLen("Packs"); var dateTime = _PTRedis.LIndex("Packs", len - 1).Frame.ToLocalTime(); return Success(new { FrameTicks = dateTime.Ticks, Frame = dateTime, Len = len, LastFrameTicks = dateTime.Ticks, LastFrame = dateTime }); } public ActionResult GetRangeData(int start, int end) { var arr = _PTRedis.LRange("Packs", start, end); List list = new List(); foreach (var obj in arr) { obj.Frame = obj.Frame.ToLocalTime(); var wCSDevicePacks = ConvertData(obj); list.Add(wCSDevicePacks); } return Success(list); } public ActionResult GetWarehouseLocationInfo() { var wareCellcount = _wareCellService.GetEmptyWareCell(); var taskUnFinishCount = _wCSTaskOldService.GetTaskUnFinishCount(); var agvTaskUnFinishCount = _agvtaskInfoService.GetTaskUnFinishCount(); WarehouseLocationInfo warehouseLocationInfo = new WarehouseLocationInfo() { EmptySpaces = wareCellcount, TotalAGVs = agvTaskUnFinishCount, TotalTasks = taskUnFinishCount, }; return Success(warehouseLocationInfo); } public ActionResult GetSRMData(string keyValue) { SRMStationDetail sRMStationDetail = new SRMStationDetail(); sRMStationDetail.WCSSRM = new WCSSRM(); if (_srmStations.TryGetValue(keyValue.ToUpper(), out var stations)) { if (stations != null && stations.Count > 0) { sRMStationDetail.SRMStations = new List() { }; } } return Success(sRMStationDetail); } /// /// 获取设备信息 /// /// 设备编号 /// public ActionResult GetDeviceData(string keyValue) { if (string.IsNullOrEmpty(keyValue)) { return Fail("设备编号不能为空!"); } var deviceDataPack = _PTRedis.Get("DeviceDataPack"); //DeviceDataPack dp = new DeviceDataPack(); if (deviceDataPack != null && deviceDataPack.StationDatas != null && deviceDataPack.StationDatas.Datas != null && deviceDataPack.StationDatas.Datas.Count() > 0) { var data = deviceDataPack.StationDatas.Datas.FirstOrDefault(o => o.Code == keyValue); if (data != null) { var result = data.GetAttributesDBDetail(); return Success(result); } } if (deviceDataPack != null && deviceDataPack.SRMDatas != null && deviceDataPack.SRMDatas.Datas != null && deviceDataPack.SRMDatas.Datas.Count() > 0) { var data = deviceDataPack.SRMDatas.Datas.FirstOrDefault(o => o.Code == keyValue?.ToUpper()); if (data != null) { var result = data.GetAttributesDBDetail(); return Success(result); } } return Fail("没有数据!"); ; } private WCSDevicePacks ConvertData(DeviceDataPack deviceDataPack) { WCSDevicePacks wCSDevicePacks = new WCSDevicePacks() { WCSStations = new List(), SRMStations = new List(), FaultInfos = new List(), WCSSRMs = new List(), }; if (deviceDataPack == null) { return wCSDevicePacks; } wCSDevicePacks.Frame = deviceDataPack.Frame.ToLocalTime(); if (deviceDataPack.StationDatas != null) { wCSDevicePacks.WCSStations.AddRange(from item in deviceDataPack.StationDatas?.Datas select new WCSStation { Code = item.Code, FaultInfo = item.D523 != null && (int)item.D523.Alarm > 0 ? item.D523.Alarm.GetDescription() : "", IsAuto = (int)item.D520.Mode, PH_STATUS = item.D523.Status.HasFlag(StationStatus.PH_Status), TaskNum = item.D520.TaskNumber.ToString(), GoodsStart = item.D520.GoodsStart, GoodsEnd = item.D520.GoodsEnd, Request = item.D521.Request == 0 ? false : true, Status = item.D523.Status.GetDescription(), BarCode = item.D80?.Content, }); } wCSDevicePacks.WCSSRMs.AddRange(from item in deviceDataPack.SRMDatas?.Datas select new WCSSRM { Code = item.Code, IsAuto = (int)item.D521.AutoStatus, SCAlarm = item.D537.Alarm.GetDescription(), TaskNum = item.D520.TaskNumber.ToString(), SRMStatus = item.D521.RunStatus.GetDescription(), LoadingStatus = item.D521.Status.GetDescription(), SHeight = 0, CarryingWeight = 0, TotalKM = item.D521.TotalKm, SLine = item.D520.SLine, SCol = item.D520.SCol, SLayer = item.D520.SLayer, SDepth = (int)item.D520.SDepth, ELine = item.D520.ELine, ECol = item.D520.ECol, EDepth = (int)item.D520.EDepth, }); if (deviceDataPack.StationDatas != null) { wCSDevicePacks.FaultInfos.AddRange(from item in deviceDataPack.StationDatas?.Datas where item?.D523 != null && (int)item.D523.Alarm > 0 select new FaultInfo { Areas = "盘条库", DeviceCode = item.Code, FaultInfos = (int)item.D523.Alarm > 0 ? item.D523.Alarm.GetDescription() : "", }); } wCSDevicePacks.FaultInfos.AddRange(from item in deviceDataPack.SRMDatas?.Datas where item?.D537 != null && (int)item.D537.Alarm > 0 select new FaultInfo { Areas = "盘条库", DeviceCode = item.Code, FaultInfos = (int)item.D537.Alarm > 0 ? item.D537.Alarm.GetDescription() : "", }); return wCSDevicePacks; } } }