Ir para o conteúdo

Use of Libraries in the Project

Overview

This document describes how the sesi-monitoramento-do-trabalhador project uses local and external libraries to:

  • collect sensor data from smartphones and wearables;
  • discover Bluetooth and BLE devices;
  • publish data over MQTT;
  • temporarily hold data when there is a disconnection;
  • synchronize offline data with the project's HTTP services.

The central point of the architecture is that the app does not implement sensor collection and the low-level MQTT flow directly. Instead, it relies mainly on the CDDL, CDAL, microbroker, and security-service libraries.

Main libraries

The most important local libraries are declared in sesi-ergotech-app/build.gradle:

  • libs/cddl.aar
  • libs/cdal.aar
  • libs/microbroker.aar
  • libs/security-service.aar
  • libs/polar-ble-sdk.aar

There are also important supporting dependencies:

  • org.eclipse.paho.client.mqttv3: MQTT client used by the stack;
  • io.netty:*: support for the embedded microbroker;
  • org.greenrobot:eventbus: internal events between service, screen, and connections;
  • com.google.android.gms:play-services-location: sensors and location;
  • rxjava, rxandroid, protobuf-javalite: support for BLE integrations, especially Polar;
  • retrofit and converter-gson: HTTP communication with external services.

Role of each library

CDDL

CDDL is the main API consumed by the application. It works as a high-level layer to:

  • start communication technologies;
  • start and stop sensors;
  • discover objects and services;
  • create publishers and subscribers;
  • connect to MQTT brokers;
  • store offline data in physical memory;
  • provide QoS and history mechanisms.

In the project's code, CDDL appears mainly in:

  • sesi-ergotech-app/src/main/java/br/ufma/lsdi/sesi_ergotech_app/cddl/CddlService.java
  • sesi-ergotech-app/src/main/java/br/ufma/lsdi/sesi_ergotech_app/cddl/SensorCddl.java
  • sesi-ergotech-app/src/main/java/br/ufma/lsdi/sesi_ergotech_app/pubsub/Publishers.java
  • sesi-ergotech-app/src/main/java/br/ufma/lsdi/sesi_ergotech_app/pubsub/Subscribers.java
  • sesi-ergotech-app/src/main/java/br/ufma/lsdi/sesi_ergotech_app/MainActivity.java

CDAL

CDAL stays more hidden from the application, but it is the layer that implements the concrete data acquisition technologies.

By inspecting the AAR, it was possible to see classes related to:

  • internal sensors;
  • Bluetooth Low Energy;
  • classic Bluetooth;
  • Polar devices;
  • BioHarness devices;
  • HXM devices;
  • sensor data models, such as SensorDataExtended.

In other words, the app almost never talks to these classes directly. It calls CDDL, and CDDL delegates the concrete implementation to CDAL.

microbroker

microbroker is a local MQTT broker embedded in the application. It serves as an internal bus for the sensor and messaging stack.

The project's flow does not send all data directly from the sensor to the external broker. First, the data passes through the internal CDDL ecosystem, which uses this local broker to distribute messages.

security-service

security-service supports the generation and use of cryptographic material, certificates, and secure connections.

It is used together with CDDL to:

  • start the secure microbroker;
  • connect with secureConnect(...);
  • obtain security information and certificates.

polar-ble-sdk

polar-ble-sdk complements CDAL for communication with Polar wearables, such as heart rate sensors, ECG, accelerometer, and other BLE streams.

How the application initializes the stack

The central initialization point is CddlService.

In CddlService.initCDDL(), the application does the following:

  1. starts a local secure microbroker with CDDL.startSecureMicroBroker(...);
  2. creates a connection with ConnectionFactory.createConnection();
  3. points that connection to the local host returned by the microbroker;
  4. connects with secureConnect(...);
  5. injects that connection into the CDDL singleton instance;
  6. calls startService() and startAllCommunicationTechnologies().

This means the sensor and messaging stack first comes up in a local, secure environment inside the app itself.

How the project collects data

Smartphone sensors

The SensorCddl class controls the smartphone's internal sensors.

