#include "SHmessage.h"



SHmessage::SHmessage(const QByteArray & data)
{
    QString dataQS = data;
    QStringList dataQL = dataQS.split(QLatin1Char(';'));

    // Format: "node-id ; child-sensor-id ; command ; ack ; type ; payload \n"


    // Defaults
    m_nodeId = m_sensorId = m_command = m_ack = m_type = -1;
    m_payload = "ERROR";
    m_error = true;

    bool ok;

    if (dataQL.size() != 6) // falsches Format
        return;

    m_nodeId   = dataQL.at(0).toInt(&ok);
    if (not ok)
        return;

    m_sensorId = dataQL.at(1).toInt(&ok);
    if (not ok)
        return;

    m_command  = dataQL.at(2).toInt(&ok);
    if (not ok)
        return;

    m_ack      = dataQL.at(3).toInt(&ok);
    if (not ok)
        return;

    m_type     = dataQL.at(4).toInt(&ok);
    if (not ok)
        return;

    m_payload = dataQL.at(5);
    m_payload.remove(m_payload.size()-1, 1); // remove \n

    // qDebug() << "SHmessage" << m_nodeId << m_sensorId << m_command << m_ack << m_type << m_payload;

    m_error = false;

}

int SHmessage::command() const
{
    return m_command;
}

int SHmessage::nodeId() const
{
    return m_nodeId;
}

int SHmessage::sensorId() const
{
    return m_sensorId;
}

QString SHmessage::payload() const
{
    return m_payload;
}


bool SHmessage::error() const
{
    return m_error;
}