//工作-处理终端状态更新
        private void Job_Terminal_StatusUpdate(APParser apPaser)
        {
            int result = 0;

            apPaser.GetParam_Terminal_Status(ref byte_ap_uid, ref byte_terminal_uid, ref result);

            string tmp1 = System.Text.Encoding.Default.GetString(byte_ap_uid);
            string tmp2 = System.Text.Encoding.Default.GetString(byte_terminal_uid);

            if (result == 2)
            {
                result = 0;
            }
            else
            {
                result = 1;
            }

            if (database.Terminal_Status_Update(tmp1, tmp2, result))
            {
            }
            else
            {
            }

            //udpSocket.Send(apEncapsulator.Response_Terminal_StatusUpdate(byte_ap_uid, byte_terminal_uid));

            database.CmdLog("05010500", tmp1, apPaser.GetParam_Origin_Hex());

            tmp1 = null;
            tmp2 = null;
        }
        //工作-处理AP状态更新
        private void Job_AP_StatusUpdate(APParser apPaser)
        {
            int result = 0;

            apPaser.GetParam_AP_Status(ref byte_ap_uid, ref result);

            string tmp = System.Text.Encoding.Default.GetString(byte_ap_uid);

            if (result == 2)
            {
                result = 0;
            }
            else
            {
                result = 1;
            }
            if (database.AP_Status_Update(tmp, result))
            {
            }
            else
            {
            }
            //udpSocket.Send(apEncapsulator.Response_AP_StatusUpdate(char_ap_uid), 37);

            database.CmdLog("05010300", tmp, apPaser.GetParam_Origin_Hex());

            tmp = null;
        }
        //工作-处理终端数据
        private void Job_Terminal_data_upload(APParser apPaser)
        {
            int size = 0;

            if (!apPaser.GetParam_Terminal_data_upload(ref byte_ap_uid, ref byte_terminal_uid, ref byte_terminal_data, ref size))
            {
                return;
            }

            TerminalHandleJob(size);
            udpSocket.Send(apEncapsulator.Response_Terminal_DataUpload(byte_ap_uid));
            string tmp1 = System.Text.Encoding.Default.GetString(byte_ap_uid);

            database.CmdLog("05020600", tmp1, apPaser.GetParam_Origin_Hex());
            tmp1 = null;
        }
        //工作-处理AP校验
        private void Job_AP_Validate(APParser apPaser)
        {
            int Count = 0;

            apPaser.GetParam_Validate_Order(ref byte_ap_uid, ref Count);
            string tmp = System.Text.Encoding.Default.GetString(byte_ap_uid);

            if (database.AP_Validate(tmp, Count.ToString()))
            {
                udpSocket.Send(apEncapsulator.Response_AP_Validate(byte_ap_uid, 0x31));
            }
            else
            {
                udpSocket.Send(apEncapsulator.Response_AP_Validate(byte_ap_uid, 0x32));
            }

            database.CmdLog("05010100", tmp, apPaser.GetParam_Origin_Hex());

            tmp = null;
        }
        private void Job_Entrance_Guard_Validate(APParser apPaser)
        {
            int num = 0;

            apPaser.GetParam_Entrance_Guard(ref byte_guard_uid, ref byte_guard_data, ref num);

            string tmp1 = System.Text.Encoding.Default.GetString(byte_guard_uid);
            string tmp2 = ByteToHexString(byte_guard_data, 0, byte_guard_data.Length, 8, "");

            if (database.Guard_Validate(tmp1, tmp2, num))
            {
                udpSocket.Send(apEncapsulator.Response_Guard_Validate(byte_guard_uid, true));
            }
            else
            {
                udpSocket.Send(apEncapsulator.Response_Guard_Validate(byte_guard_uid, false));
            }
            database.CmdLog("05010700", tmp1, apPaser.GetParam_Origin_Hex());
            tmp1 = null;
            tmp2 = null;
        }
        //handle guard validate
        private void Job_Entrance_Guard_Validate(APParser apPaser)
        {
            int num = 0;

            apPaser.GetParam_Entrance_Guard(ref byte_guard_uid, ref byte_guard_data, ref num);

            string tmp1 = System.Text.Encoding.Default.GetString(byte_guard_uid);
            string tmp2 = ByteToHexString(byte_guard_data, 0, byte_guard_data.Length, 8, "");

            /* TimeSpan span = time_last.Subtract( time_receive);
             * if(span.Minutes > 2)
             * {
             *
             * }
             * else
             * {
             *
             * }*/


            iCounter_invalid = database.Guard_Validate(tmp1, tmp2, num, ref index[0], ref index[1], ref index[2], ref iPk_id);
            if (iCounter_invalid == 0)
            {
                udpSocket.Send(apEncapsulator.Response_Guard_Validate(byte_guard_uid, true));
            }
            else
            {
                udpSocket.Send(apEncapsulator.Response_Guard_Validate(byte_guard_uid, false));
                //stop cut threading
                //VideoHandler handler = new VideoHandler(iPk_id, time_receive, database, videoFilesManager, index[0], index[1], index[2]);
                //Process_Alarm();
            }

            database.CmdLog("05010700", tmp1, apPaser.GetParam_Origin_Hex());
            tmp1 = null;
            tmp2 = null;
        }
        private void Cmd_AddList(string strCode, string strData, int ID)
        {
            //逗号分隔
            byte[] byte_data = HexStringToByte(strData);
            string test      = Encoding.ASCII.GetString(byte_data);
            //创建协议结构
            int  size            = byte_data.Length;
            byte frame_size_high = (byte)((size + 9) >> 8);
            byte frame_size_low  = (byte)(size + 9);
            byte checksum_high   = (byte)((size + 13) >> 8);
            byte checksum_low    = (byte)(size + 13);

            byte[] byteArray = new byte[13 + size];
            byteArray[0] = 36;
            byteArray[1] = 36;
            byteArray[2] = frame_size_high;
            byteArray[3] = frame_size_low;
            byteArray[4] = 2;

            byteArray[11 + size] = 37;
            byteArray[12 + size] = 37;
            //数据分配插入
            if (strCode.Equals("05020100"))
            {
                byteArray[5] = 5;
                byteArray[6] = 2;
                byteArray[7] = 1;
                byteArray[8] = 0;
                for (int i = 0; i < size; i++)
                {
                    byteArray[9 + i] = byte_data[i];
                }
            }//终端搜索
            else if (strCode.Equals("05020200"))
            {
                byteArray[5] = 5;
                byteArray[6] = 2;
                byteArray[7] = 2;
                byteArray[8] = 0;
                for (int i = 0; i < size; i++)
                {
                    byteArray[9 + i] = byte_data[i];
                }
            }//终端配置
            else if (strCode.Equals("05020300"))
            {
                byteArray[5] = 5;
                byteArray[6] = 2;
                byteArray[7] = 3;
                byteArray[8] = 0;
                for (int i = 0; i < size; i++)
                {
                    byteArray[9 + i] = byte_data[i];
                }
            }//终端删除
            else if (strCode.Equals("05020400"))
            {
                //命令
                byteArray[5] = 5;
                byteArray[6] = 2;
                byteArray[7] = 4;
                byteArray[8] = 0;
                //数据
                for (int i = 0; i < size; i++)
                {
                    byteArray[9 + i] = byte_data[i];
                }

                /*
                 * //校驗位
                 * int T_datasize = byteArray[48];
                 * int T_recheck = 0;
                 * for (int i = 0; i < 16 + T_datasize; i++)
                 * {
                 *  T_recheck += byteArray[33 + i];
                 * }//系統協議校驗位
                 *
                 * byteArray[49 + T_datasize] = (byte)(T_recheck >> 8);
                 * byteArray[50 + T_datasize] = (byte)(T_recheck);*/
            }//终端命令
            else if (strCode.Equals("05010400"))
            {
                //命令
                byteArray[5] = 5;
                byteArray[6] = 1;
                byteArray[7] = 4;
                byteArray[8] = 0;
                //数据
                for (int i = 0; i < size; i++)
                {
                    byteArray[9 + i] = byte_data[i];
                }

                /*
                 * //校驗位
                 * int T_datasize = byteArray[36];
                 * int T_recheck = 0;
                 * for (int i = 0; i < 16 + T_datasize; i++)
                 * {
                 *  T_recheck += byteArray[33 + i];
                 * }//系統協議校驗位
                 *
                 * byteArray[49 + T_datasize] = (byte)(T_recheck >> 8);
                 * byteArray[50 + T_datasize] = (byte)(T_recheck);*/
            }//AP命令


            int recheck = 0;

            for (int i = 0; i < 8 + size; i++)
            {
                recheck += byteArray[i];
            }//系統協議校驗位

            byteArray[9 + size]  = (byte)(recheck >> 8);
            byteArray[10 + size] = (byte)(recheck);

            byteArray[9 + size]  = (byte)(recheck >> 8);
            byteArray[10 + size] = (byte)(recheck);

            //添加列表
            APParser apParser = new APParser(byteArray, size + 13, ID);

            dbOrderList.Add(apParser);
        }
