OrangePi3588Media/plugins/alarm/alarm_node.cpp
2025-12-31 11:00:08 +08:00

297 lines
11 KiB
C++

#include <atomic>
#include <chrono>
#include <iostream>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "node.h"
#include "rule_engine.h"
#include "frame_ring_buffer.h"
#include "actions/action_base.h"
#include "actions/log_action.h"
#include "actions/http_action.h"
#include "actions/snapshot_action.h"
#include "actions/clip_action.h"
namespace rk3588 {
class AlarmNode : public INode {
public:
std::string Id() const override { return id_; }
std::string Type() const override { return "alarm"; }
bool Init(const SimpleJson& config, const NodeContext& ctx) override {
id_ = config.ValueOr<std::string>("id", "alarm");
// Parse labels for class name mapping
if (const SimpleJson* labels_cfg = config.Find("labels")) {
for (const auto& label : labels_cfg->AsArray()) {
labels_.push_back(label.AsString(""));
}
}
// Initialize rule engine
if (const SimpleJson* rules_cfg = config.Find("rules")) {
if (!rule_engine_.Init(*rules_cfg, labels_)) {
std::cerr << "[alarm] failed to init rule engine\n";
return false;
}
}
// Get pre-event buffer settings from clip action config
int pre_sec = 5;
int fps_hint = 25;
if (const SimpleJson* actions_cfg = config.Find("actions")) {
if (const SimpleJson* clip_cfg = actions_cfg->Find("clip")) {
pre_sec = clip_cfg->ValueOr<int>("pre_sec", 5);
fps_hint = clip_cfg->ValueOr<int>("fps", 25);
}
}
frame_buffer_ = std::make_shared<FrameRingBuffer>(pre_sec, fps_hint);
// Initialize actions
if (const SimpleJson* actions_cfg = config.Find("actions")) {
// Log action
if (const SimpleJson* log_cfg = actions_cfg->Find("log")) {
if (log_cfg->ValueOr<bool>("enable", true)) {
auto action = std::make_unique<LogAction>();
if (action->Init(*log_cfg)) {
actions_.push_back(std::move(action));
}
}
}
// Snapshot action (must be before HTTP to fill snapshot_url)
if (const SimpleJson* snap_cfg = actions_cfg->Find("snapshot")) {
if (snap_cfg->ValueOr<bool>("enable", false)) {
auto action = std::make_unique<SnapshotAction>();
if (action->Init(*snap_cfg)) {
actions_.push_back(std::move(action));
}
}
}
// Clip action (must be before HTTP to fill clip_url)
if (const SimpleJson* clip_cfg = actions_cfg->Find("clip")) {
if (clip_cfg->ValueOr<bool>("enable", false)) {
auto action = std::make_unique<ClipAction>();
if (action->Init(*clip_cfg)) {
auto* clip_ptr = static_cast<ClipAction*>(action.get());
clip_ptr->SetFrameBuffer(frame_buffer_);
clip_action_ = clip_ptr;
actions_.push_back(std::move(action));
}
}
}
// HTTP action (should be last to include media URLs)
if (const SimpleJson* http_cfg = actions_cfg->Find("http")) {
if (http_cfg->ValueOr<bool>("enable", false)) {
auto action = std::make_unique<HttpAction>();
if (action->Init(*http_cfg)) {
actions_.push_back(std::move(action));
}
}
}
} else {
// Default: just log action
auto action = std::make_unique<LogAction>();
SimpleJson empty_cfg;
action->Init(empty_cfg);
actions_.push_back(std::move(action));
}
input_queue_ = ctx.input_queue;
if (!input_queue_) {
std::cerr << "[alarm] no input queue for node " << id_ << "\n";
return false;
}
std::cout << "[alarm] initialized with " << actions_.size() << " actions\n";
return true;
}
bool Start() override {
std::cout << "[alarm] started\n";
return true;
}
void Stop() override {
// Ensure all actions fully stop (Drain only clears pending work; Stop must reclaim threads/resources).
for (auto& action : actions_) action->Drain();
for (auto& action : actions_) action->Stop();
std::cout << "[alarm] stopped, processed " << processed_frames_
<< " frames, triggered " << alarm_count_ << " alarms\n";
}
void Drain() override {
for (auto& action : actions_) {
action->Drain();
}
}
bool UpdateConfig(const SimpleJson& new_config) override {
const std::string new_id = new_config.ValueOr<std::string>("id", id_);
if (!new_id.empty() && new_id != id_) {
return false;
}
// Parse labels
std::vector<std::string> new_labels;
if (const SimpleJson* labels_cfg = new_config.Find("labels")) {
for (const auto& label : labels_cfg->AsArray()) {
new_labels.push_back(label.AsString(""));
}
}
// Build new rule engine
RuleEngine new_engine;
if (const SimpleJson* rules_cfg = new_config.Find("rules")) {
if (!new_engine.Init(*rules_cfg, new_labels)) {
return false;
}
}
// Pre-event buffer settings
int pre_sec = 5;
int fps_hint = 25;
if (const SimpleJson* actions_cfg = new_config.Find("actions")) {
if (const SimpleJson* clip_cfg = actions_cfg->Find("clip")) {
pre_sec = clip_cfg->ValueOr<int>("pre_sec", 5);
fps_hint = clip_cfg->ValueOr<int>("fps", 25);
}
}
auto new_frame_buffer = std::make_shared<FrameRingBuffer>(pre_sec, fps_hint);
// Initialize new actions
std::vector<std::unique_ptr<IAlarmAction>> new_actions;
if (const SimpleJson* actions_cfg = new_config.Find("actions")) {
if (const SimpleJson* log_cfg = actions_cfg->Find("log")) {
if (log_cfg->ValueOr<bool>("enable", true)) {
auto action = std::make_unique<LogAction>();
if (action->Init(*log_cfg)) {
new_actions.push_back(std::move(action));
}
}
}
if (const SimpleJson* snap_cfg = actions_cfg->Find("snapshot")) {
if (snap_cfg->ValueOr<bool>("enable", false)) {
auto action = std::make_unique<SnapshotAction>();
if (action->Init(*snap_cfg)) {
new_actions.push_back(std::move(action));
}
}
}
if (const SimpleJson* clip_cfg = actions_cfg->Find("clip")) {
if (clip_cfg->ValueOr<bool>("enable", false)) {
auto action = std::make_unique<ClipAction>();
if (action->Init(*clip_cfg)) {
auto* clip_ptr = static_cast<ClipAction*>(action.get());
clip_ptr->SetFrameBuffer(new_frame_buffer);
new_actions.push_back(std::move(action));
}
}
}
if (const SimpleJson* http_cfg = actions_cfg->Find("http")) {
if (http_cfg->ValueOr<bool>("enable", false)) {
auto action = std::make_unique<HttpAction>();
if (action->Init(*http_cfg)) {
new_actions.push_back(std::move(action));
}
}
}
}
if (new_actions.empty()) {
auto action = std::make_unique<LogAction>();
SimpleJson empty_cfg;
if (action->Init(empty_cfg)) {
new_actions.push_back(std::move(action));
}
}
std::vector<std::unique_ptr<IAlarmAction>> old_actions;
{
std::lock_guard<std::mutex> lock(mu_);
labels_ = std::move(new_labels);
rule_engine_ = std::move(new_engine);
frame_buffer_ = std::move(new_frame_buffer);
old_actions = std::move(actions_);
actions_ = std::move(new_actions);
clip_action_ = nullptr;
for (auto& a : actions_) {
if (auto* p = dynamic_cast<ClipAction*>(a.get())) {
clip_action_ = p;
break;
}
}
}
for (auto& action : old_actions) action->Drain();
for (auto& action : old_actions) action->Stop();
return true;
}
bool GetCustomMetrics(SimpleJson& out) const override {
std::lock_guard<std::mutex> lock(mu_);
SimpleJson::Object o;
o["alarm_total"] = SimpleJson(static_cast<double>(alarm_count_));
o["processed"] = SimpleJson(static_cast<double>(processed_frames_));
out = SimpleJson(std::move(o));
return true;
}
NodeStatus Process(FramePtr frame) override {
if (!frame) return NodeStatus::DROP;
std::lock_guard<std::mutex> lock(mu_);
if (frame_buffer_) frame_buffer_->Push(frame);
if (clip_action_) clip_action_->PushPostEventFrame(frame);
auto result = rule_engine_.Evaluate(frame);
if (result.matched) TriggerAlarm(result, frame);
++processed_frames_;
return NodeStatus::OK;
}
private:
void TriggerAlarm(const RuleMatchResult& result, FramePtr frame) {
++alarm_count_;
AlarmEvent event;
event.node_id = id_;
event.rule_name = result.rule_name;
event.timestamp_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
event.frame_id = frame->frame_id;
event.detections = result.matched_detections;
// Execute all actions in order
// Snapshot and Clip actions will fill snapshot_url and clip_url
// HTTP action should be last to include all URLs
for (auto& action : actions_) {
action->Execute(event, frame);
}
}
std::string id_;
std::vector<std::string> labels_;
RuleEngine rule_engine_;
std::shared_ptr<FrameRingBuffer> frame_buffer_;
std::vector<std::unique_ptr<IAlarmAction>> actions_;
ClipAction* clip_action_ = nullptr;
mutable std::mutex mu_;
std::shared_ptr<SpscQueue<FramePtr>> input_queue_;
uint64_t processed_frames_ = 0;
uint64_t alarm_count_ = 0;
};
REGISTER_NODE(AlarmNode, "alarm");
} // namespace rk3588