Ejemplo n.º 1
0
        public static byte[] CRC_RTU(string hexstr)
        {
            var bytes = StrHelper.strToHexByte(hexstr);

            byte[] crcBytes     = new byte[2];
            byte[] sendResponse = new byte[bytes.Length + crcBytes.Length];
            bytes.CopyTo(sendResponse, 0);
            CRC16.GetCRC(sendResponse, ref crcBytes);
            sendResponse[sendResponse.Length - 1] = crcBytes[1]; //高8位
            sendResponse[sendResponse.Length - 2] = crcBytes[0]; //低8位
            return(sendResponse);
        }
Ejemplo n.º 2
0
        /// <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);
            }
        }
Ejemplo n.º 3
0
        /// <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
        }
Ejemplo n.º 4
0
        /// <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
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private static void SendMsgToClient(byte[] ds, MacTcpUdp mu)
        {
            var rdata = StrHelper.GetHexStr(ds);
            var mac   = mu.mac;

            if (!string.IsNullOrEmpty(mac))
            {
                if (mac.Equals("98d8634a0344", StringComparison.OrdinalIgnoreCase))
                {
                    LogHelper.LogFilter(true, "返回=>" + rdata);
                }
            }
            else
            {
                mac = string.Empty;
            }
            var bdata = Encoding.UTF8.GetBytes(JsonHelper <Status <string> > .GetJson(new Status <string>()
            {
                code = "1", data = rdata, mac = mac, msg = "", commandType = "1"
            }));
            var queue = mu.clients;
            ///能否正常发通UDP
            var flag = true;

            while (queue != null && queue.Count > 0)
            {
                var el = queue.Dequeue();
                if (el != null && !(el.time.AddSeconds(10) < DateTime.Now))
                {
                    try
                    {
                        if (mu.udpServer != null)
                        {
                            LogHelper.Info("设备mac:" + mac + " 数据" + rdata);
                            var iet = (IPEndPoint)el.point;
                            if (!iet.Address.Equals(IPAddress.Any))
                            {
                                try
                                {
                                    if (flag)
                                    {
                                        mu.udpServer.SendTo(bdata, el.point);
                                    }
                                }
                                catch (SocketException ex)
                                {
                                    flag = false;
                                    queue.Clear();
                                    //LogHelper.Info("TCP:378:   " + ex.Message);

                                    LogHelper.LogFilter(true, ex.StackTrace);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("TCP:395" + ex.Message);


                        //Func<bool> func1 = () => true;
                        //var msgx = "TCP:395" + ex.Message;
                        //LogHelper.LogFilter(func1, msgx);

                        LogHelper.LogFilter(true, ex.StackTrace);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        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);
        }