Beispiel #8
0
        //DB 数据结果类似
        public static int RelatingMsg(APParser SendMsg, APParser ReceiveMsg)
        {
            EnumOrder order                = SendMsg.GetOrder();
            string    ap_uid_send          = null;
            string    terminal_uid_send    = null;
            string    ap_uid_receive       = null;
            string    terminal_uid_receive = null;

            if (order == EnumOrder.READER_SERACH_DOWN || order == EnumOrder.READER_CONFIG_DOWN || order == EnumOrder.READER_DELETE_DOWN)
            {
                if (SendMsg.GetOrder() != ReceiveMsg.GetOrder())
                {
                    return(-1);
                }

                SendMsg.GetParam_Terminal_Search(ref ap_uid_send, ref terminal_uid_send);
                ReceiveMsg.GetParam_Terminal_Search(ref ap_uid_receive, ref terminal_uid_receive);
                if (ap_uid_send.Equals(ap_uid_receive) && terminal_uid_send.Equals(terminal_uid_receive))
                {
                    return(1);
                }
                else
                {
                    return(-1);
                }
            }//终端搜索,配置,删除配置
            else if (order == EnumOrder.READER_ORDER_DOWN)
            {
                SendMsg.GetParam_AP_UID(ref ap_uid_send);
                ReceiveMsg.GetParam_AP_UID(ref ap_uid_receive);

                if (SendMsg.GetOrder() == ReceiveMsg.GetOrder())
                {
                    if (ap_uid_send.Equals(ap_uid_receive))
                    {
                        return(0);
                    }
                }//终端命令响应
                else if (ReceiveMsg.GetOrder() == EnumOrder.READER_DATA_UP)
                {
                    if (ap_uid_send.Equals(ap_uid_receive))
                    {
                        if ((SendMsg.GetTerminalOrder() == 0x84 && ReceiveMsg.GetTerminalOrder() == 0x85) ||
                            (SendMsg.GetTerminalOrder() == 0x86 && ReceiveMsg.GetTerminalOrder() == 0x87) ||
                            (SendMsg.GetTerminalOrder() == 0x89 && ReceiveMsg.GetTerminalOrder() == 0x8A) ||
                            (SendMsg.GetTerminalOrder() == 0x8B && ReceiveMsg.GetTerminalOrder() == 0x8C) ||
                            (SendMsg.GetTerminalOrder() == 0x8D && ReceiveMsg.GetTerminalOrder() == 0x8E) ||
                            (SendMsg.GetTerminalOrder() == 0x8F && ReceiveMsg.GetTerminalOrder() == 0x90) ||
                            (SendMsg.GetTerminalOrder() == 0x91 && ReceiveMsg.GetTerminalOrder() == 0x92) ||
                            (SendMsg.GetTerminalOrder() == 0x93 && ReceiveMsg.GetTerminalOrder() == 0x94) ||
                            (SendMsg.GetTerminalOrder() == 0x95 && ReceiveMsg.GetTerminalOrder() == 0x96) ||
                            (SendMsg.GetTerminalOrder() == 0x97 && ReceiveMsg.GetTerminalOrder() == 0x98))
                        {
                            return(1);
                        }
                    }
                } //终端数据返回
            }     //终端命令 || 终端数据
            else if (order == EnumOrder.AP_ORDER_UPDOWN)
            {
                SendMsg.GetParam_AP_UID(ref ap_uid_send);
                ReceiveMsg.GetParam_AP_UID(ref ap_uid_receive);

                if ((SendMsg.GetAPOrder() == 0xA0 && ReceiveMsg.GetAPOrder() == 0xA1) ||
                    (SendMsg.GetAPOrder() == 0xA2 && ReceiveMsg.GetAPOrder() == 0xA3) ||
                    (SendMsg.GetAPOrder() == 0xA4 && ReceiveMsg.GetAPOrder() == 0xA5))
                {
                    if (ap_uid_send.Equals(ap_uid_receive))
                    {
                        if ((SendMsg.GetAPOrder() == 0xA0 && ReceiveMsg.GetAPOrder() == 0xA1) ||
                            (SendMsg.GetAPOrder() == 0xA2 && ReceiveMsg.GetAPOrder() == 0xA3) ||
                            (SendMsg.GetAPOrder() == 0xA4 && ReceiveMsg.GetAPOrder() == 0xA5))
                        {
                            return(1);
                        }
                    }
                }
            }//AP命令
            return(-1);
        }
        private void DBHandleJob()
        {
            try
            {
                if (dbOrderList.Count == 0 && dbSentList.Count == 0)
                {
                    Log.write("DBMsgHandler-WaitOne");
                    resumeEvent.Reset();
                    resumeEvent.WaitOne();
                }

                lock (dbOrderList)
                {
                    if (dbOrderList.Count != 0)
                    {
                        APParser apParser = dbOrderList[0];
                        udpsocket.Send(apParser.GetParam_Origin_byte());
                        dbOrderList.RemoveAt(0);
                        dbSentList.Add(apParser);
                        Log.write("DBMsgHandler---Send a command", 0);
                    } //处理发送数据
                }     //锁存dbOrderList


                int dbSent_count = dbSentList.Count;
                if (dbSent_count == 0)
                {
                    return;
                }//已发送命令

                lock (dbResultList)
                {
                    int dbResult_count = dbResultList.Count;
                    if (dbResult_count == 0)
                    {
                        Thread.Sleep(50);
                        return;
                    }//有处理结果返回

                    for (int i = dbResult_count - 1; i >= 0; i--)
                    {
                        dbResult_count = dbResultList.Count;

                        if (dbResult_count == 0)
                        {
                            break;
                        }//结果为0,结束

                        bool isReponsed = false;
                        for (int j = dbSent_count - 1; j >= 0; j--)
                        {
                            APParser SentParser   = dbSentList[j];
                            APParser ResultParser = dbResultList[i];
                            int      iRet         = APParser.RelatingMsg(SentParser, ResultParser);
                            if (iRet == 1)
                            {
                                database.DB_Set_Cmd(SentParser.GetID(), ResultParser.GetParam_Result());
                                //Log.write("DBMsgHandler-Get relative message:sent JobDone");
                                Log.write("DBMsgHandler---matching a result:" + SentParser.GetParam_Origin_Hex() + "\r\n" + ResultParser.GetParam_Origin_Hex() + "\r\n", 2);
                                dbSentList.RemoveAt(j);
                                dbResultList.RemoveAt(i);
                                SentParser.Dispose();
                                ResultParser.Dispose();
                                isReponsed = true;

                                break;//match and break the circle
                            }
                            else if (iRet == 0)
                            {
                                database.DB_Update_Cmd(SentParser.GetID(), ResultParser.GetParam_Cmd_SendOrNot());

                                Log.write("DBMsgHandler---matching a response:" + SentParser.GetParam_Origin_Hex() + "\r\n" + ResultParser.GetParam_Origin_Hex() + "\r\n", 2);
                                isReponsed = true;
                                dbResultList.RemoveAt(i);
                                ResultParser.Dispose();
                                break;//match and break the circle
                            }
                            else
                            {
                            }
                        }

                        if (!isReponsed)
                        {
                            APParser aa = dbResultList[i];
                            aa.Dispose();
                            dbResultList.RemoveAt(i);
                            Log.write("DBMsgHandler---inequal result", 0);
                        }
                    } //遍历匹配
                }     //锁存
            }
            catch (SqlException ex)
            {
                Log.write("DBMsgHandler SqlException Error:" + ex.Message, 0);
                SqlExceptionHelper.Deal(ex);
            }
            catch (Exception ex)
            {
                Log.write("DBMsgHandler-ThreadError:" + ex.Message, 0);
                dbOrderList.Clear();
                dbResultList.Clear();
                Log.write("DBMsgHandler-Clear All", 0);
            }
        }
