Lorenzo Mirto Bertoldo @climadd
The gateway process involves four main classes that must be executed in the following order:
- BackendMessageReceiver.java –
iotGateway.src.test.java.org.lore.mqtt.backend - GatewayApp.java –
iotGateway.src.main.java.org.lore.app - SimulatorGUIApp.java –
iotGateway.src.test.java.org.lore.mqtt.backend.simulators - BackendMessageSender.java –
iotGateway.src.test.java.org.lore.mqtt.backend
-
The Gateway acts as a bridge between the Broker and the IoT Devices (Sensors and Actuators). It exposes and consumes a protocol-based API, handling bidirectional communication between distributed system components using both TCP sockets and MQTT messaging.
-
Communication with the Backend occurs via the MQTT protocol following a publish/subscribe model, while dynamically allocated TCP sockets are used to establish and manage connections with IoT Devices. The Gateway is responsible for protocol translation, acting as an adapter between a custom TCP-based device communication protocol and an MQTT-based backend integration API.
-
The Simulated Sensors generate realistic fluctuations in their measured values. As displayed in the GUI, sensors independently produce a stream of data, which is sent via TCP Socket to the Gateway. The gateway encapsulates this data into MQTT-formatted Queries for the Backend. This data retrieval process can either be scheduled by a backend script or triggered manually by user requests.
-
The Simulated Actuators mimic real IoT actuators that toggle between different modes. They store multiple boolean or enumeration values, which can be modified by gateway-issued queries through their dedicated TCP socket. The GUI displays the real-time status of all actuator variables.
-
The Simulated Backend consists of two classes:
BackendMessageSender– Handles sending messages through the MQTT Broker.BackendMessageReceiver– Receives and prints incoming messages in the terminal.
-
The Messages follow the MQTT format, and all enumeration-type data related to both messages and measurements is cataloged in the
main.java.org.lore.models.mqttpackage.
-
Request for sensor temperature data
- Topic:
azienda{idAzienda}/serra{idSerra}/sensori/temperatura/rx - Example:
3/5/sensori/temperatura/rx
- Topic:
-
Request for actuator state or status change
- Topic:
azienda{idAzienda}/serra{idSerra}/attuatori/illuminazione/rx - Example:
3/5/attuatori/illuminazione/rx
- Topic:
-
Sensor temperature response
- Topic:
azienda{idAzienda}/serra{idSerra}/sensori/temperatura/sx - Example:
3/5/sensori/temperatura/sx
- Topic:
-
Actuator state response
- Topic:
azienda{idAzienda}/serra{idSerra}/attuatori/illuminazione/sx - Example:
3/5/attuatori/illuminazione/sx
- Topic:
The message format is defined in JSON, and the communication operates as a fully protocol-based API.
{
"type": "read",
"device": "sensori"
}{
"type": "write",
"device": "attuatori",
"state": "on/off",
"level": "low/medium/high",
"mode": "auto/manual"
}{
"type": "read",
"device": "attuatori"
}{
"type": "read",
"device": "sensori",
"value": float //Exclusive to sensor device
}{
"type": "write",
"device": "attuatori",
"state": "on/off",
"level": "low/medium/high",
"mode": "auto/manual",
"result": boolean //Exclusive to sensor (true = operation successful)
}- Function: Receives MQTT messages and forwards them via TCP sockets to the appropriate IoT devices.
- Response Handling: Creates a
responseobject upon receiving a message and sends it to the backend.
- Initial Specification: Assumes a fixed number of sensor-actuator pairs (Temperature, Humidity, Lighting).
- Future Expansion: Uses hashmaps for dynamic TCP socket initialization, allowing support for additional devices.
- Listener Class:
GWBEListener - Subscription: Registers a listener using the
.subscribe()method. - Message Handling:
- Trigger: The
.messageArrived()method is invoked upon message reception. - Processing: Extracts topic fields and parses the JSON payload to determine the necessary actions.
- Trigger: The
- Purpose: Simulates backend requests.
- Logging: Prints all sent messages to the terminal.
- Connection Management: Performs
.disconnect()and.close()once all messages are sent.
- Purpose: Simulates the backend receiving MQTT messages.
- Logging: Prints all received messages to the terminal.
- Subscription: Uses
.subscribe()to listen to the appropriate topic, utilizingDummyListener.java, an interface extendingIMqttMessageListener.
- Function: Provides a graphical interface to monitor sensor measurements and actuator states.
- Sensor Values:
- Initialized to
0 - Updated every 30 seconds using
.getRandomByRange()fromRandomUtils
- Initialized to
- Actuator States: Variables like State, Level, and Mode are defined as enums in
main.java.org.lore.models.mqtt. - GUI Refresh:
- Refreshes every 2 seconds via
.refreshGUI() - Displays actuator state updates in response to MQTT "write" messages.
- Refreshes every 2 seconds via
- Thread Management: GUI contains 6 threads (one per device type), each running a dedicated TCP server.
- Logging: Prints messages to the terminal when a TCP connection is successfully established.
- Device Logic:
- Implemented in
test.java.org.lore.tcpwithin the classesTempTCPServer,UmiTCPServer, andIllTCPServer. - Each class invokes
.getRandomByRange()to generate sensor values. - The
.compute()method processes incoming MQTT messages, executing the corresponding logic based on message type (READorWRITE).
- Implemented in
