Question about message parsing code

Moderator: leecollings

Post Reply
PierreSt
Posts: 6
Joined: Sunday 18 September 2016 8:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Question about message parsing code

Post by PierreSt »

Hello,

I am trying to understand the code in MySensorBase.cpp.
In

Code: Select all

void MySensorsBase::ParseLine()
{
    if (m_bufferpos<2)
        return;
    std::string sLine((char*)&m_buffer);

    //_log.Log(LOG_STATUS, sLine.c_str());

    std::vector<std::string> results;
    StringSplit(sLine, ";", results);
    if (results.size()<5)
        return; //invalid data

    int node_id = atoi(results[0].c_str());
    int child_sensor_id = atoi(results[1].c_str());
    _eMessageType message_type = (_eMessageType)atoi(results[2].c_str());
    int ack = atoi(results[3].c_str());
    int sub_type = atoi(results[4].c_str());
    std::string payload = "";
    if (results.size() >= 6)
    {
         for (size_t ip = 0; ip < results.size() - 5; ip++)
            {
                 payload = results[5+ip];
            }
    }
[...]

If I understand the above code properly:
If the payload is a ";" separated list of values (like for a V_POSITION of a S_GPS for example) then only the last value will be considered as the "payload". Equally if on a V_TEXT, the text to be displayed contains a ";", then it will be truncated to the last "sub-string"

(cf MySensors website on the serial API:
V_POSITION 49 GPS position and altitude. Payload: latitude;longitude;altitude(m). E.g. "55.722526;13.017972;18" S_GPS)

Have I missed something?

Thanks for the clarification,

Pierre
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Question about message parsing code

Post by gizmocuz »

V_POSITION is not implemented (yet), and all values are in the 'results variable:

Code: Select all

StringSplit(sLine, ";", results);
I do agree that the person making the V_POSITION could have better picked another separator ;)
Quality outlives Quantity!
PierreSt
Posts: 6
Joined: Sunday 18 September 2016 8:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Question about message parsing code

Post by PierreSt »

In 'results', yes, but not in 'payload' which is used in the further processing.
In the case of the gps position, results.size would be 8,
Payload would successivelly be assigned results(5), results(6) and finally results(7). At the end of the loop, payload would only contain the altitude.
Equally if I have a v_text which is "some text; some further text". The final 'payload' would only be "some further text". This restriction on the ; character is not mentioned in MySensors.
Perhaps the routine could be:

for (size_t ip)
{ payload += "," + result[5+ip] }

(Cleaned to avoid the leading , of course)

Pierre
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Question about message parsing code

Post by gizmocuz »

yes, so thats not a big issue to combine the rest of the results back to 1 payload ?

i think it is already done... did you check it ?

Code: Select all

	if (results.size() >= 6)
	{
		for (size_t ip = 0; ip < results.size() - 5; ip++)
		{
			payload = results[5+ip];
		}
	
}

or are you just assuming it did not work ?
Quality outlives Quantity!
PierreSt
Posts: 6
Joined: Sunday 18 September 2016 8:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Question about message parsing code

Post by PierreSt »

I am not making any assumptions really (especially not of buggy behaviour), I am trying to understand how things work. Indeed for now I have not tested anything, I have just read the code. Hence the initial question "Have I missed something?"
I will not have my hand on the stuff needed to test for real anything before probably next week.

I actually find Domoticz code really interresting and very elegant. There are certain things I have not yet been able to grasp (the latest is the link between the UI and the mainworker, especially the error reporting mechanism - how/where the return bool from SwitchLight() is used for example), hence my questions. If I am becoming annoying, just let me know and I'll stop pestering.

And to conclude, thanks for making Domoticz available to us, it really is cool software.

Pierre
User avatar
gizmocuz
Posts: 2552
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Question about message parsing code

Post by gizmocuz »

No worries ;)
If you can include the GPS system, you might get more understanding.
Else, can you point me to the hardware page for this sketch ?
Quality outlives Quantity!
PierreSt
Posts: 6
Joined: Sunday 18 September 2016 8:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Question about message parsing code

Post by PierreSt »

It seems that indeed, the payload gets truncated. I have made a test with a S_INFO sensor sending a V_TEXT. Below the result of the serial monitor and domoticz results:
screenshot_domoticz_truncated_payload.png
screenshot_domoticz_truncated_payload.png (264.16 KiB) Viewed 1469 times
PierreSt
Posts: 6
Joined: Sunday 18 September 2016 8:47
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Question about message parsing code

Post by PierreSt »

Indeed, hence my initial question about the code. Why go through a loop to iterate over all values. If the purpose was to take only the last one a "payload = results[size-1]" would have been enough.

Having said that, looking at MySensors sensors/variables types, it seems that only the V_POSITION is meant to have a ; delimited list of values as payload. And that sensor is not implemented in Domoticz.
The thing with the V_TEXT is IMHO just a corner case by-product.

Pierre
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest