Skip to content

Add MQTT support for real-time communication alongside existing HTTP API#19

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-18
Draft

Add MQTT support for real-time communication alongside existing HTTP API#19
Copilot wants to merge 4 commits intomainfrom
copilot/fix-18

Conversation

Copy link
Copy Markdown

Copilot AI commented Jun 3, 2025

This PR implements MQTT communication support while maintaining 100% backward compatibility with the existing HTTP API. The integration follows the requirement to "keep the models as close as they currently are" by wrapping existing models in MQTT-specific containers without modifying the original model definitions.

Key Features

✅ Zero Breaking Changes

  • All existing models (Person, Plant, Sensor, SensorResult) remain completely unchanged
  • HTTP API works identically to before
  • Default behavior is HTTP-only mode (no MQTT dependency required)

✅ MQTT Integration

  • Eclipse Paho MQTT v5 Client - Industry standard, reliable MQTT library for Scala
  • Environment-configurable - Enable with MQTT_ENABLED=true
  • Structured topic hierarchy for different entity types and real-time data
  • JSON serialization/deserialization for all model types
  • Graceful connection management with automatic reconnection

✅ Real-time Capabilities

  • Entity creation events published to topics like leaf-lab/person/created
  • Live sensor readings on leaf-lab/plant/{plant_id}/sensor/{sensor_id}/reading
  • Bidirectional communication - can receive MQTT messages and store to database

Usage Examples

HTTP-only (unchanged default):

sbt run

HTTP + MQTT:

MQTT_ENABLED=true MQTT_BROKER_URL=tcp://localhost:1883 sbt run

MQTT Message Format:

{
  "sensorResult": {
    "id": 1,
    "plant_id": 1,
    "sensor_id": 1,
    "value": "25.5",
    "as_of": "2025-06-03T00:26:17.617168388Z"
  },
  "messageId": "uuid-string"
}

Architecture

The implementation adds MQTT as a parallel communication channel:

HTTP Requests ──┐
                ├──► Service Layer ──► Repository Layer ──► Database
MQTT Messages ──┘                 └──► MQTT Publisher
  • MqttModels.scala - Wrapper classes that preserve original model structure
  • MqttService.scala - MQTT client management and message handling
  • MqttEnabledService.scala - Enhanced service that publishes to MQTT on entity creation
  • MqttConfig.scala - Environment-based configuration

Testing

# Test MQTT model serialization
sbt "runMain dev.whalenet.leaf_lab.MqttTest"

# Compile verification
sbt clean compile

All tests pass and demonstrate proper JSON serialization, topic generation, and model preservation.

Repository Impact

  • Files added: 6 new MQTT-specific files + documentation
  • Files modified: build.sbt (1 dependency), Main.scala (MQTT initialization)
  • Files unchanged: Models.scala, Service.scala, Repository.scala, Database.scala
  • Backward compatibility: 100% - existing HTTP workflows unaffected

This implementation provides a foundation for transitioning to MQTT while maintaining all current functionality and allowing gradual migration.

Fixes #18.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)
  • git.io
    • Triggering command: curl -Ls REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits June 3, 2025 00:19
Co-authored-by: QMasterMoo <3915399+QMasterMoo@users.noreply.github.com>
Co-authored-by: QMasterMoo <3915399+QMasterMoo@users.noreply.github.com>
Co-authored-by: QMasterMoo <3915399+QMasterMoo@users.noreply.github.com>
Copilot AI changed the title [WIP] create models for MQTT based on current models Add MQTT support for real-time communication alongside existing HTTP API Jun 3, 2025
Copilot AI requested a review from QMasterMoo June 3, 2025 00:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create models for MQTT based on current models

2 participants