Synchronizing message operations

TC3 IoT Functions includes functionalities for synchronizing multiple message operations. This can be very useful in scenarios where data comes in from different data sources/channels. The function block FB_IotFunctions_Request provides different mechanisms for synchronizing message operations.

Example scenario

A cocktail mixer provides different ingredients and every cocktail consists of five ingredients. An ingredient is added to the recipe by pressing a button. The cocktail mixer starts mixing the cocktail after the fifth ingredient has been selected. When an ingredient is selected, an MQTT message is published to a (different) topic. These messages should be received by TC3 IoT Functions.

Basic setup

TC3 IoT Data Agent is configured with an MQTT gate and five different channels for TC3 IoT Functions. Every channel is configured for an “ingredient topic”.

Synchronizing message operations 1:

To synchronize the read operation for five different ingredients, an instance of FB_IotFunctions_Request is created. Then an instance of FB_IotFunctions_Message is created for each of the five channels.

fbRequestIngredients : FB_IotFunctions_Request;

fbReadIngredient : ARRAY[0..4] OF FB_IotFunctions_Message := [(nChannelId := 1),(nChannelId := 2),(nChannelId := 3),(nChannelId := 4),(nChannelId := 5)];
nIn : ARRAY[0..4] OF STRING;

The synchronized request then can be created as follows:

IF NOT fbRequestIngredients.bBusy THEN
  IF fbRequestIngredients.bError THEN
    ...
  ELSE
    IF fbRequestIngredients.bTimeoutOccurred THEN
      ...
    ELSE
      // request was successful
      ...      
    END_IF
  END_IF

  // Prepare next read operation for ingredients
  fbRequestIngredients.Create();
  fbRequestIngredients.EnqueueRead(ADR(fbReadIngredient [0]),ADR(nIn[0]),SIZEOF(nIn[0]));
  fbRequestIngredients.EnqueueRead(ADR(fbReadIngredient [1]),ADR(nIn[1]),SIZEOF(nIn[1]));
  fbRequestIngredients.EnqueueRead(ADR(fbReadIngredient [2]),ADR(nIn[2]),SIZEOF(nIn[2]));
  fbRequestIngredients.EnqueueRead(ADR(fbReadIngredient [3]),ADR(nIn[3]),SIZEOF(nIn[3]));
  fbRequestIngredients.EnqueueRead(ADR(fbReadIngredient [4]),ADR(nIn[4]),SIZEOF(nIn[4]));
  fbRequestIngredients.Execute();
END_IF

Sample04 shows the full sample code (see Samples).

Synchronization conditions

The function block instance fbRequest contains different synchronization conditions. These can be used to determine whether the read operations within the request were successful, threw an error or resulted in a timeout. Note how the different timeout and retry settings can help in supporting this use case (see Timeout settings).

Condition

Description

bBusy

TRUE: Request is still in progress and not all operations have been completed successfully

FALSE: Request has been finished. To figure out if an error or timeout occurred, further flags need to be evaluated.

bTimeoutOccurred

TRUE: RequestTimeout has been triggered for at least one operation.

FALSE: No timeout occurred.

bError

TRUE: An error occurred for at least one operation.

FALSE: No error occurred.

Flags of each message operation

In addition, the flags (error, success, bDataAvailable, bDataChanged) of each message operation can be analyzed in order to figure out if a request operation was successful or failed.