/// <summary> /// 8145关闭设备 8;8135;设备id /// user:123_DCD9165057AD type:other msg:123_DCD9165057AD;8;8145;01120925117040;3,0$/r$ /// 8135打开设备 8;8135;设备id /// user:123_DCD9165057AD type:other msg:123_DCD9165057AD;8;8135;01120925117040;2;8$/r$ /// user:123_Server type:other msg:123_e0ddc0a405d9;8;8135;A$/r$ /// {"code":1002,"id":"010000124b0014c6aaee","ep":1,"serial":1,"control":{"on":true},"result":0,"zigbee":"00ff2c2c2c6a6f005979"} /// user:DAJCHSF_% type:other msg:DAJCHSF_Server;devrefresh;1041656180510,true,DAJCHSF_2047DABEF936$/r$ /// /// user:MMSJ-1#1-5-501 type:other msg:MMSJ-1-1-5-501;8;8145;08$/r$ /// /// </summary> /// <param name="msg"></param> public static bool DeviceStateChange(string msg)//, string code, bool state, string success, out string relayUser { try { string appUser = msg.Split(';')[0]; string code = msg.Split(';')[2]; string deviceId = msg.Split(';')[3].Replace("$/r$", "");//新app设备id为最后一位 bool state; if (code == "8135") { state = true; } else if (code == "8145") { state = false; } else { log.Debug($"{msg}code指令不对返回失败!"); return(false);//指令不对返回失败 } using (RedisHashService service = new RedisHashService()) { string deviceEntityStr = service.GetValueFromHash("DeviceEntity", deviceId);//8231有关联,改mac的情况下,其它改状态,改名称,不需要清理 host_device deviceEntity = null; if (!string.IsNullOrEmpty(deviceEntityStr)) { deviceEntity = JsonConvert.DeserializeObject <host_device>(deviceEntityStr);//设备实体缓存 } else { using (HsfDBContext hsfDBContext = new HsfDBContext()) { deviceEntity = hsfDBContext.host_device.Where(t => t.deviceid == deviceId && t.deletemark == 0).FirstOrDefault();//注意device的唯一性 if (deviceEntity != null) { //缓存设备id与设备实体对应关系,避免查询数据库 service.SetEntryInHash("DeviceEntity", deviceId, JsonConvert.SerializeObject(deviceEntity)); } } } if (deviceEntity != null) { //拼装1002指令,发送给网关,执行改变状态操作 return(ChangeStateMain.StateChangeByType(deviceEntity, state)); } else { //relayUser = appUser; return(false);//error } } } catch (Exception) { throw; } }
/// <summary> /// 处理Nlp请求req /// </summary> /// <param name="msg"> ///1.二楼全开 ///connect user:Nlp_Server type:home msg:Nlp_Server ///connect user:Nlp_Server type:other msg:123_25995;5;513;5omT5byA5byA5YWz$/r$ /// ///connect user:Nlp_Server type:other msg:123_17920;5;513;5omT5byA6ZiB5qW8$/r$ ///123_17920;513;5omT5byA6ZiB5qW8;Zip;H4sIAAAAAAAEAAEYAOf/5omT5byA77ya6ZiB5qW844CC5aSx6LSlgj8c4BgAAAA=$/r$ /// ///2.返回结果 开启所有灯光、音乐、窗帘4 ///connect user:25995_ac83f317b8c7 type:other msg:25995_ac83f317b8c7;513;5LqM5qW85YWo5byA;Zip;H4sIAAAAAAAAAHu6p+HphPXPOhuezel83rj+aWvn44bGl/M3P9k5Ach4vmr60x0zTAAnYIhxJQAAAA==$/r$ ///25995_123;513;5omT5byA5byA5YWz;Zip;H4sIAAAAAAAEAHvWOfnpnob3e2Y9m7nrRfNeIPtp62YTk8cNTcgCpqZoAmZmaAIGBmgChoZoAkZGaALGxmgCieh8dIEkdD66QDI6H10gBY2fisZPQ+OnA/lPl2x8sWUpAHyIeM8pAQAA$/r$ /// </param> public static string HostNlpRequest(string msg) { try { if (msg.Split(';').Length >= 3) { string session_account = msg.Split(';')[0]; if (session_account.Contains("_")) { string account = session_account.Split('_')[1]; string req = msg.Split(';')[3].Replace("$/r$", ""); string deviceStr = EncryptionHelp.Decrypt(req, false);//解码无zip string code = ""; string msgResult = ""; string actionStr = ""; bool state = false; if (deviceStr.Contains("打开")) { actionStr = "打开"; code = "8135"; deviceStr = deviceStr.Replace("打开", ""); state = true; } else if (deviceStr.Contains("关闭")) { actionStr = "关闭"; code = "8145"; deviceStr = deviceStr.Replace("关闭", ""); state = false; } if (!string.IsNullOrEmpty(code)) { using (RedisHashService service = new RedisHashService()) { List <host_device> deviceList = null; //获取当前房间的设备列表,先找缓存 string devices = service.GetValueFromHash("AccountDevices", account); if (!string.IsNullOrEmpty(devices)) { deviceList = JsonConvert.DeserializeObject <List <host_device> >(devices);//list多件开关,ALL数组 } else { using (HsfDBContext hsfDBContext = new HsfDBContext()) { deviceList = hsfDBContext.host_device.Where(t => t.account == account && t.deletemark == 0).ToList(); service.SetEntryInHash("AccountDevices", account, JsonConvert.SerializeObject(deviceList)); } } if (deviceList.Count() != 0) { var deviceControl = deviceList.Where(t => t.chinaname.Contains(deviceStr)).ToList();//包含“开关”名称的所有设备 if (deviceControl.Count() != 0) { string okDevices = ""; string failDevices = ""; foreach (var item in deviceControl) { //发送指令给网关,改变状态,避免两次查库 //DeviceStateChange($"{session_account};8;{code};{item.deviceid};$/r$") if (ChangeStateMain.StateChangeByType(item, state)) { okDevices += item.chinaname + "。"; } else { failDevices += item.chinaname + "。"; } } string resultDevices = ""; if (!string.IsNullOrEmpty(okDevices)) { resultDevices += $"已经为您{actionStr}:{okDevices}"; } if (!string.IsNullOrEmpty(failDevices)) { resultDevices += $"{actionStr}:{failDevices}失败"; } msgResult = $"{session_account};513;{req};Zip;{EncryptionHelp.Encryption(resultDevices, true)}$/r$"; log.Debug($"{resultDevices}! cachekey:{msgResult}"); return(msgResult); } else { log.Debug($"不存在该设备! {req}"); return(null); } } else { log.Debug($"不存在该账户的设备列表! {msg}"); return(null); } } } else { log.Debug($"code不符合规范! {msg}"); return(null); } } else { log.Debug($"命令不符合规范! {msg}"); return(null); } } else { log.Debug($"命令不符合规范! {msg}"); return(null); } } catch (Exception) { throw; } }