Example #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;
                }
            }
        }
Example #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]);
             }
         }
     }
 }
Example #3
0
 private void axHSCEventSDK1_NewEvent(object sender, EventArgs e)
 {
     if (eventDic.ContainsKey(axHSCEventSDK1.sEventName))//报警信息
     {
         var alarmEntity = AlarmParseTool.parseAlarm(axHSCEventSDK1, eventDic);
         if (alarmEntity != null)
         {
             string alarmMessage = alarmEntity.toJson();
             FileWorker.LogHelper.WriteLog("正在向kafka发送报警数据" + alarmMessage);
             Debug.WriteLine(alarmMessage);
             KafkaWorker.sendAlarmMessage(alarmMessage);
         }
     }
     else if (axHSCEventSDK1.sEventName == "正常开门")//正常刷卡信息
     {
         var cardId = axHSCEventSDK1.sUserCardID;
         axHSCCardHolderSDK1.sCardID = cardId;
         axHSCCardHolderSDK1.GetPIDUserInfo();
         var accessEntity = AccessParseTool.parseAccess(axHSCEventSDK1, axHSCCardHolderSDK1);
         if (accessEntity != null)
         {
             string accessMessage = accessEntity.toJson();
             FileWorker.LogHelper.WriteLog("正在向kafka发送正常刷卡数据" + accessMessage);
             KafkaWorker.sendAccessMessage(accessMessage);
         }
     }
     else if (axHSCEventSDK1.sEventName == "控制器上线")//设备状态信息
     {
         var deviceEntity = DeviceStateParseTool.parseDeviceState(axHSCEventSDK1, "ES01");
         if (deviceEntity != null)
         {
             string deviceMessage = deviceEntity.toJson();
             FileWorker.LogHelper.WriteLog("正在向kafka发送设备状态数据" + deviceMessage);
             KafkaWorker.sendDeviceMessage(deviceMessage);
         }
     }
     else if (axHSCEventSDK1.sEventName == "控制器离线")
     {
         var deviceEntity = DeviceStateParseTool.parseDeviceState(axHSCEventSDK1, "ES02");
         if (deviceEntity != null)
         {
             string deviceMessage = deviceEntity.toJson();
             FileWorker.LogHelper.WriteLog("正在向kafka发送设备状态数据" + deviceMessage);
             KafkaWorker.sendDeviceMessage(deviceMessage);
         }
     }
 }
Example #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
        }
Example #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);
                }
            }
        }