SDK Session Control Implementation
Overview
This document describes the implementation of session control mechanisms in the roboticks-SDK, including:- Disabled Auto-Session Creation: Sessions are no longer created automatically
- Start/Stop Session Methods: Explicit methods for session lifecycle control
- Remote Session Control: MQTT-based commands from backend
- Module-to-DeviceManager Communication: ZeroMQ-based session control
- HelloWorld Example: Demonstrates session start/stop from a module
Architecture
Session Control Flow
Implementation Details
1. DeviceManager Changes
Header (DeviceManager.hpp)
Added Configuration:Implementation (DeviceManager.cpp)
1. Disabled Auto-Session Creation (Line 219-225)- Creates new session if none exists
- Starts existing session by ID if provided
- Publishes session creation to backend via MQTT
- Returns true on success
- Gets active session
- Calls finalizeSession() which:
- Completes the session
- Saves to disk
- Publishes completion to backend
- Uploads artifacts (if auto_upload enabled)
- Creates subscriber on
/roboticks/session/controltopic - Listens for session control commands from modules
- Called during DeviceManager initialization
- Parses JSON command:
{"action": "start"}or{"action": "stop"} - Calls appropriate method (startSession() or stopSession())
- Logs success/failure
- Cleans up subscriber
- Called during DeviceManager shutdown
2. HelloWorld Module Changes
Header (HelloWorldModule.hpp)
Added Members:Implementation (HelloWorldModule.cpp)
1. Initialize Session Control Publisher (Line 24-43)Usage Examples
Example 1: Module-Initiated Session Control
Example 2: Direct API Usage
Example 3: Backend MQTT Command
Backend sends:Session Lifecycle
State Transitions
- CREATED: Session object exists but not started
- ACTIVE: Session running, logs being collected
- COMPLETED: Session ended, ready for upload
- UPLOADED: Artifacts uploaded to backend
Backend Integration
MQTT Topics
Device Publishes:-
Session Creation:
roboticks/fleet/sessions -
Session Completion:
roboticks/fleet/sessions
- Commands:
roboticks/devices/{dsn}/commands- CommandType::START_SESSION (3)
- CommandType::STOP_SESSION (4)
File Upload Integration (TODO)
The session completion now integrates with the file upload mechanism documented in DEVICE_FILE_UPLOAD.md. Planned Flow:- Session completes (stopSession() called)
- DeviceManager collects all artifacts
- For each artifact:
- Request presigned URL via MQTT:
roboticks/fleet/{device_id}/file_upload/request - Receive response:
roboticks/fleet/{device_id}/file_upload/response - Upload file directly to S3 using presigned URL
- Request presigned URL via MQTT:
- Mark session as UPLOADED
- Notify backend of completion
Testing
Manual Test Procedure
-
Build SDK:
-
Run HelloWorld Composition:
-
Expected Behavior:
- DeviceManager starts WITHOUT auto-creating session
- HelloWorldModule starts and creates session via ZeroMQ
- Session runs for 60 seconds
- HelloWorldModule automatically stops session after 60s
- Session is finalized and marked as completed
-
Check Logs:
Look for:
- “Session auto-start disabled”
- “Session control subscriber created successfully”
- “Sent session control command: start”
- “Started existing session” or “Created and started new session”
- “60 seconds elapsed - stopping session”
- “Session completion published via MQTT”
Migration Guide
For Existing Code
Before:For Modules
To control sessions from a module:- Create publisher in onInitialize()
- Publish start command in onStart()
- Publish stop command when done (timer, condition, etc.)
Future Enhancements
- File Upload Implementation: Complete artifact upload using AWS IoT presigned URLs
- Signal Handling: Graceful shutdown on SIGINT/SIGTERM with file upload
- Interrupted Session Recovery: Resume and complete interrupted sessions
- Session Persistence: Save/load session state across restarts
- Session Metadata API: Rich metadata tagging from modules
- Multi-Session Support: Run multiple sessions concurrently
Related Documentation
- DEVICE_FILE_UPLOAD.md - File upload via presigned URLs
- ROBOTICKS_SDK_ARCHITECTURE.md - SDK architecture overview
- SDK_FILE_UPLOAD_IMPLEMENTATION.md - File upload implementation guide