Exemplo n.º 1
0
        internal void HostAction()
        {
            int waitTime = 5000;
            //if (RoomDoorType == 1)
            //{
            //    waitTime = 1000;
            //}

            SetTime();
            while (!End)
            {
                try
                {
                    wait.WaitOne(waitTime);
                    if (block > 0)
                        block--;
                    else
                    {                        
                        List<RAC960Record> readData = ReadRecord();                   
                        if (readData == null)
                        {                            
                            if (block < -1)
                            {
                                Random ramdom = new Random();
                                block = ramdom.Next(12, 36); ;
                                ServerData.CheckControlConnect(Name, false);
                            }
                            else
                            {
                                block--;
                            }
                            continue;
                        }
                        else
                        {
                            block = 0;
                            ServerData.CheckControlConnect(Name, true);
                        }
                        if (readData.Count > 0)
                        {
                            #region 卡片記錄
                            bool haveChange = false;
                            foreach (RAC960Record data in readData)
                            {
                                if (data.EventCode.Substring(2) == "00")//合法卡
                                {
                                    string cmd = string.Format("Insert into tbldeviceStateLog (TypeID,TypeCode,TimeStamp,ReaderID,ABA,ControlID) "
                                        + "Values({0},{1},'{2}',{3},'{4}','{5}');", 1, 1, data.DateTime.ToString("yyyy/MM/dd HH:mm:ss"), ReaderID, data.Card, Name);
                                    DatabaseAccess.DatabaseAcces(cmd);
                                    if (RoomDoorType == 1)//入
                                    {
                                        if (ServerData.RoomPerson.ContainsKey(RoomName))
                                        {
                                            //string PersonName = data.Card;
                                            //if (ServerData.PersonnelData.ContainsKey(data.Card))
                                            //{
                                            //    PersonName = ServerData.PersonnelData[data.Card];
                                            //    if (string.IsNullOrWhiteSpace(PersonName))
                                            //        PersonName = data.Card;
                                            //}
                                            List<RoomCardData> RoomPersonList = ServerData.RoomPerson[RoomName];
                                            bool HaveData = false;
                                            lock (RoomPersonList)
                                            {
                                                for (int i = 0; i < RoomPersonList.Count; i++)
                                                {
                                                    if (RoomPersonList[i].CardID == data.Card)
                                                    {
                                                        if (data.DateTime > RoomPersonList[i].LastTime)
                                                        {
                                                            RoomCardData roomCardData = RoomPersonList[i];
                                                            roomCardData.LastTime = data.DateTime;
                                                            if (!roomCardData.In)
                                                            {
                                                                roomCardData.In = true;
                                                                haveChange = true;
                                                                cmd = string.Format("Insert into tblEngineRoomLog (ERNo,StartTime,ABA,Result) "
                                                                    + "Values ('{0}','{1}','{2}',0);"
                                                                    , RoomName, data.DateTime.ToString("yyyy/MM/dd HH:mm:ss"), data.Card);
                                                                DatabaseAccess.DatabaseAcces(cmd);
                                                            }
                                                        }
                                                        HaveData = true;
                                                    }
                                                }
                                            }
                                            if (!HaveData)
                                            {
                                                RoomCardData newCardData = new RoomCardData(data.Card, ServerData.GetCardName(data.Card),ServerData.GetCarComp(data.Card), true, data.DateTime,false);
                                                RoomPersonList.Add(newCardData);
                                                haveChange = true;
                                                cmd = string.Format("Insert into tblEngineRoomLog (ERNo,StartTime,ABA,Result) "
                                                                   + "Values ('{0}','{1}','{2}',0);"
                                                                   , RoomName, data.DateTime.ToString("yyyy/MM/dd HH:mm:ss"), data.Card);
                                                DatabaseAccess.DatabaseAcces(cmd);
                                            }
                                        }
                                    }
                                    else if (RoomDoorType == 2)//出
                                    {
                                        if (ServerData.RoomPerson.ContainsKey(RoomName))
                                        {
                                            List<RoomCardData> RoomPersonList = ServerData.RoomPerson[RoomName];
                                            lock (RoomPersonList)
                                            {
                                                for (int i = 0; i < RoomPersonList.Count; i++)
                                                {
                                                    if (RoomPersonList[i].CardID == data.Card)
                                                    {
                                                        if (data.DateTime > RoomPersonList[i].LastTime)
                                                        {
                                                            RoomCardData roomCardData = RoomPersonList[i];
                                                            roomCardData.LastTime = data.DateTime;
                                                            if (roomCardData.In)
                                                            {
                                                                roomCardData.In = false;
                                                                haveChange = true;
                                                                cmd = string.Format("update tblEngineRoomLog Set EndTime = '{1}' "
                                                                   + "where ERNo = '{0}' and ABA = '{2}' "
                                                                   + "and StartTime = (Select Top 1 StartTime from tblEngineRoomLog where ERNo = '{0}' and ABA = '{2}' order by StartTime desc );"
                                                                   , RoomName, data.DateTime.ToString("yyyy/MM/dd HH:mm:ss"), data.Card);
                                                                DatabaseAccess.DatabaseAcces(cmd);
                                                            }
                                                            ServerData.SendRoomEvent(RoomInterface.ControllEventType.ReadCard, Name, data.Card);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                }
                                else if (data.EventCode.Substring(2) == "14")//非法卡
                                {
                                    string cmd = string.Format("Insert into tbldeviceStateLog (TypeID,TypeCode,TimeStamp,ReaderID,ABA,ControlID) "
                                        + "Values({0},{1},'{2}',{3},'{4}','{5}');", 1, 2, data.DateTime.ToString("yyyy/MM/dd HH:mm:ss"), ReaderID, data.Card, Name);
                                    DatabaseAccess.DatabaseAcces(cmd);
                                    string PersonName = data.Card;
                                    if (ServerData.PersonnelData.ContainsKey(data.Card))
                                    {
                                        PersonName = ServerData.PersonnelData[data.Card];
                                        if (string.IsNullOrWhiteSpace(PersonName))
                                            PersonName = data.Card;
                                    }
                                    ServerData.SendRoomEvent(RoomInterface.ControllEventType.ErrorCard, Name, PersonName);
                                }
                            }
                            if (haveChange)
                            {
                                List<RoomCardData> RoomPersonList = ServerData.RoomPerson[RoomName];
                                List<RoomInterface.PersonData> PersonList = new List<RoomInterface.PersonData>();
                                lock (RoomPersonList)
                                {
                                    foreach (RoomCardData data in RoomPersonList)
                                    {
                                        if (data.In)
                                        {
                                            PersonList.Add(new RoomInterface.PersonData(data.CardID, data.Name,data.Company,data.LastTime,data.IsManual));
                                        }
                                    }
                                }
                                ServerData.SendRoomEvent(RoomInterface.ControllEventType.RoomPersonChange, RoomName, PersonList);
                            }
                            #endregion                            
                        }
                        if (DetectDoor)
                        {
                            bool DoorStatus = getDoorStatus();
                            byte state = 0;
                            if (!DoorStatus)
                                state = 1;
                            if (ServerData.ADAMStatus[RoomName][DoorNum] != state)
                            {
                                ServerData.ADAMStatus[RoomName][DoorNum] = state;
                                ServerData.SendRoomEvent(RoomInterface.ControllEventType.ADAMStatusChange, RoomName, ServerData.ADAMStatus[RoomName]);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    TCommon.SaveLog(Name + ex.Message);
                }

                
            }
        }
Exemplo n.º 2
0
 public void HostAction()
 {
     SetTime();
     while (!end)
     {
         try
         {
             wait.WaitOne(5000);
             if (block > 0)
                 block--;
             else
             {
                 List<LogData> readlog = ReadLog(LogType.CradLog);                     
                 if (readlog == null)
                 {
                     if (block < -1)
                     {
                         Random ramdom = new Random();
                         block = ramdom.Next(12,36);
                         ServerData.CheckControlConnect(Name, false);
                     }
                     else
                     {
                         block--;
                     }
                     continue;
                 }
                 else
                 {
                     ServerData.CheckControlConnect(Name, true);
                 }
                 if (readlog.Count > 0)
                 {
                     ServerData.CheckControlConnect(Name, true);
                     bool haveChange = false;
                     foreach (LogData logData in readlog)
                     {
                         string CardNO = logData.CardNum;
                         if (logData.DeviceID == 22)
                             logData.DeviceID = 0;
                         if (readerType[logData.DeviceID] == 1) //1 WEG
                         {
                             CardNO = TCommon.GetABA(logData.CardNum);
                         }
                         if (logData.StatusCode == 1) //合法卡
                         {
                             string cmd = string.Format("Insert into tbldeviceStateLog (TypeID,TypeCode,TimeStamp,ReaderID,ABA,ControlID) "
                                    + "Values({0},{1},'{2}',{3},'{4}','{5}');", 1, 1, logData.date.ToString("yyyy/MM/dd HH:mm:ss"), readerID[logData.DeviceID], CardNO, Name);
                             DatabaseAccess.DatabaseAcces(cmd);
                             if (readerIO[logData.DeviceID] == 1) //入
                             {
                                 if (ServerData.RoomPerson.ContainsKey(RoomName))
                                 {
                                     string PersonName = CardNO;
                                     if (ServerData.PersonnelData.ContainsKey(CardNO))
                                     {
                                         PersonName = ServerData.PersonnelData[CardNO];
                                         if (string.IsNullOrWhiteSpace(PersonName))
                                             PersonName = CardNO;                                                        
                                     }
                                     List<RoomCardData> RoomPersonList = ServerData.RoomPerson[RoomName];
                                     bool HaveData = false;
                                     lock (RoomPersonList)
                                     {
                                         for (int i = 0; i < RoomPersonList.Count; i++)
                                         {
                                             if (RoomPersonList[i].CardID == CardNO)
                                             {
                                                 if (logData.date > RoomPersonList[i].LastTime)
                                                 {
                                                     RoomCardData roomCardData = RoomPersonList[i];
                                                     roomCardData.LastTime = logData.date;
                                                     if (!roomCardData.In)
                                                     {
                                                         roomCardData.In = true;
                                                         haveChange = true;
                                                         cmd = string.Format("Insert into tblEngineRoomLog (ERNo,StartTime,ABA,Result) "
                                                             + "Values ('{0}','{1}','{2}',0);"
                                                             , RoomName, logData.date.ToString("yyyy/MM/dd HH:mm:ss"), CardNO);
                                                         DatabaseAccess.DatabaseAcces(cmd);
                                                     }
                                                 }
                                                 HaveData = true;
                                                 break;
                                             }
                                         }
                                     }
                                     if (!HaveData)
                                     {
                                         RoomCardData newCardData = new RoomCardData(CardNO, PersonName,ServerData.GetCarComp(CardNO), true, logData.date,false);
                                         RoomPersonList.Add(newCardData);
                                         haveChange = true;
                                         cmd = string.Format("Insert into tblEngineRoomLog (ERNo,StartTime,ABA,Result) "
                                                             + "Values ('{0}','{1}','{2}',0);"
                                                             , RoomName, logData.date.ToString("yyyy/MM/dd HH:mm:ss"), CardNO);
                                         DatabaseAccess.DatabaseAcces(cmd);
                                     }
                                 }
                             }
                             else if (readerIO[logData.DeviceID] == 2)//出
                             {
                                 if (ServerData.RoomPerson.ContainsKey(RoomName))
                                 {
                                     List<RoomCardData> RoomPersonList = ServerData.RoomPerson[RoomName];
                                     lock (RoomPersonList)
                                     {
                                         for (int i = 0; i < RoomPersonList.Count; i++)
                                         {
                                             if (RoomPersonList[i].CardID == CardNO)
                                             {
                                                 if (logData.date > RoomPersonList[i].LastTime)
                                                 {
                                                     RoomCardData roomCardData = RoomPersonList[i];
                                                     RoomPersonList.RemoveAt(i);
                                                     haveChange = true;
                                                     cmd = string.Format("update tblEngineRoomLog Set EndTime = '{1}' "
                                                             + "where ERNo = '{0}' and ABA = '{2}' "
                                                             + "and StartTime = (Select Top 1 StartTime from tblEngineRoomLog where ERNo = '{0}' and ABA = '{2}' order by StartTime desc);"
                                                             , RoomName, logData.date.ToString("yyyy/MM/dd HH:mm:ss"), CardNO);
                                                     DatabaseAccess.DatabaseAcces(cmd);
                                                 }
                                                 ServerData.SendRoomEvent(RoomInterface.ControllEventType.ReadCard, Name, CardNO);
                                                 break;
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         else if (logData.StatusCode == 7)//非法卡
                         {
                             string cmd = string.Format("Insert into tbldeviceStateLog (TypeID,TypeCode,TimeStamp,ReaderID,ABA,ControlID) "
                                     + "Values({0},{1},'{2}',{3},'{4}','{5}');", 1, 2, logData.date.ToString("yyyy/MM/dd HH:mm:ss"), readerID[logData.DeviceID], CardNO, Name);
                             DatabaseAccess.DatabaseAcces(cmd);
                             string PersonName = CardNO;
                             if (ServerData.PersonnelData.ContainsKey(CardNO))
                             {
                                 PersonName = ServerData.PersonnelData[CardNO];
                                 if (string.IsNullOrWhiteSpace(PersonName))
                                     PersonName = CardNO;
                             }
                             ServerData.SendRoomEvent(RoomInterface.ControllEventType.ErrorCard, Name, PersonName);
                         }
                         if (haveChange)
                         {
                             List<RoomCardData> RoomPersonList = ServerData.RoomPerson[RoomName];
                             List<RoomInterface.PersonData> PersonList = new List<RoomInterface.PersonData>();
                             lock (RoomPersonList)
                             {
                                 foreach (RoomCardData data in RoomPersonList)
                                 {
                                     if (data.In)
                                     {
                                         PersonList.Add(new RoomInterface.PersonData(data.CardID, data.Name,data.Company,data.LastTime,false));
                                     }
                                 }
                             }
                             ServerData.SendRoomEvent(RoomInterface.ControllEventType.RoomPersonChange, RoomName, PersonList);
                         }
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             TCommon.SaveLog(Name + ex.Message);
         }
     
         
     }
    
 }