public async Task <IotContext> Send(IotRequest request) { if (!Client.IsConnected) { await Open(); } var ctx = new IotContext(request); request.Tenant = this.tenant.Name; if (!actions.TryAdd(request.Mac, ctx)) { throw new AceException("不能频繁发送操作请求!"); } logger.LogDebug($"IotClient-SEND: {request.Tenant}-{request.Mac}-{request.Cmd} {request.Body}"); await ctx.Send(Client); ctx.Wait.WaitOne(access.CmdTimeout); if (ctx.Response != null) { return(ctx); } actions.TryRemove(request.Mac, out ctx); throw new AceException("操作超时,请检查网络是否连接!"); }
private async void OnReceived(IotRequest res) { logger.LogDebug($"IotClient-RECE: {res.Tenant}-{res.Mac}-{res.Cmd} {res.Body}"); try { var cmd = res.Cmd.Split('-')[0]; switch (cmd) { case "BACK": if (actions.TryRemove(res.Mac, out IotContext ctx)) { ctx.Response = res; ctx.Wait.Set(); } break; case "LOGIN": await iotService.Login(res.Mac, res.Body); break; case "LOGOUT": await iotService.Logout(res.Mac); break; case "UPLOAD": await iotService.Upload(res.Mac, res.Body); break; case "ONLINE": await iotService.Online(res.Mac); break; default: break; } } catch (Exception ex) { logger.LogError(ex, $"IotClient-ERRR: {res.Tenant}-{res.Mac}-{res.Cmd} {res.Body} {ex.GetMessage()}"); } finally { if (iotService.NeedCloseSession) { iotService.Session.Dispose(); } if (cacheService.NeedCloseSession) { cacheService.Session.Dispose(); } } }
public IotContext(IotRequest request) { this.Request = request; }