public async Task<IActionResult> ExecCmd(IotRequest request) { var opt = new IoT_OptLog { DCreate = request.DCreate, Mac = request.Mac, Cmd = request.Cmd, Body = request.Body, User_Id = AppCtx.AC.User.Id }; opt.InitializeId(); try { // check opt var device = cacheService.GetDevice(request.Mac); opt.Sbno = device.Sbno; Check.Require(device.Cmds.ContainsKey(request.Cmd), $"该设备({device.Cpno})不支持操作:{request.Cmd}"); var cmd = device.Cmds[request.Cmd]; opt.Name = cmd.Name; var ctx = await client.Send(request); var result = ctx.Response.Body; switch (result.Substring(0, 2)) { case "00": opt.Result = ctx.Response.Body; opt.Success = true; if (result.Length > 2) { return Ok(result.Substring(2)); } return Ok(null); case "01": throw new AceException("校验错误"); case "02": throw new AceException("会话错误"); case "03": throw new AceException("非法设备"); default: throw new AceException(result.Substring(2)); } } catch (Exception ex) { opt.Result = ex.GetMessage(); throw; } finally { opt.DUpdate = DateTime.Now; iotService.LogOpt(opt); } }
public void LogOpt(IoT_OptLog opt) { if (cacheService.Get("enable_optlog", false)) { Session.Insert(opt); } }