Files
ANSCMSCORE/_gen_src_04.py

196 lines
6.8 KiB
Python
Raw Normal View History

2026-03-28 12:05:34 +11:00
import os
R = r"C:\Projects\CMSCore"
def w(rel, txt):
p = os.path.join(R, rel)
os.makedirs(os.path.dirname(p), exist_ok=True)
with open(p, "w", encoding="utf-8", newline="\r\n") as f:
f.write(txt.lstrip("\n"))
print(f" OK: {rel}")
# ============================================================================
# 6. device_agent.h
# ============================================================================
w("anscloud-device/include/anscloud/device/device_agent.h", r'''#pragma once
// ==========================================================================
// ANSCloud SDK - Device Agent (AIBOX side)
//
// Manages the full device lifecycle:
// - Connects to broker via IMessageBroker (your existing RabbitMQ API)
// - Declares device queues, binds to exchanges
// - Listens for commands, dispatches to your CommandHandler
// - Publishes heartbeat, telemetry, events, status
// - Background threads for timers and message processing
// ==========================================================================
#include <anscloud/common/types.h>
#include <anscloud/common/i_message_broker.h>
#include <string>
#include <functional>
#include <memory>
#include <atomic>
#include <thread>
#include <mutex>
#include <chrono>
namespace anscloud {
// --------------------------------------------------------------------------
// Configuration
// --------------------------------------------------------------------------
struct DeviceAgentConfig {
std::string device_id;
std::string firmware_version;
std::string ansvis_version;
std::string local_ansvis_api; // e.g. "http://127.0.0.1:8080"
// Telemetry intervals (seconds)
int heartbeat_interval_sec = 30;
int metrics_interval_sec = 60;
// Logging
bool verbose_logging = false;
};
// --------------------------------------------------------------------------
// Provider callbacks - your application implements these
// --------------------------------------------------------------------------
using MetricsProvider = std::function<SystemMetrics()>;
using CameraListProvider = std::function<std::vector<CameraInfo>()>;
using InferenceProvider = std::function<InferenceMetrics()>;
// --------------------------------------------------------------------------
// Agent status info
// --------------------------------------------------------------------------
struct DeviceAgentStatus {
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_sent = 0;
uint64_t reconnect_count = 0;
std::string last_error;
};
// --------------------------------------------------------------------------
// DeviceAgent
// --------------------------------------------------------------------------
class DeviceAgent {
public:
DeviceAgent();
~DeviceAgent();
// Non-copyable
DeviceAgent(const DeviceAgent&) = delete;
DeviceAgent& operator=(const DeviceAgent&) = delete;
// --- Setup ---
// Pass YOUR existing RabbitMQ API instance (already configured with host/port/credentials).
// DeviceAgent does NOT own the broker - you manage its lifetime.
void configure(IMessageBroker* broker, const DeviceAgentConfig& config);
// --- Provider registration ---
void set_command_handler(CommandCallback handler);
void set_metrics_provider(MetricsProvider provider);
void set_camera_list_provider(CameraListProvider provider);
void set_inference_provider(InferenceProvider provider);
// --- Lifecycle ---
bool start(); // NON-BLOCKING: connects, declares topology, starts background threads
void stop(); // Publishes offline status, stops threads, disconnects
bool is_running() const;
bool is_connected() const;
// --- Manual publishing ---
bool publish_event(const DeviceEvent& evt);
bool publish_telemetry(); // gathers from providers and publishes
bool send_heartbeat();
bool send_metrics();
// --- Callbacks ---
void on_connection_changed(ConnectionCallback cb);
void on_error(ErrorCallback cb);
void on_broadcast(BroadcastCallback cb);
// --- Status ---
DeviceAgentStatus get_status() const;
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
};
} // namespace anscloud
''')
# ============================================================================
# 7. device_agent_c.h (C API for FFI)
# ============================================================================
w("anscloud-device/include/anscloud/device/device_agent_c.h", r'''#pragma once
// ==========================================================================
// ANSCloud SDK - Device Agent C API (for FFI: Go, Python, C#, etc.)
// ==========================================================================
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stdbool.h>
// Opaque handle
typedef void* AnsDeviceHandle;
// C callback types
typedef const char* (*AnsDeviceCmdHandlerFn)(
void* user_data,
const char* command_json // full Command as JSON
); // returns CommandResponse as JSON (caller frees with ansdevice_free_string)
typedef void (*AnsDeviceConnectionFn)(void* user_data, bool connected, const char* info);
typedef void (*AnsDeviceErrorFn)(void* user_data, int code, const char* message);
typedef void (*AnsDeviceBroadcastFn)(void* user_data, const char* message_json);
// --- Lifecycle ---
AnsDeviceHandle ansdevice_create(void);
void ansdevice_destroy(AnsDeviceHandle h);
// Configure: broker_ptr is your IMessageBroker* cast to void*
bool ansdevice_configure(AnsDeviceHandle h, void* broker_ptr, const char* config_json);
// --- Handlers ---
void ansdevice_set_command_handler(AnsDeviceHandle h, AnsDeviceCmdHandlerFn fn, void* user_data);
void ansdevice_on_connection_changed(AnsDeviceHandle h, AnsDeviceConnectionFn fn, void* user_data);
void ansdevice_on_error(AnsDeviceHandle h, AnsDeviceErrorFn fn, void* user_data);
void ansdevice_on_broadcast(AnsDeviceHandle h, AnsDeviceBroadcastFn fn, void* user_data);
// --- Lifecycle ---
bool ansdevice_start(AnsDeviceHandle h);
void ansdevice_stop(AnsDeviceHandle h);
bool ansdevice_is_running(AnsDeviceHandle h);
bool ansdevice_is_connected(AnsDeviceHandle h);
// --- Publishing ---
bool ansdevice_publish_event(AnsDeviceHandle h, const char* event_json);
bool ansdevice_send_heartbeat(AnsDeviceHandle h);
bool ansdevice_send_metrics(AnsDeviceHandle h);
// --- Status ---
const char* ansdevice_get_status(AnsDeviceHandle h); // returns JSON, free with ansdevice_free_string
// --- Memory ---
void ansdevice_free_string(const char* str);
// --- Version ---
const char* ansdevice_version(void);
#ifdef __cplusplus
}
#endif
''')
print(" [6-7] device_agent.h, device_agent_c.h done")