/// <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> /// 添加新的设备数据库 /// </summary> /// <param name="hsfDBContext"></param> /// <param name="_SoundHost"></param> public static void AddDevice(HsfDBContext hsfDBContext, host_device item) { item.id = Guid.NewGuid().ToString(); item.createtime = DateTime.Now; item.deletemark = 0; item.cachekey = item.devmac + "_" + item.devport; hsfDBContext.host_device.Add(item); hsfDBContext.SaveChanges(); }
/// <summary> /// 添加新的设备数据库 /// </summary> /// <param name="hsfDBContext"></param> /// <param name="_SoundHost"></param> public static void AddDeviceEntity(HsfDBContext hsfDBContext, host_device item, string appUser, string account, string mac) { item.createuser = appUser; item.account = account; item.mac = mac; item.Id = Guid.NewGuid().ToString(); item.createtime = DateTime.Now; item.deletemark = 0; item.cachekey = item.devmac + "_" + item.devport; hsfDBContext.host_device.Add(item); hsfDBContext.SaveChanges(); }
/// <summary> /// 获得总线网关要添加的设备信息sendsocket(8, "8;876;"+zhujitype+";"+devmac);//按一下总线开关,缓存总线开关mac地址,点击总线网关,返回当前要添加的几键开关 /// </summary> /// <param name="msg">1.查询总线网关下的开关connect user:123_Server type:other msg:123_e0ddc0a405d9;8;876;Safe_Center;58.57.32.162$/r$ /// 2.返回结果 123_e0ddc0a405d9;876;all;Zip;H4sIAAAAAAAEAGWQyw6CMBRE/6VrQ3gENLI38RuMi5tykRv6SlswxPjvoryqLufMTDvt5cHOFTuqTogdq7AnjrRq3pACBRJXIMC5msKCH8xoszhjU98EngQ+WvkhyvdRlkZJkZanpIyLKWq09evBwBtscQjKRjvypFWAeANKoVgISbgFazuHdlNG33sQHQZ158H/6eQXpAsAznWn1omfx8xrLY7B933fxNP2VVJXVNMQhmYUpioU6FGCbSfyvL4AcWAUMpEBAAA=$/r$ /// </param> public static string Host876(string msg) { try { if (msg.Split(';').Length >= 4) { using (RedisStringService service = new RedisStringService()) { string appUser = msg.Split(';')[0]; string account = appUser.Split('_')[0]; string devmac = msg.Split(';')[4].Replace("$/r$", ""); //本地外网ip string BusSwitchMac = service.Get("BusSwitch_" + devmac); //先提前按一下,瑞瀛网关服务器缓存开关mac地址,两分钟 if (!string.IsNullOrEmpty(BusSwitchMac)) { BusSwitchMac = BusSwitchMac.Replace("\"", ""); string[] macs = BusSwitchMac.Split(';'); List <host_device> deviceList = new List <host_device>(); foreach (var item in macs) { host_device host_Device = new host_device()//可能是个数组,有多个开关同时按的情况 { devmac = devmac + ";F1;" + BusSwitchMac, devtype = "03", devport = "0" }; deviceList.Add(host_Device); } string zipStr = EncryptionHelp.Encryption(JsonConvert.SerializeObject(deviceList), true); string msgResult = $"{appUser};876;all;Zip;{zipStr}$/r$";//拼接 log.Debug($"876 OK,返回设备类型的设备列表成功!返回信息:{msgResult}"); return(msgResult); } return(null); } } else { log.Debug($"855 Fail,命令不符合规范!"); return(null); } } catch (Exception) { throw; } }
/// <summary> /// 瑞瀛网关开关指令发送 /// </summary> /// <param name="deviceEntity"></param> /// <param name="state"></param> /// <returns></returns> public static bool RYZigSendMsg(host_device deviceEntity, bool state) { string devmac = deviceEntity.devmac; //192.168.1.55;f1;50000 string ip = devmac.Split(';')[0]; //192.168.1.55 string ff = devmac.Split(';')[1]; //f1 string mac = devmac.Split(';')[2]; //06 string port = deviceEntity.devport; //0;1,0;1,0;3,自定义添加的时候做了+1,还多了分号 int iport = int.Parse(port.Replace(";", "")) - 1; //去分号,减1 string iportStr = "0" + iport; string st = state ? "01" : "00"; string cachekey = deviceEntity.cachekey;//缓存处理-- //发送到zigbee网关并遍历状态 if (!string.IsNullOrEmpty(st)) { //cc dd 开头固定 //f1 第一个485通道 //06 物理地址 //05 00 08 //00 继电器 //01 为打开继电器 //8D C8 为crc校验码 string msg1 = $"{mac} 05 00 08 {iportStr} {st}"; string crc = EncryptionHelp.CRCCalc(msg1); //crc校验 string msg = $"cc dd {ff} {msg1} {crc}"; string ipmsg = msg + "|" + devmac; //ccddf10605000800018c7f|192.168.82.107;f1;06//普通字符串+16进制字符串 string mac16 = RYZigClient.ToHex(ipmsg, "utf-8", false); RYZigClient.Send(mac16); //发送到网关服务器YunZig log.Debug($"向RYZig网关发送指令{ipmsg},确认器开始遍历状态是否改变 {state.ToString()}"); if (StateResult(cachekey, state.ToString())) { return(true); } else { log.Debug($"设备状态改变超时失败! cachekey:{cachekey}!"); return(false); } } else { log.Debug($"设备状态改变失败!cachekey:{cachekey}! 不存在设备类型:{deviceEntity.devtype}"); return(false); } }
/// <summary> /// zigbee网关发送 /// 拼装1002指令,发送给网关,执行改变状态操作 /// 1.Zigbee窗帘(Smart_ZigbeeCurtain):pt控制窗帘开度百分比,0为全关闭,100为全打开 /// 速度:打开窗帘230、4289。关闭窗帘229、3599 等104速度太慢,存在关闭的时候104关闭不充分,打开13,关闭86 /// {"code":1002,"id":"010000124b000f81eea6","ep":8,"serial":1,"control":{"on":false,"pt":0},"result":0,"zigbee":"00ff2c2c2c6a6f0057f3"}//关闭 /// 2.Zigbee开关(Panel_Zigbee):on /// 速度:打开开关239、392。关闭开关228、411 /// {"code":1002,"id":"010000124b0014c5d116","ep":1,"serial":1,"control":{"on":true},"result":0,"zigbee":"00ff2c2c2c6a6f0057f3"} /// </summary> /// <param name="deviceEntity"></param> /// <param name="state"></param> /// <returns></returns> public static bool YunZigSendMsg(host_device deviceEntity, object obj, bool state) { string cachekey = deviceEntity.cachekey;//缓存处理-- //发送到zigbee网关并遍历状态 if (obj != null) { Zigbee1002 zigbee1002 = new Zigbee1002() { code = 1002, id = deviceEntity.devmac, //mac地址,010000124b0014c6aaee ep = Convert.ToInt32(deviceEntity.devport), //端口 serial = 1, control = obj, result = 0, zigbee = deviceEntity.devchannel//网关,00ff2c2c2c6a6f005979 }; string _1002 = JsonConvert.SerializeObject(zigbee1002); YunZigClient.SendMsg(_1002);//发送到网关服务器YunZig log.Debug($"向YunZig网关发送1002指令{_1002},确认器开始遍历状态是否改变{cachekey}"); if (StateResult(cachekey, state.ToString())) { //返回广播消息,直接开始广播 //user:hiddenpath_% type:other msg:hiddenpath_Server;devrefresh;924150429051,false,hiddenpath_ASDFDSSE123$/r$ //relayUser = appUser.Split('_')[0] + "_%"; //relayUser = appUser; //return $"{appUser};{code};{device};Zip;{success}$/r$";//ok//closeok@808 181248576 //return $"{relayUser};devrefresh;{device},{state},{appUser}$/r$";//后面的发给前面的,与请求的对调一下 return(true); } else { log.Debug($"设备状态改变超时失败! cachekey:{cachekey}!"); //relayUser = appUser; //return $"{appUser};{code};{device};Zip;H4sIAAAAAAAEAEstKsovAgBxvN1dBQAAAA==$/r$";//error return(false); } } else { log.Debug($"设备状态改变失败!cachekey:{cachekey}! 不存在设备类型:{deviceEntity.devtype}"); return(false); } }
/// <summary> /// 状态改变具体执行方法 /// </summary> /// <param name="deviceEntity">设备实体</param> /// <param name="state">要改变成什么状态</param> /// <returns></returns> public static bool StateChangeByType(host_device deviceEntity, bool state) { object obj = null; switch (deviceEntity.devtype) { case "Smart_ZigbeeCurtain": //zigbee窗帘 obj = new { pt = state ? 100 : 0 }; return(YunZigSendMsg(deviceEntity, obj, state)); case "Panel_Zigbee": //zigbee开关 obj = new { on = state }; return(YunZigSendMsg(deviceEntity, obj, state)); default: return(false); } }
/// <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="state"></param> /// <param name="cachekey"></param> public static bool DeviceStateChange(string cachekey, bool state)//, string code, bool state, string success, out string relayUser { try { using (RedisHashService service = new RedisHashService()) { string deviceEntityStr = service.GetValueFromHash("DeviceMacEntity", cachekey);//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.cachekey == cachekey && t.deletemark == 0).FirstOrDefault();//注意device的唯一性 if (deviceEntity != null) { //缓存设备id与设备实体对应关系,避免查询数据库 service.SetEntryInHash("DeviceMacEntity", cachekey, JsonConvert.SerializeObject(deviceEntity)); } } } if (deviceEntity != null) { //拼装1002指令,发送给网关,执行改变状态操作 return(StateChangeByType(deviceEntity, state)); } else { //relayUser = appUser; return(false);//error } } } catch (Exception) { throw; } }
/// <summary> /// zigbee网关发送 /// 拼装1002指令,发送给网关,执行改变状态操作 /// 1.Zigbee窗帘(Smart_ZigbeeCurtain):pt控制窗帘开度百分比,0为全关闭,100为全打开 /// 速度:打开窗帘230、4289。关闭窗帘229、3599 等104速度太慢,存在关闭的时候104关闭不充分,打开13,关闭86 /// {"code":1002,"id":"010000124b000f81eea6","ep":8,"serial":1,"control":{"on":false,"pt":0},"result":0,"zigbee":"00ff2c2c2c6a6f0057f3"}//关闭 /// 2.Zigbee开关(Panel_Zigbee):on /// 速度:打开开关239、392。关闭开关228、411 /// {"code":1002,"id":"010000124b0014c5d116","ep":1,"serial":1,"control":{"on":true},"result":0,"zigbee":"00ff2c2c2c6a6f0057f3"} /// </summary> /// <param name="deviceEntity"></param> /// <param name="state"></param> /// <returns></returns> public static bool YunZigSendMsg(host_device deviceEntity, object obj, bool state) { string cachekey = deviceEntity.cachekey;//缓存处理-- //发送到zigbee网关并遍历状态 if (obj != null) { Zigbee1002 zigbee1002 = new Zigbee1002() { code = 1002, id = deviceEntity.devmac, //mac地址,010000124b0014c6aaee ep = Convert.ToInt32(deviceEntity.devport), //端口 serial = 1, control = obj, result = 0 //zigbee = deviceEntity.devchannel//网关,00ff2c2c2c6a6f005979 }; string _1002 = JsonConvert.SerializeObject(zigbee1002); if (CloudHostServer.Gateway_SessionDic.ContainsKey(deviceEntity.devchannel)) { CloudHostServer.Gateway_SessionDic[deviceEntity.devchannel].Send(_1002); return(true); } else { log.Debug($"请求网关的session不存在 {deviceEntity.devchannel}: {_1002}"); return(false); } } else { log.Debug($"设备状态改变失败!cachekey:{cachekey}! 不存在设备类型:{deviceEntity.devtype}"); return(false); } }