Initial idea
This commit is contained in:
288
_gen_src_02.py
Normal file
288
_gen_src_02.py
Normal file
@@ -0,0 +1,288 @@
|
||||
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}")
|
||||
|
||||
# ============================================================================
|
||||
# 2. i_message_broker.h - Abstract interface (your existing RabbitMQ API plugs in)
|
||||
# ============================================================================
|
||||
w("anscloud-common/include/anscloud/common/i_message_broker.h", r'''#pragma once
|
||||
// ==========================================================================
|
||||
// ANSCloud SDK - Abstract Message Broker Interface
|
||||
//
|
||||
// Your existing RabbitMQ C++ API implements this interface.
|
||||
// The SDK never depends on rabbitmq-c directly.
|
||||
// ==========================================================================
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <cstdint>
|
||||
|
||||
namespace anscloud {
|
||||
|
||||
// Callback when a message is received on a consumer
|
||||
using MessageReceivedFn = std::function<void(
|
||||
const std::string& consumer_tag,
|
||||
const std::string& exchange,
|
||||
const std::string& routing_key,
|
||||
const std::string& correlation_id,
|
||||
const std::string& reply_to,
|
||||
const std::string& body
|
||||
)>;
|
||||
|
||||
// Callback for connection state changes
|
||||
using BrokerConnectionFn = std::function<void(bool connected, const std::string& info)>;
|
||||
|
||||
// Callback for errors
|
||||
using BrokerErrorFn = std::function<void(int code, const std::string& message)>;
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// IMessageBroker - Abstract interface for AMQP operations
|
||||
//
|
||||
// Implement this by wrapping your existing RabbitMQ C++ API.
|
||||
// Both DeviceAgent and GatewayAgent receive an IMessageBroker* at configure().
|
||||
// --------------------------------------------------------------------------
|
||||
class IMessageBroker {
|
||||
public:
|
||||
virtual ~IMessageBroker() = default;
|
||||
|
||||
// --- Connection lifecycle ---
|
||||
virtual bool connect() = 0;
|
||||
virtual void disconnect() = 0;
|
||||
virtual bool is_connected() const = 0;
|
||||
|
||||
// --- Topology declaration ---
|
||||
virtual bool declare_exchange(
|
||||
const std::string& exchange_name,
|
||||
const std::string& type = "topic", // "topic", "direct", "fanout"
|
||||
bool durable = true
|
||||
) = 0;
|
||||
|
||||
virtual bool declare_queue(
|
||||
const std::string& queue_name,
|
||||
bool durable = true,
|
||||
bool exclusive = false,
|
||||
bool auto_delete = false
|
||||
) = 0;
|
||||
|
||||
virtual bool bind_queue(
|
||||
const std::string& queue_name,
|
||||
const std::string& exchange_name,
|
||||
const std::string& routing_key
|
||||
) = 0;
|
||||
|
||||
// --- Publishing ---
|
||||
virtual bool publish(
|
||||
const std::string& exchange,
|
||||
const std::string& routing_key,
|
||||
const std::string& body,
|
||||
const std::string& correlation_id = "",
|
||||
const std::string& reply_to = "",
|
||||
bool persistent = true
|
||||
) = 0;
|
||||
|
||||
// --- Consuming ---
|
||||
// Start consuming from a queue. Returns consumer tag (or empty on failure).
|
||||
virtual std::string start_consuming(
|
||||
const std::string& queue_name,
|
||||
MessageReceivedFn on_message
|
||||
) = 0;
|
||||
|
||||
// Cancel a consumer by tag.
|
||||
virtual bool cancel_consumer(const std::string& consumer_tag) = 0;
|
||||
|
||||
// Process pending messages (call from your event loop).
|
||||
// timeout_ms: max time to block waiting for messages (0 = non-blocking).
|
||||
// Returns number of messages processed.
|
||||
virtual int process_messages(uint32_t timeout_ms = 100) = 0;
|
||||
|
||||
// --- Connection callbacks ---
|
||||
virtual void on_connected(BrokerConnectionFn fn) = 0;
|
||||
virtual void on_error(BrokerErrorFn fn) = 0;
|
||||
};
|
||||
|
||||
} // namespace anscloud
|
||||
''')
|
||||
|
||||
# ============================================================================
|
||||
# 3. json_serializer.h
|
||||
# ============================================================================
|
||||
w("anscloud-common/include/anscloud/common/json_serializer.h", r'''#pragma once
|
||||
// ==========================================================================
|
||||
// ANSCloud SDK - JSON Serialization
|
||||
// Uses nlohmann/json (or simple manual serialization for now)
|
||||
// ==========================================================================
|
||||
|
||||
#include <anscloud/common/types.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace anscloud {
|
||||
namespace json {
|
||||
|
||||
// --- Serialize to 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& status);
|
||||
std::string serialize(const CameraInfo& cam);
|
||||
std::string serialize(const std::vector<CameraInfo>& cams);
|
||||
std::string serialize(const InferenceMetrics& inf);
|
||||
|
||||
// --- Deserialize from JSON string ---
|
||||
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_camera_list(const std::string& json);
|
||||
|
||||
// --- Utilities ---
|
||||
std::string now_iso8601();
|
||||
std::string generate_uuid();
|
||||
|
||||
} // namespace json
|
||||
} // namespace anscloud
|
||||
''')
|
||||
|
||||
# ============================================================================
|
||||
# 4. types.cpp
|
||||
# ============================================================================
|
||||
w("anscloud-common/src/types.cpp", r'''#include <anscloud/common/types.h>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace anscloud {
|
||||
|
||||
// --- DeviceStatus ---
|
||||
const char* to_string(DeviceStatus s) {
|
||||
switch (s) {
|
||||
case DeviceStatus::Online: return "online";
|
||||
case DeviceStatus::Offline: return "offline";
|
||||
case DeviceStatus::Connecting: return "connecting";
|
||||
case DeviceStatus::Error: return "error";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
DeviceStatus device_status_from_string(const std::string& s) {
|
||||
static const std::unordered_map<std::string, DeviceStatus> m = {
|
||||
{"online", DeviceStatus::Online}, {"offline", DeviceStatus::Offline},
|
||||
{"connecting", DeviceStatus::Connecting}, {"error", DeviceStatus::Error}
|
||||
};
|
||||
auto it = m.find(s);
|
||||
return it != m.end() ? it->second : DeviceStatus::Unknown;
|
||||
}
|
||||
|
||||
// --- CommandType ---
|
||||
const char* to_string(CommandType t) {
|
||||
switch (t) {
|
||||
case CommandType::GetSystemInfo: return "get_system_info";
|
||||
case CommandType::GetConfig: return "get_config";
|
||||
case CommandType::SetConfig: return "set_config";
|
||||
case CommandType::GetCameraList: return "get_camera_list";
|
||||
case CommandType::AddCamera: return "add_camera";
|
||||
case CommandType::RemoveCamera: return "remove_camera";
|
||||
case CommandType::UpdateCamera: return "update_camera";
|
||||
case CommandType::GetCameraSnapshot:return "get_camera_snapshot";
|
||||
case CommandType::StartStream: return "start_stream";
|
||||
case CommandType::StopStream: return "stop_stream";
|
||||
case CommandType::RestartService: return "restart_service";
|
||||
case CommandType::RebootDevice: return "reboot_device";
|
||||
case CommandType::UpdateFirmware: return "update_firmware";
|
||||
case CommandType::RunDiagnostics: return "run_diagnostics";
|
||||
case CommandType::GetLogs: return "get_logs";
|
||||
case CommandType::SetSchedule: return "set_schedule";
|
||||
case CommandType::GetSchedule: return "get_schedule";
|
||||
default: return "custom";
|
||||
}
|
||||
}
|
||||
|
||||
CommandType command_type_from_string(const std::string& s) {
|
||||
static const std::unordered_map<std::string, CommandType> m = {
|
||||
{"get_system_info", CommandType::GetSystemInfo},
|
||||
{"get_config", CommandType::GetConfig},
|
||||
{"set_config", CommandType::SetConfig},
|
||||
{"get_camera_list", CommandType::GetCameraList},
|
||||
{"add_camera", CommandType::AddCamera},
|
||||
{"remove_camera", CommandType::RemoveCamera},
|
||||
{"update_camera", CommandType::UpdateCamera},
|
||||
{"get_camera_snapshot", CommandType::GetCameraSnapshot},
|
||||
{"start_stream", CommandType::StartStream},
|
||||
{"stop_stream", CommandType::StopStream},
|
||||
{"restart_service", CommandType::RestartService},
|
||||
{"reboot_device", CommandType::RebootDevice},
|
||||
{"update_firmware", CommandType::UpdateFirmware},
|
||||
{"run_diagnostics", CommandType::RunDiagnostics},
|
||||
{"get_logs", CommandType::GetLogs},
|
||||
{"set_schedule", CommandType::SetSchedule},
|
||||
{"get_schedule", CommandType::GetSchedule}
|
||||
};
|
||||
auto it = m.find(s);
|
||||
return it != m.end() ? it->second : CommandType::Custom;
|
||||
}
|
||||
|
||||
// --- EventType ---
|
||||
const char* to_string(EventType t) {
|
||||
switch (t) {
|
||||
case EventType::Alert: return "alert";
|
||||
case EventType::DetectionLPR: return "detection_lpr";
|
||||
case EventType::DetectionFace: return "detection_face";
|
||||
case EventType::DetectionObject: return "detection_object";
|
||||
case EventType::DetectionMotion: return "detection_motion";
|
||||
case EventType::LineCrossing: return "line_crossing";
|
||||
case EventType::Intrusion: return "intrusion";
|
||||
case EventType::Loitering: return "loitering";
|
||||
case EventType::CameraDisconnected: return "camera_disconnected";
|
||||
case EventType::CameraReconnected: return "camera_reconnected";
|
||||
case EventType::StorageWarning: return "storage_warning";
|
||||
case EventType::SystemError: return "system_error";
|
||||
default: return "custom";
|
||||
}
|
||||
}
|
||||
|
||||
EventType event_type_from_string(const std::string& s) {
|
||||
static const std::unordered_map<std::string, EventType> m = {
|
||||
{"alert", EventType::Alert},
|
||||
{"detection_lpr", EventType::DetectionLPR},
|
||||
{"detection_face", EventType::DetectionFace},
|
||||
{"detection_object", EventType::DetectionObject},
|
||||
{"detection_motion", EventType::DetectionMotion},
|
||||
{"line_crossing", EventType::LineCrossing},
|
||||
{"intrusion", EventType::Intrusion},
|
||||
{"loitering", EventType::Loitering},
|
||||
{"camera_disconnected", EventType::CameraDisconnected},
|
||||
{"camera_reconnected", EventType::CameraReconnected},
|
||||
{"storage_warning", EventType::StorageWarning},
|
||||
{"system_error", EventType::SystemError}
|
||||
};
|
||||
auto it = m.find(s);
|
||||
return it != m.end() ? it->second : EventType::Custom;
|
||||
}
|
||||
|
||||
// --- CommandStatus ---
|
||||
const char* to_string(CommandStatus s) {
|
||||
switch (s) {
|
||||
case CommandStatus::Success: return "success";
|
||||
case CommandStatus::Failed: return "failed";
|
||||
case CommandStatus::Timeout: return "timeout";
|
||||
case CommandStatus::Rejected: return "rejected";
|
||||
case CommandStatus::Pending: return "pending";
|
||||
case CommandStatus::PartialSuccess: return "partial_success";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace anscloud
|
||||
''')
|
||||
|
||||
print(" [2-4] i_message_broker.h, json_serializer.h, types.cpp done")
|
||||
Reference in New Issue
Block a user