示例#1
0
        private void ReceiveMessage(object obj)
        {
            IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse(serverIp), serverPort);

            while (true)
            {
                try
                {
                    byte[] bytRecv    = udpcRecv.Receive(ref remoteIpep);
                    string bytRecvStr = string.Join(" ", bytRecv);
                    FileWorker.LogHelper.WriteLog("接收到的原始数据是:" + bytRecvStr);
                    string msg = Encoding.Default.GetString(bytRecv);
                    FileWorker.LogHelper.WriteLog("原始数据解析后是:" + msg);

                    AlarmEntity alarm        = udpToAlarm(msg);
                    string      messageAlarm = alarm.toJson();
                    KafkaWorker.sendAlarmMessage(messageAlarm);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    break;
                }
            }
        }
示例#2
0
 private void Client_DataReceived(object sender, SimpleTCP.Message e)
 {
     FileWorker.LogHelper.WriteLog("刷卡信息:" + e.MessageString);
     string[] messageArr = e.MessageString.Split(new string[] { "TRDS" }, StringSplitOptions.RemoveEmptyEntries);
     for (int j = 0; j < messageArr.Length; j++)
     {
         messageArr[j] = "TRDS" + messageArr[j];
     }
     for (int i = 0; i < messageArr.Length; i++)
     {
         string[] receMessage = messageArr[i].Split(new char[] { '|' });
         if (receMessage[0] == "TRDS")
         {
             if (receMessage.Length >= 10)
             {
                 AccessEntity accessEnt = AccessParseTool.parseAccess(receMessage);
                 string       jsonMess  = accessEnt.toJson();
                 KafkaWorker.sendAccessMessage(jsonMess);
                 anoAlarm = GetConfigMess();
                 if (anoAlarm.Keys.Contains(receMessage[9]))
                 {
                     AlarmEntity alarmEnt  = AlarmParseTool.parseAlarm(receMessage, anoAlarm);
                     string      jsonAlarm = alarmEnt.toJson();
                     KafkaWorker.sendAlarmMessage(jsonAlarm);
                 }
             }
             else
             {
                 FileWorker.LogHelper.WriteLog("刷卡信息不全," + messageArr[i]);
             }
         }
     }
 }
