00001
00002 #ifdef SRCP073
00003
00004 #include "srcp073_client.h"
00005
00006 Srcp073Client::Srcp073Client( QObject *parent ) : VirtualClient( parent )
00007 {
00008 _socket = new QTcpSocket( this ) ;
00009 }
00010
00011
00012 Srcp073Client::~Srcp073Client()
00013 {
00014 setConnection( FALSE ) ;
00015 }
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 void Srcp073Client::setConnection( bool c )
00029 {
00030 if( c && _socket->state() != QAbstractSocket::ConnectedState )
00031 _socket->connectToHost( _serverAddress, _serverPort ) ;
00032 else
00033 _socket->disconnectFromHost() ;
00034 }
00035
00036 void Srcp073Client::sendMsg( QString msg )
00037 {
00038 qDebug() << "SRCP 0.7.3 sent : " << msg ;
00039
00040
00041 msg += "\n" ;
00042
00043 int r = _socket->write( msg.toAscii () );
00044
00045 if ( r == -1 )
00046 emit writeErrorOccured( trUtf8( "Impossible to send the message to the server") );
00047
00048
00049 }
00050
00051 void Srcp073Client::setPower( bool power )
00052 {
00053 if ( power )
00054 sendMsg( "SET POWER ON" ) ;
00055 else
00056 sendMsg( "SET POWER OFF" ) ;
00057 }
00058
00059 void Srcp073Client::setNewRailState( Rail *rail )
00060 {
00061 int action = 1 ;
00062 if ( rail->isOpen() )
00063 action = 0 ;
00064
00065 sendMsg( QString( "SET GA %1 %2 1 %3 %4" ).arg( rail->decoder() ).arg( rail->address() ).arg( action ).arg( rail->delay() ) ) ;
00066 }
00067
00068 void Srcp073Client::setNewEngineState( Engine *engine )
00069 {
00070 int maxSpeed = 0 ;
00071 int numFunctions = 0 ;
00072
00073 if ( engine->decoder() == "M1" )
00074 {
00075 maxSpeed = 14 ;
00076 numFunctions = 1 ;
00077 }
00078 else if ( engine->decoder() == "M2" )
00079 {
00080 maxSpeed = 14 ;
00081 numFunctions = 4 ;
00082 }
00083 else if ( engine->decoder() == "M3" )
00084 {
00085 maxSpeed = 28 ;
00086 numFunctions = 4 ;
00087 }
00088 else if ( engine->decoder() == "M4" )
00089 {
00090 maxSpeed = 14 ;
00091 numFunctions = 4 ;
00092 }
00093 else if ( engine->decoder() == "M5" )
00094 {
00095 maxSpeed = 14 ;
00096 numFunctions = 4 ;
00097 }
00098 else
00099 {
00100
00101 }
00102
00103 int speed = round( engine->speed() * maxSpeed / 100 ) ;
00104
00105 QString s = QString( "SET GL %1 %2 %3 %4 %5 %6 %7" ).arg( engine->decoder() ).arg( engine->address() ).arg( engine->isReversed() ).arg( speed ).arg( maxSpeed ).arg( engine->light() ).arg( numFunctions ) ;
00106
00107
00108 if ( numFunctions == 4 )
00109 s += QString( " %1 %2 %3" ).arg( engine->f2() ).arg( engine->f3() ).arg( engine->f4() ) ;
00110
00111 sendMsg( s ) ;
00112 }
00113
00114 #endif // SRCP073