public Result PostDeviceOperateDevice([FromBody] IEnumerable <DeviceOperateDevice> devices) { if (devices == null || !devices.Any()) { return(Result.GenError <Result>(Error.ParamError)); } if (devices.Any(x => x.DeviceId == 0)) { return(Result.GenError <Result>(Error.DeviceOperateDeviceNotEmpty)); } if (devices.GroupBy(x => x.DeviceId).Any(y => y.Count() > 1)) { return(Result.GenError <Result>(Error.DeviceOperateDeviceDuplicate)); } var opId = devices.FirstOrDefault()?.OpId ?? 0; var sames = devices.Select(x => x.DeviceId); if (DeviceOperateDeviceHelper.GetHaveSame(opId, sames)) { return(Result.GenError <Result>(Error.DeviceOperateDeviceIsExist)); } var userId = Request.GetIdentityInformation(); var markedDateTime = DateTime.Now; foreach (var device in devices) { device.CreateUserId = userId; device.MarkedDateTime = markedDateTime; device.Remark = device.Remark ?? ""; } DeviceOperateDeviceHelper.Instance.Add(devices); return(Result.GenError <Result>(Error.Success)); }
public DataResult GetDeviceOperateDevice([FromQuery] bool menu, int opId, int qId) { var result = new DataResult(); result.datas.AddRange(menu ? DeviceOperateDeviceHelper.GetMenus(opId, qId) : DeviceOperateDeviceHelper.GetOpDetails(opId, qId)); if (qId != 0 && !result.datas.Any()) { result.errno = Error.DeviceOperateDeviceNotExist; return(result); } return(result); }
public Result PutDeviceOperateDevice([FromBody] IEnumerable <DeviceOperateDevice> devices) { if (devices == null || !devices.Any()) { return(Result.GenError <Result>(Error.ParamError)); } if (devices.Any(x => x.DeviceId == 0)) { return(Result.GenError <Result>(Error.DeviceOperateDeviceNotEmpty)); } if (devices.GroupBy(x => x.DeviceId).Any(y => y.Count() > 1)) { return(Result.GenError <Result>(Error.DeviceOperateDeviceDuplicate)); } var opId = devices.FirstOrDefault()?.OpId ?? 0; var sames = devices.Select(x => x.DeviceId); var ids = devices.Select(x => x.Id); if (DeviceOperateDeviceHelper.GetHaveSame(opId, sames, ids)) { return(Result.GenError <Result>(Error.DeviceOperateDeviceIsExist)); } var cnt = DeviceOperateDeviceHelper.Instance.GetCountByIds(ids); if (cnt != devices.Count()) { return(Result.GenError <Result>(Error.DeviceOperateDeviceNotExist)); } var markedDateTime = DateTime.Now; foreach (var device in devices) { device.MarkedDateTime = markedDateTime; device.Remark = device.Remark ?? ""; } DeviceOperateDeviceHelper.Instance.Update(devices); return(Result.GenError <Result>(Error.Success)); }
/// <summary> /// 删除3个月前的监控日志 /// </summary> private static void OperateSchedule() { if (RedisHelper.SetIfNotExist(lockKey, ServerConfig.GetLockValue)) { try { RedisHelper.SetExpireAt(lockKey, DateTime.Now.AddMinutes(30)); var now = DateTime.Now; //当前排班 var schedules = DeviceOperateScheduleHelper.Instance.GetAllData <DeviceOperateScheduleOp>().OrderBy(x => x.Order); var maxOrder = schedules.Any() ? schedules.Max(x => x.Order) : 0; var ops = DeviceOperateHelper.Instance.GetAll <DeviceOperate>(); //var devices = DeviceLibraryHelper.Instance.GetAll<DeviceLibrary>(); //排班设备 var opDevices = DeviceOperateDeviceHelper.GetScheduleDevices(); //var op = DeviceOperateOperatorHelper.Instance.GetAll<DeviceOperateOperator>(); //排班操作工 var operators = DeviceOperateOperatorHelper.GetScheduleOperates(); var newScs = new List <DeviceOperateSchedule>(); var upScs = new List <DeviceOperateSchedule>(); var delScs = new List <DeviceOperateSchedule>(); foreach (var op in ops) { if (op.IsEnable && op.IsSchedule) { var opId = op.Id; //排班设备 var opDs = opDevices.Where(x => x.OpId == opId); //排班操作工 var opOs = operators.Where(x => x.OpId == opId); var opScs = new List <DeviceOperateScheduleOp>(); var scs = schedules.Where(x => x.OpId == opId); foreach (var sc in scs) { if (sc.State != DeviceOperateScheduleState.未开始) { opScs.Add(sc); continue; } //排班设备 var device = opDs.FirstOrDefault(x => x.Id == sc.DeviceId); if (sc.OperatorId != 0) { //排班操作工 var optor = opOs.FirstOrDefault(x => x.Id == sc.OperatorId); if (optor == null || !optor.CanAssign(now)) { sc.MarkedDateTime = now; sc.Update = true; sc.OperatorId = 0; sc.AssignTime = default(DateTime); } } if (device != null) { opScs.Add(sc); continue; } sc.MarkedDateTime = now; sc.Add = false; sc.Update = false; delScs.Add(sc); } foreach (var device in opDs) { var deviceId = device.Id; var sc = opScs.FirstOrDefault(x => x.DeviceId == deviceId); if (sc == null) { opScs.Add(new DeviceOperateScheduleOp(now, ++maxOrder, opId, deviceId)); } else { if (sc.MarkedDelete) { sc.MarkedDateTime = now; sc.MarkedDelete = false; sc.Order = ++maxOrder; sc.Update = true; } if (sc.Remark != device.Remark) { sc.MarkedDateTime = now; sc.Remark = device.Remark; sc.Update = true; } } } var canOps = opOs.Where(x => x.CanAssign(now)); var assignScs = opScs.Where(x => x.OperatorId != 0); var assignOps = assignScs.GroupBy(x => x.OperatorId).Select(x => x.Key).ToList(); var cnt = canOps.Count() * 3; var waitScs = opScs.Where(x => x.OperatorId == 0).Take(cnt).ToArray(); var scsOrder = waitScs.Select(x => x.Order).OrderBy(x => x).ToArray(); var deviceIds = waitScs.Select(x => x.DeviceId); var deviceLeftTimes = deviceIds.ToDictionary(x => x, x => 0m); if (deviceIds.Any()) { var deviceLibraryDetails = DeviceHelper.Instance.GetAllByIds <DeviceDetail>(deviceIds).ToDictionary(x => x.Id); try { #if DEBUG var url = "http://192.168.22.222:61102" + UrlMappings.Urls[UrlMappings.deviceListGate]; #else var url = ServerConfig.GateUrl + UrlMappings.Urls[UrlMappings.deviceListGate]; #endif //向GateProxyLink请求数据 var resp = HttpServer.Get(url, new Dictionary <string, string> { { "ids", deviceIds.Join() } }); if (resp != "fail") { var scriptIds = deviceLibraryDetails.Values.Select(x => x.ScriptId); var dataNameDictionaries = scriptIds.Any() ? DataNameDictionaryHelper.GetDataNameDictionaryDetails(scriptIds) : new List <DataNameDictionaryDetail>(); var dataResult = JsonConvert.DeserializeObject <DeviceResult>(resp); if (dataResult.errno == Error.Success) { foreach (DeviceInfo deviceInfo in dataResult.datas) { var deviceId = deviceInfo.DeviceId; if (deviceLibraryDetails.ContainsKey(deviceId)) { deviceInfo.ScriptId = deviceLibraryDetails[deviceId].ScriptId; var deviceData = deviceInfo.DeviceData; if (AnalysisHelper.GetValue(deviceData, dataNameDictionaries, deviceInfo.ScriptId, AnalysisHelper.leftProcessTimeDId, out var v)) { if (deviceLeftTimes.ContainsKey(deviceId)) { deviceLeftTimes[deviceId] = v; } } } } } } } catch (Exception e) { Log.Error($"OperateSchedule {UrlMappings.Urls[UrlMappings.deviceListGate]},信息:{e}"); } } waitScs = waitScs.OrderBy(x => deviceLeftTimes[x.DeviceId]).ThenBy(x => x.Order).ToArray(); for (var i = 0; i < waitScs.Length; i++) { var waitSc = waitScs[i]; var sc = opScs.FirstOrDefault(x => x.Id == waitSc.Id); if (sc != null && sc.Order != scsOrder[i]) { sc.MarkedDateTime = now; sc.Update = true; sc.Order = scsOrder[i]; } } if (assignOps.Count() != canOps.Count()) { var nowAssignOps = canOps.Where(x => assignOps.All(y => y != x.Id)); foreach (var opr in nowAssignOps) { var operatorId = opr.Id; var sc = opScs.FirstOrDefault(x => x.OperatorId == 0); if (sc != null) { sc.MarkedDateTime = now; sc.Update = true; sc.AssignTime = now; sc.OperatorId = operatorId; } } } newScs.AddRange(opScs.Where(x => x.Add)); upScs.AddRange(opScs.Where(x => x.Id != 0 && x.Update)); } } if (newScs.Any()) { DeviceOperateScheduleHelper.Instance.Add(newScs); } if (upScs.Any()) { DeviceOperateScheduleHelper.Instance.Update <DeviceOperateSchedule>(upScs); } if (delScs.Any()) { DeviceOperateScheduleHelper.Instance.Delete(delScs.Select(x => x.Id)); } RedisHelper.SetForever(upKey, 0); } catch (Exception e) { Log.Error(e); } RedisHelper.Remove(lockKey); } }