/// <summary> /// 00 201007 0000 /// </summary> /// <param name="count">数量</param> /// <param name="time">时间</param> /// <param name="suffix">后缀</param> /// <returns></returns> private static IEnumerable <string> GenFlowCard(int count, DateTime time, string suffix) { var redisKey = $"{RedisPre}:{CodeType.流程卡}"; var str = new List <string>(); var ws = Stopwatch.StartNew(); while (ws.ElapsedMilliseconds < 10 * 1000) { if (!RedisHelper.SetIfNotExist(redisKey, DateTime.Today.ToStr())) { continue; } var startKey = $"{RedisPre}:{DateTime.Today.ToDateStr()}-{CodeType.流程卡}"; var startValue = RedisHelper.Get <int>(startKey); for (var i = 0; i < count; i++) { str.Add($"{((int)CodeType.流程卡):D2}{DateTime.Today.ToStrShort()}{startValue++:D4}{suffix}"); } RedisHelper.SetForever(startKey, startValue); RedisHelper.Remove(redisKey); break; } return(str); }
/// <summary> /// 更新设备 /// </summary> /// <returns></returns> private static void UpdateDevices(Dictionary <int, SmartProcessDevice> devices) { foreach (var device in devices) { var deviceKey = GetDeviceKey(device.Key); RedisHelper.SetForever(deviceKey, device.Value); } }
private static void NeedArrange(object obj) { if (RedisHelper.SetIfNotExist(DateLock, ServerConfig.GetLockValue)) { RedisHelper.SetExpireAt(DateLock, DateTime.Now.AddMinutes(5)); var last = RedisHelper.Get <DateTime>(DateKey); var now = DateTime.Now; if (last == default(DateTime) || last < now.Date) { var tasks = new List <SmartTaskOrderConfirm>(); var schedule = new List <SmartTaskOrderScheduleDetail>(); //ArrangeSchedule(ref tasks, ref schedule, true); RedisHelper.SetForever(DateKey, now); } } }
/// <summary> /// 更新加工人 /// </summary> /// <returns></returns> private static void UpdateProcessors(IEnumerable <SmartProcessor> processors) { RedisHelper.SetForever(ProcessorKey, processors); }
/// <summary> /// 更新设备 /// </summary> /// <returns></returns> private static void UpdateDevices(int categoryId, IEnumerable <SmartProcessDevice> devices) { var deviceKey = GetDeviceKey(categoryId); RedisHelper.SetForever(deviceKey, devices); }
/// <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); } }
/// <summary> /// 缓存钉钉部门用户 /// </summary> private static void SaveDingTalkUserListCache(string key, DingTalkUserList token) { var data = token.ToJSON(); RedisHelper.SetForever(key, data); }