admin
2025-04-27 9280d221d473730e81738628d1b247131f500a64
读写redis处理,未完工
1个文件已删除
5个文件已修改
177 ■■■■ 已修改文件
.gitignore 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
CMakeLists.txt 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
PreProcessFn.cpp 77 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
build/compile_commands.json 82 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
build/config.json 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
main.cpp 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
.gitignore
@@ -85,3 +85,7 @@
build/CMakeFiles/AIRecognize.dir/depend.make
build/CMakeFiles/AIRecognize.dir/progress.make
build/CMakeFiles/AIRecognize.dir/compiler_depend.internal
.vscode/settings.json
build/CMakeFiles/Progress/1
build/CMakeFiles/Progress/count.txt
.vscode/settings.json
CMakeLists.txt
@@ -45,6 +45,15 @@
find_package(PkgConfig REQUIRED)
pkg_check_modules(RABBITMQ REQUIRED librabbitmq)
# 手动查找 hiredis 头文件和库文件
find_path(HIREDIS_INCLUDE_DIR hiredis.h PATH_SUFFIXES hiredis)
find_library(HIREDIS_LIBRARY NAMES hiredis)
# 检查是否找到 hiredis
if (HIREDIS_INCLUDE_DIR AND HIREDIS_LIBRARY)
message(STATUS "Found HIREDIS headers: ${HIREDIS_INCLUDE_DIR}")
endif()
# 查找 jsoncpp 库
find_package(jsoncpp REQUIRED)
@@ -88,6 +97,7 @@
    ${RABBITMQ_INCLUDE_DIRS}
    ${jsoncpp_INCLUDE_DIRS}
    ${CURL_INCLUDE_DIRS}
    ${HIREDIS_INCLUDE_DIR}
)
target_link_libraries(
@@ -102,4 +112,5 @@
    ${RABBITMQ_LIBRARIES}
    ${jsoncpp_LIBRARIES}
    ${CURL_LIBRARIES}
    ${HIREDIS_LIBRARY}
    )
PreProcessFn.cpp
@@ -9,7 +9,7 @@
#include <mysql/mysql.h> 
#include <future>
#include "iostream"
#include <hiredis.h>
#include <string>
#include <locale>
#include <codecvt>
@@ -340,6 +340,30 @@
    return result;
}
/// <summary>
/// 写入数据至redis模块
/// </summary>
void fnWriteToRedis(redisContext*& context, string ipccode)
{
    redisReply* reply;
    //检测redis是否连接,报警写入redis
    if (!context == NULL && !context->err) {
        // 记录报警的时候,同时写入redis数据库
        std::string key = "camera::run::" + ipccode;
        std::string value = "1";
        //写入redis
        reply = (redisReply*)redisCommand(context, "SET %s %s", key.c_str(), value.c_str());
        if (reply == NULL) {
            printf("信息写入redis失败\n");
            redisFree(context);
        }
        freeReplyObject(reply);
    }
}
/// @brief 视频流拉取处理
/// @param _rtspUrl 视频流源地址
/// @param queJC 未处理的视频流帧队列
@@ -463,6 +487,43 @@
    rtspStream.release();    
}
/// <summary>
/// 连接并校验 Redis 服务器密码
/// </summary>
/// <param name="server"></param>
/// <param name="port"></param>
/// <param name="password"></param>
/// <param name="context"></param>
/// <returns></returns>
bool connectToRedis(const char* server, int port, const char* password, redisContext*& context) {
    // 连接 Redis 服务器
    context = redisConnect(server, port);
    if (context == nullptr || context->err) {
        std::cerr << "连接redis服务端失败!" << std::endl;
        return false;
    }
    //// 校验密码
    //redisReply* reply = (redisReply*)redisCommand(context, "AUTH %s", password);
    //if (reply == NULL) {
    //    std::cerr << "连接校验失败!" << std::endl;
    //    redisFree(context);
    //    return false;
    //}
    //else {
    //    // 检查回复以确定是否鉴权成功
    //    if (reply->type == REDIS_REPLY_ERROR && strcmp(reply->str, "OK") != 0) {
    //        std::cerr << "密码错误: " << reply->str << std::endl;
    //        freeReplyObject(reply);
    //        redisFree(context);
    //        return false;
    //    }
    //    freeReplyObject(reply);
    //}
    return true;
}
std::string jsontostr(Json::Value& json)
{
    string return_str;
@@ -509,6 +570,18 @@
        std::cout << "连接 MySQL 数据库成功。" << std::endl;
        mysql_set_character_set(mysql, "utf8mb4");
        //mysql_query(mysql, "SET NAMES GB2312");//解决中文乱码问题       
    }
    //初始化redis链接
    redisContext* context;
    if (!connectToRedis(redispath.c_str(), stoi(redisport), redispass.c_str(), context))
    {
        cout << "redis连接初始化失败!" << endl;
    }
    else
    {
        cout << "redis连接初始化成功!" << endl;
    }    
    std::string limitDM = "1";//堆煤限定阈值,超过阈值进行报警,配置文件赋值,每个摄像头都不一样
@@ -1719,6 +1792,8 @@
            }
            currentFrame++;            
            fnWriteToRedis(context,ipccode);
        }
        catch (const std::exception& ex)
        {            
build/compile_commands.json
File was deleted
build/config.json
@@ -1,6 +1,6 @@
{
    "redis":{
        "ip": "127.0.0.1",
        "ip": "192.168.1.188",
        "port": "6379",
        "pwd": ""
    },
main.cpp
@@ -9,6 +9,7 @@
#include <future>
#include <json/json.h>
#include "affine.h"
#include <hiredis.h>
#include "PreProcessFn.h"
using namespace std;