0

I am trying to get the DataLogger and Logbook to work on the movesense device. What I am trying to do is similar/continuation to what is asked in this question. When I am configuring my service when a device is connecting I would like to wait for the DataLogger to start(return status code 200) before continuing the execution of the rest of my code.

As part of the onSubscribe() function I execute the following code to configure the DataLogger:

WB_RES::DataLoggerConfig logConfig;
WB_RES::DataEntry entry;
WB_RES::DataLoggerStateValues::Type logState = WB_RES::DataLoggerStateValues::Type::DATALOGGER_LOGGING;

entry.path = <pathToService>;
<del>logConfig.dataEntries.dataEntry= {entry}; </del>
logConfig.dataEntries.dataEntry = whiteboard::MakeArray<WB_RES::DataEntry>(&entry, 1);

result = asyncPut(WB_RES::LOCAL::MEM_DATALOGGER_CONFIG(), AsyncRequestOptions::Empty, logConfig);

The status code I get after this command is the 202 Accepted. What I understood from the previous question are that I need to check that this function returns return code 200 in the onPutResult() function.

This I do by

switch(resourceId.localResourceId)
{
case WB_RES::LOCAL::MEM_DATALOGGER_CONFIG::LID:
{
  if(resultCode == whiteboard::HTTP_CODE_OK)
  {
    asyncPut(WB_RES::LOCAL::MEM_DATALOGGER_STATE(), AsyncRequestOptions::Empty,WB_RES::DataLoggerStateValues::Type::DATALOGGER_LOGGING);
  }
}
break;

And again in the onPutResult() I check that the datalogger is set in the correct state using this piece of code

case WB_RES::LOCAL::MEM_DATALOGGER_STATE::LID:
{
  if(resultCode == whiteboard::HTTP_CODE_OK)
  {
    // Boolean variable that indicate that the dataLogger is running.
    DataLoggerRunning = true; 
  }
  else
  {
    DEBUGLOG("onPutResult::MEM_DATALOGGER_STATE::
               SomethingIsNotRight");
  }
}
break;

Now I want to detect if the variable DataLoggerRunning is true before starting the my service. This is where I am a bit lost, I tried just waiting in a while loop, and looking for a sleep function in the movesense-device-lib (with no luck) in the hope that would not crash the code while waiting for the datalogger to start. The while loop resulted in an infinite loop, and I presume that a sleep function would also be blocking if I could find one.

Is this the wrong approach or am I on the right track, if so help making progress would be appreciated.

EDIT: Updated code for adding path to datalogger. If the old code is used we will get an 404 not found for the path.

Morten
  • 112
  • 2
  • 8

1 Answers1

0

The setup part of your code is correct. The result 202 in asyncPut means that the request is dispatched to another thread, and you correctly see the actual result of the PUT operation in the onPutResult.

Since Movesense is an asynchronous architecture you cannot wait for a variable in a loop (or there is a way but it is quite inadvisable since it can cause issues elsewhere), especially not in a simple while loop. You don't really need the DataLoggerRunning flag unless you need it elsewhere. The DataLogger is running when you get the onPutResult() callback, though there might not be any log yet (if there is no data). The way the DataLogger works is that it subscribes to given paths and stores data from the notifications it gets.

Basically you have a couple of options:

  1. Just call your startup code in the opPutResult since the datalogger is now running. Any requests you make must be fast (let's say < 200ms) or asynchronous.
  2. Trigger a "Deferred Procedure Call" (whiteboard::DpcFunctor class). This causes a disconnect in the execution and the Movesense scheduler will perform your function a bit later.
  3. If you need to wait a bit, call startTimer() and call the rest of your code in the onTimer()-callback
PetriL
  • 1,211
  • 1
  • 7
  • 11