Initial idea
This commit is contained in:
125
anscloud-common/include/anscloud/common/i_message_broker.h
Normal file
125
anscloud-common/include/anscloud/common/i_message_broker.h
Normal file
@@ -0,0 +1,125 @@
|
||||
#pragma once
|
||||
//=============================================================================
|
||||
// IMessageBroker - Abstract interface for message broker operations
|
||||
//
|
||||
// Your existing RabbitMQ C++ API implements this interface.
|
||||
// The SDK (DeviceAgent / GatewayAgent) depends only on this abstraction,
|
||||
// not on any concrete AMQP library.
|
||||
//=============================================================================
|
||||
|
||||
#ifndef ANSCLOUD_COMMON_I_MESSAGE_BROKER_H
|
||||
#define ANSCLOUD_COMMON_I_MESSAGE_BROKER_H
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
|
||||
namespace anscloud {
|
||||
|
||||
// Forward declare
|
||||
struct BrokerConfig;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Message received from broker
|
||||
//-----------------------------------------------------------------------------
|
||||
struct BrokerMessage {
|
||||
std::string exchange;
|
||||
std::string routing_key;
|
||||
std::string body; // JSON payload
|
||||
std::string correlation_id; // For RPC matching
|
||||
std::string reply_to; // Reply queue/routing key
|
||||
std::string message_id;
|
||||
std::string content_type; // "application/json"
|
||||
uint64_t delivery_tag = 0;
|
||||
bool redelivered = false;
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Publish options
|
||||
//-----------------------------------------------------------------------------
|
||||
struct PublishOptions {
|
||||
std::string correlation_id;
|
||||
std::string reply_to;
|
||||
std::string message_id;
|
||||
std::string content_type = "application/json";
|
||||
bool persistent = true;
|
||||
int expiration_ms = 0; // 0 = no expiry (TTL)
|
||||
};
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Callback types
|
||||
//-----------------------------------------------------------------------------
|
||||
using MessageCallback = std::function<void(const BrokerMessage& msg)>;
|
||||
using ConnectionCallback = std::function<void(bool connected)>;
|
||||
using ErrorCallback = std::function<void(const std::string& error)>;
|
||||
|
||||
//=============================================================================
|
||||
// IMessageBroker - Pure virtual interface
|
||||
//
|
||||
// Your existing RabbitMQ C++ API creates a class that inherits from this:
|
||||
//
|
||||
// class MyRabbitMQBroker : public anscloud::IMessageBroker {
|
||||
// // ... implement all pure virtual methods using your existing API
|
||||
// };
|
||||
//
|
||||
//=============================================================================
|
||||
class IMessageBroker {
|
||||
public:
|
||||
virtual ~IMessageBroker() = default;
|
||||
|
||||
//--- Connection lifecycle ------------------------------------------------
|
||||
virtual bool connect(const std::string& host, int port,
|
||||
const std::string& username, const std::string& password,
|
||||
const std::string& vhost) = 0;
|
||||
virtual void disconnect() = 0;
|
||||
virtual bool is_connected() const = 0;
|
||||
|
||||
//--- Exchange & Queue declarations (idempotent) --------------------------
|
||||
virtual bool declare_exchange(const std::string& name,
|
||||
const std::string& type = "topic",
|
||||
bool durable = true) = 0;
|
||||
|
||||
virtual bool declare_queue(const std::string& name,
|
||||
bool durable = true,
|
||||
bool exclusive = false,
|
||||
bool auto_delete = false) = 0;
|
||||
|
||||
virtual bool bind_queue(const std::string& queue,
|
||||
const std::string& exchange,
|
||||
const std::string& routing_key) = 0;
|
||||
|
||||
//--- Publishing ----------------------------------------------------------
|
||||
virtual bool publish(const std::string& exchange,
|
||||
const std::string& routing_key,
|
||||
const std::string& body,
|
||||
const PublishOptions& options = {}) = 0;
|
||||
|
||||
//--- Consuming -----------------------------------------------------------
|
||||
// Start consuming from a queue. Messages delivered via callback.
|
||||
// Returns a consumer tag (string) that can be used to cancel.
|
||||
virtual std::string start_consuming(const std::string& queue,
|
||||
MessageCallback callback,
|
||||
bool auto_ack = true) = 0;
|
||||
|
||||
// Cancel a consumer by tag
|
||||
virtual void cancel_consumer(const std::string& consumer_tag) = 0;
|
||||
|
||||
// Process pending messages (call from event loop).
|
||||
// timeout_ms: max time to wait for messages. 0 = non-blocking poll.
|
||||
// Returns number of messages processed.
|
||||
virtual int process_messages(int timeout_ms = 100) = 0;
|
||||
|
||||
//--- Callbacks -----------------------------------------------------------
|
||||
virtual void on_connected(ConnectionCallback cb) = 0;
|
||||
virtual void on_error(ErrorCallback cb) = 0;
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
// Factory function type - your application provides this
|
||||
//=============================================================================
|
||||
using BrokerFactory = std::function<std::unique_ptr<IMessageBroker>()>;
|
||||
|
||||
} // namespace anscloud
|
||||
|
||||
#endif // ANSCLOUD_COMMON_I_MESSAGE_BROKER_H
|
||||
47
anscloud-common/include/anscloud/common/json_serializer.h
Normal file
47
anscloud-common/include/anscloud/common/json_serializer.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
#ifndef ANSCLOUD_COMMON_JSON_SERIALIZER_H
|
||||
#define ANSCLOUD_COMMON_JSON_SERIALIZER_H
|
||||
|
||||
#include "types.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace anscloud {
|
||||
namespace json {
|
||||
|
||||
// Serialization (struct -> JSON string)
|
||||
std::string serialize(const Heartbeat& hb);
|
||||
std::string serialize(const DeviceTelemetry& tel);
|
||||
std::string serialize(const SystemMetrics& m);
|
||||
std::string serialize(const Command& cmd);
|
||||
std::string serialize(const CommandResponse& resp);
|
||||
std::string serialize(const DeviceEvent& evt);
|
||||
std::string serialize(const DeviceStatusMessage& msg);
|
||||
std::string serialize(const CameraInfo& cam);
|
||||
std::string serialize(const std::vector<CameraInfo>& cams);
|
||||
std::string serialize(const DeviceState& state);
|
||||
std::string serialize(const std::vector<DeviceState>& states);
|
||||
std::string serialize(const CommandResult& result);
|
||||
|
||||
// Deserialization (JSON string -> struct)
|
||||
Heartbeat deserialize_heartbeat(const std::string& json);
|
||||
DeviceTelemetry deserialize_telemetry(const std::string& json);
|
||||
SystemMetrics deserialize_metrics(const std::string& json);
|
||||
Command deserialize_command(const std::string& json);
|
||||
CommandResponse deserialize_response(const std::string& json);
|
||||
DeviceEvent deserialize_event(const std::string& json);
|
||||
DeviceStatusMessage deserialize_status_message(const std::string& json);
|
||||
CameraInfo deserialize_camera(const std::string& json);
|
||||
std::vector<CameraInfo> deserialize_cameras(const std::string& json);
|
||||
DeviceState deserialize_device_state(const std::string& json);
|
||||
std::vector<DeviceState> deserialize_device_states(const std::string& json);
|
||||
CommandResult deserialize_command_result(const std::string& json);
|
||||
|
||||
// Utility
|
||||
std::string now_iso8601();
|
||||
std::string generate_uuid();
|
||||
|
||||
} // namespace json
|
||||
} // namespace anscloud
|
||||
|
||||
#endif // ANSCLOUD_COMMON_JSON_SERIALIZER_H
|
||||
255
anscloud-common/include/anscloud/common/types.h
Normal file
255
anscloud-common/include/anscloud/common/types.h
Normal file
@@ -0,0 +1,255 @@
|
||||
#pragma once
|
||||
#ifndef ANSCLOUD_COMMON_TYPES_H
|
||||
#define ANSCLOUD_COMMON_TYPES_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
|
||||
namespace anscloud {
|
||||
|
||||
//=============================================================================
|
||||
// Protocol Constants - Exchange & Queue names
|
||||
//=============================================================================
|
||||
namespace protocol {
|
||||
// Exchanges
|
||||
constexpr const char* EX_DEVICE_TELEMETRY = "ex.device.telemetry";
|
||||
constexpr const char* EX_DEVICE_STATUS = "ex.device.status";
|
||||
constexpr const char* EX_DEVICE_EVENTS = "ex.device.events";
|
||||
constexpr const char* EX_COMMAND = "ex.command";
|
||||
constexpr const char* EX_COMMAND_RESPONSE = "ex.command.response";
|
||||
constexpr const char* EX_BROADCAST = "ex.broadcast";
|
||||
|
||||
// Queue name patterns (format with device_id)
|
||||
inline std::string queue_cmd(const std::string& device_id) {
|
||||
return "q.cmd." + device_id;
|
||||
}
|
||||
inline std::string queue_broadcast(const std::string& device_id) {
|
||||
return "q.broadcast." + device_id;
|
||||
}
|
||||
|
||||
// Gateway consumer queues
|
||||
constexpr const char* Q_TELEMETRY_INGEST = "q.telemetry.ingest";
|
||||
constexpr const char* Q_EVENTS_PROCESSOR = "q.events.processor";
|
||||
constexpr const char* Q_STATUS_TRACKER = "q.status.tracker";
|
||||
constexpr const char* Q_COMMAND_RESPONSES = "q.command.responses";
|
||||
} // namespace protocol
|
||||
|
||||
//=============================================================================
|
||||
// Broker Configuration
|
||||
//=============================================================================
|
||||
struct BrokerConfig {
|
||||
std::string host = "localhost";
|
||||
int port = 5672;
|
||||
std::string username = "guest";
|
||||
std::string password = "guest";
|
||||
std::string vhost = "/";
|
||||
bool use_tls = false;
|
||||
int heartbeat_sec = 60;
|
||||
int reconnect_delay_ms = 1000;
|
||||
int reconnect_max_delay_ms = 30000;
|
||||
int connection_timeout_ms = 10000;
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
// Device Identity
|
||||
//=============================================================================
|
||||
struct DeviceCredentials {
|
||||
std::string device_id; // e.g. "AIBOX-001"
|
||||
std::string device_secret; // Authentication token
|
||||
std::string tenant_id; // Multi-tenancy isolation
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
// Device Status
|
||||
//=============================================================================
|
||||
enum class DeviceStatus {
|
||||
Online,
|
||||
Offline,
|
||||
Connecting,
|
||||
Error
|
||||
};
|
||||
|
||||
const char* to_string(DeviceStatus status);
|
||||
DeviceStatus device_status_from_string(const std::string& str);
|
||||
|
||||
struct DeviceStatusMessage {
|
||||
std::string device_id;
|
||||
DeviceStatus status = DeviceStatus::Offline;
|
||||
std::string timestamp; // ISO 8601
|
||||
std::string firmware_version;
|
||||
std::string ansvis_version;
|
||||
std::string ip_address;
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
// Telemetry Types
|
||||
//=============================================================================
|
||||
struct SystemMetrics {
|
||||
double cpu_usage_percent = 0.0;
|
||||
double ram_usage_percent = 0.0;
|
||||
double ram_used_mb = 0.0;
|
||||
double ram_total_mb = 0.0;
|
||||
double gpu_usage_percent = 0.0;
|
||||
double gpu_memory_used_mb = 0.0;
|
||||
double gpu_memory_total_mb = 0.0;
|
||||
double gpu_temp_celsius = 0.0;
|
||||
double disk_usage_percent = 0.0;
|
||||
double disk_used_gb = 0.0;
|
||||
double disk_total_gb = 0.0;
|
||||
int process_count = 0;
|
||||
double uptime_hours = 0.0;
|
||||
};
|
||||
|
||||
struct CameraInfo {
|
||||
std::string camera_id;
|
||||
std::string name;
|
||||
std::string rtsp_url;
|
||||
std::string status; // "active", "error", "disabled"
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
double fps = 0.0;
|
||||
std::string error_message;
|
||||
};
|
||||
|
||||
struct InferenceMetrics {
|
||||
int active_models = 0;
|
||||
double total_fps = 0.0;
|
||||
double avg_latency_ms = 0.0;
|
||||
int detections_per_second = 0;
|
||||
};
|
||||
|
||||
struct Heartbeat {
|
||||
std::string device_id;
|
||||
std::string timestamp;
|
||||
uint64_t uptime_seconds = 0;
|
||||
int active_cameras = 0;
|
||||
int active_models = 0;
|
||||
};
|
||||
|
||||
struct DeviceTelemetry {
|
||||
std::string device_id;
|
||||
std::string timestamp;
|
||||
SystemMetrics system;
|
||||
std::vector<CameraInfo> cameras;
|
||||
InferenceMetrics inference;
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
// Command Types
|
||||
//=============================================================================
|
||||
enum class CommandType {
|
||||
// System
|
||||
GetSystemInfo,
|
||||
GetConfig,
|
||||
SetConfig,
|
||||
RestartService,
|
||||
RebootDevice,
|
||||
UpdateFirmware,
|
||||
RunDiagnostics,
|
||||
// Camera
|
||||
GetCameraList,
|
||||
AddCamera,
|
||||
RemoveCamera,
|
||||
UpdateCamera,
|
||||
GetCameraSnapshot,
|
||||
// AI Model
|
||||
GetModelList,
|
||||
LoadModel,
|
||||
UnloadModel,
|
||||
// Custom
|
||||
Custom
|
||||
};
|
||||
|
||||
const char* to_string(CommandType type);
|
||||
CommandType command_type_from_string(const std::string& str);
|
||||
|
||||
enum class CommandStatus {
|
||||
Pending,
|
||||
Executing,
|
||||
Success,
|
||||
Failed,
|
||||
Timeout,
|
||||
Cancelled
|
||||
};
|
||||
|
||||
const char* to_string(CommandStatus status);
|
||||
CommandStatus command_status_from_string(const std::string& str);
|
||||
|
||||
struct Command {
|
||||
std::string command_id; // UUID
|
||||
std::string device_id; // Target device
|
||||
CommandType type = CommandType::Custom;
|
||||
std::string custom_type; // When type == Custom
|
||||
std::string params_json; // JSON parameters
|
||||
std::string correlation_id; // For RPC matching
|
||||
std::string timestamp;
|
||||
int timeout_seconds = 30;
|
||||
};
|
||||
|
||||
struct CommandResponse {
|
||||
std::string command_id;
|
||||
std::string device_id;
|
||||
CommandStatus status = CommandStatus::Pending;
|
||||
std::string result_json; // JSON result payload
|
||||
std::string error_message;
|
||||
std::string correlation_id;
|
||||
std::string timestamp;
|
||||
int execution_time_ms = 0;
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
// Event Types
|
||||
//=============================================================================
|
||||
enum class EventType {
|
||||
Alert,
|
||||
DetectionLPR,
|
||||
DetectionFace,
|
||||
DetectionObject,
|
||||
CameraStatusChanged,
|
||||
ModelStatusChanged,
|
||||
SystemAlert,
|
||||
Custom
|
||||
};
|
||||
|
||||
const char* to_string(EventType type);
|
||||
EventType event_type_from_string(const std::string& str);
|
||||
|
||||
struct DeviceEvent {
|
||||
std::string event_id; // UUID
|
||||
std::string device_id;
|
||||
EventType type = EventType::Custom;
|
||||
std::string custom_type;
|
||||
std::string data_json; // Event payload
|
||||
std::string camera_id; // Optional: related camera
|
||||
std::string timestamp;
|
||||
std::string severity; // "info", "warning", "critical"
|
||||
};
|
||||
|
||||
//=============================================================================
|
||||
// Gateway-side types
|
||||
//=============================================================================
|
||||
struct CommandResult {
|
||||
bool success = false;
|
||||
CommandStatus status = CommandStatus::Pending;
|
||||
std::string result_json;
|
||||
std::string error_message;
|
||||
int execution_time_ms = 0;
|
||||
};
|
||||
|
||||
struct DeviceState {
|
||||
std::string device_id;
|
||||
DeviceStatus status = DeviceStatus::Offline;
|
||||
std::string last_seen; // ISO 8601
|
||||
std::string ip_address;
|
||||
std::string firmware_version;
|
||||
std::string ansvis_version;
|
||||
SystemMetrics last_metrics;
|
||||
int camera_count = 0;
|
||||
};
|
||||
|
||||
} // namespace anscloud
|
||||
|
||||
#endif // ANSCLOUD_COMMON_TYPES_H
|
||||
Reference in New Issue
Block a user