Files
2026-03-28 12:05:34 +11:00

124 lines
4.3 KiB
C++

#pragma once
#ifndef ANSCLOUD_DEVICE_AGENT_H
#define ANSCLOUD_DEVICE_AGENT_H
#include <anscloud/common/types.h>
#include <anscloud/common/i_message_broker.h>
#include <string>
#include <functional>
#include <memory>
#include <atomic>
#ifdef ANSCLOUD_DEVICE_EXPORTS
#define ANSCLOUD_DEVICE_API __declspec(dllexport)
#else
#define ANSCLOUD_DEVICE_API __declspec(dllimport)
#endif
namespace anscloud {
//=============================================================================
// DeviceAgent Configuration
//=============================================================================
struct DeviceAgentConfig {
BrokerConfig broker;
DeviceCredentials credentials;
// Telemetry intervals (seconds)
int heartbeat_interval_sec = 30;
int metrics_interval_sec = 60;
int snapshot_interval_sec = 0; // 0 = disabled
// Device info
std::string firmware_version;
std::string ansvis_version;
std::string ip_address;
std::string local_api_endpoint; // e.g. "http://127.0.0.1:8080"
};
//=============================================================================
// Application callback types - your app implements these
//=============================================================================
using CommandHandler = std::function<CommandResponse(const Command& cmd)>;
using MetricsProvider = std::function<SystemMetrics()>;
using CameraListProvider = std::function<std::vector<CameraInfo>()>;
using InferenceProvider = std::function<InferenceMetrics()>;
using SnapshotProvider = std::function<std::string(const std::string& camera_id)>; // returns base64 JPEG
//=============================================================================
// Agent runtime status
//=============================================================================
struct AgentStatus {
bool connected = false;
bool running = false;
uint64_t uptime_seconds = 0;
uint64_t commands_received = 0;
uint64_t commands_executed = 0;
uint64_t heartbeats_sent = 0;
uint64_t telemetry_sent = 0;
uint64_t events_published = 0;
uint64_t reconnect_count = 0;
std::string last_error;
};
//=============================================================================
// DeviceAgent - AIBOX cloud agent
//
// Inject your existing RabbitMQ C++ API via BrokerFactory:
//
// DeviceAgent agent([](){ return std::make_unique<MyRabbitMQBroker>(); });
// agent.configure(config);
// agent.set_command_handler(...);
// agent.start(); // non-blocking
//
//=============================================================================
class ANSCLOUD_DEVICE_API DeviceAgent {
public:
// Construct with broker factory (your RabbitMQ API)
explicit DeviceAgent(BrokerFactory factory);
~DeviceAgent();
// Non-copyable, movable
DeviceAgent(const DeviceAgent&) = delete;
DeviceAgent& operator=(const DeviceAgent&) = delete;
DeviceAgent(DeviceAgent&&) noexcept;
DeviceAgent& operator=(DeviceAgent&&) noexcept;
//--- Configuration (call before start) -----------------------------------
void configure(const DeviceAgentConfig& config);
void set_command_handler(CommandHandler handler);
void set_metrics_provider(MetricsProvider provider);
void set_camera_list_provider(CameraListProvider provider);
void set_inference_provider(InferenceProvider provider);
void set_snapshot_provider(SnapshotProvider provider);
//--- Lifecycle (non-blocking) --------------------------------------------
bool start(); // Connects, declares queues, starts background threads
void stop(); // Publishes offline, disconnects, stops threads
bool is_running() const;
bool is_connected() const;
//--- Manual publishing ---------------------------------------------------
void publish_event(const DeviceEvent& event);
void publish_telemetry(const DeviceTelemetry& telemetry);
void send_heartbeat_now();
void send_metrics_now();
//--- Status & callbacks --------------------------------------------------
AgentStatus get_status() const;
void on_connection_changed(ConnectionCallback cb);
void on_error(ErrorCallback cb);
void on_broadcast(std::function<void(const std::string& message)> cb);
private:
class Impl;
std::unique_ptr<Impl> m_impl;
};
} // namespace anscloud
#endif // ANSCLOUD_DEVICE_AGENT_H