123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- 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 WMS.BZServices.HJ;
- using WCS.Protocol.HJ.DataStructure;
- namespace WMS.BZWeb.Areas.DeviceMonitorManager.Controllers
- {
- [Area("DeviceMonitorManager")]
- public class HJDeviceMonitorController : MvcControllerBase
- {
- private readonly RedisClient _HJRedis;
- private readonly WareCellService _wareCellService;
- private readonly WCSTaskOldService _wCSTaskOldService;
- private readonly AgvTaskInfoService _agvtaskInfoService;
-
- private DateTime MinTime = DateTime.Parse("2023/01/01");
- private readonly Dictionary<string, List<string>> _srmStations = new Dictionary<string, List<string>>()
- {
- { "SRM1",new List<string>{ "1021", "1022" , "1011", "1012" } },
- { "SRM2",new List<string>{ "1023", "1024" , "1013", "1014" } },
- { "SRM3",new List<string>{ "1025", "1026" , "1015", "1016" } }
- };
- public HJDeviceMonitorController(IServiceProvider serviceProvider, WareCellService wareCellService,
- WCSTaskOldService wCSTaskOldService, AgvTaskInfoService agvtaskInfoService)
- {
- _wCSTaskOldService = wCSTaskOldService;
- _wareCellService = wareCellService;
- _agvtaskInfoService = agvtaskInfoService;
- DeviceDataPack dataPack = new DeviceDataPack();
- MessagePackSerializer.DefaultOptions = StandardResolver.Options.WithCompression(MessagePackCompression.Lz4Block);
- var redisDict = serviceProvider.GetRequiredService<Dictionary<string, RedisClient>>();
- if (redisDict.Any())
- {
- _HJRedis = redisDict["HJRedis"];
-
- _HJRedis.Serialize = obj => MessagePackSerializer.Serialize(obj);// JsonConvert.SerializeObject(obj);
- _HJRedis.DeserializeRaw = (bytes, type) => MessagePackSerializer.Deserialize(type, bytes);// JsonConvert.DeserializeObject(json, type);
-
- }
- }
- #region 视图功能
- // GET: DeviceMonitorManager/HJDeviceMonitor
- public ActionResult Index()
- {
- //var sssd = _HJRedis.Get<DeviceDataPack>("DeviceDataPack");
- //_PTRedis.Set<DeviceDataPack>("DeviceDataPack", sssd);
- //var ppsssd = _PTRedis.Get<DeviceDataPack>("DeviceDataPack");
- var StartFrame = _HJRedis.LIndex<DeviceDataPack>("Packs", 0).Frame.ToLocalTime();
- var Len = _HJRedis.LLen("Packs");
- var LastFrame = _HJRedis.LIndex<DeviceDataPack>("Packs", Len - 1).Frame.ToLocalTime();
- ViewBag.Max = LastFrame.Ticks;
- ViewBag.Min = StartFrame.Ticks;
- ViewBag.Len = Len;
- ViewBag.Frame = LastFrame;
- ////long timestamp = DateTime.Now.ToLocalTime().Ticks; // 获取当前时间戳
- //////long ticks = timestamp / 10000 - 62135596800000; // 将时间戳转换为Ticks
- ////DateTime dateTime = new DateTime(timestamp); // 将Ticks转换为DateTime格式
- ////ViewBag.Max = timestamp;
- ////long pretimestamp = DateTime.Now.AddDays(-2).ToLocalTime().Ticks; // 获取当前时间戳
- ////ViewBag.Min = pretimestamp;
- //var Len = _HJRedis.LLen("Packs");
- //var sss = _HJRedis.LIndex<DeviceDataPack>("Packs", Len - 1);
- ////var sssd = _HJRedis.Get<DeviceDataPack>("DeviceDataPack");
- //var json = JsonConvert.SerializeObject(sssd);
- //for (int i = (int)Len; i > 0; i--)
- //{
- // var sdsdsd = _HJRedis.LIndex<DeviceDataPack>("Packs", i - 1);
- // _PTRedis.RPush("Packs", sdsdsd);
- //}
- ////var client = new RedisClient("118.178.107.197:6379,database=0,password=123456");
- ////client.Set("test", "tewtet");
- //_HJRedis.Set("testhjhjhj","testetsehjhjhjhjhjhjhjhj");
- ////// 设置hash值
- ////_HJRedis.HSet("myhash", "field1", "value1");
- ////// 获取hash值
- ////var value = _HJRedis.HGet("myhash", "field1");
- ////// 获取所有hash值
- ////var values = _HJRedis.HGetAll<string>("myhash");
- ////Dictionary<string, List<UserInfo>> result = new Dictionary<string, List<UserInfo>>();
- ////List<UserInfo> list = new List<UserInfo>();
- ////for (int i = 0; i < 50000; i++)
- ////{
- //// list.Add(new UserInfo { Email = "aa.com", QQ = i.ToString() });
- ////}
- ////result.Add("dictest", list);
- ////Stopwatch sw = Stopwatch.StartNew();
- ////_HJRedis.HSet<List<UserInfo>>("TestHash", result);
- ////sw.Stop();
- ////var sss = sw.ElapsedMilliseconds;
- ////sw.Start();
- ////var desgt = _HJRedis.HGet<List<UserInfo>>("TestHash", "dictest");
- ////sw.Stop();
- ////var swsw = sw.ElapsedMilliseconds;
- return View();
- }
- /// <summary>
- /// 表单页
- /// <summary>
- /// <returns></returns>
- [HttpGet]
- public ActionResult DDJDetail()
- {
- return View();
- }
- /// <summary>
- ///
- /// <summary>
- /// <returns></returns>
- [HttpGet]
- public ActionResult StationDetail()
- {
- return View();
- }
- #endregion
- public ActionResult GetDatetime(string timestamp)
- { // 638237068780288016
- // 565105737982546638
- var dateTime = new DateTime(Convert.ToInt64(timestamp));
- long ticks = Convert.ToInt64(timestamp) * 10000 + 62135596800000; // 将时间戳转换为Ticks
- //DateTime dateTime = new DateTime(ticks); // 将Ticks转换为DateTime格式
- return Success(dateTime);
- }
- public ActionResult GetLastDatas()
- {
- var len = _HJRedis.LLen("Packs");
- var device = _HJRedis.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 = _HJRedis.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 = _HJRedis.LLen("Packs");
- var dateTime = _HJRedis.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 = _HJRedis.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);
- }
- /// <summary>
- /// 获取首页设备数据
- /// </summary>
- /// <returns></returns>
- public ActionResult GetData()
- {
- WCSDevicePacks wCSDevicePacks = new WCSDevicePacks()
- {
- SRMStations = new List<Info.Models.WCSDeviceMonitor.SRMStation>() {
- new SRMStation(){Code="SRM1", IsAuto=1,TaskNum="5464"},
- },
- WCSStations = new List<WCSStation> { new WCSStation { Code = "1015", FaultInfo = "err", IsAuto = 1, PH_STATUS = true, TaskNum = "3423" } }
- };
- return Success(wCSDevicePacks);
- }
- /// <summary>
- /// 获取堆垛机详情数据
- /// </summary>
- /// <param name="keyValue"></param>
- /// <returns></returns>
- 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 = _HJRedis.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();
- wCSDevicePacks.WCSStations.AddRange(from item in deviceDataPack.StationDatas?.Datas
- select new WCSStation
- {
- Code = item.Code,
- FaultInfo = (int)item.D523.Alarm > 0 ? item.D523.Alarm.GetDescription() : "",
- IsAuto = (int)item.D520.Mode,
- PH_STATUS = item.D523.Status.HasFlag(WCS.Protocol.HJ.Station.StatusEunm.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,
- });
- 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;
- }
- }
- }
|