/// <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> /// 服务器获取钉钉access_token /// </summary> private static void GetDingTalkAccessToken(bool init = false) { var nKey = nTokenLockKey + NotifyPlatformEnum.DingTalk; if (init || ServerConfig.IsAnalysis && RedisHelper.SetIfNotExist(nKey, ServerConfig.GetLockValue)) { RedisHelper.SetExpireAt(nKey, DateTime.Now.AddMinutes(30)); var dingTalk = Configs.Where(x => !x.IsChat && x.Platform == NotifyPlatformEnum.DingTalk).Where(x => !x.CorpId.IsNullOrEmpty() && !x.AgentId.IsNullOrEmpty()); var dingTalkGroups = dingTalk.GroupBy(x => new { x.CorpId, x.AgentId }).ToDictionary(x => x.Key); foreach (var(key, value) in dingTalkGroups) { var corpId = key.CorpId; var agentId = key.AgentId; var tKey = $"{nRedisPre}:Token_{corpId}_{agentId}"; var tObj = GetDingTalkAccessTokenCache(tKey); var get = false; var refresh = false; if (tObj != null && tObj.exptime > DateTime.Now) { if (tObj.refreshed >= _interval) { refresh = true; } } else { get = true; } if (!get && !refresh) { continue; } var first = value.First(); var appKey = first.AppKey; var appSecret = first.AppSecret; var nToken = GetDingTalkAccessToken(appKey, appSecret); if (nToken == null) { continue; } if (get) { SaveDingTalkAccessTokenCache(tKey, nToken); } else { tObj.refresh = nToken.refresh; SaveDingTalkAccessTokenCache(tKey, tObj); } } RedisHelper.Remove(nKey); } }
private static void GetConfigs(bool init = false) { if (init || ServerConfig.IsAnalysis && RedisHelper.SetIfNotExist(nConfigLockKey, ServerConfig.GetLockValue)) { RedisHelper.SetExpireAt(nConfigLockKey, DateTime.Now.AddMinutes(30)); try { Configs = ServerConfig.ApiDb.Query <NotifyWebhook>("SELECT * FROM `notify_webhook` WHERE MarkedDelete = 0;").ToList(); } catch (Exception e) { Log.Error(e); } RedisHelper.Remove(nConfigLockKey); } }
public static void Init() { try { #if DEBUG Log.Print("HOpScheduleHelper Debug"); #else Log.Print("HOpScheduleHelper Release"); #endif RedisHelper.Remove(lockKey); _timer5S = new Timer(DoSth_5s, null, 5000, 1000 * 5); } catch (Exception e) { Log.Error(e); } }
private static void UnLockProcessor() { RedisHelper.Remove(ProcessorLockKey); }
private static void UnLockDevice(int categoryId) { var deviceKey = GetDeviceLockKey(categoryId); RedisHelper.Remove(deviceKey); }
/// <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> /// 服务器获取钉钉access_token /// </summary> private static void GetDingTalkUserList(bool init = false) { var nKey = nUserListLockKey + NotifyPlatformEnum.DingTalk; if (init || ServerConfig.IsAnalysis && RedisHelper.SetIfNotExist(nKey, ServerConfig.GetLockValue)) { RedisHelper.SetExpireAt(nKey, DateTime.Now.AddMinutes(30)); var now = DateTime.Now; var dingTalk = Configs.Where(x => !x.IsChat && x.Platform == NotifyPlatformEnum.DingTalk).Where(x => !x.CorpId.IsNullOrEmpty() && !x.AgentId.IsNullOrEmpty()); var dingTalkGroups = dingTalk.GroupBy(x => new { x.CorpId, x.AgentId }).ToDictionary(x => x.Key); foreach (var(key, value) in dingTalkGroups) { var corpId = key.CorpId; var agentId = key.AgentId; var tKey = $"{nRedisPre}:Token_{corpId}_{agentId}"; var token = GetDingTalkAccessToken(tKey); if (token.IsNullOrEmpty()) { continue; } var uKey = $"{nRedisPre}:UserList_{corpId}_{agentId}"; var userList = GetDingTalkUserListCache(uKey); var get = false; var refresh = false; if (userList != null) { if (userList.refreshed >= _interval) { refresh = true; } } else { get = true; } if (!get && !refresh) { continue; } var nList = new DingTalkUserList(now); foreach (var dep in value) { try { var b = true; while (b) { b = false; var offset = dep.Offset; var url = "https://oapi.dingtalk.com/user/listbypage"; var f = HttpServer.Get(url, new Dictionary <string, string> { { "access_token", token }, { "department_id", dep.DepId.ToString() }, { "offset", offset.ToString() }, { "size", dep.Size.ToString() }, }); if (f == "fail") { Log.Error($"GetDingTalkUserList 请求失败,url:{url}"); } else { var res = JsonConvert.DeserializeObject <DingTalkUserList>(f); if (res.errcode != 0) { Log.Error($"GetDingTalkUserList 返回失败,url:{url},返回码:{res.errcode},原因:{res.errmsg}"); } else { b = res.hasMore; foreach (var user in res.userlist) { var u = nList.userlist.FirstOrDefault(x => x.userid == user.userid); if (u != null) { u.name = user.name; u.unionid = user.unionid; u.department = u.department.Concat(user.department).Distinct().OrderBy(x => x).ToList(); u.mobile = user.mobile; u.email = user.email; } else { nList.userlist.Add(user); } } } } } } catch (Exception e) { Log.Error($"GetDingTalkUserList 失败,原因:{e.Message},错误:{e.StackTrace}"); } } //nList.userlist = nList.userlist.OrderBy(x => x.userid).ThenBy(x => x.name).ToList(); if (get) { SaveDingTalkUserListCache(uKey, nList); } else { userList.refresh = nList.refresh; SaveDingTalkUserListCache(uKey, userList); } } RedisHelper.Remove(nKey); } }
/// <summary> /// 服务器获取微信access_token /// </summary> private static void GetWeiXinUserList(bool init = false) { var nKey = nUserListLockKey + NotifyPlatformEnum.WorkWeiXin; if (init || ServerConfig.IsAnalysis && RedisHelper.SetIfNotExist(nKey, ServerConfig.GetLockValue)) { RedisHelper.SetExpireAt(nKey, DateTime.Now.AddMinutes(30)); var now = DateTime.Now; var weiXin = Configs.Where(x => !x.IsChat && x.Platform == NotifyPlatformEnum.WorkWeiXin).Where(x => !x.CorpId.IsNullOrEmpty() && !x.CorpSecret.IsNullOrEmpty() && !x.AgentId.IsNullOrEmpty()); var weiXinGroups = weiXin.GroupBy(x => new { x.CorpId, x.AgentId }).ToDictionary(x => x.Key); foreach (var(key, value) in weiXinGroups) { var corpId = key.CorpId; var agentId = key.AgentId; var tKey = $"{nRedisPre}:Token_{corpId}_{agentId}"; var token = GetWeiXinAccessToken(tKey); if (token.IsNullOrEmpty()) { continue; } var uKey = $"{nRedisPre}:UserList_{corpId}_{agentId}"; var userList = GetWeiXinUserListCache(uKey); var get = false; var refresh = false; if (userList != null) { if (userList.refreshed >= _interval) { refresh = true; } } else { get = true; } if (!get && !refresh) { continue; } var depIds = value.GroupBy(x => x.DepId).Select(x => x.Key); var nList = new WeiXinUserList(now); foreach (var depId in depIds) { try { //var url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist"; var url = "https://qyapi.weixin.qq.com/cgi-bin/user/list"; var f = HttpServer.Get(url, new Dictionary <string, string> { { "access_token", token }, { "department_id", depId.ToString() }, { "fetch_child", 0.ToString() }, }); if (f == "fail") { Log.Error($"GetWeiXinUserList 请求失败,url:{url}"); } else { f = HttpUtility.UrlDecode(f, Encoding.UTF8); var res = JsonConvert.DeserializeObject <WeiXinUserList>(f); if (res.errcode != 0) { Log.Error($"GetWeiXinUserList 返回失败,url:{url},返回码:{res.errcode},原因:{res.errmsg}"); } else { foreach (var user in res.userlist) { var u = nList.userlist.FirstOrDefault(x => x.userid == user.userid); if (u != null) { u.name = user.name; u.open_userid = user.open_userid; u.department = u.department.Concat(user.department).Distinct().OrderBy(x => x).ToList(); u.mobile = user.mobile; u.gender = user.gender; u.email = user.email; } else { nList.userlist.Add(user); } } } } } catch (Exception e) { Log.Error($"GetWeiXinUserList 失败,原因:{e.Message},错误:{e.StackTrace}"); } } //nList.userlist = nList.userlist.OrderBy(x => x.userid).ThenBy(x => x.name).ToList(); if (get) { SaveWeiXinUserListCache(uKey, nList); } else { userList.refresh = nList.refresh; SaveWeiXinUserListCache(uKey, userList); } } RedisHelper.Remove(nKey); } }