123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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<string, List<string>> _srmStations = new Dictionary<string, List<string>>()
- {
- { "SRM1",new List<string>{ "1001", "1002" , "1003", "1004" } },
- { "SRM2",new List<string>{ "1005", "1006" , "1007", "1008" } },
- { "SRM3",new List<string>{ "1009", "1010" , "1011", "1012" } },
- { "SRM4",new List<string>{ "1013", "1014" , "1015", "1016" } }
- };
- public PTDeviceMonitorController(IServiceProvider serviceProvider, WareCellService wareCellService,
- WCSTaskOldService wCSTaskOldService, AgvTaskInfoService agvtaskInfoService)
- {
- var redisDict = serviceProvider.GetRequiredService<Dictionary<string, RedisClient>>();
- 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<DeviceDataPack>("Packs", 0).Frame.ToLocalTime();
- var Len = _PTRedis.LLen("Packs");
- var LastFrame = _PTRedis.LIndex<DeviceDataPack>("Packs", Len - 1).Frame.ToLocalTime();
- ViewBag.Max = LastFrame.Ticks;
- ViewBag.Min = StartFrame.Ticks;
- ViewBag.Len = Len;
- ViewBag.Frame = LastFrame;
- return View();
- }
- /// <summary>
- /// 表单页
- /// <summary>
- /// <returns></returns>
- [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>("DeviceDataPack");
- var wCSDevicePacks = ConvertData(device);
- wCSDevicePacks.Frame = device.Frame.ToLocalTime();
- wCSDevicePacks.Index = len.ToString();
- return Success(wCSDevicePacks);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="timestamp"></param>
- /// <param name="len"></param>
- /// <returns></returns>
- 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<DeviceDataPack>("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);
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public ActionResult GetLastFrame()
- {
- var len = _PTRedis.LLen("Packs");
- var dateTime = _PTRedis.LIndex<DeviceDataPack>("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<DeviceDataPack>("Packs", start, end);
- List<WCSDevicePacks> list = new List<WCSDevicePacks>();
- 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<SRMStation>() { };
- }
- }
- return Success(sRMStationDetail);
- }
- /// <summary>
- /// 获取设备信息
- /// </summary>
- /// <param name="keyValue">设备编号</param>
- /// <returns></returns>
- public ActionResult GetDeviceData(string keyValue)
- {
- if (string.IsNullOrEmpty(keyValue))
- {
- return Fail("设备编号不能为空!");
- }
- var deviceDataPack = _PTRedis.Get<DeviceDataPack>("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<StationData>(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<SRMData>(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<WCSStation>(),
- SRMStations = new List<SRMStation>(),
- FaultInfos = new List<FaultInfo>(),
- WCSSRMs = new List<WCSSRM>(),
- };
- 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;
- }
- }
- }
|