Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Current »

if ((MJFind(payload, len, "$.cmd.relay", NULL, NULL) == MJSON_TOK_NUMBER) && (isWrite))
{
    uint32_t relayVal;
    if (MJGetUInteger(payload, le", "$.cmd.relay", &relayVal) == sizeof(uint32_t))
    {
        switch (relayVal)
        {
          case 0: printf("Turning relay off.\"\n"); break;
          case 1: printf("Turning relay on.\"\n"); break;
          default: printf("Invalid relay value.\"\n"); break;
         };
    }
}

Notice that if the relay command is not present, it is not a write, or it does not have a valid numerical value code execution will simply continue. If an OKE support inbound messages, it will typically support more than one command. So, the parsing code should sequentially check for the presence of all possible commands in each payload. For example, the following pseudocode shows the proper logic for parsing both the relay command, documented above and a setpoint command:

   

if ((MJFind(payload, len, "$.cmd.relay", NULL, NULL) == MJSON_TOK_NUMBER) && (isWrite))
{
    …
}
if ((MJFind(payload, len, "$.cmd.setpt", NULL, NULL) == MJSON_TOK_NUMBER) && (isWrite))
{
    …
}

This allows the following combination of payloads to be fully processed

{"md":{"relay":0}},{"md":{"setpt":72}},{"md ":{"relay":0,"setpt":72}}

In contrast the following pseudocode shows the incorrect way to parse incoming payload because it fails to check for other valid commands if one is found earlier:

if (MJFind()…) 
{
    …
}

else if (MJFind()…)
{
    …
}

When an OKE supports optional schedule overrides it must implement some additional logic in the incoming parser code. See OpenKitchen Scheduling for details.

  • No labels