Beispiel #10
0
        private void ReceiveMsg()
        {
            // 调用接收回调函数
            try
            {
                int length = server.ReceiveFrom(buffer, ref remoteEP);//接收数据报
                //message += Encoding.ASCII.GetString(buffer, 0, length);
                //MsgAlloc();

                Log.write("udpSocket-Receive size:" + length.ToString(), 2);
                if (buffer[0] != 36 || buffer[1] != 36)
                {
                    return;
                }/*$$*/

                int frame_size = buffer[2] * 0x100 + buffer[3];
                if (buffer[2 + frame_size] != 37 || buffer[3 + frame_size] != 37)
                {
                    return;
                }/*%%*/

                int all_Size = frame_size + 4;

                int valid_sum_check   = buffer[frame_size] * 0x100 + buffer[frame_size + 1];
                int valid_sum_recheck = 0;
                for (int i = 0; i < frame_size; i++)
                {
                    valid_sum_recheck += buffer[i];
                }

                if (valid_sum_recheck != valid_sum_check)
                {
                    Log.write("udpSocket-Receive:valid sum check error");
                    return;
                }

                byte[] byte_data = new byte[frame_size + 4];
                Array.Copy(buffer, byte_data, frame_size + 4);

                //状态更新
                if (!isRefreshStatus)
                {
                    isRefreshStatus = true;
                    Event_Timeout(true, null);
                }


                if (timeout_isSent)
                {
                    if (APParser.IsTimeout(byte_data, all_Size))
                    {
                        timeout_isResponse = true;
                        Log.write("UDPSocket-Receive Heartbeat");
                        return;
                    }
                }//心跳检测

                if (!APParser.IsAvaible(byte_data, all_Size))
                {
                    return;
                }//验证数据是否为有效数据

                APParser apParser = new APParser(byte_data, all_Size);

                switch (apParser.MsgKind())
                {
                case 1:    //Ap order
                {
                    lock (apOrderList)
                    {
                        if (apOrderList.Count > BufferSize)
                        {
                            Log.write("udpSocket-buffer full!", 1);
                            apParser.Dispose();
                            return;
                        }
                    }        //检测是否满了

                    lock (apOrderList)
                    {
                        apOrderList.Add(apParser);
                    }        //为AP上送数据
                    Event_ResumeAPMsgHandler(null, null);
                }
                break;

                case 2:    // db result
                {
                    lock (dbResultList)
                    {
                        dbResultList.Add(apParser);
                    }        //为数据库操作返回结果
                }
                break;

                case 3:    //alarm
                {
                    lock (alarmList)
                    {
                        alarmList.Add(apParser);
                    }        //为报警
                    Event_ResumeAlarmHandler(null, null);
                }
                break;

                case 4:    //ap lot
                {
                    lock (apLotList)
                    {
                        apLotList.Add(apParser);
                    }        //Ap Lot List
                    Event_ResumeAPLotHandler(null, null);
                }
                break;

                default:
                    break;
                }

                Log.write("udpSocket-Receive Availble Msg:" + apParser.GetParam_Origin_Hex(), 2);

                byte_data = null;

                //超时复位
                timeout_acc = 0;
            }
            catch (Exception ex)
            {
                //Log.write("UDPSocket---ReceiveMsg:ERROR-" + ex.Message);
                return;
            }
        }