/// <summary> /// 校验时间的方法 /// </summary> /// <param name="socket"></param> /// <param name="info"></param> /// <param name="bys"></param> private void checkTimeMethod(Socket socket, MacIPVersionInfo info, byte[] bys) { //Task.Factory.StartNew(() => //{ //}); var time = TianMao.GetCRC("55ff0e" + StrHelper.GetCheckTimeStr()); var check_time_msg = StrHelper.strToHexByte(time); try { socket.Send(check_time_msg); } catch (Exception ex) { Console.WriteLine("tcp 462" + ex.Message); var ipe = (IPEndPoint)socket.RemoteEndPoint; var address = ipe.Address.ToString(); var port = ipe.Port; Program.removeOnlineTcpRelation(address, port); Func <bool> func1 = () => true; var msgx = "tcp 462" + ex.Message; LogHelper.LogFilter(func1, msgx); } }
private void MIDDLENUANTONG(Socket socket, MacIPVersionInfo info, byte[] bys) { var list = info.listcmd; var mac = info.mac; if (!string.IsNullOrEmpty(mac)) { var macInfo = Program.GetRelation(mac); if (macInfo != null && info.listcmd != null && info.listcmd.Count > 0) { var dev = macInfo.device; if (dev != null) { foreach (var str in info.listcmd) { try { dev.Send(Encoding.UTF8.GetBytes(str)); Thread.Sleep(500); } catch { } } } } } }
/// <summary> /// 设备重启 /// </summary> /// <param name="socket"></param> /// <param name="info"></param> /// <param name="bys"></param> private void DEVICE_RESTART(Socket socket, MacIPVersionInfo info, byte[] bys) { var msg = StrHelper.GetHexStr(bys).Replace(" ", ""); //0x55 + 0xFF + 0x13 + mac(6B) + CRC if (msg.Length == 20) { var mac = msg.Substring(6, 12); LogHelper.LogFilter(true, $"设备重启=>{mac},{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},{msg}"); } }
/// <summary> /// 汉枫 /// </summary> /// <param name="socket"></param> /// <param name="info"></param> /// <param name="bys"></param> private void HanFengMethod(Socket socket, MacIPVersionInfo info, byte[] bys) { var ipEnd = (IPEndPoint)socket.RemoteEndPoint; var mac = info.mac; if (!string.IsNullOrEmpty(mac)) { Program.upgradeDevice(mac, socket); } var heartBeatKey = "HeartBeat:" + ipEnd.Address.ToString() + "" + ipEnd.Port; if (RedisHelper <string> .IsKeyExist(heartBeatKey) == false) { var task = new TaskFactory(); task.StartNew(() => { var ip = info.IP; var orgText = StrHelper.GetHexStr(bys); if (!string.IsNullOrEmpty(mac) && (mac.Equals("98d8634a034e") || mac.Equals("98d8634a0344") || mac.Equals("98d8638c7fa2"))) { LogHelper.LogFilter(true, $"心跳包=>{info.mac},{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } DALMacIPInfo.MacIPInfo_Add(new MacIPInfo() { mac = info.mac, IP = ipEnd.Address.ToString(), localip = ip, ssid = "", OrgText = orgText }); }); RedisHelper <string> .StoreOneKey(heartBeatKey, "1", heartbeatTime); } }
/// <summary> /// 忆林的 /// </summary> /// <param name="socket"></param> /// <param name="info"></param> /// <param name="bys"></param> private void YiLinMethod(Socket pox, MacIPVersionInfo info, byte[] bys) { var mac = info.mac; if (!string.IsNullOrEmpty(mac)) { Program.upgradeDevice(mac, pox); } //LogWriteLock.EnterWriteLock(); //var st = new SocketTime(); //st.socket = pox; //st.time = DateTime.Now; //socketMap[mac] = st; //LogWriteLock.ExitWriteLock(); var ipEnd = (IPEndPoint)pox.RemoteEndPoint; var heartBeatKey = "HeartBeat:" + ipEnd.Address.ToString() + "" + ipEnd.Port; var key = "Socket.Tcp.SetTime." + mac; #region 设置时间 if (!RedisHelper <string> .IsKeyExist(key)) { Task.Factory.StartNew(() => { try { ///60*60*24 RedisHelper <string> .StoreOneKey(key, "1", 86400); var set_time_str = "55ff00010091010110a1000a0461" + StrHelper.BackTime(); var set_time_bytes = CRC16.CRC_XModem(set_time_str); pox.Send(set_time_bytes); Thread.Sleep(50); } catch (SocketException se) { Func <bool> func1 = () => true; var msgx = "TCP:670:" + se.ErrorCode + " " + se.Message; LogHelper.LogFilter(func1, msgx); } }); } #endregion #region 设置心跳包 if (RedisHelper <string> .IsKeyExist(heartBeatKey) == false) { var task = new TaskFactory(); task.StartNew(() => { var orgText = StrHelper.GetHexStr(bys); if (!string.IsNullOrEmpty(mac) && (mac.Equals("98d8634a034e") || mac.Equals("98d8634a0344") || mac.Equals("98d8638c7fa2"))) { LogHelper.LogFilter(true, $"心跳包=>{mac},{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); } DALMacIPInfo.MacIPInfo_Add(new MacIPInfo() { mac = mac, IP = ipEnd.Address.ToString(), localip = "", ssid = "", OrgText = orgText }); }); ///缓存2分钟 RedisHelper <string> .StoreOneKey(heartBeatKey, "1", heartbeatTime); } else { } #endregion }
/// <summary> /// 报警方法 /// </summary> /// <param name="socket"></param> /// <param name="info"></param> /// <param name="bys"></param> private void BaoJinMethod(Socket socket, MacIPVersionInfo info, byte[] ds) { if (ds.Length == 11) { var id = ds.ToList().GetRange(4, 6).ToArray(); var hexIds = StrHelper.byteToHexStr(id); var id10 = Convert.ToInt32(hexIds, 16); var sta = DALDefenseStatus.DefenseStatusGetByDeviceId(id10); if ((sta == null) || (sta != null && sta.status == 1)) { ///警报事件 if (ds[3] == 4) { if (id10 > 0) { var device = DALYZKDeviceInfo_UserInfocs.YZKDeviceInfo_UserInfo_GetDataByRecordid(id10 + ""); if (device != null) { var device_mac = device.device_mac; DALNotifyException.NotifyExceptionAddX(new NotifyException() { bxkLabel = device.bxk_label, deviceId = device.deviceinfo_userinfo_id + "", deviceSetName = device.device_setname, recordTime = DateTime.Now, uid = device.uid, alarm_type = 0 }); var tokens = DALYZKPushToken.YZKPushToken_GetByUid(device.uid).FindAll(a => a.devicetype != 3); foreach (var token in tokens) { if (token != null) { if (token.devicetype == 0) { var devicetoken = token.devicetoken; if (!string.IsNullOrEmpty(devicetoken)) { var alert = ""; var category = ""; var ticker = ""; var title = "设备报警通知"; var text = string.Format("报警通知,您房间{0}的{1}设备发生报警,请留意", device.bxk_label, device.device_setname); var dt = new Dictionary <string, string> { { "type", "8" }, { "uid", device.uid } }; var pushData = YouMengHelper.makePush(alert, category, DeviceType.Android_Center, devicetoken, ticker, title, text, dt); } //DALNotifyException.NotifyExceptionGet(d.deviceinfo_userinfo_id + "", (int)AlarmType.DEFAULT); } else { var devicetoken = token.devicetoken; if (!string.IsNullOrEmpty(devicetoken)) { var alert = ""; var category = ""; var ticker = ""; var title = "设备报警通知"; var text = string.Format("报警通知,您房间{0}的{1}设备发生报警,请留意", device.bxk_label, device.device_setname); var dt = new Dictionary <string, string>(); dt.Add("type", "8"); dt.Add("uid", device.uid); var pushData = YouMengHelper.makePush(alert, category, DeviceType.YZK_IOS_APPStore, devicetoken, ticker, title, text, dt); } } } } } } } ///表明是需要短信通知的报警事件 else if (ds[3] == 5) { #region 短信通知报警设置 ///4-9 ///id if (id10 > 0) { var device = DALYZKDeviceInfo_UserInfocs.YZKDeviceInfo_UserInfo_GetDataByRecordid(id10 + ""); if (device != null) { var room = device.room_id; var home_id = device.home_id; var roomName = string.Empty; var uid = device.uid; if (!string.IsNullOrEmpty(uid)) { var users = DALYZKUser.YZKUser_GetByuuid(uid); if (users != null) { var device_setName = device.device_setname; var bxk_label = device.bxk_label.Replace("-", ""); //bxk_label就是xxx房间, 去掉bxk_label中的“_”, xxx设备是setname字段 //var msg = string.Format("报警通知,您{0}房间的{1}设备发生报警事件,请留意",bxk_label,device_setName); var mobile = users.mobile; var dfname = DALDefenseStatus.DefenseStatus_GetByHomeId(home_id); if (dfname != null && dfname.status == 1) { YZKMsgHelper.NotifyDanger(mobile, bxk_label, device_setName); ///记录异常信息 DALNotifyException.NotifyExceptionAdd(new NotifyException() { bxkLabel = bxk_label, deviceSetName = device_setName, uid = uid, deviceId = device.deviceinfo_userinfo_id + "" }); } } } } } #endregion } } } }
/// <summary> /// 命令返回 /// </summary> /// <param name="socket"></param> /// <param name="info"></param> /// <param name="bys"></param> private void defualtMethod(Socket socket, MacIPVersionInfo info, byte[] ds) { var ipe = (IPEndPoint)socket.RemoteEndPoint; var address = ipe.Address.ToString(); var port = ipe.Port; var mu = Program.GetRelation(address, port); if (mu != null) { #region 长度小于5时等待接下来的数据 if (ds.Length < 5) { var orgds = ds; var tmp = new byte[1024]; //Console.WriteLine("原先ds长度:" + ds.Length); ds = new byte[1024]; var thread = new Thread(() => { try { var len = socket.Receive(tmp, SocketFlags.None); if (len > 0) { var qu = new Queue <byte>(); if (orgds.Length == 3 && ((orgds[0] == 85 && orgds[1] == 170 && orgds[2] == 255) || (orgds[0] == 85 && orgds[1] == 170 && orgds[2] == 0))) { ///去掉前三个字节 } else { foreach (var q in orgds) { qu.Enqueue(q); } } for (var i = 0; i < len; i++) { qu.Enqueue(tmp[i]); } var index = 0; while (qu.Count > 0) { var q = qu.Dequeue(); if (index < 1023) { ds[index++] = q; } } ds = ds.ToList().GetRange(0, index).ToArray(); SendMsgToClient(ds, mu); } //var hex = StrHelper.GetHexStr(ds); //Console.WriteLine("后来ds长度:" + ds.Length + "字符串:" + hex); else if (len == 0) { Func <bool> func1 = () => true; var msgx = "再次获取的时候居然断开了。。。。"; LogHelper.LogFilter(func1, msgx); Program.removeOnlineTcpRelation(address, port); } } catch (SocketException sek) { Console.WriteLine(sek.Message); Program.removeOnlineTcpRelation(address, port); } }); thread.Start(); thread.Join(150); } #endregion else { SendMsgToClient(ds, mu); } } }
public static MacIPVersionInfo GetHearBeatInfo(byte[] msg) { HeartBeatType ht = GetMsgType(msg); var mo = new MacIPVersionInfo(); mo.heartBeatType = ht; if (ht == HeartBeatType.HANFEN) { var mac = string.Empty; mo.IP = GetHANFENIP(msg, out mac); mo.mac = mac; mo.version = ""; } else if (ht == HeartBeatType.YILIN) { var mac = string.Empty; mo.IP = ""; mo.mac = GetMacMsg(msg, HeartBeatType.YILIN); mo.version = ""; } else if (ht == HeartBeatType.DEFAULT) { var mac = string.Empty; mo.heartBeatType = ht; mo.IP = ""; mo.mac = mac; mo.version = ""; } else if (ht == HeartBeatType.CMD) { var mac = string.Empty; mo.heartBeatType = ht; mo.IP = ""; mo.mac = mac; mo.version = ""; } else if (ht == HeartBeatType.BUFANGCHEFANG) { var mac = string.Empty; mo.heartBeatType = ht; mo.IP = ""; mo.mac = mac; mo.version = ""; } else if (ht == HeartBeatType.CHECK_TIME) { var mac = string.Empty; mo.heartBeatType = ht; mo.IP = ""; mo.mac = mac; mo.version = ""; } else if (ht == HeartBeatType.BAOJING) { var mac = string.Empty; mo.heartBeatType = ht; mo.IP = ""; mo.mac = mac; mo.version = ""; } else if (ht == HeartBeatType.DEVICE_RESTART) { string mac = string.Empty; mo.heartBeatType = ht; mo.IP = ""; mo.mac = mac; mo.version = ""; } else if (ht == HeartBeatType.NUANTONG) { /* * 16) 暖通设备控制支持 * 目前暖通控制通过云端执行操作,格式如 * 0x55 + 0xFF + 0x11 + mac(6B) +tCount(1B) + [cCount(1B) + command]+ CRC * * tCount:表示有几个指令 * []:表示的是指令集,个数由tCount决定, 每个指令为指令长度+指令内容组成; * 云端收到操作后按照指令集依次发送到对应mac设备, 如mac设备离线忽略操作; */ mo.heartBeatType = ht; mo.IP = ""; mo.version = ""; var str = StrHelper.GetHexStr(msg).Replace(" ", ""); if (str.Length >= 20) { ///mac var mac = str.Substring(6, 12); LogHelper.LogFilter(true, "暖通==>" + str); var tcount = str.Substring(18, 2); var count = Convert.ToInt32(tcount, 16); //index=14 //55ff11 262040589573 01 07 0110660130fc9b //55ff11 262040589573 02 07 0110660130fc9b 78070110660130fc9b78 //55ff11 262040589573 02 07 0110660130fc9b 07 0110660130fc9b78 var start = 20; for (var i = 0; i < count; i++) { if (start >= str.Length) { break; } var cmdCount = Convert.ToInt32(str.Substring(start, 2).ToLower(), 16); var cmd = str.Substring(start + 2, cmdCount * 2); mo.listcmd.Add(cmd); start = start + cmdCount * 2 + 2; } } } return(mo); }