00001 00002 #include "srcp083_client.h" 00003 00004 Srcp083Client::Srcp083Client( QObject *parent ) : SrcpVirtualClient( parent ) 00005 { 00006 00007 } 00008 00009 Srcp083Client::~Srcp083Client() 00010 { 00011 00012 } 00013 00014 void Srcp083Client::srcpHandshake() 00015 { 00016 SrcpSocket *target = static_cast<SrcpSocket*>( sender() ) ; 00017 QString welcome = target->srcpReadLine() ; 00018 00019 if ( welcome.isEmpty() ) 00020 { 00021 emit errorOccured( trUtf8("The SRCP welcome string is empty.") ) ; 00022 setConnection( FALSE ) ; 00023 return ; 00024 } 00025 00026 QStringList welcomeInfos = welcome.split( ";" ) ; // Separate key-value pairs 00027 00028 bool serverSupportSrcp083 = FALSE ; 00029 QString temp ; 00030 for( int i = 0 ; i < welcomeInfos.count() ; ++i ) 00031 { 00032 temp = welcomeInfos.at( i ).trimmed() ; 00033 00034 if ( temp.contains( "SRCP" ) && 00035 temp.split( " " ).at( 1 ) == "0.8.3" ) 00036 { 00037 serverSupportSrcp083 = TRUE ; 00038 break ; 00039 } 00040 } 00041 00042 if ( serverSupportSrcp083 ) 00043 { 00044 target->sendMsg( "SET PROTOCOL SRCP 0.8.3" ) ; 00045 Srcp083::MsgCode r = returnMsgCode( target->srcpReadLine() ) ; 00046 if ( r == Srcp083::Ok || r == Srcp083::ProtocolOk ) 00047 { 00048 target->sendMsg( QString( "SET CONNECTIONMODE SRCP %1" ).arg( target->type() ) ) ; 00049 r = returnMsgCode( target->srcpReadLine() ) ; 00050 if ( r == Srcp083::Ok || r == Srcp083::ConnectionModeOk ) 00051 { 00052 target->sendMsg( "GO" ) ; 00053 r = returnMsgCode( target->srcpReadLine() ) ; 00054 if ( r == Srcp083::Ok ) 00055 { 00056 if ( target->type() == "INFO" ) 00057 target->start() ; // start the info thread 00058 return ; 00059 } 00060 } 00061 } 00062 } 00063 00064 emit errorOccured( trUtf8( "The SRCP hanshake failled" ) ) ; 00065 setConnection( FALSE ) ; 00066 } 00067 00068 Srcp083::MsgCode Srcp083Client::returnMsgCode( QString reply ) 00069 { 00070 QStringList r = reply.split( " " ) ; 00071 00072 if ( r.count() > 1 ) 00073 return static_cast<Srcp083::MsgCode>( r.at( 1 ).toInt() ); // FIXME Not safe !! 00074 else 00075 return static_cast<Srcp083::MsgCode>( 0 ) ; // FIXME Not safe !! 00076 } 00077 00078 void Srcp083Client::parseInfo( QString info ) 00079 { 00080 QStringList r = info.split( " " ) ; 00081 00082 if ( returnMsgCode( info ) != Srcp083::Info ) 00083 return ; 00084 00085 if ( r.at( 4 ) == "GL" ) 00086 emit infoReceivedEngineState( 00087 r.at( 5 ).toInt(), // address 00088 r.at( 6 ).toInt() == 1, // direction 00089 (int)round( r.at( 7 ).toFloat() * 100 / r.at( 8 ).toFloat() ), // speed in % 00090 r.at( 9 ).toInt() == 1, // light, f1 00091 10 > r.size() ? FALSE : r.at( 10 ).toInt() == 1,// f2 00092 11 > r.size() ? FALSE : r.at( 11 ).toInt() == 1,// f3 00093 12 > r.size() ? FALSE : r.at( 12 ).toInt() == 1 // f4 00094 ) ; 00095 else if( r.at( 4 ) == "GA" ) 00096 emit infoReceivedRailState( r.at( 5 ).toInt(), r.at( 7).toInt() == 1 ) ; // address, state 00097 else if( r.at( 4 ) == "POWER" ) 00098 emit infoReceivedPower( r.at( 5 ) == "ON" ) ; 00099 } 00100 00101 Srcp083::MsgCode Srcp083Client::sendCommand( QString command ) 00102 { 00103 commandClient()->sendMsg( command ); 00104 return returnMsgCode( commandClient()->srcpReadLine() ) ; 00105 } 00106 00107 void Srcp083Client::setPower( const bool &power ) 00108 { 00109 Srcp083::MsgCode reply = sendCommand( QString( "SET %1 POWER %2" ).arg( _bus ).arg( power ? "ON" : "OFF" ) ) ; 00110 00111 if ( reply != Srcp083::Ok ) 00112 emit errorOccured( trUtf8("Power state cannot be set.") ); 00113 } 00114 00115 void Srcp083Client::requestNewRailState() 00116 { 00117 /* FIXME problem due to the srcpd */ 00118 /* 00119 // Srcp083::MsgCode initReply = sendCommand( QString( "INIT %1 GA %2 %3" ).arg( _bus ).arg( rail->address() ).arg( rail->decoder() ) ) ; 00120 Srcp083::MsgCode initReply = sendCommand( QString( "GET %1 GA %2 1" ).arg( _bus ).arg( rail->address() ) ) ; 00121 if ( initReply != Srcp083::Ok ) 00122 emit errorOccured( trUtf8( "Error sending data to the server" ) ) ; 00123 */ 00124 00125 Rail *rail = dynamic_cast<Rail*>( sender() ) ; 00126 00127 // SET <bus> GA <addr> <port> <value> <delay> 00128 QString command = QString( "SET %1 GA %2 %3 %4 %5" ).arg( _bus ).arg( rail->address() ).arg( "1" ).arg( !rail->isOpen() ).arg( rail->delay() ) ; // !isOpen() : we want to change from state to !sate 00129 Srcp083::MsgCode reply = sendCommand( command ); 00130 00131 if ( reply == Srcp083::Ok ) 00132 return ; 00133 00134 // Lazy behaviour, init element only if needed 00135 if ( reply == Srcp083::NoData ) 00136 { 00137 // INIT <bus> GA <addr> <protocol> <opt. parameter> 00138 Srcp083::MsgCode initReply = sendCommand( QString( "INIT %1 GA %2 %3" ).arg( _bus ).arg( rail->address() ).arg( rail->decoder() ) ) ; 00139 if ( initReply != Srcp083::Ok || sendCommand( command ) != Srcp083::Ok ) 00140 emit errorOccured( trUtf8( "Error sending data to the server" ) ) ; 00141 } 00142 00143 } 00144 00145 void Srcp083Client::setNewEngineState() 00146 { 00147 Engine *engine = dynamic_cast<Engine*>( sender() ) ; 00148 00149 int maxSpeed = 0 ; 00150 int numFunctions = 0 ; 00151 00152 if ( engine->decoder() == "M1" ) 00153 { 00154 maxSpeed = 14 ; 00155 numFunctions = 1 ; 00156 } 00157 else if ( engine->decoder() == "M2" ) 00158 { 00159 maxSpeed = 14 ; 00160 numFunctions = 4 ; 00161 } 00162 else if ( engine->decoder() == "M3" ) 00163 { 00164 maxSpeed = 28 ; 00165 numFunctions = 4 ; 00166 } 00167 else if ( engine->decoder() == "M4" ) 00168 { 00169 maxSpeed = 14 ; 00170 numFunctions = 4 ; 00171 } 00172 else if ( engine->decoder() == "M5" ) 00173 { 00174 maxSpeed = 14 ; 00175 numFunctions = 4 ; 00176 } 00177 else 00178 { 00179 // FIXME add error msg 00180 } 00181 00182 // FIXME 00183 Srcp083::MsgCode initReply = sendCommand( QString( "GET %1 GL %2" ).arg( _bus ).arg( engine->address() ) ) ; 00184 if ( initReply != Srcp083::Ok ) 00185 emit errorOccured( trUtf8( "Error sending data to the server" ) ) ; 00186 00187 int speed = round( engine->speed() * maxSpeed / 100 ) ; // only some speed step are allowed 00188 00189 // SET <bus> GL <addr> <drivemode> <V> <V_max> <f1> ... <fn> 00190 QString command = QString( "SET %1 GL %2 %3 %4 %5 %6" ).arg( _bus ).arg( engine->address() ).arg( engine->isReversed() ).arg( speed ).arg( maxSpeed ).arg( engine->light() ) ; 00191 00192 // FIXME 00193 if ( numFunctions == 4 ) 00194 command += QString( " %1 %2 %3" ).arg( engine->f2() ).arg( engine->f3() ).arg( engine->f4() ) ; 00195 00196 00197 00198 Srcp083::MsgCode reply = sendCommand( command ); 00199 00200 if ( reply == Srcp083::Ok ) 00201 return ; 00202 00203 // Lazy behaviour, init element only if needed 00204 if ( reply == Srcp083::NoData ) 00205 { 00206 // INIT <bus> GL <addr> <protocol> <opt. parameter> 00207 // Srcp083::MsgCode initReply = sendCommand( QString( "INIT %1 GL %2 %3" ).arg( _bus ).arg( rail->address() ).arg( rail->decoder() ) ) ; 00208 // if ( initReply != Srcp083::Ok || sendCommand( command ) != Srcp083::Ok ) 00209 // emit errorOccured( trUtf8( "Error sending data to the server" ) ) ; 00210 } 00211 00212 }