示例#3
0
        void a()
        {
            byte[] packet_bytes = new byte[] {
                0xd0, 0xc5, 0xcf, 0xa2, 0x20, 0x0d, 0x0a, 0x0d,
                0x0a, 0x20, 0xc8, 0xd5, 0xc6, 0xda, 0x3a, 0x32,
                0x30, 0x31, 0x39, 0x2d, 0x31, 0x32, 0x2d, 0x30,
                0x36, 0x20, 0x20, 0x20, 0x20, 0x20, 0xca, 0xb1,
                0xbc, 0xe4, 0x3a, 0x31, 0x36, 0x3a, 0x33, 0x37,
                0x3a, 0x31, 0x32, 0x20, 0x20, 0x20, 0x20, 0x20,
                0x20, 0xd3, 0xc3, 0xbb, 0xa7, 0xb1, 0xe0, 0xba,
                0xc5, 0x3a, 0x30, 0x30, 0x31, 0x30, 0x20, 0x20,
                0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
                0x20, 0xd3, 0xc3, 0xbb, 0xa7, 0xc3, 0xfb, 0xb3,
                0xc6, 0x3a, 0x49, 0x44, 0x36, 0x61, 0x62, 0x63,
                0x41, 0x42, 0x43, 0xb5, 0xab, 0xca, 0xc7, 0x0d,
                0x0a, 0x20, 0xd3, 0xc3, 0xbb, 0xa7, 0xb7, 0xc0,
                0xc7, 0xf8, 0x3a, 0x30, 0x30, 0x31, 0x20, 0x20,
                0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
                0x20, 0x20, 0xbe, 0xaf, 0xc7, 0xe9, 0x3a, 0xbd,
                0xf4, 0xbc, 0xb1, 0xc7, 0xf3, 0xd6, 0xfa, 0xb1,
                0xa8, 0xbe, 0xaf, 0x20, 0x20, 0x31, 0x32, 0x3a,
                0xb0, 0xb2, 0xbc, 0xec, 0x36, 0xba, 0xc5, 0xd1,
                0xe9, 0xd6, 0xa4, 0x0d, 0x0a, 0x20, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f, 0x5f,
                0x5f
            };

            byte[] packet_bytes1 = new byte[] { 211, 195, 187, 167, 177, 168, 190, 175, 207, 234, 207, 184, 208, 197, 207, 162, 32, 13, 10, 13, 10, 32, 200, 213, 198, 218, 58, 50, 48, 49, 57, 45, 49, 50, 45, 49, 48, 32, 32, 32, 32, 32, 202, 177, 188, 228, 58, 49, 53, 58, 51, 50, 58, 51, 56, 32, 32, 32, 32, 32, 32, 211, 195, 187, 167, 177, 224, 186, 197, 58, 49, 49, 50, 57, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 211, 195, 187, 167, 195, 251, 179, 198, 58, 50, 45, 55, 197, 228, 207, 223, 188, 228, 177, 168, 190, 175, 214, 247, 187, 250, 13, 10, 32, 211, 195, 187, 167, 183, 192, 199, 248, 58, 48, 48, 52, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 190, 175, 199, 233, 58, 189, 244, 188, 177, 199, 243, 214, 250, 177, 168, 190, 175, 32, 205, 168, 181, 192, 195, 232, 202, 246, 58, 49, 55, 35, 176, 178, 188, 236, 205, 168, 181, 192, 209, 233, 214, 164, 185, 241, 204, 168, 13, 10, 32, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95 };


            byte[] bytRecvHex = new byte[packet_bytes1.Length];
            //for (int i = 0; i < packet_bytes1.Length; i++)
            //{
            //    int d = NumberTrans.decToHex(packet_bytes1[i]);
            //    bytRecvHex[i] = (byte)d;
            //}
            string      msg     = System.Text.Encoding.Default.GetString(packet_bytes1);
            AlarmEntity alarm   = udpToAlarm(msg);
            string      message = alarm.toJson();
            //int length = msg.Length;
            //var str =(cutSubstring(msg, length));
        }
示例#4
0
        public void UpdateTestClient(Obj obixObj, Watch watch)
        {
            //FileWorker.LogHelper.WriteLog("收到数据:"+watch.type.ToString()+";;长度是"+obixObj.list().Length.ToString());
            if (watch.type == WatchType.Alarm)
            {
                if (obixObj.list().Length > 0)
                {
                    if (obixObj.list()[0].list().Length > 0)
                    {
                        //FileWorker.LogHelper.WriteLog("obixObj.list()[0].list().Length长度是" + obixObj.list()[0].list().Length.ToString());
                        if (obixObj.list()[0].list()[0].list().Length > 0)
                        {
                            //FileWorker.LogHelper.WriteLog("obixObj.list()[0].list()[0].list().Length长度是" + obixObj.list()[0].list()[0].list().Length.ToString());
                            foreach (Obj obj in obixObj.list()[0].list()[0].list())
                            {
                                try
                                {
                                    string time             = ((Abstime)obj.get("timestamp")).Val.ToString();
                                    string type             = ((Int)obj.get("eventType")).Val.ToString();
                                    string desc             = obj.Display.ToString();
                                    object id               = obj.Href;
                                    object ack              = obj.get("ack");
                                    object clear            = obj.get("clear");
                                    object pointDescription = null;
                                    object priority         = ((Int)obj.get("priority")).Val;
                                    string source           = string.Empty;
                                    string alarmType        = null;
                                    string fname            = string.Empty;
                                    string lname            = string.Empty;
                                    object cardnumber       = null;
                                    string user             = string.Empty;
                                    string card             = string.Empty;
                                    string deviceName       = string.Empty;
                                    string deptName         = string.Empty;

                                    if (obj.get("companyname") != null)
                                    {
                                        deptName = obj.get("companyname").ToString();
                                    }
                                    if (obj.get("devicedescription") != null)
                                    {
                                        deviceName = obj.get("devicedescription").ToString();
                                    }
                                    if (obj.get("source") != null)
                                    {
                                        source = obj.get("source").Href.ToString();
                                    }
                                    if (obj.get("fname") != null)
                                    {
                                        fname = obj.get("fname").ToString();
                                    }
                                    if (obj.get("lname") != null)
                                    {
                                        lname = obj.get("lname").ToString();
                                    }
                                    if (obj.get("cardnumber") != null)
                                    {
                                        cardnumber = obj.get("cardnumber").Href;
                                    }
                                    if (obj.get("accessUser") != null)
                                    {
                                        user = obj.get("accessUser").Href.ToString();
                                    }
                                    if (obj.get("accessCredential") != null)
                                    {
                                        card = obj.get("accessCredential").Href.ToString();
                                    }
                                    if (obj.get("AlarmStatus") != null)
                                    {
                                        alarmType = ((Str)obj.get("AlarmStatus")).Val;
                                    }
                                    if (obj.get("pointdescription") != null)
                                    {
                                        pointDescription = ((Str)obj.get("pointdescription")).Val;
                                    }
                                    FileWorker.LogHelper.WriteLog(string.Format("eventType:{0};desc:{1};source:{2};user:{3};credential:{4}",
                                                                                type, desc, source, user, card));

                                    if (source != string.Empty)
                                    {
                                        source = DataFormatTool.pickTailFromString(source);
                                    }
                                    time = DataFormatTool.formatDatetime(time);
                                    //event type=500是刷卡,只有刷卡时才产生卡号、人员编号
                                    if (type == "500")
                                    {
                                        if (card != string.Empty)
                                        {
                                            card = DataFormatTool.pickTailFromString(card);
                                        }
                                        if (user != string.Empty)
                                        {
                                            user = DataFormatTool.pickTailFromString(user);
                                        }
                                        AccessEntity access = AccessParseTool.parseAccess(card, source, "1", user, time, deptName, deviceName, lname + fname, "", "刷卡+密码开门");
                                        KafkaWorker.sendAccessMessage(access.toJson());
                                        //continue;
                                    }
                                    if (type == "608")//按钮开门,无卡号、人员编号
                                    {
                                        AccessEntity access = AccessParseTool.parseAccess("", source, "2", "", time, "", deviceName, "", "", "按钮开门");
                                        KafkaWorker.sendAccessMessage(access.toJson());
                                        continue;
                                    }
                                    if (type == "903" || type == "10903")
                                    {
                                        DeviceStateEntity devState = DeviceStateParseTool.parseDeviceState(source, type);
                                        KafkaWorker.sendDeviceMessage(devState.toJson());
                                    }
                                    //423,400,10900,900,500,903
                                    if (alarmRuleDic.ContainsKey(type))//报警
                                    {
                                        AlarmEntity alarm = AlarmParseTool.parseAlarm(source, alarmRuleDic[type].Item1, alarmRuleDic[type].Item2, time, airportIata, airportName);
                                        KafkaWorker.sendAlarmMessage(alarm.toJson());
                                        continue;
                                    }
                                }
                                catch (Exception ex)
                                {
                                    FileWorker.LogHelper.WriteLog("消息接收及解析、发送过程中出现异常:" + ex.Message);
                                    continue;
                                }
                            }
                        }
                    }
                }
            }
            #region 暂时不用
            else //watch.type == WatchType.Object
            {
                if (obixObj.list().Length > 0)
                {
                    DateTime updateTime = DateTime.Now;
                    foreach (Obj obj in obixObj.list()[0].list())
                    {
                        //bool rowFound = false;
                        //for (int i = 0; i < grd.Rows.Count; i++)
                        //{
                        //    if (grd.Rows[i].Cells[0].Value.Equals(obj.Href))
                        //    {
                        //        if (obj.isErr())
                        //        {
                        //            //Object deleted in PW
                        //            grd.Rows.RemoveAt(i);
                        //        }
                        //        else
                        //        {
                        //            grd.Rows[i].Cells[1].Value = DateTime.Now;
                        //            updateTime = (DateTime)grd.Rows[i].Cells[1].Value;
                        //        }
                        //        rowFound = true;
                        //        break;
                        //    }
                        //}
                        //if (!rowFound)
                        //{
                        //    updateTime = DateTime.Now;
                        //    //grd.Rows.Add(obj.Href, updateTime);
                        //}

                        ////when object's parent url is subscribed.
                        //System.Uri uri = obj.Href.toUri();
                        //if (uri.Segments.Length >= 5)
                        //{
                        //    string strUri = uri.ToString();
                        //    if (strUri.EndsWith("/"))
                        //        strUri = strUri.Substring(0, strUri.Length - 1);
                        //    int nIndex = strUri.LastIndexOf("/");
                        //    strUri = strUri.Substring(0, nIndex);
                        //    for (int i = 0; i < grd.Rows.Count; i++)
                        //    {
                        //        string strObjHref = Convert.ToString(grd.Rows[i].Cells[0].Value);
                        //        if (strObjHref.EndsWith("/"))
                        //            strObjHref = strObjHref.Substring(0, strObjHref.Length - 1);
                        //        if (strObjHref.Equals(strUri))
                        //        {
                        //            grd.Rows[i].Cells[1].Value = updateTime;
                        //            break;
                        //        }
                        //    }
                        //}
                    }
                }
            }
            #endregion
        }
