123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- using System.ComponentModel;
- using Microsoft.AspNetCore.Mvc;
- using Newtonsoft.Json;
- using PlcSiemens.Core.Extension;
- using ServiceCenter;
- using ServiceCenter.Extensions;
- using ServiceCenter.Redis;
- using ServiceCenter.SqlSugars;
- using System.Net.NetworkInformation;
- using System.Text;
- using SqlSugar;
- using WCS.Core;
- using WCS.Entity;
- using WCS.Entity.Protocol.DataStructure;
- using WCS.Entity.Protocol.HUB;
- using WCS.Entity.Protocol.Protocol.Robot;
- using WCS.Entity.Protocol.SRM;
- using WCS.WorkEngineering.Extensions;
- using WCS.WorkEngineering.Systems;
- using WCS.WorkEngineering.WebApi.Models.WCS.Request;
- using WCS.WorkEngineering.WebApi.Models.WCS.Response;
- using WCS.WorkEngineering.WebApi.Models.WMS.Response;
- using TaskStatus = WCS.Entity.TaskStatus;
- using static Dm.net.buffer.ByteArrayBuffer;
- using System.Security.Claims;
- using WCS.Entity.Protocol.Truss;
- // ReSharper disable PossibleLossOfFraction
- namespace WCS.WorkEngineering.WebApi.Controllers
- {
- /// <summary>
- /// 设备Ip通讯检测结构
- /// </summary>
- public class DeviceIpTestResults
- {
- public string Ip { get; set; }
- public bool Result { get; set; }
- }
- /// <summary>
- /// WCS相关接口控制器
- /// </summary>
- [ApiController]
- [Route("api/[controller]/[action]")]
- public class WcsController : ControllerBase
- {
- /// <summary>
- /// 获取设备配置信息接口
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- public List<Device> GetDeviceList()
- {
- return Device.All.ToList();
- }
- /// <summary>
- /// 获取设备运行状态
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- [HttpPost]
- public List<RunInfo> GetDevRunInfo([FromBody] DevRunInfoViewMode req)
- {
- var runInfos = new List<RunInfo>();
- var db = new SqlSugarHelper().Default;
- var plcEx = new SqlSugarHelper().PLCEX;
- var type = "";
- switch (req.Type)
- {
- case DevType.Robot: //机械臂
- type = DevType.Robot.ToString();
- //获取时段内设备的运行状态
- var runInfoList = plcEx.Queryable<DevRunInfo>().Where(x => x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime).ToList().GroupBy(x => x.Code);
- foreach (var runInfo in runInfoList)
- {
- var info = new RunInfo()
- {
- Code = runInfo.Key,
- EnterDepot = db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock)
- .Where(x => x.Status == TaskStatus.Finish && x.Type == TaskType.SetPlate &&
- x.Device == runInfo.Key && x.EndTime >= req.StateTime &&
- x.EndTime < req.EndTime)
- .SplitTable(x => x.Take(2))
- .Count(),
- OutDepot = db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock)
- .Where(x => x.Status == TaskStatus.Finish && x.Type == TaskType.OutDepot &&
- x.Device == runInfo.Key && x.EndTime >= req.StateTime &&
- x.EndTime < req.EndTime)
- .SplitTable(x => x.Take(2))
- .Count(),
- Free = runInfo.Where(x =>
- x.RunMode == RobotMode.Automatic.GetDescription() &&
- x.RunStatus == RobotRunStatus.Idle.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
- Alarm = runInfo.Where(x => x.RunMode == RobotMode.Alarm.GetDescription())
- .Sum(x => x.Duration) / 1000 / 60,
- Manual = runInfo.Where(x => x.RunMode == RobotMode.Manua.GetDescription())
- .Sum(x => x.Duration) / 1000 / 60,
- Automatic = runInfo.Where(x => x.RunMode == RobotMode.Automatic.GetDescription()
- && x.RunStatus != RobotRunStatus.Idle.GetDescription()
- && x.RunStatus != RobotRunStatus.Maintain.GetDescription())
- .Sum(x => x.Duration) / 1000 / 60,
- };
- #region 开始分析设备动作,不计算停止时间
- var devActions = new List<DevActionViewMode>();
- double totalTime = 0; //总耗时
- double totalCount = 0; //总次数
- //入库
- var infos = runInfo.Where(x => x.RunMode == RobotMode.Automatic.GetDescription()).OrderBy(x => x.Frame).ToList();
- totalTime = infos.Where(x => x.RunStatus == RobotRunStatus.InboundCrawling.GetDescription() || x.RunStatus == RobotRunStatus.InStorage.GetDescription()).Sum(x => x.Duration);
- totalCount = infos.Count(x => x.RunStatus == RobotRunStatus.InboundCrawling.GetDescription());
- devActions.Add(new DevActionViewMode()
- {
- Type = DevActionType.In.GetDescription(),
- ActionAmount = totalCount,
- ActionTime = totalTime == 0 ? 0 : Math.Round(totalTime / totalCount, 2),
- });
- //出库
- infos = runInfo.Where(x => x.RunMode == RobotMode.Automatic.GetDescription()).ToList();
- totalTime = infos.Where(x => x.RunStatus == RobotRunStatus.OutBoundCrawling.GetDescription() || x.RunStatus == RobotRunStatus.OutStorage.GetDescription()).Sum(x => x.Duration);
- totalCount = infos.Count(x => x.RunStatus == RobotRunStatus.OutBoundCrawling.GetDescription());
- devActions.Add(new DevActionViewMode()
- {
- Type = DevActionType.Out.GetDescription(),
- ActionAmount = totalCount,
- ActionTime = totalTime == 0 ? 0 : Math.Round(totalTime / totalCount, 2),
- });
- info.DevAction = devActions;
- #endregion 开始分析设备动作,不计算停止时间
- #region 计算任务的工位使用数量
- var stationCounts = new List<StationCount>();
- type = DevType.RobotTaskSum.ToString();
- var taskSum = plcEx.Queryable<DevRunInfo>().Where(x => x.Code == runInfo.Key && x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime)
- .ToList().Select(x =>
- {
- var item = x;
- item.Duration = long.Parse(item.RunStatus.Split("--")[2]);
- return item;
- }).ToList();
- //入库
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { Duration: 3, RunMode: "1" }),
- StationIndex = 1
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { Duration: 3, RunMode: "2" }),
- StationIndex = 2
- });
- //出库
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.Out.GetDescription(),
- StationNum = taskSum.Count(x => x is { Duration: 4, RunMode: "1" }),
- StationIndex = 1
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.Out.GetDescription(),
- StationNum = taskSum.Count(x => x is { Duration: 4, RunMode: "2" }),
- StationIndex = 2
- });
- info.StationCount = stationCounts;
- #endregion 计算任务的工位使用数量
- runInfos.Add(info);
- }
- break;
- case DevType.Truss:
- type = DevType.Truss.ToString();
- //获取时段内设备的运行状态
- var trussInfoList = plcEx.Queryable<DevRunInfo>().Where(x => x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime).ToList().GroupBy(x => x.Code);
- foreach (var runInfo in trussInfoList)
- {
- var info = new RunInfo()
- {
- Code = runInfo.Key,
- EnterDepot = db.Queryable<WCS_TaskOld>().With(SqlWith.NoLock)
- .Where(x => x.Status == TaskStatus.Finish && x.Type == TaskType.SetPlate &&
- x.Device == runInfo.Key && x.EndTime >= req.StateTime &&
- x.EndTime < req.EndTime)
- .SplitTable(x => x.Take(2))
- .Count(),
- Free = runInfo.Where(x => x.RunMode == TrussStatus.Idle.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
- Alarm = runInfo.Where(x => x.RunMode == TrussStatus.Alarm.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
- Manual = runInfo.Where(x => x.RunMode == TrussStatus.Manual.GetDescription() || x.RunMode == TrussStatus.KeepInRepair.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
- Automatic = runInfo.Where(x => x.RunMode == TrussStatus.Execute.GetDescription()).Sum(x => x.Duration) / 1000 / 60,
- };
- #region 开始分析设备动作,不计算停止时间
- var devActions = new List<DevActionViewMode>();
- double totalTime = 0; //总耗时
- double totalCount = 0; //总次数
- //入库
- var infos = runInfo.Where(x => x.RunMode == TrussStatus.Execute.GetDescription()).ToList();
- totalTime = infos.Where(x => x.RunMode == TrussStatus.Execute.GetDescription()).Sum(x => x.Duration);
- totalCount = infos.Count(x => x.RunMode == TrussStatus.Execute.GetDescription());
- devActions.Add(new DevActionViewMode()
- {
- Type = DevActionType.In.GetDescription(),
- ActionAmount = totalCount,
- ActionTime = totalTime == 0 ? 0 : Math.Round(totalTime / totalCount, 2),
- });
- info.DevAction = devActions;
- #endregion 开始分析设备动作,不计算停止时间
- #region 计算任务的工位使用数量
- var stationCounts = new List<StationCount>();
- type = DevType.TrussTaskSum.ToString();
- var taskSum = plcEx.Queryable<DevRunInfo>().Where(x => x.Code == runInfo.Key && x.Type == type && x.Frame >= req.StateTime && x.Frame < req.EndTime)
- .ToList();
- //入库
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "1" }),
- StationIndex = 1
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "2" }),
- StationIndex = 2
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "3" }),
- StationIndex = 3
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "4" }),
- StationIndex = 4
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "5" }),
- StationIndex = 5
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "6" }),
- StationIndex = 6
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "7" }),
- StationIndex = 7
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "8" }),
- StationIndex = 8
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "9" }),
- StationIndex = 9
- });
- stationCounts.Add(new StationCount()
- {
- Type = DevActionType.In.GetDescription(),
- StationNum = taskSum.Count(x => x is { RunMode: "10" }),
- StationIndex = 10
- });
- info.StationCount = stationCounts;
- #endregion 计算任务的工位使用数量
- runInfos.Add(info);
- }
- break;
- }
- return runInfos;
- }
- #region 设备IP相关
- /// <summary>
- /// 检查Ip是否正常联通
- /// </summary>
- /// <param name="strIpOrDName">输入参数,表示IP地址或域名</param>
- /// <returns></returns>
- [HttpGet]
- public static bool PingIpOrDomainName(string strIpOrDName)
- {
- try
- {
- var objPingSender = new Ping();
- var objPinOptions = new PingOptions
- {
- DontFragment = true
- };
- const string data = "";
- var buffer = Encoding.UTF8.GetBytes(data);
- const int intTimeout = 120;
- var objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
- var strInfo = objPinReply.Status.ToString();
- return strInfo == "Success";
- }
- catch (Exception)
- {
- return false;
- }
- }
- /// <summary>
- /// 获取设备IP检测结果
- /// </summary>
- /// <returns>设备IP检测结果</returns>
- [HttpGet]
- public List<DeviceIpTestResults> DeviceIpTest()
- {
- if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
- var deviceIpTestResults = new List<DeviceIpTestResults>();
- ServiceHub.DeviceIPList.ForEach(ip =>
- {
- deviceIpTestResults.Add(new DeviceIpTestResults
- {
- Ip = ip,
- Result = PingIpOrDomainName(ip)
- });
- });
- return deviceIpTestResults;
- }
- /// <summary>
- /// 获取设备Ip集合
- /// </summary>
- /// <returns>设备Ip集合</returns>
- [HttpGet]
- public List<string> GetDeviceIpList()
- {
- if (!ServiceHub.DeviceIPList.Any()) throw new Exception("未配置任何Ip");
- return ServiceHub.DeviceIPList;
- }
- #endregion 设备IP相关
- }
- /// <summary>
- /// 设备运行状态
- /// </summary>
- public class DevRunInfoViewMode
- {
- /// <summary>
- /// 类型
- /// </summary>
- public DevType Type { get; set; }
- /// <summary>
- /// 开始时间
- /// </summary>
- public DateTime StateTime { get; set; }
- /// <summary>
- /// 结束时间
- /// </summary>
- public DateTime EndTime { get; set; }
- }
- /// <summary>
- /// 分页参数
- /// </summary>
- public class PagedInfo<T>
- {
- /// <summary>
- /// 每页行数
- /// </summary>
- public int PageSize { get; set; } = 10;
- /// <summary>
- /// 当前页
- /// </summary>
- public int PageIndex { get; set; } = 1;
- /// <summary>
- /// 总记录数
- /// </summary>
- public int TotalNum { get; set; }
- /// <summary>
- /// 总页数
- /// </summary>
- public int TotalPage
- {
- get
- {
- if (TotalNum > 0)
- {
- return TotalNum % this.PageSize == 0 ? TotalNum / this.PageSize : TotalNum / this.PageSize + 1;
- }
- else
- {
- return 0;
- }
- }
- set { }
- }
- public List<T> Result { get; set; }
- public Dictionary<string, object> Extra { get; set; } = new Dictionary<string, object>();
- public PagedInfo()
- {
- }
- }
- public class RunInfo
- {
- public string Code { get; set; }
- /// <summary>
- /// 出库任务
- /// </summary>
- public int OutDepot { get; set; }
- /// <summary>
- /// 入库任务
- /// </summary>
- public int EnterDepot { get; set; }
- /// <summary>
- /// 移库任务
- /// </summary>
- public int MoveDepot { get; set; }
- /// <summary>
- /// 空闲
- /// </summary>
- public double Free { get; set; }
- /// <summary>
- /// 手动
- /// </summary>
- public double Manual { get; set; }
- /// <summary>
- /// 自动
- /// </summary>
- public double Automatic { get; set; }
- /// <summary>
- /// 报警
- /// </summary>
- public double Alarm { get; set; }
- /// <summary>
- /// 运行
- /// </summary>
- public double Working { get; set; }
- /// <summary>
- /// 交互总耗时
- /// </summary>
- public double InteractTotal { get; set; }
- /// <summary>
- /// 设备执行分析
- /// </summary>
- public List<DevActionViewMode> DevAction { get; set; }
- /// <summary>
- /// 任务执行工位使用数量
- /// </summary>
- public List<StationCount> StationCount { get; set; }
- }
- /// <summary>
- /// 设备动作分析
- /// </summary>
- public class DevActionViewMode
- {
- /// <summary>
- /// 动作次数
- /// </summary>
- public double ActionAmount { get; set; }
- /// <summary>
- /// 动作平均耗时
- /// </summary>
- public double ActionTime { get; set; }
- /// <summary>
- /// 动作类型
- /// </summary>
- public string Type { get; set; }
- }
- /// <summary>
- /// 设备动作类型
- /// </summary>
- public enum DevActionType
- {
- /// <summary>
- /// 入库
- /// </summary>
- [Description("入库")]
- In = 0,
- /// <summary>
- /// 出库
- /// </summary>
- [Description("出库")]
- Out = 1,
- /// <summary>
- /// 移库
- /// </summary>
- [Description("移库")]
- Move = 2,
- }
- /// <summary>
- /// 任务执行工位数量
- /// </summary>
- public class StationCount
- {
- /// <summary>
- /// 执行数量
- /// </summary>
- public double StationNum { get; set; }
- /// <summary>
- /// 工位数
- /// </summary>
- public double StationIndex { get; set; }
- /// <summary>
- /// 任务类型
- /// </summary>
- public string Type { get; set; }
- }
- }
|