增加告警存入数据库

This commit is contained in:
GuanYuankai 2025-11-04 01:55:52 +00:00
parent 59436351cd
commit cff2c9fae7
7 changed files with 13 additions and 15 deletions

1
.gitignore vendored
View File

@ -50,6 +50,7 @@ mosquitto/data/
mosquitto/log/ mosquitto/log/
hls_streams/ hls_streams/
hls_streams/live hls_streams/live
*.db
# 特例列表 # 特例列表
!rknn_sdk/librknn_api/aarch64/*.so !rknn_sdk/librknn_api/aarch64/*.so

View File

@ -75,7 +75,7 @@ target_include_directories(edge_proxy_lib PUBLIC
/usr/include/opencv4 /usr/include/opencv4
${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/src
${GST_INCLUDE_DIRS} ${GST_INCLUDE_DIRS}
/usr/include/rga # rga.h im2d.h /usr/include/rga
) )
target_link_libraries(edge_proxy_lib PRIVATE target_link_libraries(edge_proxy_lib PRIVATE

Binary file not shown.

Binary file not shown.

View File

@ -1,16 +1,16 @@
// alarm/alarm_service.cpp // alarm/alarm_service.cpp
#include "alarm_service.h" #include "alarm_service.h"
#include "dataStorage/data_storage.h"
#include <chrono> #include <chrono>
#include <fstream> #include <fstream>
AlarmService::AlarmService(boost::asio::io_context& io, AlarmService::AlarmService(boost::asio::io_context& io,
PiperTTSInterface& tts_service, PiperTTSInterface& tts_service,
MqttClient& mqtt_client, DataStorage& data_storage) MqttClient& mqtt_client)
: m_io_context(io), : m_io_context(io),
m_tts_service(tts_service), m_tts_service(tts_service),
m_mqtt_client(mqtt_client), m_mqtt_client(mqtt_client),
m_data_storage(data_storage),
m_tts_running(true) { m_tts_running(true) {
m_tts_thread = std::thread(&AlarmService::tts_worker, this); m_tts_thread = std::thread(&AlarmService::tts_worker, this);
spdlog::info("AlarmService created and TTS worker thread started."); spdlog::info("AlarmService created and TTS worker thread started.");
@ -278,7 +278,7 @@ void AlarmService::trigger_alarm_action(AlarmRule& rule, double value,
m_mqtt_client.publish(topic, AlarmEvent::toJson(event).dump(), 1, false); m_mqtt_client.publish(topic, AlarmEvent::toJson(event).dump(), 1, false);
// 3. 写入数据库 // 3. 写入数据库
if (!m_data_storage.storeAlarmEvent(event)) { if (!DataStorage::getInstance().storeAlarmEvent(event)) {
spdlog::error("Failed to store ACTIVE alarm event for rule: {}", spdlog::error("Failed to store ACTIVE alarm event for rule: {}",
rule.rule_id); rule.rule_id);
} }
@ -316,7 +316,7 @@ void AlarmService::clear_alarm(AlarmRule& rule, double value,
m_mqtt_client.publish(topic, AlarmEvent::toJson(event).dump(), 1, false); m_mqtt_client.publish(topic, AlarmEvent::toJson(event).dump(), 1, false);
// 3. 写入数据库 // 3. 写入数据库
if (!m_data_storage.storeAlarmEvent(event)) { if (!DataStorage::getInstance().storeAlarmEvent(event)) {
spdlog::error("Failed to store CLEARED alarm event for rule: {}", spdlog::error("Failed to store CLEARED alarm event for rule: {}",
rule.rule_id); rule.rule_id);
} }
@ -362,7 +362,7 @@ void AlarmService::schedule_tts(const std::string& text) {
} }
nlohmann::json AlarmService::getActiveAlarmsJson() { nlohmann::json AlarmService::getActiveAlarmsJson() {
auto alarms = m_data_storage.getActiveAlarms(); auto alarms = DataStorage::getInstance().getActiveAlarms();
json j_alarms = json::array(); json j_alarms = json::array();
for (const auto& alarm : alarms) { for (const auto& alarm : alarms) {
j_alarms.push_back(AlarmEvent::toJson(alarm)); j_alarms.push_back(AlarmEvent::toJson(alarm));
@ -371,7 +371,7 @@ nlohmann::json AlarmService::getActiveAlarmsJson() {
} }
nlohmann::json AlarmService::getAlarmHistoryJson(int limit) { nlohmann::json AlarmService::getAlarmHistoryJson(int limit) {
auto alarms = m_data_storage.getAlarmHistory(limit); auto alarms = DataStorage::getInstance().getAlarmHistory(limit);
json j_alarms = json::array(); json j_alarms = json::array();
for (const auto& alarm : alarms) { for (const auto& alarm : alarms) {
j_alarms.push_back(AlarmEvent::toJson(alarm)); j_alarms.push_back(AlarmEvent::toJson(alarm));

View File

@ -13,17 +13,16 @@
#include "alarm_defs.h" #include "alarm_defs.h"
#include "alarm_event.h" #include "alarm_event.h"
#include "dataStorage/data_storage.h"
#include "mqtt/mqtt_client.h" #include "mqtt/mqtt_client.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "tts/piper_tts_interface.h" // 你的 TTS 接口 #include "tts/piper_tts_interface.h"
using json = nlohmann::json; using json = nlohmann::json;
class AlarmService { class AlarmService {
public: public:
AlarmService(boost::asio::io_context& io, PiperTTSInterface& tts_service, AlarmService(boost::asio::io_context& io, PiperTTSInterface& tts_service,
MqttClient& mqtt_client, DataStorage& data_storage); MqttClient& mqtt_client);
~AlarmService(); ~AlarmService();
@ -59,13 +58,12 @@ class AlarmService {
boost::asio::io_context& m_io_context; boost::asio::io_context& m_io_context;
PiperTTSInterface& m_tts_service; PiperTTSInterface& m_tts_service;
MqttClient& m_mqtt_client; MqttClient& m_mqtt_client;
DataStorage& m_data_storage;
std::vector<AlarmRule> m_rules; std::vector<AlarmRule> m_rules;
std::map<std::string, AlarmState> m_alarm_states; std::map<std::string, AlarmState> m_alarm_states;
std::string m_rules_config_path; // <--- 新增:存储配置文件路径 std::string m_rules_config_path;
std::mutex m_rules_mutex; // <--- 新增:保护 m_rules 和 m_alarm_states std::mutex m_rules_mutex;
// TTS 播报队列 // TTS 播报队列
std::mutex m_tts_queue_mutex; std::mutex m_tts_queue_mutex;

View File

@ -116,8 +116,7 @@ int main(int argc, char* argv[]) {
PiperTTSInterface tts_service(config.getPiperExecutablePath(), PiperTTSInterface tts_service(config.getPiperExecutablePath(),
config.getPiperModelPath()); config.getPiperModelPath());
AlarmService alarm_service(g_io_context, tts_service, mqtt_client, AlarmService alarm_service(g_io_context, tts_service, mqtt_client);
data_storage);
if (!alarm_service.load_rules(config.getAlarmRulesPath())) { if (!alarm_service.load_rules(config.getAlarmRulesPath())) {
spdlog::error("Failed to load alarm rules. Alarms may be disabled."); spdlog::error("Failed to load alarm rules. Alarms may be disabled.");