diff --git a/src/yuanbenmain.cpp b/src/yuanbenmain.cpp deleted file mode 100644 index 391b868..0000000 --- a/src/yuanbenmain.cpp +++ /dev/null @@ -1,1175 +0,0 @@ -#include "main.h" -//4 13 22 31 -#include -#include -#include -#include"yolov5_detect.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -#define MSS_TEST_BUS_NAME "yolov5test" -static jbus_hdl_t g_jbus; -static jmss_hdl_t g_jmss; -static jisp_hdl_t g_jisp; -static bool g_running; -jmss_stm_t *g_stm[5]; - - -#define modelpath_fire "/demo/bin/fire2025_pre.rknn" -#define DOWNLOAD_VERSION_PATH "/tmp/version" -#define ID_FILE_PWD "/etc/DeviceID" -std::string DeviceID; -std::string response_controllerId = "NULL"; - -static char labels_fire[2][20] = {"fire", "any"}; -static char labels_smog[2][20] = {"smog", "any"}; - -#define SERIAL_PORT_INFRARED_SENSOR "/dev/ttyS4" -#define SERIAL_PORT_SOLENOID "/dev/ttyS5" -#define BAUD_RATE B115200 -static int serialPortInfraredSensor; -static int serialPortSolenoid; -float temperature_img[24][32]; -unsigned char buffer[1544]; -std::mutex mtx; - -using namespace cv; -using namespace std; -using json = nlohmann::json; - -int ALARM_TEMPERATURE; -int WARN_TEMPERATURE; -int MOVE_THRESHOLD; -int IGNORE_TEMPERATURE; -double Confidence_Threshold; - -vector> last_result; -vector> now_result; - -int width = 1920, height = 1080; -JMediaRawFrameType_e type = JMEDIA_RAWFRAMETYPE_NV12; - - -bool check_whether_in_last_result(vector> &last_result, vector &now, std::ofstream& temperature_log){ - if(last_result.empty()) return true; - //查找now的框坐标是否有出现在上一帧 - for(auto i : last_result){ - //范围5像素以内都视为不变 - if(abs(i[0] - now[0]) <= MOVE_THRESHOLD && abs(i[1] - now[1]) <= MOVE_THRESHOLD && abs(i[2] - now[2]) <= MOVE_THRESHOLD && abs(i[3] - now[3]) <= MOVE_THRESHOLD){ - temperature_log << "有重复 此帧结果:(" << now[0] << " " << now[1] << ") (" << now[2] << " " << now[3] << ")" - << " 上帧结果:(" << i[0] << " " << i[1] << ") (" << i[2] << " " << i[3] << ")"<< endl; - cout << "上帧结果:(" << i[0] << " " << i[1] << ") (" << i[2] << " " << i[3] << ")" << endl; - cout << "此帧结果:(" << now[0] << " " << now[1] << ") (" << now[2] << " " << now[3] << ")" << endl; - return true; - } - } - cout << "无重复 此帧结果:(" << now[0] << " " << now[1] << ") (" << now[2] << " " << now[3] << ")" << endl; - temperature_log << "无重复 此帧结果:(" << now[0] << " " << now[1] << ") (" << now[2] << " " << now[3] << ")" << endl; - return false; -} - - -string Get_Time(int input){ - // 获取当前系统时间 - std::time_t alarmTime_std = std::time(nullptr); - // 将时间转换为本地时间 - std::tm* localTime = std::localtime(&alarmTime_std); - // 从本地时间结构中获取时间信息 - int year = localTime->tm_year + 1900; - int month = localTime->tm_mon + 1; - int day = localTime->tm_mday; - int hour = localTime->tm_hour; - int minute = localTime->tm_min; - int second = localTime->tm_sec; - // 拼接成字符串 - std::ostringstream oss_alarmTime; - if(input == 1){ - oss_alarmTime << std::setfill('0') - << std::setw(4) << year << "-" - << std::setw(2) << month << "-" - << std::setw(2) << day << " " - << std::setw(2) << hour << ":" - << std::setw(2) << minute << ":" - << std::setw(2) << second; - }else{ - oss_alarmTime << std::setfill('0') - << std::setw(2) << month << "_" - << std::setw(2) << day << "_" - << std::setw(2) << hour << "_" - << std::setw(2) << minute << "_" - << std::setw(2) << second; - } - // 获取拼接后的字符串 - std::string formattedTime = oss_alarmTime.str(); - return formattedTime; -} - - - -double calibration(double x){ - double p1 = -9.052e-08; - double p2 = 8.313e-05; - double p3 = -0.02813; - double p4 = 4.16; - double p5 = -219.7; - double res = p1*pow(x, 4) + p2*pow(x, 3) + p3*pow(x, 2) + p4*x + p5; - return res; -} - - -size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* response){ - size_t totalSize = size * nmemb; - response->append(static_cast(contents), totalSize); - return totalSize; -} - - -bool _geturlFromfile(const char * filepath,char *url, int maxLength) { - if (filepath == NULL) { - printf("getrulVersion is error, url == null.\n"); - return false; - } - FILE *fp = fopen(filepath, "r"); - if (fp == NULL) { - printf("open %s failed, error is %s.\n", filepath, strerror(errno)); - return false; - } - int iffind=0; - char *line = NULL; - size_t len = 0; - size_t read; - while ((read = getline(&line, &len, fp)) != -1) { - if (read == 0 || line[0] == '#') { - continue; - } - char *pline = strstr(line, "upload_url"); - if (pline != NULL && (pline = strstr(pline, "=")) != NULL) { - pline++; //过滤掉等于号 - //过滤掉空格 - while (*pline == ' ') { - pline++; - } - int pline_len = strlen(pline) - 1; - int version_len = (pline_len > maxLength ? maxLength:pline_len); - memcpy(url, pline, version_len); - printf("upload_url = %s\n", url); - iffind=1; - break; - } - } - if (iffind == 0 ){ - printf("Can not find upload_url\n"); - return false; - } - free(line); - fclose(fp); - return true; -} - - -// 函数用于读取文件并将其内容保存在全局字符串变量中 -void readFileAndStoreInGlobal(const std::string& filename) { - std::ifstream file(filename); - if (file.is_open()) { - std::getline(file, DeviceID); // 读取一行并存储在全局字符串中 - file.close(); - } else { - std::cerr << "无法打开文件: " << filename << std::endl; - } -} - - -std::string generateIndices(char result[4],Alarm* Alarm) { - std::string indices; - for (int i = 0; i < 4; ++i) { - if (result[i] == '1') { - // 将索引值加入到生成的字符串中 - indices += std::to_string(i + 1); - Alarm->ifalarm = 1; - } - } - return indices; -} - - -void checkCoverage(int x1, int x2, int width, char result[4]) { - // 初始化为 '1',表示覆盖 - // std::string resultString1(result, 4); - // std::cout << "result in: " << resultString1 << std::endl; - char result_now[4]={'1','1','1','1'}; - int line1 = width*7/32; - int line2 = width/2; - int line3 = width*25/32; - if(x1>line1){ - result_now[0] = '0'; - } - if(x1>line2){ - result_now[0] = '0'; - result_now[1] = '0'; - } - if(x1>line3){ - result_now[0] = '0'; - result_now[1] = '0'; - result_now[2] = '0'; - } - - if(x2& data) { - uint16_t sum = 0; - for (size_t i = 0; i < data.size()-1; ++i) { - sum += data[i]; - } - return static_cast(sum & 0xFF); -} - - -void printVector(const std::vector& vec) { - std::cout << "Vector contents: "; - for (const auto& byte : vec) { - std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast(byte) << " "; - } - std::cout << std::dec << std::endl; // 切换回十进制 -} - - -int main(int argc, char **argv) -{ - std::string folderPath = "/demo/pic"; - if(!createDirectory(folderPath)){ - return 0; - } - - // 打开 JSON 文件 - cout << "解析JSON文件" << endl; - std::ifstream file("/demo/bin/config.json"); - - // 检查文件是否成功打开 - if (!file.is_open()) { - std::cerr << "Failed to open json file" << std::endl; - return 1; - } - - try { - // 解析 JSON 文件 - json Config_jsonData; - file >> Config_jsonData; - // 关闭文件 - - // 赋值给全局常量 - WARN_TEMPERATURE = Config_jsonData["WARN_TEMPERATURE"]; - ALARM_TEMPERATURE = Config_jsonData["ALARM_TEMPERATURE"]; - MOVE_THRESHOLD = Config_jsonData["MOVE_THRESHOLD"]; - IGNORE_TEMPERATURE = Config_jsonData["IGNORE_TEMPERATURE"]; - Confidence_Threshold = Config_jsonData["Confidence_Threshold"]; - } - catch (const json::parse_error& e) - { - std::cerr << "JSON parsing error: " << e.what() << std::endl; - return 0; - } - file.close(); - - cout << "配置 WARN_TEMPERATURE :" << WARN_TEMPERATURE << endl; - cout << "配置 ALARM_TEMPERATURE :" << ALARM_TEMPERATURE << endl; - cout << "配置 MOVE_THRESHOLD :" << MOVE_THRESHOLD << endl; - cout << "配置 IGNORE_TEMPERATURE :" << IGNORE_TEMPERATURE << endl; - cout << "配置 Confidence_Threshold :" << Confidence_Threshold << endl; - cout << SERIAL_PORT_INFRARED_SENSOR <=2?(JMediaRawFrameType_e)atoi(argv[1]):JMEDIA_RAWFRAMETYPE_NV12; - int width = 1920, height = 1080; - jmss_raw_t *rawchn = JES_MSS_RawOpen(g_jmss, 0, width, height, type); - if(rawchn == NULL){ - printf("==============>>>: %s:%d open rawchn failed\n", strrchr(__FILE__,'/'),__LINE__); - return -1; - } - int ret; - - - readFileAndStoreInGlobal((char *)ID_FILE_PWD); - // 打印全局字符串变量 - std::cout << "ID内容:" << DeviceID < ControlInstructions(20,0x00); - ControlInstructions[0] = 0xCA; //协议头 - ControlInstructions[1] = 0x14; //总长度 - ControlInstructions[2] = 0x01; //操作指令 - - //打开JES的通道 - jmss_raw_t *rawchn = JES_MSS_RawOpen(g_jmss, 0, width, height, type); - if(rawchn == NULL){ - printf("==============>>>: %s:%d open rawchn failed\n", strrchr(__FILE__,'/'),__LINE__); - } - //日志相关内容 - std::string filename = "/demo/bin/temperature_log.txt"; - std::ofstream temperature_log(filename, std::ios::out | std::ios::trunc); - temperature_log << "配置 WARN_TEMPERATURE :" << WARN_TEMPERATURE << endl; - temperature_log << "配置 ALARM_TEMPERATURE :" << ALARM_TEMPERATURE << endl; - temperature_log << "配置 MOVE_THRESHOLD :" << MOVE_THRESHOLD << endl; - temperature_log << "配置 IGNORE_TEMPERATURE :" << IGNORE_TEMPERATURE << endl; - temperature_log << "配置 Confidence_Threshold :" << Confidence_Threshold << endl; - //环境温度 - double temp_env = 0; - - auto last_time = std::chrono::high_resolution_clock::now();// 记录开始时间 - // 创建一个 Alarm 结构体对象 - Alarm Alarm; - // 调用初始化函数来初始化结构体成员 - initializeAlarm(&Alarm); - //用于从全局buffer中复制数据 - unsigned char copyBuffer[1544]; - - /* 参数初始化 */ - int output_nboxes_left =0; - yolov5_detect_result_group_t detect_result_group; - - /* 算法模型初始化 */ - char *path = reinterpret_cast(args); - printf("loading model %s\n",path); - rknn_context ctx; - yolov5_detect_init(&ctx, path); - // yolov5_detect_init(&ctx, "./fire_rv1126.rknn"); - - //配置label - char labels[2][20]; - getLabels(path, labels); - // 设置压缩质量 - std::vector compression_params; - compression_params.push_back(cv::IMWRITE_JPEG_QUALITY); - compression_params.push_back(30); // 设置压缩质量,范围为 0-100 - int overtem_cnt = 0; - std::vector over_tmp_deque(10, 0); - /* 算法运行 */ - while(!quit){ - int time2run = 1; - // auto current_time = std::chrono::high_resolution_clock::now(); - // auto elapsed_time = std::chrono::duration_cast(current_time - last_time);// 计算距离开始时间的时间差 - - // if (elapsed_time.count() - 1000 > 0) { - // last_time = current_time; - // time2run = 1; - // } - sleep(2); - //获取摄像头buffer - JVMediaFrame_t frm; - if(JES_MSS_RawGetFrame(rawchn,&frm) < 0){ - sleep(1); - continue; - } - int overtmp_falg = 0; - Alarm.ifalarm = 0; - Alarm.ifwarn = 0; - - Mat src; - NV12ToRGB(width, height, frm.buffer, src); - //裁剪 - cv::Rect roi(220, 130, 1480, 570); - src = src(roi); - //固定时间间隔去处理红外参数 - char result[4]={'0','0','0','0'}; - char result_warn[4]={'0','0','0','0'}; - char result_fire_rknn[4]={'0','0','0','0'}; - //初始化ControlInstructions - ControlInstructions[3]=0x00; - ControlInstructions[4]=0x00; - ControlInstructions[5]=0x00; - ControlInstructions[6]=0x00; - - if(time2run){ - cout << "run ." << endl; - mtx.lock(); - memcpy(copyBuffer, buffer, sizeof(buffer)); - mtx.unlock(); - //计算温度 - double temperature; - int Col,Row; - int j = 0; - double max_temperature = 0; - for (int i = 4; i < 1540; i += 2) { - // temperature = calibration((buffer[i+1]*256+buffer[i])/100.0); - temperature = (buffer[i+1]*256+buffer[i])/100.0 + 6; - // cout << (buffer[i+1]*256+buffer[i])/100.0 << " " << temperature << endl; - - //按视觉顺序存入数组 - Col = 31-(j)%32; - Row = (j)/32; - temperature_img[Row][Col] = temperature; - j++; - } - temp_env = (buffer[1541]*256+buffer[1540])/100.0; - WARN_TEMPERATURE = (temp_env*2)>WARN_TEMPERATURE? (temp_env*2):WARN_TEMPERATURE; - // ALARM_TEMPERATURE = WARN_TEMPERATURE + 20; - //截去上下5行数据 - for(int i=5; i<19; i++){ - for(j=0; j<32; j++){ - if(temperature_img[i][j] > IGNORE_TEMPERATURE) continue; - if(temperature_img[i][j] > max_temperature) max_temperature = temperature_img[i][j]; - - if(temperature_img[i][j] > ALARM_TEMPERATURE){ - overtmp_falg = 1; - if (0 <= j && j <= 6) - result[0] = '1'; - else if (6 < j && j <= 15) - result[1] = '1'; - else if (15 < j && j <= 24) - result[2] = '1'; - else if (24 < j && j <= 31) - result[3] = '1'; - } - - if(temperature_img[i][j] > WARN_TEMPERATURE){ - Alarm.ifwarn = 1; - if (0 < j && j <= 6) - result_warn[0] = '1'; - else if (6 < j && j <= 15) - result_warn[1] = '1'; - else if (13 < j && j <= 24) - result_warn[2] = '1'; - else if (24 < j && j <= 31) - result_warn[3] = '1'; - } - } - } - - int overtem_cnt_for_log; - if(overtmp_falg){ - over_tmp_deque.push_back(1); - if (over_tmp_deque.size() > 10) { - over_tmp_deque.erase(over_tmp_deque.begin()); - } - overtem_cnt = 0; - for (int i = 0; i < over_tmp_deque.size(); ++i) { - if (over_tmp_deque[i] == 1) { - overtem_cnt++; - } - } - overtem_cnt_for_log = overtem_cnt; - - if(overtem_cnt >= 3){ - Alarm.ifalarm = 1; - over_tmp_deque.assign(9, 0); - over_tmp_deque.push_back(1); - }else{ - memset(result, '0', 4*sizeof(char)); - } - }else{ - over_tmp_deque.push_back(0); - // 保持队列长度为10,如果超过长度则移除最前面的元素 - if (over_tmp_deque.size() > 10) { - over_tmp_deque.erase(over_tmp_deque.begin()); - } - } - - - // temp_env = (buffer[1541]*256+buffer[1540])/100.0; - cout << "最大温度: " << max_temperature << endl; - cout << "环境温度" << temp_env << endl; - cout << "预警温度: " << WARN_TEMPERATURE << endl; - cout << "直接喷水温度: " << ALARM_TEMPERATURE << endl; - if(max_temperature>WARN_TEMPERATURE){ - temperature_log << endl << "---------------------------------------------------" << endl; - temperature_log << Get_Time(1) << endl; - temperature_log << "最大温度: " << max_temperature << endl; - temperature_log << "环境温度: " << temp_env << endl; - temperature_log << "预警温度: " << WARN_TEMPERATURE << endl; - temperature_log << "直接喷水温度: " << ALARM_TEMPERATURE << endl; - temperature_log << "超温队列:"; - for (int i = 0; i < over_tmp_deque.size(); ++i) { - temperature_log << over_tmp_deque[i] << " "; - } - temperature_log << endl; - } - if(Alarm.ifalarm){ - printf("temprature > %d°C !\n", ALARM_TEMPERATURE); - // 将字符数组拼接成字符串 - std::string resultString(result, 4); - std::cout << ALARM_TEMPERATURE <<"度结果: " << resultString << std::endl; - temperature_log << ALARM_TEMPERATURE <<"度结果: " << resultString << std::endl; - std::string resultString_warn(result_warn, 4); - std::cout << WARN_TEMPERATURE << "度结果: " << resultString_warn << std::endl; - temperature_log << WARN_TEMPERATURE <<"度结果: " << resultString_warn << std::endl; - }else if(Alarm.ifwarn){ - printf("temprature > %d°C !\n", WARN_TEMPERATURE); - std::string resultString_warn(result_warn, 4); - std::cout << WARN_TEMPERATURE << "结果: " << resultString_warn << std::endl; - temperature_log << WARN_TEMPERATURE << "结果: " << resultString_warn << std::endl; - } - } - - if(Alarm.ifwarn){ - struct timeval start; - struct timeval end; - now_result.clear(); - temperature_log << "上帧总的结果:" << endl; - for(auto i : last_result){ - temperature_log << "(" << i[0] << " " << i[1] << ") (" << i[2] << " " << i[3] << ")" << endl; - } - printf("%s_rknn_run\n",labels[0]); - yolov5_detect_run(ctx, src, &detect_result_group); - } - - /* 算法结果在图像中画出并保存 */ - int name_int = 0; - // char result[4]={'0','0','0','0'}; - for (int i = 0; i < detect_result_group.count; i++) - { - yolov5_detect_result_t *det_result = &(detect_result_group.results[i]); - if( det_result->prop < Confidence_Threshold ) - { - continue; - } - int name_int = std::stoi(det_result->name); - if(Alarm.ifwarn){ - printf("%s @ (%d %d %d %d) %f\n", - labels[name_int], - det_result->box.left, det_result->box.top, det_result->box.right, det_result->box.bottom, - det_result->prop); - vector now; - now.push_back(det_result->box.left); - now.push_back(det_result->box.top); - now.push_back(det_result->box.right); - now.push_back(det_result->box.bottom); - now_result.push_back(now); - if(check_whether_in_last_result(last_result,now,temperature_log)){ - continue; - } - } - int x1 = det_result->box.left; - int y1 = det_result->box.top; - int x2 = det_result->box.right; - int y2 = det_result->box.bottom; - - //检测区域 - if(Alarm.ifwarn){ - checkCoverage(x1, x2, 1480, result_fire_rknn); - } - - char label_text[50]; - memset(label_text, 0 , sizeof(label_text)); - sprintf(label_text, "%s %0.2f",labels[name_int], det_result->prop); - plot_one_box(src, x1, x2, y1, y2, label_text, i%10); - } - - if(Alarm.ifwarn){ - // if(!now_result.empty()){ - // last_result.clear(); - // last_result = now_result; - // } - - //若上帧结果为空,依然清理结果队列 --2024.11.4 - last_result.clear(); - last_result = now_result; - - std::string resultString_fire(result_fire_rknn, 4); - std::cout << "rknn检测结果: " << resultString_fire << std::endl; - temperature_log << "rknn检测结果: " << resultString_fire << std::endl; - //对45度区域和火焰检测区域做出 与操作 - and_result(result_warn,result_fire_rknn); - //对火与45度结果 与 60 度结果 做或操作 - or_result(result,result_warn); - temperature_log << "报警输入:" << string(result, 4) <(orig_data.c_str()), orig_data.length()); - Alarm.alarmBase64 = encoded_data; - Alarm.alarmMsg = std::string(labels[name_int]); - pthread_t upload_message_tidp; - pthread_create(&upload_message_tidp, NULL, upload_message, static_cast(&Alarm)); - pthread_t upload_message_controller_tidp; - int ret =pthread_create(&upload_message_controller_tidp, NULL, upload_message_controller, static_cast(&Alarm)); - if (ret != 0) { - std::cerr << "Error creating controller thread: " << strerror(ret) << std::endl; - } else { - std::cerr << "success creating controller thread" << std::endl; - } - } - } - - /* 算法模型空间释放 */ - yolov5_detect_release(ctx); - //文件关闭 - temperature_log.close(); - return 0; - -} - -void *upload_message(void *args) -{ - pthread_detach(pthread_self()); - // 获取上报url - char upload_url[200] = {0}; - if(!_geturlFromfile(DOWNLOAD_VERSION_PATH,upload_url,sizeof(upload_url))){ - printf("结束进程\n"); - return 0; - } - - Alarm* alarm = static_cast(args); - - // 获取当前系统时间 - std::time_t alarmTime_std = std::time(nullptr); - - // 将时间转换为本地时间 - std::tm* localTime = std::localtime(&alarmTime_std); - - // 从本地时间结构中获取时间信息 - int year = localTime->tm_year + 1900; - int month = localTime->tm_mon + 1; - int day = localTime->tm_mday; - int hour = localTime->tm_hour; - int minute = localTime->tm_min; - int second = localTime->tm_sec; - - // 拼接成字符串 - std::ostringstream oss_alarmTime; - oss_alarmTime << std::setfill('0') - << std::setw(4) << year << "-" - << std::setw(2) << month << "-" - << std::setw(2) << day << " " - << std::setw(2) << hour << ":" - << std::setw(2) << minute << ":" - << std::setw(2) << second; - - // 获取拼接后的字符串 - std::string formattedTime = oss_alarmTime.str(); - - std::string cameraId = DeviceID; - std::string controllerId = response_controllerId; - std::string msg = (alarm->alarmMsg).c_str(); - std::string type = "1"; - std::string time = formattedTime; - std::string img = (alarm->alarmBase64).c_str(); - std::string Coverage = (alarm->alarmCoverage).c_str(); - - std::cout << "cameraId: " << cameraId << " msg: " << msg << " type: " << type << " time: " << time <(args); - - // 获取当前系统时间 - std::time_t alarmTime_std = std::time(nullptr); - - // 将时间转换为本地时间 - std::tm* localTime = std::localtime(&alarmTime_std); - - // 从本地时间结构中获取时间信息 - int year = localTime->tm_year + 1900; - int month = localTime->tm_mon + 1; - int day = localTime->tm_mday; - int hour = localTime->tm_hour; - int minute = localTime->tm_min; - int second = localTime->tm_sec; - - // 拼接成字符串 - std::ostringstream oss_alarmTime; - oss_alarmTime << std::setfill('0') - << std::setw(4) << year << "-" - << std::setw(2) << month << "-" - << std::setw(2) << day << " " - << std::setw(2) << hour << ":" - << std::setw(2) << minute << ":" - << std::setw(2) << second; - - // 获取拼接后的字符串 - std::string formattedTime = oss_alarmTime.str(); - - std::string cameraId = DeviceID; - std::string Coverage = (alarm->alarmCoverage).c_str(); - - std::cout << "cameraid: " << cameraId << " errNum: " << Coverage <(current_time - last_time);// 计算距离开始时间的时间差 - - if (elapsed_time.count() - 30000 > 0) { - last_time = current_time; - time2run = 1; - } - - if(time2run){ - std::cout << "心跳上报" << std::endl; - std::string cameraId = DeviceID; - std::string IP = "987654"; - int state = 1; - std::string http_address = "http"; - - std::string MessageString = -R"({ - "cameraId": ")" + cameraId + R"(", - "IP": ")" + IP + R"(", - "state": )" + std::to_string(state) + R"(, - "http_address": ")" + http_address + R"(" -})"; - CURL *curl; - CURLcode res; - curl_global_init(CURL_GLOBAL_ALL); - curl = curl_easy_init(); - - if(curl) - { - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET"); - curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.23:9527/device/heartbeat"); - /* Now specify the POST data */ - struct curl_slist *plist = nullptr; - plist = curl_slist_append(plist, "Content-Type:application/json;charset=UTF-8"); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, plist); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, MessageString.c_str()); - - std::string response; - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); - - res = curl_easy_perform(curl); - /* Check for errors */ - // if(res != CURLE_OK) - // fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res)); - if (res != CURLE_OK) { - std::cerr << "heart beat: Failed to perform cURL request: " << curl_easy_strerror(res) << std::endl; - } else { - std::cout << "heart beat: Request successful!" << std::endl; - std::cout << "heart beat: Response: " << response << std::endl; - // 解析 JSON 字符串 - try { - json jsonData = json::parse(response); - - // 提取 controllerId 字段的内容 - response_controllerId = jsonData["controllerId"]; - - std::cout << "Controller ID: " << response_controllerId << std::endl; - } catch (const json::parse_error& e) { - std::cerr << "JSON parsing error: " << e.what() << std::endl; - } - } - - - curl_easy_cleanup(curl); - } - curl_global_cleanup(); - } - time2run = 0; - } -} - - -void *read_serial_thread(void *args) -{ - pthread_detach(pthread_self()); - ssize_t bytesRead; - serialPortInfraredSensor = open(SERIAL_PORT_INFRARED_SENSOR, O_RDWR | O_NOCTTY | O_NDELAY); - if (serialPortInfraredSensor == -1) { - printf("Failed to open serial port: %s\n", SERIAL_PORT_INFRARED_SENSOR); - return 0; - } - - struct termios tty; - memset(&tty, 0, sizeof(tty)); - if (tcgetattr(serialPortInfraredSensor, &tty) != 0) { - printf("Failed to get serial port attributes\n"); - close(serialPortInfraredSensor); - return 0; - } - cfsetospeed(&tty, BAUD_RATE); - cfsetispeed(&tty, BAUD_RATE); - tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; - tty.c_cflag &= ~(PARENB | PARODD); - tty.c_cflag &= ~CSTOPB; - tty.c_cflag |= CREAD | CLOCAL; - tty.c_iflag = IGNPAR; - tty.c_oflag = 0; - tty.c_lflag = 0; - if (tcsetattr(serialPortInfraredSensor, TCSANOW, &tty) != 0) { - printf("Failed to set serial port attributes\n"); - close(serialPortInfraredSensor); - return 0; - } - - // while(1){ - // mtx.lock(); - // bytesRead = read(serialPortInfraredSensor, buffer, sizeof(buffer)); - // mtx.unlock(); - // if (bytesRead>0) { - // if((buffer[0]== 0x5a)&&(buffer[1]==0x5a)) { - // // printf("readed serialPortInfraredSensor date\n"); - // }else{ - // // printf("read failed\n"); - // } - // }else{ - // // printf("empty to read\n"); - // } - // } - fd_set readfds; - struct timeval timeout; - int selectResult; - - while (1) { - FD_ZERO(&readfds); - FD_SET(serialPortInfraredSensor, &readfds); - - timeout.tv_sec = 5; // 5秒超时 - timeout.tv_usec = 0; - - selectResult = select(serialPortInfraredSensor + 1, &readfds, NULL, NULL, &timeout); - - if (selectResult > 0) { - if (FD_ISSET(serialPortInfraredSensor, &readfds)) { - mtx.lock(); - bytesRead = read(serialPortInfraredSensor, buffer, sizeof(buffer)); - mtx.unlock(); - - if (bytesRead > 0) { - if (buffer[0] == 0x5a && buffer[1] == 0x5a) { - // printf("readed serialPortInfraredSensor date\n"); - } else { - // printf("read failed\n"); - } - } - } - } else if (selectResult == 0) { - // 超时,没有数据可读 - // printf("Timeout, no data available\n"); - } else { - // select出错 - printf("select() failed\n"); - break; - } - } -} \ No newline at end of file