//工作-处理终端状态更新 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 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); } }
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; } }