📄 frmclient.cpp
字号:
#include "frmclient.h"#include <qevent.h> #include <qmessagebox.h>#include <qmultilineedit.h>#include <qlineedit.h>#include <qstring.h>#include <qimage.h>#include <qpainter.h>#include <qfile.h>#include <qdatetime.h> #include "./openCVinclude/cv.h"#include "./openCVinclude/highgui.h"CvCapture* capture = 0;frmclient::frmclient( QWidget* parent, const char* name, bool modal, WFlags fl ) : ClientForm( parent, name, modal, fl ){ this->SystemInit(); this->DisplayPicture(); connect( btnOpen, SIGNAL( clicked()), this, SLOT(OpenPort())); //////////////GSM Control connect( btnGSM, SIGNAL (clicked()), this, SLOT(InitGSM())); connect( btnDeleteMsg, SIGNAL( clicked()), this, SLOT(DeleMsg())); connect( btnReadMsg, SIGNAL(clicked()), this, SLOT(ReadMsg())); connect( btnSendMsg, SIGNAL(clicked()), this, SLOT(SendMsg())); /////////////////////////home device control/* connect( btnLight, SIGNAL(clicked()), this, SLOT(LightCtrl())); connect( btnPower, SIGNAL(clicked()), this, SLOT(PowerCtrl())); connect( btnGasSwitch, SIGNAL(clicked()), this, SLOT(GasSwitchCtrl())); connect( btnAir, SIGNAL(clicked()), this, SLOT(AirConditionCtrl())); //////////////////////////sensor control connect( btnIrdaAlert, SIGNAL( clicked() ) , this , SLOT( IrdaAlertCtrl() ) ) ; connect( btnGasAlert, SIGNAL( clicked() ) , this , SLOT( GasAlertCtrl() ) ) ; connect( btnSmokeAlert, SIGNAL( clicked() ) , this , SLOT( SmokeAlertCtrl() )) ;*/ // connection of user's action on control modules connect( btnGPS, SIGNAL( clicked() ) , this , SLOT( GpsControl() ) ) ;// connect( btnNetwork, SIGNAL( clicked() ) , this , SLOT( NetworkControl() ) ) ;// connect( btnCamera, SIGNAL( clicked() ) , this , SLOT( CameraControl() ) ) ;// connect( btnRecord, SIGNAL( clicked() ) , this , SLOT( RecordControl() ) ) ; //////// connection of network connect( socket, SIGNAL(connected()), this ,SLOT(SocketConnected()) ); connect( socket, SIGNAL(connectionClosed()), this, SLOT(ServerConnectionClosed()) ); connect( socket, SIGNAL(readyRead()), this,SLOT(SocketReadyRead()) ); connect (btnClearDisplay, SIGNAL(clicked()), txtMsgContent, SLOT(clear())); connect(cmbBaud, SIGNAL( activated(int)), this, SLOT( baudRateChange(int) ) ); connect(cmbPort, SIGNAL( activated(int)), this, SLOT( PortChange(int) ) ); /////////////////QTimer connect( SendBasicTimer , SIGNAL( timeout() ) , this , SLOT(SendBasicInfo() ) ) ; connect( SengEmergentTimer, SIGNAL(timeout()), this, SLOT(SendEmergentMessage())); connect( ReadGsmTimer, SIGNAL (timeout()), this, SLOT(ReadGsmPort())); connect( ReadBoardTimer, SIGNAL (timeout()), this, SLOT(ReadBoardPort())); connect( CameraTimer, SIGNAL(timeout()), this, SLOT(SendPicture())); connect (refreshTimer, SIGNAL(timeout()), this, SLOT(RefreshTime())); refreshTimer->start(1000,FALSE); }frmclient::~frmclient(){ if(bconnect) { socket->flush() ; ::sleep(1); socket->close(); }}void frmclient::PortChange(int){ portIndex=cmbPort->currentItem();}void frmclient::baudRateChange(int){ nBaudindex=cmbBaud->currentItem(); }void frmclient::OpenPort(){ switch(portIndex) { case 0: break; case 1: if(gsmfd>0) { gsmfd=-1; pGsmControl->Close();//停止GSM串口 ReadGsmTimer->stop(); } gsmfd=pGsmControl->Open(portIndex,nBaudindex); break; case 2: if(gpsfd>0) { pBoardSerial->closePort(gpsfd); gpsfd=-1; ReadBoardTimer->stop(); } gpsfd=pBoardSerial->openPort(portIndex); if(gpsfd>0) { pBoardSerial->setPara(gpsfd,nBaudindex,8,1,0); ReadBoardTimer->start(2*1000,FALSE);//3 second } else perror("Error: The port is not open.\n"); break; default: break; } }//////////////////////////////////////////////////////////////////////// GSM Control Function start ///////////////////////////////////////////////////////////////////////void frmclient::InitGSM() { if(GSMEnable) { GSMEnable=FALSE; btnGSM->setPixmap( GSMdisableicon ) ; ReadGsmTimer->stop(); binitGsm=FALSE; } else { if( gsmfd>0) { ReadGsmTimer->start(200,FALSE); binitGsm=TRUE; pGsmControl->InitGPRS(); } else { GSMEnable=FALSE; QMessageBox::information(this,"information","Serial port is not open"); } }}void frmclient::DeleMsg(){ QString strMsgIndex; strMsgIndex=txtMsgIndex->text(); if(strMsgIndex.length()<1) { QMessageBox::information(this,"information","Message Index is empty"); return; } if(gsmfd) { bdeleMsg=TRUE; pGsmControl->DeleteMessage(strMsgIndex); } }void frmclient::ReadMsg(){ QString strMsgIndex; strMsgIndex=txtMsgIndex->text(); if(strMsgIndex.length()<1) { QMessageBox::information(this,"information","Message Index is empty"); return; } if(gsmfd) { breadMsg=TRUE; strMsgContent=""; pGsmControl->ReadMessage(strMsgIndex); } }void frmclient::SendMsg(){ QString strSendMsg=txtMsgContent->text(); strMobilephonenumber=txtPoneNumber->text(); SendMsgData(strSendMsg, strMobilephonenumber); }void frmclient::SendMsgData(QString strContent, QString strNumber){ strEncodeMsg=""; QString strServerNumber=txtServerNumber->text(); if(strNumber.length()!=11) { QMessageBox::information(this,"information","Moble Phone Number is error!"); return; } if(strServerNumber.length()!=11) { QMessageBox::information(this,"information","SMS Server Number is error!"); return; } if(strContent.isEmpty()) { QMessageBox::information(this,"information","Message content is empty!"); return; } if(gsmfd) { bsendMsg=TRUE; strEncodeMsg=pGsmControl->PreSendMessage( strContent, strNumber,strServerNumber) ; txtMsgContent->insert(strEncodeMsg); } }/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void frmclient::ReadGsmPort(){ int buflen= -1; if(gsmfd) { memset(GsmRecvbuf,0,sizeof(GsmRecvbuf)); buflen=::read( gsmfd, GsmRecvbuf, SBUFFER_LEN); if (buflen>0) { GsmRecvbuf[buflen]=0; QString str(GsmRecvbuf); txtMsgContent->insert(str); this->ProcessGsmData(str); } }}void frmclient::ProcessGsmData(QString strRecv){ if(binitGsm) { if(strRecv.find("OK", false)>=0 ) { binitGsm=FALSE; this->enableGsmButton(TRUE); txtMsgContent->insert("Init Ok\n"); btnGSM->setPixmap( GSMenableicon ) ; GSMEnable=TRUE; } } ///////////////Delete Message operation if(bdeleMsg) { if(strRecv.find("OK", false)>=0 ) { bdeleMsg=FALSE; txtMsgContent->insert("Detele Message success\n"); } } ////////////Read message operation if(breadMsg) { strMsgContent+=strRecv; if(strRecv.find("OK",false)>=0)//读取返回确认信息 { strMsgContent=pGsmControl->ProcessMessage(strMsgContent); breadMsg=FALSE; txtMsgContent->insert(strMsgContent); txtMsgContent->insert("\n"); if(bnewMsg) { int index=strMsgContent.find( "From:" ) ; strMobilephonenumber=strMsgContent.mid( index+8, 11 ); if(strMsgContent.find("ctrl", false) >0) { ControlMsgProcess(strMsgContent); QString strOk="Your control command executed successfully !"; this->SendMsgData(strOk,strMobilephonenumber); } if(strMsgContent.find("read", false) >=0) { // txtMsgContent->insert("running here\n"); this->SendStatusMessage(); } if(strMsgContent.find("know",false) >=0) //报警反馈 { SengEmergentTimer->stop(); IRDAalertmessage=Gasalertmessage=Smokealertmessage=0; if(IRDAalert) { IRDAalert=0;// picIrdaAlert->setPixmap( Alarmnoalarmicon ); } if(Smokealert) { Smokealert=0;// picSmokeAlert->setPixmap( Alarmnoalarmicon ); } if(Gasalert) { Gasalert=0;// picGasAlert->setPixmap( Alarmnoalarmicon ); } if(bconnect>0) { SendBasicTimer->start( 200 , TRUE ) ; bSendBasicInfo=TRUE; } Emergentflag=FALSE; } bnewMsg=FALSE; } //if(strMsgContent.find("realert", false) >=0)// 重新启动报警 //Emergentflag=FALSE; } } /////New Message arrived Read Message Auto if(strRecv.find("+CMTI",false)>=0) { int index=strRecv.find(","); QString msgIndex; msgIndex=strRecv.mid(index+1,2); strMsgContent=""; breadMsg=TRUE; txtMsgContent->insert("New Message arrive\n"); bnewMsg=TRUE; pGsmControl->ReadMessage(msgIndex); } //////////////send Message content if(strRecv.find (">",false)>=0) { pGsmControl->SendMessage(strEncodeMsg); strEncodeMsg=""; } }/*void frmclient::ReadBoardPort(){ int buflen= -1; if(gpsfd) { memset(BoardRecvbuf,0,sizeof(BoardRecvbuf)); buflen=::read( gpsfd, BoardRecvbuf, SBUFFER_LEN); if (buflen>0) { BoardRecvbuf[buflen]=0; QString strRecv0(BoardRecvbuf); nReadCount++; if(nReadCount<4) { strBoardMsg+=strRecv0; } else { nReadCount=0; if( (strBoardMsg.find( "#$#1", false) >=0) &&IRDAenable)//红外警报 { IRDAalert=1; IRDAalertmessage=1;// picIrdaAlert->setPixmap( Alarmalarmicon); if(Emergentflag==FALSE) { SengEmergentTimer->start(60*1000,FALSE);//10秒一次 SendEmergentMessage(); } if(bconnect>0) { SendBasicTimer->start( 200 , TRUE ) ; bSendBasicInfo=TRUE; } } if((strBoardMsg.find( "#$#2", false)>=0) && Smokeenable)//烟雾警报 { Smokealert=1; Smokealertmessage=1;// picSmokeAlert->setPixmap( Alarmalarmicon ); if(Emergentflag==FALSE) { SengEmergentTimer->start(60*1000,FALSE);//10秒一次 SendEmergentMessage(); }// if(bconnect>0) { SendBasicTimer->start( 200 , TRUE ) ; bSendBasicInfo=TRUE; } } if((strBoardMsg.find( "#$#3", false)>=0) && Gasenable)//煤气警报 { Gasalert=1; Gasalertmessage=1;// picGasAlert->setPixmap( Alarmalarmicon ); if(Emergentflag==FALSE) { SengEmergentTimer->start(60*1000,FALSE);//10秒一次 SendEmergentMessage(); } if(bconnect>0) { SendBasicTimer->start( 200 , TRUE ) ; bSendBasicInfo=TRUE; } } strBoardMsg="k"; } } }}*/void frmclient::ControlMsgProcess(QString strMsg){ // control the home device if ( strMsg.find( "poweron", false ) >=0 ) bPowerOn=TRUE; else if ( strMsg.find( "poweroff", false ) >=0 ) bPowerOn=FALSE; else if ( strMsg.find( "gasswitchon", false ) >=0 ) bGasswitchOn=TRUE; else if ( strMsg.find( "gasswitchoff", false ) >=0 ) bGasswitchOn=FALSE; else if ( strMsg.find( "airconditionon", false ) >=0 ) bAirconditionOn=TRUE; else if ( strMsg.find( "airconditionoff", false ) >=0 ) bAirconditionOn=FALSE; else if ( strMsg.find( "lighton", false ) >=0 ) bLightOn=TRUE; else if ( strMsg.find( "lightoff", false ) >=0 ) bLightOn=FALSE; // btnPower->setPixmap( bPowerOn==TRUE ? Poweronicon : Powerofficon) ; // btnGasSwitch->setPixmap( bGasswitchOn==TRUE ? Poweronicon :Powerofficon ) ; //btnAir->setPixmap( bAirconditionOn==TRUE ? Poweronicon :Powerofficon ) ; // btnLight->setPixmap( bLightOn==TRUE ? Poweron1icon : Powerofficon ) ; ControlPower(); // control the sensor if ( strMsg.find( "irdaon", false ) >=0 )//短信启动报警使能 IRDAenable=TRUE; else if ( strMsg.find( "irdaoff", false ) >=0 ) IRDAenable=FALSE; else if ( strMsg.find( "smokeon", false ) >=0 ) Smokeenable=TRUE; else if ( strMsg.find( "smokeoff", false ) >=0 ) Smokeenable=FALSE; if ( strMsg.find( "gason", false ) >=0 ) Gasenable=TRUE; else if ( strMsg.find( "gasoff", false ) >=0 ) Gasenable=FALSE; /* btnIrdaAlert->setPixmap(IRDAenable==TRUE ? Alarmonicon :Alarmofficon); btnSmokeAlert->setPixmap( Smokeenable==TRUE ? Alarmonicon :Alarmofficon); btnGasAlert->setPixmap( Gasenable==TRUE ? Alarmonicon :Alarmofficon);*/ if(bconnect>0) { SendBasicTimer->start( 200 , TRUE ) ; bSendBasicInfo=TRUE; } }void frmclient::ControlPower(){ QString strCtrl=QString( "GSA" ) ; QString strcmd; strCtrl+=strcmd.setNum( bPowerOn ) ; strCtrl+=strcmd.setNum( bGasswitchOn ) ; strCtrl+=strcmd.setNum( bAirconditionOn) ; strCtrl+=strcmd.setNum( bLightOn) ; if(gpsfd) { const char *chCtrl=strCtrl.latin1(); int len=::strlen( chCtrl ) ; pBoardSerial->nwrite( gpsfd, chCtrl, len) ; } }void frmclient::SendStatusMessage(){ QString strStatus=""; if ( bPowerOn) strStatus+=QString( "PowerOn " ) ; if ( !bPowerOn ) strStatus+=QString( "PowerOff " ) ; if ( bGasswitchOn ) strStatus+=QString( "GasswitchOn " ) ; if ( !bGasswitchOn ) strStatus+=QString( "GasswitchOff " ) ; if ( bAirconditionOn) strStatus+=QString( "AirOn " ) ; if ( !bAirconditionOn ) strStatus+=QString( "AirOff " ) ; if ( bLightOn ) strStatus+=QString( "LightOn " ) ; if ( !bLightOn ) strStatus+=QString( "LightOff " ) ; if ( IRDAenable ) strStatus+=QString( "IRDAOn " ) ; if ( !IRDAenable ) strStatus+=QString( "IRDAOff " ) ; if ( Gasenable==1 ) strStatus+=QString( "GasOn " ) ; if ( Gasenable==0 ) strStatus+=QString( "GasOff " ) ; if ( Smokeenable==1 ) strStatus+=QString( "SmokeOn " ) ; if ( Smokeenable==0 ) strStatus+=QString( "SmokeOff " ) ; SendMsgData(strStatus,strMobilephonenumber);}void frmclient::SendEmergentMessage(){ txtMsgContent->insert("\r\n run SendEmergentMessage\r\n");////////////////////debug if ( GSMEnable==TRUE ) { QString strEmergentMsg=""; if ( IRDAalertmessage==1) strEmergentMsg+=QString( "There is illegal invasion !!" ) ; if ( Gasalertmessage==1 ) strEmergentMsg+=QString( "Gas is leaking !!" ) ; if ( Smokealertmessage==1 ) strEmergentMsg+=QString( "Fire ! Fire !" ) ; strMobilephonenumber=txtPoneNumber->text(); this->SendMsgData(strEmergentMsg, strMobilephonenumber); Emergentflag=TRUE; } }////////////////////////////////////////////// 家用电器控制操作,共四个函数//////////////////////////////////////////void frmclient::LightCtrl()//灯开关控制{ if( (bconnect>0) || (controlfd>0)) { bLightOn=!bLightOn;// btnLight->setPixmap( bLightOn==TRUE? Poweron1icon : Powerofficon ) ; if(bconnect>0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -