From 9280d221d473730e81738628d1b247131f500a64 Mon Sep 17 00:00:00 2001 From: admin <zzjincn@163.com> Date: 星期日, 27 四月 2025 17:56:44 +0800 Subject: [PATCH] 读写redis处理,未完工 --- PreProcessFn.cpp | 79 ++++++++++++++++++++++++++++++++++++++- 1 files changed, 77 insertions(+), 2 deletions(-) diff --git a/PreProcessFn.cpp b/PreProcessFn.cpp index b089513..70a2ea9 100644 --- a/PreProcessFn.cpp +++ b/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> +/// 鍐欏叆鏁版嵁鑷硆edis妯″潡 +/// </summary> +void fnWriteToRedis(redisContext*& context, string ipccode) +{ + redisReply* reply; + + //妫�娴媟edis鏄惁杩炴帴锛屾姤璀﹀啓鍏edis + 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; @@ -510,6 +571,18 @@ mysql_set_character_set(mysql, "utf8mb4"); //mysql_query(mysql, "SET NAMES GB2312");//瑙e喅涓枃涔辩爜闂 } + + + //鍒濆鍖杛edis閾炬帴 + redisContext* context; + if (!connectToRedis(redispath.c_str(), stoi(redisport), redispass.c_str(), context)) + { + cout << "redis杩炴帴鍒濆鍖栧け璐ワ紒" << endl; + } + else + { + cout << "redis杩炴帴鍒濆鍖栨垚鍔燂紒" << endl; + } std::string limitDM = "1";//鍫嗙叅闄愬畾闃堝�硷紝瓒呰繃闃堝�艰繘琛屾姤璀︼紝閰嶇疆鏂囦欢璧嬪�硷紝姣忎釜鎽勫儚澶撮兘涓嶄竴鏍� @@ -1718,7 +1791,9 @@ currentFrame = 0; } - currentFrame++; + currentFrame++; + + fnWriteToRedis(context,ipccode); } catch (const std::exception& ex) { -- Gitblit v1.8.0