/// <summary> /// 启动指令队列 /// </summary> /// <param name="second">执行时间间隔,默认10分钟</param> public static void StartCmdQueue(int second = 600) { //最少一分钟跑一次 if (second < 60) { second = 60; } ThreadPool.QueueUserWorkItem(p => { while (true) { try { List <CmdQueue> list = CmdLogic.GetCmdQueues(); InLogQueue("获取到待执行指令数量:" + list.Count(), LogLevel.Info, "local"); if (list.Count > 0) { foreach (var l in list) { //多个指令以_区分 string[] cmdCodes = l.CmdCode.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries); foreach (string code in cmdCodes) { CmdConfig cmd = CacheHelper.GetCachCmdConfig(code, l.Model); CmdLog log = new CmdLog(); log.CmdCode = code; log.CmdName = l.ExceTime + "分钟:" + cmd.CmdName; log.Imei = l.IMEI; log.Param = l.Param; log.IsSucess = false; log.SendLoginName = l.Notice; log.SendDate = DateTime.Now; SendCmdModel model = DeviceData.SendCmd(code, l.APIDeviceId ?? 0, l.Model, CacheHelper.GetTokenByDeviceId(l.IMEI), l.Param); log.IsSucess = model.State == 0; log.Notice = "每隔" + l.ExceTime + "分钟下发主动监测。"; log.Response = model.Content + ":" + model.Message; log.ResponseTime = DateTime.Now; CmdLogic.SaveCmdLog(log);//保存日志 } CmdLogic.UpdateQueueTime(l); } } } catch (Exception ex) { InLogQueue_Error(ex, "local"); } Thread.Sleep(second * 1000); } }); }
/// <summary> /// 下发指令 /// </summary> /// <param name="cmdCode">指令code</param> /// <param name="deviceId">设备id</param> /// <returns></returns> public JsonResult AjaxSendCmd(string cmdCode, int deviceId, string param, int min) { BaseResult res = new BaseResult(); Device d = DeviceLogic.GetDeviceById(deviceId); CmdConfig cmd = CmdLogic.GetCmdConfigByCode(cmdCode, d.APIDeviceModel.Value); if ((d.APIDeviceModel ?? 0) <= 0) { res.Message = "指令下发失败,设备型号为空!"; res.State = State.Falid; return(Json(res)); } CmdLog log = new CmdLog(); log.CmdCode = cmdCode; log.CmdName = cmd.CmdName; log.Imei = d.Imei; log.Param = param; log.IsSucess = false; log.SendLoginName = AuthUser.LoginName; //是否立即下发,不是的加入队列 if ((cmd.IsNowSend ?? true) == false)//设置主动上传间隔 { log.SendDate = DateTime.Now; if (min > 0) { CmdQueue queue = new CmdQueue(); queue.APIDeviceId = d.APIDeviceId; queue.Model = d.APIDeviceModel.Value; queue.CmdCode = cmdCode; queue.Created = DateTime.Now; queue.LastExecTime = DateTime.Now; queue.Param = param; queue.ExceTime = min; queue.IMEI = d.Imei; queue.Notice = AuthUser.LoginName; CmdLogic.SaveCmdQueue(queue); SaveUserLog(AuthUser.LoginName + "设置了设备" + d.Imei + cmd.CmdName + ":" + min + "分钟", LogLevel.Info, AuthUser.LoginName, "AjaxSendCmd", "下发指令"); } else { CmdLogic.DelCmdQueue(cmdCode, d.APIDeviceId.Value); } log.CmdName = cmd.CmdName + ":" + min + "分钟"; res.State = State.Success; res.Message = "设置成功," + min + "分钟"; //return Json(res); } else if (d != null) { log.SendDate = DateTime.Now; SendCmdModel model = DeviceData.SendCmd(cmdCode, d.APIDeviceId ?? 0, d.APIDeviceModel ?? 0, CacheHelper.GetToken(d.UserId), param); if (model.State != 0) { res.State = State.Falid; res.Message = model.Content + ":" + model.Message; SaveUserLog(AuthUser.LoginName + "下发设备" + d.Imei + "的" + cmd.CmdName + "指令失败:" + res.Message, LogLevel.Sensitive, AuthUser.LoginName, "AjaxSendCmd", "下发指令"); } else { log.IsSucess = true; res.State = State.Success; res.Message = model.Content; SaveUserLog(AuthUser.LoginName + "下发设备" + d.Imei + "的" + cmd.CmdName + "指令:" + res.Message, LogLevel.Info, AuthUser.LoginName, "AjaxSendCmd", "下发指令"); //WriteLog( + res.Message); } log.Response = model.State + "|" + res.Message; log.ResponseTime = DateTime.Now; } else { res.State = State.NotFind; res.Message = "设备不存在"; WriteLog(AuthUser.LoginName + "下发设备" + d.Imei + "的" + cmd + "指令失败:" + res.Message); } //保存指令下发记录 CmdLogic.SaveCmdLog(log); if (res.Message.StartsWith("Off")) { res.Message = "设备不线,指令离线下发"; } return(Json(res)); }