//--------------------------------------------------------------------------- #pragma hdrstop #include #include "PacketHandllingF.h" #include "ClientSessionF.h" #include "VMSCommLibF.h" #include "VmsProtocolF.h" //--------------------------------------------------------------------------- #define TERROR(args...) ASession->SysLogWrite(eLOG_ERROR, ##args) #define TDEBUG(args...) ASession->LogWrite(eLOG_DEBUG, ##args) #pragma package(smart_init) //--------------------------------------------------------------------------- typedef int (*RecvPktHandlerFunc)(TClientSession *ASession, int APktLen); //--------------------------------------------------------------------------- static int UnknownPktHandler(TClientSession *ASession, int APktLen) { if (ASession->CDSLogCtlr) TERROR("+CLI Invalid packet handler: %s. will be closed.", ASession->IpAddress.c_str()); MERROR("+CLI Invalid packet handler: %s. will be closed.", ASession->IpAddress.c_str()); ASession->LogData("ERCV", (BYTE*)ASession->GetRecvBuff(), APktLen); ASession->Disconnect(); return -1; } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- static RecvPktHandlerFunc G_FuncPktHandler[e_rpkt_MAX] = {NULL}; //--------------------------------------------------------------------------- struct _init_pkt_handler { //Á¤Àû ÆÐŶÇڵ鷯 ÇÔ¼ö ÃʱâÈ­ _init_pkt_handler() { for (int ii = 0; ii < e_rpkt_MAX; ii++) { G_FuncPktHandler[ii] = UnknownPktHandler; } } }; static _init_pkt_handler init_pkt_handler; //--------------------------------------------------------------------------- struct RegisterHandler { RegisterHandler(BYTE AOpCode, RecvPktHandlerFunc AFuncHandler) { G_FuncPktHandler[(int)AOpCode] = AFuncHandler; } }; //--------------------------------------------------------------------------- #define REGISTER_HANDLER(PKT_TYPE) \ static int Handler_##PKT_TYPE(TClientSession* ASession, int APktLen); \ static RegisterHandler _register_##PKT_TYPE(PKT_TYPE, Handler_##PKT_TYPE); \ static int Handler_##PKT_TYPE(TClientSession* ASession, int APktLen) void CALLBACK RecvCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags) { TClientSession *pSession = static_cast(lpOverlapped)->FSession; if (!pSession->IsConnected) { MERROR("+SVR RecvCompletion(IsConnected false): %s[%d]", pSession->IpAddress.c_str(), pSession->Socket); return; } /// ¿¡·¯ ¹ß»ý½Ã ÇØ´ç ¼¼¼Ç Á¾·á if (dwError || cbTransferred == 0) { MERROR("+SVR RecvCompletion(dwError || cbTransferred==0): %s[%d], will be closed", pSession->IpAddress.c_str(), pSession->Socket); pSession->Disconnect(); return; } /// ¹ÞÀº µ¥ÀÌÅÍ Ã³¸® pSession->OnRead(cbTransferred); } //--------------------------------------------------------------------------- void CALLBACK SendCompletion(DWORD dwError, DWORD cbTransferred, LPWSAOVERLAPPED lpOverlapped, DWORD dwFlags) { TClientSession *pSession = static_cast(lpOverlapped)->FSession; if (!pSession->IsConnected) { MERROR("+SVR SendCompletion(IsConnected false): %s[%d]", pSession->IpAddress.c_str(), pSession->Socket); return; } /// ¿¡·¯ ¹ß»ý½Ã ÇØ´ç ¼¼¼Ç Á¾·á if (dwError || cbTransferred == 0) { MERROR("+SVR SendCompletion(dwError || cbTransferred==0): %s[%d], will be closed", pSession->IpAddress.c_str(), pSession->Socket); pSession->Disconnect(); return; } pSession->OnWriteComplete(cbTransferred, cbTransferred); } //--------------------------------------------------------------------------- static TCDSCtlr* FindControllerByIpAddr(AnsiString AIpAddress) { CtlrItr it; for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it) { TCDSCtlr *pObj = (TCDSCtlr*)it->second; if (!pObj->Used) continue; if (pObj->CTLR_IP.AnsiCompare(AIpAddress) == 0) { return pObj; } } return NULL; } //--------------------------------------------------------------------------- static TCDSCtlr* FindControllerById(AnsiString ACtrlNmbrId) { CtlrItr it; for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it) { TCDSCtlr *pObj = (TCDSCtlr*)it->second; if (!pObj->Used) continue; if (pObj->CTLR_NMBR_ID.AnsiCompare(ACtrlNmbrId) == 0) { return pObj; } } return NULL; } //--------------------------------------------------------------------------- bool TClientSession::ReqLogin() { MINFO("+CLI Client send request Login Info: %s.", FIpAddress.c_str()); // VMS ID ¿äû st_vms_id_req req; if (!SendRequest((char*)&req, sizeof(req))) { MERROR("+CLI Client send request faild: %s. will be closed.", FIpAddress.c_str()); return false; } FState = eSS_LoginReq; return true; } //--------------------------------------------------------------------------- bool TClientSession::CheckLoginClient() { #if 0 if (g_AppCfg.IsIpChecking == false) { return ReqLogin(); } #endif TCDSCtlr *pCDSCtlr = FindControllerByIpAddr(FIpAddress); if (!pCDSCtlr) { MERROR("+CLI Unknown client address: %s. will be closed.", FIpAddress.c_str()); return false; } //ÀÌÀü Á¢¼ÓÁ¤º¸°¡ ÀÖ´ÂÁö È®ÀÎÇÑ´Ù. if (FCDSCtlr) { TClientSession *pSession = FCDSCtlr->FSession; if (pSession) { //ÀÌÀü Á¢¼ÓÀ» Á¾·áÇϰí ÇöÀç Á¢¼ÓÀ» »õ·Î¿î VMS Á¦¾î±â Åë½ÅÀ¸·Î »ç¿ëÇÑ´Ù. MERROR("+CLI Client allready connect: %s. old connection will be closed.", FIpAddress.c_str()); pSession->Disconnect(); } } return ReqLogin(); // // VMS ID ¿äû st_vms_id_req req; if (!SendRequest((char*)&req, sizeof(req))) { MERROR("+CLI Client send request faild: %s. will be closed.", FIpAddress.c_str()); return false; } FState = eSS_Connected; //¿¬°á¿äû ¼ö¸³ FCDSCtlr = pCDSCtlr; FCDSLogCtlr = pCDSCtlr; FCDSCtlr->FSession = this; FCDSCtlr->LOGIN_IPADDR = FIpAddress; //¿¬°áµÈ IPÁÖ¼Ò ¼³Á¤ FCDSCtlr->Server.commState = comm_open; FCDSCtlr->Server.ConnectTm = Now().FormatString("yyyy-MM-dd hh:nn:ss"); FCtlrId.printf("%10.010d", FCDSCtlr->CTLR_NMBR.ToIntDef(0)); POST_MSG(MAINHANDLE, WM_TCP_THREAD, WM_TCP_SERVER_STTS, (int)pCDSCtlr); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff(); pMsg->Type = eVmsScenarioDownload; pMsg->ObjPtr = (DWORD)FCDSCtlr->FSession; g_jobQ.PushBlocking((DWORD)pMsg); return true; } //--------------------------------------------------------------------------- bool TClientSession::OnDisconnected() { if (FCDSCtlr) { FCDSCtlr->Server.DisconnectTm = Now().FormatString("yyyy-MM-dd hh:nn:ss"); FCDSCtlr->Server.commState = comm_close; FCDSCtlr->FSession = NULL; //FCDSCtlr->LOGIN_IPADDR = ""; LERROR("VMS %s disconnected.", CtlrId.c_str()); MERROR("%s.%d, VMS %s disconnected.", FIpAddress.c_str(), FSocket, CtlrId.c_str()); TCDSCtlr* pCDSCtlr = FCDSCtlr; POST_MSG(MAINHANDLE, WM_TCP_THREAD, WM_TCP_SERVER_STTS, (int)pCDSCtlr); } else { MERROR("%s.%d, disconnected.", FIpAddress.c_str(), FSocket); } FState = eSS_Disconnect; return true; } //--------------------------------------------------------------------------- /* * 1°³ÀÇ ÆÐŶÀ» ¸ðµÎ Àü¼ÛÇÑ ÈÄ È£ÃâµÊ */ void TClientSession::OnWrite(char *APktPtr, int ASendLen) { st_head *pHd = (st_head*)APktPtr; if (!pHd) return; if (ASendLen > 0) { LINFO("SEND: %d Bytes", ASendLen); LogData("SEND", APktPtr, ASendLen); } InfoPacket(pHd->OpCode, ASendLen, true, 0); switch(pHd->OpCode) { case VMS_ID_REQ: // 0xFF // server -> client case DNLD_SCENARIO_REQ: // 0x02 // server -> client case CTRL_RESET_REQ: // 0x03 // server -> client case DNLD_FILE_REQ: // 0x04 // server -> client case PARAMETER_REQ: // 0x05 // server -> client case CTRL_LED_POWER_REQ: // 0x06 // server -> client case CTRL_FAN_POWER_REQ: // 0x07 // server -> client case CTRL_BRGH_REQ: // 0x08 // server -> client case CTRL_CONFIG_REQ: // 0x09 // server -> client case CTRL_TIME_REQ: // 0x10 // server -> client FTmrSend = Now(); if (FChkRecvTO == false) { FTmrRecv = Now(); } FChkRecvTO = true; break; default: FChkRecvTO = false; // 0x01 // server -> client break; } } //--------------------------------------------------------------------------- /* * ÇÁ·ÎÅäÄÝ¿¡ µû¸¥ ó¸® */ void TClientSession::OnRead(int ALen) { st_head *pHd; int pktLen; FRecvIdx += ALen; if (FRecvIdx > 0) { LINFO("RECV: %d Bytes", FRecvIdx); LogData("RECV", (char*)FRecvBuff, FRecvIdx); } while(true) { // ÇÁ·ÎÅäÄÝ Çì´õ ¸¸Å­ µ¥ÀÌÅͰ¡ µé¾î¿Ô´ÂÁö üũ if (FRecvIdx < (int)sizeof(st_head)) { break; } pHd = (st_head*)FRecvBuff; // ÇÁ·ÎÅäÄÝ Çì´õÀÇ ±æÀ̰¡ ºñÁ¤»óÀûÀÎÁö üũ if (pHd->Length < 0 || pHd->Length > 2048) { MERROR("+CLI Packet parsing data length overflow: %s, %d bytes", FIpAddress.c_str(), pHd->Length); Disconnect(); break; } pktLen = (int)(pHd->Length+sizeof(st_head)+1); // µ¥ÀÌÅͱæÀÌ, Çì´õ»çÀÌÁî, ETX if (FRecvIdx < pktLen) { break; } try { if (pHd->OpCode != e_rpkt_NOTIFY_STTS) // Á¦¾î±â¿¡¼­ ½º½º·Î ¿Ã¸®´Â OpCode { FChkRecvTO = false; FTmrRecv = Now(); } if (FState == eSS_LoginReq && pHd->OpCode != e_rpkt_VMS_ID_RES) { MERROR("+CLI Not Login Unknown OPCODE: %s, %02. will be closed.", FIpAddress.c_str(), pHd->OpCode); Disconnect(); break; } // ÆÐŶó¸® InfoPacket(pHd->OpCode, pktLen, false, 0); if (G_FuncPktHandler[pHd->OpCode](this, pktLen) < 0) { Disconnect(); return; } SendFlush(); } catch(Exception &e) { MERROR("+CLI Packet parsing exception: %s, %s, %s", FIpAddress.c_str(), AnsiString(e.ClassName()).c_str(), AnsiString(e.Message).c_str()); Disconnect(); break; } } } //--------------------------------------------------------------------------- template bool TClientSession::ParsePacket(PKT_TYPE& pkt, int APktLen) { if (APktLen > FRecvIdx) { MERROR("+CLI ParsePacket Error: %s, PktLen: %d, RecvIdx: %d", FIpAddress.c_str(), APktLen, FRecvIdx); return false; } memcpy((char*)&pkt, FRecvBuff, APktLen); FRecvIdx -= APktLen; if (FRecvIdx > 0) { memmove(FRecvBuff, FRecvBuff+APktLen, FRecvIdx); } return true; } //--------------------------------------------------------------------------- bool TClientSession::InfoPacket(BYTE AOpCode, int ALen, bool ASend, int AResult/*=0*/) { TCDSCtlr* pCDSCtlr = FCDSLogCtlr; if (!pCDSCtlr) return -1; //if (!pCDSCtlr->FDispLog) return -1; //if (!g_LogCfg.Debug) return -1; if (!g_LogCfg.Info) return -1; AnsiString sCmd; switch(AOpCode) { case VMS_ID_REQ: // 0xFF // server -> client if (ASend) sCmd = "VMS_ID_REQ"; else sCmd = "VMS_ID_RES"; break; case NOTIFY_STTS_ACK: // 0x01 // server -> client if (ASend) sCmd = "NOTIFY_STTS_ACK"; else sCmd = "NOTIFY_STTS_RES"; break; case DNLD_SCENARIO_REQ: // 0x02 // server -> client if (ASend) sCmd = "DNLD_SCENARIO_REQ"; else sCmd = "DNLD_SCENARIO_RES"; break; case DNLD_FILE_REQ: // 0x04 // server -> client if (ASend) sCmd = "DNLD_FILE_REQ"; else sCmd = "DNLD_FILE_RES"; break; case PARAMETER_REQ: // 0x05 // server -> client if (ASend) sCmd = "PARAMETER_REQ"; else sCmd = "PARAMETER_RES"; break; case CTRL_LED_POWER_REQ: // 0x06 // server -> client if (ASend) sCmd = "CTRL_LED_POWER_REQ"; else sCmd = "CTRL_LED_POWER_RES"; break; case CTRL_FAN_POWER_REQ: // 0x07 // server -> client if (ASend) sCmd = "CTRL_FAN_POWER_REQ"; else sCmd = "CTRL_FAN_POWER_RES"; break; case CTRL_BRGH_REQ: // 0x08 // server -> client if (ASend) sCmd = "CTRL_BRGH_REQ"; else sCmd = "CTRL_BRGH_RES"; break; case CTRL_CONFIG_REQ: // 0x09 // server -> client if (ASend) sCmd = "CTRL_CONFIG_REQ"; else sCmd = "CTRL_CONFIG_RES"; break; case CTRL_TIME_REQ: // 0x10 // server -> client if (ASend) sCmd = "CTRL_TIME_REQ"; else sCmd = "CTRL_TIME_RES"; break; case CTRL_RESET_REQ: // 0x10 // server -> client if (ASend) sCmd = "CTRL_RESET_REQ"; else sCmd = "CTRL_RESET_RES"; break; default: sCmd.printf("UNKNOWN Packet: %02X", AOpCode); return false; } if (ASend) { if (AResult == 0) LINFO("SEND: %s, %d Bytes", sCmd.c_str(), ALen); else LERROR("SEND FAIL: %s, %d Bytes", sCmd.c_str(), ALen); } else { LINFO("RECV: %s, %d Bytes", sCmd.c_str(), ALen); } return true; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_VMS_ID_RES) { st_vms_id_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, VMS_ID_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, VMS_ID_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("VMS_ID_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); AnsiString sVmsId; sVmsId.printf("%10.10s", res.vmsid); if (ASession->State == eSS_LoginReq) { TCDSCtlr *pCDSCtlr = FindControllerById(sVmsId); if (!pCDSCtlr) { MERROR("+CLI Unknown client id: %s,%s. will be closed.", ASession->IpAddress.c_str(), sVmsId.c_str()); ASession->Disconnect(); return -1; } if (g_AppCfg.IsIpChecking) { if (ASession->IpAddress != pCDSCtlr->CTLR_IP) { MERROR("+CLI Controller IP Address Miss Match: %s,%s. (%s). will be closed.", ASession->IpAddress.c_str(), pCDSCtlr->CTLR_IP.c_str(), sVmsId.c_str()); ASession->Disconnect(); return -1; } } //ÀÌÀü Á¢¼ÓÁ¤º¸°¡ ÀÖ´ÂÁö È®ÀÎÇÑ´Ù. if (pCDSCtlr) { TClientSession *pSession = pCDSCtlr->FSession; if (pSession) { //ÀÌÀü Á¢¼ÓÀ» Á¾·áÇϰí ÇöÀç Á¢¼ÓÀ» »õ·Î¿î VMS Á¦¾î±â Åë½ÅÀ¸·Î »ç¿ëÇÑ´Ù. MERROR("+CLI Client allready connect: %s. old connection will be closed.", ASession->IpAddress.c_str()); pSession->Disconnect(); } } ASession->State = eSS_Connected; //¿¬°á¿äû ¼ö¸³ ASession->CDSCtlr = pCDSCtlr; ASession->CDSLogCtlr = pCDSCtlr; ASession->CDSCtlr->FSession = ASession; ASession->CDSCtlr->LOGIN_IPADDR = ASession->IpAddress; //¿¬°áµÈ IPÁÖ¼Ò ¼³Á¤ ASession->CDSCtlr->Server.commState = comm_open; ASession->CDSCtlr->Server.ConnectTm = Now().FormatString("yyyy-MM-dd hh:nn:ss"); ASession->CtlrId.printf("%10.010d", pCDSCtlr->CTLR_NMBR.ToIntDef(0)); POST_MSG(MAINHANDLE, WM_TCP_THREAD, WM_TCP_SERVER_STTS, (int)pCDSCtlr); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IPC_JOB_MESSAGE *pMsg = g_jobBuff.GetBuff(); pMsg->Type = eVmsScenarioDownload; pMsg->ObjPtr = (DWORD)ASession; g_jobQ.PushBlocking((DWORD)pMsg); } else { if (sVmsId != ASession->CtlrId) { MERROR("+CLI Receive packet Vms Id error: %s, %s. will be closed.", ASession->CtlrId.c_str(), sVmsId.c_str()); ASession->Disconnect(); return -2; } } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_CTRL_RESET_RES) { st_reset_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, CTRL_RESET_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_RESET_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("CTRL_RESET_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_NOTIFY_STTS) { st_notify_stts res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, NOTIFY_STTS parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, NOTIFY_STTS parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, NOTIFY_STTS controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, NOTIFY_STTS controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); st_notify_stts *pData = &res; INT_VMS_STATE *FStts = &ASession->CDSCtlr->RSTATE; TDEBUG("NOTIFY_STTS: %d bytes, Length:%d, time: %14.14s, led:%02X, door: %02X, fan:%02X, hetr:%02X, tmpr:%02X, brgh:%02X, comm:%02X", APktLen, res.Length, pData->stts.time, pData->stts.led, pData->stts.door, pData->stts.fan, pData->stts.hetr, pData->stts.tmpr, pData->stts.brgh, pData->stts.comm); memcpy(FStts->ControllerCurrentTime, pData->stts.time, INT_VMS_MAX_DATETIME); /* ¼±Åà Á¦¾î±â ½Ã°£ (YYYYMMDDHHMMSS) */ if (pData->stts.led == 0x00) FStts->ModulePowerStatus = vms_module_power_off; else if (pData->stts.led == 0x01) FStts->ModulePowerStatus = vms_module_power_on; else FStts->ModulePowerStatus = vms_module_power_unknown; if (pData->stts.door == 0x00) FStts->DoorStatus = vms_door_close; else if (pData->stts.door == 0x01) FStts->DoorStatus = vms_door_open; else FStts->DoorStatus = vms_door_unknown; if (pData->stts.fan == 0x00) FStts->FanStatus = vms_fan_off; else if (pData->stts.fan == 0x01) FStts->FanStatus = vms_fan_on; else FStts->FanStatus = vms_fan_unknown; if (pData->stts.hetr == 0x00) FStts->HeaterStatus = vms_heater_off; else if (pData->stts.hetr == 0x01) FStts->HeaterStatus = vms_heater_on; else FStts->HeaterStatus = vms_heater_unknown; FStts->BodyTemp = (short)pData->stts.tmpr; /* ÇÔü¿Âµµ°ª(¡É), ¹üÀ§(-128~127) */ FStts->LuminanceStatus = pData->stts.brgh; /* È­¸éÀÇ ¹à±â°ª (ÃÖ´ë ÈÖµµ°ªÀ» 100À¸·Î ÇßÀ» ¶§ÀÇ ¹éºÐÀ² °ª), ¹üÀ§(0~100) */ FStts->Wcomm = pData->stts.comm == 0x00 ? vms_comm_normal : vms_comm_error; /* ¹«¼±Åë½Å»óÅÂ, 0:Á¤»ó 1:Àå¾Ö */ // // ÀÀ´ä st_notify_stts_ack ack; memcpy(ack.vmsid, res.vmsid, MAX_VMS_ID); if (!ASession->SendRequest((char*)&ack, sizeof(ack))) { ASession->Disconnect(); TERROR("%s.%d, Client ack response send faild. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, Client ack response send faild. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return false; } POST_MSG(MAINHANDLE, WM_TCP_THREAD, WM_CTLR_STATE_STTS, (int)ASession->CDSCtlr); return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_DNLD_SCENARIO_RES) { st_dnld_scenario_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, DNLD_SCENARIO_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, DNLD_SCENARIO_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, DNLD_SCENARIO_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, DNLD_SCENARIO_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("DNLD_SCENARIO_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); if (res.Ack != ACK_NORMAL) { ASession->Disconnect(); TERROR("%s.%d, DNLD_SCENARIO_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, DNLD_SCENARIO_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -3; } TCDSCtlr *AObj = ASession->CDSCtlr; if (AObj) { if (AObj->CTRLMODE->Schedule) { VMS_PROVIDE_RESULE ProvideSave; memset(&ProvideSave, 0x00, sizeof(ProvideSave)); ProvideSave.Type = provide_form; ProvideSave.Count = 1; ProvideSave.pObj[0] = (void*)AObj; AObj->CTRLMODE->Enable = object_disable; AObj->CTRLMODE->SvcDate = Now().FormatString("yyyymmddhhnnss"); AObj->CTRLMODE->SaveFlag = true; AObj->CTRLMODE->Result = true; APP_PostDbThreadMessage(dbm_provide_result, sizeof(ProvideSave), &ProvideSave); } } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_DNLD_FILE_RES) { st_dnld_file_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, DNLD_FILE_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, DNLD_FILE_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, DNLD_FILE_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, DNLD_FILE_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("DNLD_FILE_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); if (res.Ack != ACK_NORMAL) { ASession->Disconnect(); TERROR("%s.%d, DNLD_FILE_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, DNLD_FILE_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -3; } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_PARAMETER_RES) { st_parameter_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, PARAMETER_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, PARAMETER_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, PARAMETER_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, PARAMETER_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("PARAMETER_RES: %d bytes, Length:%d, Ack:%02X, led:%02X, fan:%02X, fanTmpr:%02X, hetr:%02X, hetrTmpr:%02X, brgh:%02X, brghVal:%02X, brghDay:%02X, brghNight:%02X, time:%14.14s", APktLen, res.Length, res.Ack, res.para.led, res.para.fan, res.para.fanTmpr, res.para.hetr, res.para.hetrTmpr, res.para.brgh, res.para.brghVal, res.para.brghDay, res.para.brghNight, res.para.time); //ÆÄ¶ó¹ÌÅÍ ¿äû ÀÀ´ä if (res.Ack != ACK_NORMAL) { ASession->Disconnect(); TERROR("%s.%d, PARAMETER_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, PARAMETER_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -3; } TCDSCtlr *AObj = ASession->CDSCtlr; if (AObj) { AObj->PANL_PWER_MODE = res.para.led; // N NUMBER(3) Y 2 Àü±¤ÆÇ Àü¿ø ¸ðµå(0x00:²¨Áü,0x01:ÄÑÁü,0x02:ÀÚµ¿,0x09:¾Ë¼ö¾øÀ½) AObj->FAN_MODE = res.para.fan; // N NUMBER(3) Y 2 FAN µ¿ÀÛ¸ðµå(0x00:²¨Áü,0x01:ÄÑÁü,0x02:ÀÚµ¿,0x09:¾Ë¼ö¾øÀ½) AObj->FAN_RUN_TMPR = res.para.fanTmpr; // N NUMBER(3) Y 30 ÆÒ µ¿ÀÛ ¿Âµµ AObj->HETR_MODE = res.para.hetr; // N NUMBER(3) Y 2 È÷ÅÍ µ¿ÀÛ¸ðµå(0x00:²¨Áü,0x01:ÄÑÁü,0x02:ÀÚµ¿,0x09:¾Ë¼ö¾øÀ½) AObj->HETR_RUN_TMPR = res.para.hetrTmpr; // N NUMBER(3) Y 0 È÷ÅÍ µ¿ÀÛ ¿Âµµ AObj->BRGH_MODE = res.para.brgh; // N NUMBER(3) Y 2 ÈÖµµ ¸ðµå(0x00:ÁÖ°£,0x01:¾ß°£,0x00:ÀÚµ¿,0x09:¾Ë¼ö¾øÀ½) AObj->BRGH_CURR_STEP = res.para.brghVal; // N NUMBER(3) Y 40 ÈÖµµ ÇöÀç ´Ü°è(0~100) AObj->BRGH_WEEK_STEP = res.para.brghDay; // N NUMBER(3) Y 64 ÈÖµµ ÁÖ°£ ´Ü°è(0~100) AObj->BRGH_NGHT_STEP = res.para.brghNight; // N NUMBER(3) Y 48 ÈÖµµ ¾ß°£ ´Ü°è(0~100) AObj->ParamResTime.printf("%s", res.para.time); CTLR_STTS stts; stts.Type = 2; stts.ObjPtr = (void*)AObj; APP_PostDbThreadMessage(dbm_parma_res, sizeof(stts), &stts); } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_CTRL_LED_POWER_RES) { st_ctrl_led_power_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, CTRL_LED_POWER_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_LED_POWER_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, CTRL_LED_POWER_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_LED_POWER_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("CTRL_LED_POWER_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); if (res.Ack != ACK_NORMAL) { ASession->Disconnect(); TERROR("%s.%d, CTRL_LED_POWER_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_LED_POWER_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -3; } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_CTRL_FAN_POWER_RES) { st_ctrl_fan_power_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, CTRL_FAN_POWER_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_FAN_POWER_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, CTRL_FAN_POWER_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_FAN_POWER_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("CTRL_FAN_POWER_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); if (res.Ack != ACK_NORMAL) { ASession->Disconnect(); TERROR("%s.%d, CTRL_FAN_POWER_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_FAN_POWER_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -3; } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_CTRL_BRGH_RES) { st_ctrl_brgh_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, CTRL_BRGH_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_BRGH_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, CTRL_BRGH_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_BRGH_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("CTRL_BRGH_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); if (res.Ack != ACK_NORMAL) { ASession->Disconnect(); TERROR("%s.%d, CTRL_BRGH_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_BRGH_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -3; } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_CTRL_CONFIG_RES) { st_ctrl_cfg_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, CTRL_CONFIG_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_CONFIG_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, CTRL_CONFIG_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_CONFIG_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("CTRL_CONFIG_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); if (res.Ack != ACK_NORMAL) { ASession->Disconnect(); TERROR("%s.%d, CTRL_CONFIG_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_CONFIG_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -3; } return 0; } //--------------------------------------------------------------------------- ///////////////////////////////////////////////////////// REGISTER_HANDLER(e_rpkt_CTRL_TIME_RES) { st_ctrl_time_res res; if (false == ASession->ParsePacket(res, APktLen)) { TERROR("%s.%d, CTRL_TIME_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_TIME_RES parsePacket error. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -1; } if (!ASession->CDSCtlr) { TERROR("%s.%d, CTRL_TIME_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_TIME_RES controller not found. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -2; } //ASession->InfoPacket(res.OpCode, APktLen, false, 0); TDEBUG("CTRL_TIME_RES: %d bytes, Length:%d, Ack:%02X", APktLen, res.Length, res.Ack); if (res.Ack != ACK_NORMAL) { ASession->Disconnect(); TERROR("%s.%d, CTRL_TIME_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); MERROR("%s.%d, CTRL_TIME_RES failed. will be closed.", ASession->IpAddress.c_str(), ASession->Socket); return -3; } return 0; } //--------------------------------------------------------------------------- int TClientSession::DownloadScenarioForm(bool ASchedule/*=true*/) { int nSelCnt = 0; TCDSCtlr *AObj = FCDSCtlr; if (!AObj) return -1; int nFormCnt = AObj->pForms->Count(); if (nFormCnt == 0) return nSelCnt; if (nFormCnt > INT_VMS_MAX_FORM) nFormCnt = INT_VMS_MAX_FORM; AnsiString sDispStrTm = Now().FormatString("yyyymmddhhnn"); AnsiString sDispEndTm = (IncMinute(Now(), 10)).FormatString("yyyymmddhhnn"); if (AObj->CTRLMODE->Control == 'B') { //µðÆúÆ® ½Ã³ª¸®¿ÀÀÎ °æ¿ì... sDispEndTm = "20991231235959"; } st_dnld_scenario_req req; st_scenario *pScn; memset((char*)&req, 0x00, sizeof(st_dnld_scenario_req)); req.Stx = VMS_STX; req.OpCode = (BYTE)DNLD_SCENARIO_REQ; memcpy(req.vmsid, FCtlrId.c_str(), sizeof(req.vmsid)); req.Length = 0; // count senario// //scenario info //ETX AObj->pForms->Lock(); try { for (int ii = 0; ii < nFormCnt; ii++) { TVmsForm *pForm = AObj->pForms->GetItem(ii); if (!pForm->Success) continue; pScn = &req.scenario[nSelCnt++]; pScn->seq = (BYTE)nSelCnt; // ½Ã³ª¸®¿À ¼ø¹ø pScn->type = pForm->fileType; // Ç¥ÃâŸÀÔ if (pForm->fileType == P_FILE_TYPE_VIDEO || pForm->fileType == P_FILE_TYPE_STREAM) { pScn->dispEffect = 0x00; // Ç¥ÃâÈ¿°ú } else { pScn->dispEffect = (BYTE)pForm->VMS_FORM_DSPL_MTHD_CD.ToIntDef(0); } sprintf(pScn->fileName, "%s", pForm->FtpFileName.c_str()); // ÆÄÀϸí pScn->dispTm = (pForm->fileType == P_FILE_TYPE_VIDEO) ? 0 : (BYTE)pForm->DSPL_HH; // Ç¥Ãâ¹®±¸ Ç¥Ãâ½Ã°¢('03' : 3ÃÊ) memcpy(pScn->dispStrTm, sDispStrTm.c_str(), MAX_DISP_TIME); // Ç¥Ãâ½ÃÀ۽ð£(0000 00 00 0000) memcpy(pScn->dispEndTm, sDispEndTm.c_str(), MAX_DISP_TIME); // Ç¥ÃâÁ¾·á½Ã°¢(999912312359) pScn->dnldFlag = 'D'; // ÆÄÀÏ´Ù¿î·Îµå ¿©ºÎ D : ´Ù¿î·Îµå if (nSelCnt >= MAX_SCENARIO) break; } } __finally { AObj->pForms->UnLock(); } if (nSelCnt == 0) return 0; AObj->CTRLMODE->Schedule = ASchedule; // ½ºÄÉÁì ¸ðµåÀÎ °æ¿ì µðºñ¿¡ ÀúÀåÇÑ´Ù. req.type = (AObj->CTRLMODE->Control == 'B') ? 0x00 : 0x01; req.cnt = (BYTE)nSelCnt; // ÇØ´ç VMSÀÇ Àüü½Ã³ª¸®¿À ¼ö req.Length = (short)((nSelCnt * sizeof(st_scenario))+2); req.scenario[nSelCnt].seq = VMS_ETX; //½Ã³ª¸®¿À ±¸Á¶Ã¼ÀÇ Ã¹¹øÂ°°¡ ETX ÀÚ¸®ÀÓ int nSendBytes = sizeof(st_head) + req.Length + 1; if (!SendRequest((char*)&req, nSendBytes)) { SERROR("+CLI DownloadScenarioForm send request faild: %s will be closed.", FIpAddress.c_str()); MERROR("+CLI DownloadScenarioForm send request faild: %s will be closed.", FIpAddress.c_str()); Disconnect(); return -1; } return nSelCnt; } //--------------------------------------------------------------------------- int TClientSession::ReqParameter() { TCDSCtlr *AObj = FCDSCtlr; if (!AObj) return -1; st_parameter_req req; memcpy(req.vmsid, FCtlrId.c_str(), sizeof(req.vmsid)); req.OpCode = (BYTE)PARAMETER_REQ; if (!SendRequest((char*)&req, sizeof(req))) { SERROR("+CLI ReqParameter send request faild: %s will be closed.", FIpAddress.c_str()); MERROR("+CLI ReqParameter send request faild: %s will be closed.", FIpAddress.c_str()); Disconnect(); return -1; } return 1; } //--------------------------------------------------------------------------- int TClientSession::ReqLedPowerControl(BYTE AOnOff) { TCDSCtlr *AObj = FCDSCtlr; if (!AObj) return -1; st_ctrl_led_power_req req; memcpy(req.vmsid, FCtlrId.c_str(), sizeof(req.vmsid)); req.cmd = AOnOff; if (!SendRequest((char*)&req, sizeof(req))) { SERROR("+CLI ReqLedPowerControl send request faild: %s will be closed.", FIpAddress.c_str()); MERROR("+CLI ReqLedPowerControl send request faild: %s will be closed.", FIpAddress.c_str()); Disconnect(); return -1; } return 1; } //--------------------------------------------------------------------------- int TClientSession::ReqFanPowerControl(BYTE AOnOff) { TCDSCtlr *AObj = FCDSCtlr; if (!AObj) return -1; st_ctrl_fan_power_req req; memcpy(req.vmsid, FCtlrId.c_str(), sizeof(req.vmsid)); req.cmd = AOnOff; if (!SendRequest((char*)&req, sizeof(req))) { SERROR("+CLI ReqFanPowerControl send request faild: %s will be closed.", FIpAddress.c_str()); MERROR("+CLI ReqFanPowerControl send request faild: %s will be closed.", FIpAddress.c_str()); Disconnect(); return -1; } return 1; } //--------------------------------------------------------------------------- int TClientSession::ReqTimeControl(char *ATime) { TCDSCtlr *AObj = FCDSCtlr; if (!AObj) return -1; st_ctrl_time_req req; memcpy(req.vmsid, FCtlrId.c_str(), sizeof(req.vmsid)); //memcpy(req.set.time, ATime, MAX_SYS_TIME); memcpy(req.set.time, AnsiString(Now().FormatString("yyyymmddhhnnss")).c_str(), MAX_SYS_TIME); if (!SendRequest((char*)&req, sizeof(req))) { SERROR("+CLI ReqTimeControl send request faild: %s will be closed.", FIpAddress.c_str()); MERROR("+CLI ReqTimeControl send request faild: %s will be closed.", FIpAddress.c_str()); Disconnect(); return -1; } return 1; } //--------------------------------------------------------------------------- int TClientSession::ReqBrightSet(char *ACmd) { TCDSCtlr *AObj = FCDSCtlr; if (!AObj) return -1; SIGNT_BRGH_SET *pCmd = (SIGNT_BRGH_SET*)ACmd; st_ctrl_brgh_req req; memcpy(req.vmsid, FCtlrId.c_str(), sizeof(req.vmsid)); req.set.brgh = pCmd->BrghMode; req.set.brghVal = pCmd->BrghCurrStep; req.set.brghDay = pCmd->BrghWeekStep; req.set.brghNight = pCmd->BrghNghtStep; if (!SendRequest((char*)&req, sizeof(req))) { SERROR("+CLI ReqBrightSet send request faild: %s will be closed.", FIpAddress.c_str()); MERROR("+CLI ReqBrightSet send request faild: %s will be closed.", FIpAddress.c_str()); Disconnect(); return -1; } return 1; } //--------------------------------------------------------------------------- int TClientSession::ReqConfigSet(char *ACmd) { TCDSCtlr *AObj = FCDSCtlr; if (!AObj) return -1; SIGNT_CONFIG_SET *pCmd = (SIGNT_CONFIG_SET*)ACmd; st_ctrl_cfg_req req; memcpy(req.vmsid, FCtlrId.c_str(), sizeof(req.vmsid)); req.set.fanTmpr = pCmd->FanRunTmpr; req.set.hetrTmpr = pCmd->HetrRunTmpr; memcpy(req.set.dispOnTm, pCmd->PanlOnTime, sizeof(req.set.dispOnTm)); memcpy(req.set.dispOffTm, pCmd->PanlOffTime, sizeof(req.set.dispOffTm)); if (!SendRequest((char*)&req, sizeof(req))) { SERROR("+CLI ReqConfigSet send request faild: %s will be closed.", FIpAddress.c_str()); MERROR("+CLI ReqConfigSet send request faild: %s will be closed.", FIpAddress.c_str()); Disconnect(); return -1; } return 1; } //--------------------------------------------------------------------------- int TClientSession::ReqReset(int AMode) { TCDSCtlr *AObj = FCDSCtlr; if (!AObj) return -1; st_reset_req req; memcpy(req.vmsid, FCtlrId.c_str(), sizeof(req.vmsid)); req.OpCode = (BYTE)CTRL_RESET_REQ; req.type = (BYTE)AMode; //Á¦¾î±â ¸®¼Â if (!SendRequest((char*)&req, sizeof(req))) { SERROR("+CLI ReqReset send request faild: %s will be closed.", FIpAddress.c_str()); MERROR("+CLI ReqReset send request faild: %s will be closed.", FIpAddress.c_str()); Disconnect(); return -1; } return 1; } //---------------------------------------------------------------------------