It uses calls such as:

  • CDDL.getInstance().getInternalSensorList()
  • CDDL.getInstance().startSensor(sensorName)
  • CDDL.getInstance().stopSensor(sensorName)

The available sensors are persisted locally in SQLite so the interface knows:

  • which sensors exist;
  • which are available;
  • which are active;
  • which were chosen by the user.

Wearables and device discovery

The app also uses CDDL subscribers to listen for device discovery and disconnection events.

In SensorCddl:

  • subscribeObjectDiscoveredTopic() is used to detect new wearables;
  • subscribeObjectDisconnectedTopic() is used to detect connection loss.

When a wearable appears:

  1. the event arrives through CDDL's internal connection;
  2. the app resolves the name and Bluetooth address with BluetoothUtil;
  3. it saves the device and its sensors in SQLite;
  4. it updates the status to available or active;
  5. it triggers events for the interface.

This design reinforces that sensor discovery comes from the CDDL/CDAL stack, not from a manual implementation in the app.

How the project uses MQTT

Two MQTT connections

The project works with two conceptually different connections.

1. Internal connection

This is the connection used by CDDL to talk to the local microbroker.

It is created in CddlService.initCDDL() and is used so that sensors, discovery, and messages travel within the application's internal ecosystem.

2. External connection

This is the connection to the broker configured by the user, controlled by ConnectionBroker.

This class:

  • creates a ConnectionImpl with ConnectionFactory.createConnection();
  • sets host;
  • enables publishConnectionChangedStatus;
  • enables enableIntermediateBuffer;
  • connects with secureConnect(...).

This external connection is the one that actually sends data outside the application over MQTT.

How data goes from the sensors to the external broker

The main flow is as follows:

  1. a sensor is started with CDDL.startSensor(...);
  2. the concrete collection technology runs underneath, implemented by CDAL;
  3. the data becomes a Message inside the CDDL stack;
  4. a local Subscriber in the app listens for these messages on the internal connection;
  5. the app decides how to republish it;
  6. a Publisher attached to ConnectionBroker sends the message to the external MQTT broker.

This handoff happens mainly in Subscribers and Publishers.

Subscribers

The Subscribers class creates specialized subscribers for sensors such as:

  • ActivityDetectionSensor
  • PolarSense_PPG
  • PolarSense_PPI
  • PolarSense_HR
  • PolarSense_ACC
  • PolarSense_MAGNETOMETER
  • PolarSense_GYRO
  • PolarH10_ECG
  • PolarH10_HR
  • PolarH10_ACC
  • HXM030655_HeartRateSensor
  • BHBHT_HeartRateSensor
  • BHBHT_RespirationRateSensor
  • BHBHT_PostureSensor

Each subscriber connects to CDDL.getInstance().getConnection(), i.e., the internal connection with the local microbroker.

When a message arrives:

  • the subscriber checks its History;
  • if the external connection is active, the app calls history.takeAll();
  • each Message is resent through Publishers.publish(...).

Publishers

The Publishers class creates publishers connected to the external broker via ConnectionBroker.getConnection().

It has:

  • a default publisher for most sensors;
  • specialized publishers with filters for some cases;
  • a publisher for manual data with ReliabilityQoS.EXACTLY_ONCE.

In practice, the app uses CDDL's own publishers to communicate over MQTT. It does not manually build publish/subscribe logic with the Paho client in business code.

Filters and QoS

The project uses several QoS mechanisms provided by the CDDL stack.

TimeBasedFilterQoS

Used to reduce the frequency of messages from several sensors. In many cases, the initial minimum interval is 1000 ms.

This behavior is adjusted by the application settings in Settings.setTimeBasedFilter(...).

HistoryQoS

Used to keep a temporary message history in case of disconnection.

The app configures KEEP_LAST with a depth proportional to the disconnection support time. This history can later be drained and resent once the connection to the external broker is available again.

LatencyBudgetQoS

Configured in Publishers.setDelayLatencyBudgetQoS(...) and controlled by Settings.enableLatencyBudget(...).

This feature allows delaying delivery within a configured window, helping to modulate the sending rate.

Compression

