private void MyServer_NewRequestReceived(HansAppSession session, HansRequestInfo requestInfo) { Task.Run(() => { string msg = requestInfo.Body.StartMark + requestInfo.Body.BodyString + requestInfo.Body.EndMark;//encoding.GetString(requestInfo.Body.BodyBuffer); string[] sArray = Regex.Split(msg, "}{", RegexOptions.IgnoreCase); for (int i = 0; i < sArray.Length; i++) { if (!sArray[i].StartsWith("{")) { sArray[i] = "{" + sArray[i]; } int _left = Regex.Matches(sArray[i], "{").Count; int _right = Regex.Matches(sArray[i], "}").Count; int cha = _left - _right; for (int b = 0; b < cha; b++) { sArray[i] = sArray[i] + "}"; } var res = DynamicJson.Parse(sArray[i]); if (res.IsDefined("code")) { double code = res.code; if (code == 101) { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); session.Send("{\"code\":1001,\"res\":0,\"timestamp\": " + Convert.ToInt64(ts.TotalSeconds) + "}"); break; } if (code == 010) { session.Send("alive"); break; } else { log.Debug($"收到消息: {msg}"); switch (code) { //102为什么不可以?手动操作的时候只有104,没有102 case 102: //{"code":102,"id":"010000124b000f81eea6","ep":8,"serial":1,"control":{"on":true,"pt":100},"result":0} if (res.IsDefined("result")) { string state = ""; //离线为"" using (RedisHashService service = new RedisHashService()) { if (res.result == 0) //成功0,失败3离线 { if (res.IsDefined("control")) //可能有不存在on的 { var control = res.control; if (control.IsDefined("on")) { state = control.on.ToString(); } else if (control.IsDefined("pt")) { state = Convert.ToInt32(control.pt) > 0 ? "True" : "False"; //窗帘开度,存在关闭的时候104关闭不充分,打开13,关闭86 } else { state = "False"; //在线没有st,但是没有开关状态的情况,默认关闭 } } else { state = "False"; //在线没有st,但是没有开关状态的情况,默认关闭 } } string cachekey = res.id + "_" + res.ep; //发布YunZigStateChangeQueue状态同步消息 PutMqttData(service, cachekey, state); } } break; case 104: //手动,app无法区分{"code":104,"control":2,"id":"010000124b0014c6aaee","ol":true,"ep":1,"pid":260,"did":0,"st":{"on":false}} if (res.IsDefined("control")) { if (res.control == 2) { string state = ""; //离线为"" using (RedisHashService service = new RedisHashService()) { if (res.ol == true) //在线 { if (res.IsDefined("st")) //可能有不存在on的 { var st = res.st; if (st.IsDefined("on")) { state = st.on.ToString(); } else if (st.IsDefined("pt")) { state = Convert.ToInt32(st.pt) > 0 ? "True" : "False"; //窗帘开度,存在关闭的时候104关闭不充分,打开13,关闭86 } else { state = "False"; //在线没有on,但是没有开关状态的情况,默认关闭 } } else { state = "False"; //在线没有st,但是没有开关状态的情况,默认关闭 } } string cachekey = res.id + "_" + res.ep; //发布YunZigStateChangeQueue状态同步消息 PutMqttData(service, cachekey, state); } } } break; case 1002: //改变设备状态 case 5001: //查询某个设备状态 if (res.IsDefined("zigbee")) { if (Gateway_SessionDic.ContainsKey(res.zigbee)) { Gateway_SessionDic[res.zigbee].Send(sArray[i]); } else { log.Debug($"请求网关的session不存在 {res.zigbee}: {msg}"); } } break; case 501: using (RedisHashService service = new RedisHashService()) { foreach (var item in res.device) //多键开关 { string state = ""; //离线为"" if (item.ol == true) //在线 { if (item.IsDefined("st")) //可能有不存在on的 { var st = item.st; if (st.IsDefined("on")) { state = st.on.ToString(); } else if (st.IsDefined("pt")) { state = Convert.ToInt32(st.pt) > 0 ? "True" : "False"; //窗帘开度,存在关闭的时候104关闭不充分,打开13,关闭86 } else { state = "False"; //在线没有on,但是没有开关状态的情况,默认关闭 } } else { state = "False"; //在线没有on,但是没有开关状态的情况,默认关闭 } } service.SetEntryInHash("DeviceStatus", res.id + "_" + res.ep, state); } } break; default: break; } } } else if (res.IsDefined("mac") && res.IsDefined("id")) { //缓存当前socket连接,初次连接心跳之前会收到网关注册信息{"id":"00ff2c2c2c6a6f005979","mac":"2c:6a:6f:00:59:79"} //Func<string, HansAppSession, HansAppSession> dd = (key,oldValue)=>session; //当网关重新连接的时候,ip换了的时候,替换之前的session Gateway_SessionDic.AddOrUpdate(res.id, session, (Func <string, HansAppSession, HansAppSession>)((key, oldValue) => session)); using (RedisHashService service = new RedisHashService()) { service.SetEntryInHash("DeviceStatus", res.id + "_13579", "True");//mac_port, //网关会自动上传104所有设备状态 } log.Debug($"收到网关注册消息:缓存session: {session.RemoteEndPoint} ,网关状态信息: {msg}"); Console.WriteLine($"连接网关Ip: {session.RemoteEndPoint} Mac: {res.mac} {DateTime.Now.ToString()}"); } else if (res.IsDefined("lot")) { SmartHome_Session = session; log.Debug($"收到Lot长连接session: {session.RemoteEndPoint} 信息: {msg}"); } else { log.Info($"{session.RemoteEndPoint.ToString()} 未识别信息: {sArray[i]}"); } } }); }
private void MyServer_NewRequestReceived(HansAppSession session, HansRequestInfo requestInfo) { Task.Run(() => { string msg = requestInfo.Body.StartMark + requestInfo.Body.BodyString + requestInfo.Body.EndMark;//encoding.GetString(requestInfo.Body.BodyBuffer); string[] sArray = Regex.Split(msg, "}{", RegexOptions.IgnoreCase); for (int i = 0; i < sArray.Length; i++) { if (!sArray[i].StartsWith("{")) { sArray[i] = "{" + sArray[i]; } int _left = Regex.Matches(sArray[i], "{").Count; int _right = Regex.Matches(sArray[i], "}").Count; int cha = _left - _right; for (int b = 0; b < cha; b++) { sArray[i] = sArray[i] + "}"; } var res = DynamicJson.Parse(sArray[i]); if (res.IsDefined("code")) { double code = res.code; if (code == 101) { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); session.Send("{\"code\":1001,\"res\":0,\"timestamp\": " + Convert.ToInt64(ts.TotalSeconds) + "}"); break; } if (code == 010) { session.Send("alive"); break; } else { log.Debug($"收到消息: {msg}"); switch (code) { case 104: //{"code":104,"control":2,"id":"010000124b0014c6aaee","ol":true,"ep":1,"pid":260,"did":0,"st":{"on":false}} if (res.IsDefined("control")) { if (res.control == 2) { //缓存设备状态 if (res.ol == true) //在线 { if (res.IsDefined("st")) //可能有不存在on的 { var st = res.st; if (st.IsDefined("on")) { service.HashSet <string>("DeviceStatus", res.id + "_" + res.ep, st.on.ToString()); //mac_port, } else { //在线没有on,但是没有开关状态的情况,默认关闭 service.HashSet <string>("DeviceStatus", res.id + "_" + res.ep, "False"); //mac_port, } } else { //在线没有st,但是没有开关状态的情况,默认关闭 service.HashSet <string>("DeviceStatus", res.id + "_" + res.ep, "False"); //mac_port, } } else { service.HashSet <string>("DeviceStatus", res.id + "_" + res.ep, ""); //离线为"" } } } break; case 1002: if (res.IsDefined("zigbee")) { Gateway_SessionDic[res.zigbee].Send(sArray[i]); } else { log.Debug($"请求网关的session不存在 {res.zigbee}: {msg}"); } break; case 5001: if (res.IsDefined("zigbee")) { if (Gateway_SessionDic.ContainsKey(res.zigbee)) { Gateway_SessionDic[res.zigbee].Send(sArray[i]); } else { log.Debug($"请求网关的session不存在 {res.zigbee}: {msg}"); } } break; case 501: foreach (var item in res.device) { //if (item.ol != false && item.st.on != null) //{ // service.HashSet<string>("DeviceStatus", item.id + "_" + item.ep, item.st.on.ToString());//mac_port, //} //else //{ // service.HashSet<string>("DeviceStatus", item.id + "_" + item.ep, "");//离线为"" //} if (item.ol == true) //在线 { if (item.IsDefined("st")) //可能有不存在on的 { var st = item.st; if (st.IsDefined("on")) { service.HashSet <string>("DeviceStatus", item.id + "_" + item.ep, st.on.ToString()); //mac_port, } else { //在线没有on,但是没有开关状态的情况,默认关闭 service.HashSet <string>("DeviceStatus", item.id + "_" + item.ep, "False"); //mac_port, } } else { //在线没有st service.HashSet <string>("DeviceStatus", item.id + "_" + item.ep, "False"); //mac_port, } } else { service.HashSet <string>("DeviceStatus", item.id + "_" + item.ep, ""); //离线为"" } } break; default: break; } } } else if (res.IsDefined("mac") && res.IsDefined("id")) { //缓存当前socket连接,初次连接心跳之前会收到网关注册信息{"id":"00ff2c2c2c6a6f005979","mac":"2c:6a:6f:00:59:79"} //Func<string, HansAppSession, HansAppSession> dd = (key,oldValue)=>session; //当网关重新连接的时候,ip换了的时候,替换之前的session Gateway_SessionDic.AddOrUpdate(res.id, session, (Func <string, HansAppSession, HansAppSession>)((key, oldValue) => session)); service.HashSet <string>("DeviceStatus", res.id + "_13579", "True"); //mac_port, //网关会自动上传104所有设备状态 log.Debug($"收到消息缓存session: {session.RemoteEndPoint} ,网关状态信息: {msg}"); } else if (res.IsDefined("lot")) { SmartHome_Session = session; log.Debug($"收到Lot长连接session: {session.RemoteEndPoint} 信息: {msg}"); } else { log.Info($"{session.RemoteEndPoint.ToString()} 未识别信息: {sArray[i]}"); } } }); }