void Manager::readFromSocket() { QDataStream in(m_tcpSocket); in.setVersion(QDataStream::Qt_4_0); QString response; in >> response; Q_EMIT message(tr("Response received: ") + response); Protocol protocol; bool ok = false; bool result = protocol.str2Response(response, m_currentCommand, &ok); if(!ok) { Q_EMIT message("Failed to parse response."); } else if(!result) { Q_EMIT message("Device cannot execute request."); } else { switch(m_currentCommand) { case SetLedState: case SetLedColor: case SetLedRate: { Q_EMIT message("Device has changed it's state successful."); break; } case GetLedState: { Q_EMIT message("Device state received successful."); m_state.on = protocol.on(); Q_EMIT stateChanged(); break; } case GetLedColor: { Q_EMIT message("Device color received successful."); m_state.color = protocol.color(); Q_EMIT stateChanged(); break; } case GetLedRate: { Q_EMIT message("Device rate received successful."); m_state.rate = protocol.rate(); Q_EMIT stateChanged(); break; } default: { break; } } } executeCommands(); }
QString Manager::processCommand(const QString &_command) { Protocol protocol; QString result = protocol.response2Str(false, MaxCommand); if(!m_emulator->deviceEnabled()) { return result; } bool ok; ECommand command = protocol.str2Request(_command, &ok); if(!ok) { return result; } switch(command) { case SetLedState: { m_emulator->setStateOn(protocol.on()); break; } case GetLedState: { protocol.setStateOn(m_emulator->stateOn()); break; } case SetLedColor: { m_emulator->setColor(protocol.color()); break; } case GetLedColor: { protocol.setColor(m_emulator->color()); break; } case SetLedRate: { if(!m_emulator->setRate(protocol.rate())) { return result; } break; } case GetLedRate: { protocol.setRate(m_emulator->rate()); break; } default: { break; } } return protocol.response2Str(true, command); }