When the latency budget is enabled, the project can also enable data compression through:

  • Publishers.enableDataCompression()
  • Publishers.disableDataCompression()

ReliabilityQoS

Explicitly used for publishing manual data, with EXACTLY_ONCE.

Buffer and fault tolerance

The project has two resilience layers for connectivity failures.

External connection intermediate buffer

In ConnectionBroker.secureConnect(...), the external connection enables:

  • setPublishConnectionChangedStatus(true)
  • setEnableIntermediateBuffer(true)

This shows that the connection stack itself supports holding messages temporarily when there is connectivity fluctuation.

Offline storage in physical memory

Separately from the live MQTT flow, CDDL can also store data locally in physical memory.

In MainActivity, the application:

  • checks the amount of pending data with getCountDataSavedPhysicalMemory();
  • detects the backlog's starting date with getOfflineDataCollectionStartTime();
  • reads the data with getDataSavedPhysicalMemory(start, end);
  • removes the synchronized data with deleteDataSavedPhysicalMemory(start, end).

This mode is controlled by the app's settings and supports offline operation.

Difference between MQTT sending and offline synchronization

This point is important: the project does not use only MQTT.

Online flow

In the online flow:

  • the sensor produces data;
  • CDDL delivers the messages locally;
  • the app republishes those messages to the external broker via MQTT.

Offline flow

In the offline flow:

  • the data is stored locally by CDDL;
  • it is then read in batches by MainActivity;
  • instead of going over MQTT, it is sent over HTTP using Retrofit;
  • the destination of this flow is MeasurementSesiServidorRepository.

In other words, real-time data primarily flows over MQTT, but offline backlog synchronization flows over REST.

Role of the project's classes

CddlService

Responsible for:

  • bringing up the CDDL stack;
  • starting the local secure microbroker;
  • creating the internal connection;
  • creating publishers and subscribers;
  • starting communication technologies;
  • triggering the connection to the external broker.

SensorCddl

Responsible for:

  • starting sensors chosen by the user;
  • stopping sensors;
  • discovering wearables;
  • updating sensor and device states;
  • persisting the local sensor catalog.

DeviceCddl

Responsible for:

  • persisting and updating the state of wearable devices;
  • marking devices and sensors as unavailable on restart.

Subscribers

Responsible for:

  • subscribing to sensor services on the internal connection;
  • applying filters and history;
  • resending received messages to the external publishers.

Publishers

Responsible for:

  • publishing messages to the external broker;
  • configuring filters and sending QoS;
  • publishing manual data and default messages.

ConnectionBroker

Responsible for:

  • maintaining the external MQTT connection;
  • connecting, reconnecting, and disconnecting;
  • emitting state change events to the interface.

Settings

Responsible for applying operational policies over the stack:

  • collection only during working hours;
  • disconnection support time;
  • refresh rate;
  • latency budget;
  • compression.

MainActivity

Responsible for:

  • enabling and monitoring offline synchronization;
  • reacting to data backlog;
  • sending offline batches via HTTP;
  • mediating login, certificates, and general application initialization.

Architectural summary

In practical terms, the project's design is:

  1. CDAL collects real data from sensors and devices;
  2. CDDL abstracts these technologies and offers a messaging, discovery, QoS, and connection API;
  3. microbroker functions as the stack's local internal broker;
  4. the app subscribes to data on the internal connection using Subscribers;
  5. the app republishes to the external broker via Publishers and ConnectionBroker;
  6. in the case of offline operation, CDDL stores the data locally;
  7. the offline backlog is later synchronized via HTTP, not via MQTT.

Conclusion

The project uses the libraries with a clear separation of responsibilities:

  • CDAL implements the concrete data acquisition;
  • CDDL provides the orchestration and messaging layer;
  • microbroker supports the local internal bus;
  • security-service protects the connections and cryptographic material;
  • polar-ble-sdk supports collection on Polar devices;
  • Retrofit handles the HTTP flows, especially offline synchronization.

Thus, the app stays more focused on business rules, interface state, collection policies, and message routing, while the complexity of sensors and MQTT is concentrated in the libraries.