OrangePi3588Media/plugins/alarm/rule_engine.h
2025-12-26 16:58:18 +08:00

60 lines
1.5 KiB
C++

#pragma once
#include <chrono>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "frame/frame.h"
#include "utils/simple_json.h"
namespace rk3588 {
struct RoiRect {
float x = 0.0f;
float y = 0.0f;
float w = 1.0f;
float h = 1.0f;
};
struct AlarmRule {
std::string name;
std::set<int> class_ids;
RoiRect roi;
int min_duration_ms = 0;
int cooldown_ms = 5000;
std::string schedule; // "HH:MM-HH:MM" format, empty = always active
};
struct RuleMatchResult {
bool matched = false;
std::string rule_name;
std::vector<Detection> matched_detections;
};
class RuleEngine {
public:
bool Init(const SimpleJson& rules_config, const std::vector<std::string>& labels);
RuleMatchResult Evaluate(const std::shared_ptr<Frame>& frame);
void Reset();
private:
bool IsInRoi(const Rect& bbox, const RoiRect& roi, int img_w, int img_h) const;
bool IsInSchedule(const std::string& schedule) const;
bool CheckDuration(const std::string& rule_name, bool currently_matched);
bool CheckCooldown(const std::string& rule_name);
void TriggerCooldown(const std::string& rule_name);
std::vector<AlarmRule> rules_;
std::vector<std::string> labels_;
// Duration tracking: rule_name -> first match time
std::map<std::string, std::chrono::steady_clock::time_point> duration_start_;
// Cooldown tracking: rule_name -> last trigger time
std::map<std::string, std::chrono::steady_clock::time_point> last_trigger_;
};
} // namespace rk3588