Initial idea

This commit is contained in:
2026-03-28 12:05:34 +11:00
commit 6c6bb79cab
21 changed files with 3860 additions and 0 deletions

View File

@@ -0,0 +1,123 @@
#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

View File

@@ -0,0 +1,114 @@
#pragma once
#ifndef ANSCLOUD_DEVICE_AGENT_C_H
#define ANSCLOUD_DEVICE_AGENT_C_H
#ifdef ANSCLOUD_DEVICE_EXPORTS
#define ANSCLOUD_DEVICE_CAPI __declspec(dllexport)
#else
#define ANSCLOUD_DEVICE_CAPI __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C" {
#endif
//=============================================================================
// Opaque handle
//=============================================================================
typedef struct AnsDeviceAgent* AnsDeviceAgentHandle;
//=============================================================================
// C-compatible config structs
//=============================================================================
typedef struct {
const char* host;
int port;
const char* username;
const char* password;
const char* vhost;
int use_tls;
} AnsDeviceBrokerConfig;
typedef struct {
const char* device_id;
const char* device_secret;
const char* tenant_id;
} AnsDeviceCredentials;
typedef struct {
AnsDeviceBrokerConfig broker;
AnsDeviceCredentials credentials;
int heartbeat_interval_sec;
int metrics_interval_sec;
const char* firmware_version;
const char* ansvis_version;
const char* ip_address;
} AnsDeviceConfig;
//=============================================================================
// Callback types
//=============================================================================
// command_json: JSON string of Command. Return: JSON string of CommandResponse.
// Caller must free returned string with ansdevice_free_string().
typedef const char* (*AnsDeviceCommandHandlerFn)(const char* command_json, void* user_data);
// metrics_json: caller must free returned JSON string
typedef const char* (*AnsDeviceMetricsProviderFn)(void* user_data);
typedef void (*AnsDeviceConnectionCallbackFn)(int connected, void* user_data);
typedef void (*AnsDeviceErrorCallbackFn)(const char* error, void* user_data);
typedef void (*AnsDeviceBroadcastCallbackFn)(const char* message, void* user_data);
//=============================================================================
// Lifecycle
//=============================================================================
// broker_factory_fn: function pointer that creates your IMessageBroker.
// For simplicity in C API, pass NULL to use a default (if you register one).
ANSCLOUD_DEVICE_CAPI AnsDeviceAgentHandle ansdevice_create(void);
ANSCLOUD_DEVICE_CAPI void ansdevice_destroy(AnsDeviceAgentHandle handle);
ANSCLOUD_DEVICE_CAPI int ansdevice_configure(AnsDeviceAgentHandle handle, const AnsDeviceConfig* config);
ANSCLOUD_DEVICE_CAPI int ansdevice_start(AnsDeviceAgentHandle handle);
ANSCLOUD_DEVICE_CAPI void ansdevice_stop(AnsDeviceAgentHandle handle);
ANSCLOUD_DEVICE_CAPI int ansdevice_is_running(AnsDeviceAgentHandle handle);
ANSCLOUD_DEVICE_CAPI int ansdevice_is_connected(AnsDeviceAgentHandle handle);
//=============================================================================
// Handlers & providers
//=============================================================================
ANSCLOUD_DEVICE_CAPI void ansdevice_set_command_handler(AnsDeviceAgentHandle handle,
AnsDeviceCommandHandlerFn fn, void* user_data);
ANSCLOUD_DEVICE_CAPI void ansdevice_set_metrics_provider(AnsDeviceAgentHandle handle,
AnsDeviceMetricsProviderFn fn, void* user_data);
//=============================================================================
// Publishing
//=============================================================================
ANSCLOUD_DEVICE_CAPI int ansdevice_publish_event(AnsDeviceAgentHandle handle, const char* event_json);
ANSCLOUD_DEVICE_CAPI void ansdevice_send_heartbeat(AnsDeviceAgentHandle handle);
ANSCLOUD_DEVICE_CAPI void ansdevice_send_metrics(AnsDeviceAgentHandle handle);
//=============================================================================
// Callbacks
//=============================================================================
ANSCLOUD_DEVICE_CAPI void ansdevice_on_connection_changed(AnsDeviceAgentHandle handle,
AnsDeviceConnectionCallbackFn fn, void* user_data);
ANSCLOUD_DEVICE_CAPI void ansdevice_on_error(AnsDeviceAgentHandle handle,
AnsDeviceErrorCallbackFn fn, void* user_data);
ANSCLOUD_DEVICE_CAPI void ansdevice_on_broadcast(AnsDeviceAgentHandle handle,
AnsDeviceBroadcastCallbackFn fn, void* user_data);
//=============================================================================
// Status & memory
//=============================================================================
// Returns JSON string. Caller must free with ansdevice_free_string().
ANSCLOUD_DEVICE_CAPI const char* ansdevice_get_status(AnsDeviceAgentHandle handle);
ANSCLOUD_DEVICE_CAPI void ansdevice_free_string(const char* str);
ANSCLOUD_DEVICE_CAPI const char* ansdevice_version(void);
#ifdef __cplusplus
}
#endif
#endif // ANSCLOUD_DEVICE_AGENT_C_H