示例#5
0
        private void startListener()
        {
            IPAddress   address  = IPAddress.Parse(localIp);
            IPEndPoint  ep       = new IPEndPoint(address, int.Parse(port));
            TcpListener listener = new TcpListener(ep);

            listener.Start();

            startReceiveData();

            byte[] buffer = new byte[2 * 8192];
            var    sender = listener.AcceptTcpClient();

            sender.ReceiveBufferSize = 2 * 8192;
            // Set the timeout for synchronous receive methods to
            // 40 second (1000 milliseconds.) because after 30 seconds without any new message an ALIVE message
            // should be received
            sender.ReceiveTimeout = 60 * 1000;
            while (true)
            {
                // Accept connection again
                //
                if ((sender == null) || !sender.Connected)
                {
                    sender = listener.AcceptTcpClient();    // blocking call ...
                }
                // Try to read received message
                //
                int iReadNum = 0;
                iReadNum = sender.GetStream().Read(buffer, 0, sender.ReceiveBufferSize); //bytesize
                // Any message received?
                if (iReadNum == 0)
                {
                    // Here simple retry again.. Timeout and reconnect handling should be implemented
                    continue;
                }
                // Message received .. it is always of type unicode!
                string tcpMessage = System.Text.Encoding.Unicode.GetString(buffer).Substring(0, iReadNum / 2);
                Debug.WriteLine(tcpMessage);
                // The format of the received message string is described in the next chapter “Event messsages”
                if (tcpMessage.Contains("ALIVE="))//如果是心跳
                {
                    string dtStr = tcpMessage.Substring(tcpMessage.IndexOf("ALIVE="), 20);
                }
                if (tcpMessage.Contains("START=") || tcpMessage.Contains("ALIVE="))
                {
                    continue;
                }
                if (tcpMessage.Contains("MSGTEXT=\"door is closed\"") || tcpMessage.Contains("MSGTEXT=\"door is open\""))  //开门关门暂不处理
                {
                    continue;
                }
                else if (tcpMessage.Contains("MSGTEXT=\"access\"") || tcpMessage.Contains("MSGTEXT=\"access, via remote command\""))//合法卡刷卡、远程开门,发送至正常刷卡队列
                {
                    SwipeEntity swipte  = tcpToSwipe(tcpMessage);
                    string      message = swipte.toJson();
                    KafkaWorker.sendSwipeMessage(message);
                }
                else //报警信息发送至报警队列
                {
                    AlarmEntity alarm   = tcpToAlarm(tcpMessage);
                    string      message = alarm.toJson();
                    KafkaWorker.sendAlarmMessage(message);
                }
            }
        }