Add heartbeat endpoint message schema

This commit is contained in:
gqc
2026-07-08 18:35:44 +08:00
parent ebc7aaa11c
commit 041c830bfb
5 changed files with 234 additions and 7 deletions
+80
View File
@@ -24,12 +24,14 @@
#include "vertix.h"
#include "snapshot_notify.h"
#include "myrga.h"
#include "utils.h"
#ifdef mkdir
#undef mkdir
#endif
void *upload_info(void *arg);
static void detect_log_line(const char *fmt, ...);
typedef struct _upload_target_info_t
{
@@ -59,7 +61,9 @@ int TypeID = 0;
int UploadPort = 0;
static char is_uploading = 0;
static char is_heartbeat_uploading = 0;
static time_t last_upload = 0;
static time_t last_heartbeat_post = 0;
static time_t last_heartbeat_log = 0;
static int last_object_count[OBJ_CLASS_MAX] = {0};
static pthread_mutex_t upload_state_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -107,6 +111,80 @@ static void release_upload()
pthread_mutex_unlock(&upload_state_mutex);
}
static int try_reserve_heartbeat_upload()
{
int reserved = 0;
pthread_mutex_lock(&upload_state_mutex);
if (is_heartbeat_uploading == 0)
{
is_heartbeat_uploading = 1;
reserved = 1;
}
pthread_mutex_unlock(&upload_state_mutex);
return reserved;
}
static void release_heartbeat_upload()
{
pthread_mutex_lock(&upload_state_mutex);
is_heartbeat_uploading = 0;
pthread_mutex_unlock(&upload_state_mutex);
}
static void *upload_heartbeat_info(void *arg)
{
(void)arg;
cJSON *heartbeat = cJSON_CreateObject();
cJSON_AddStringToObject(heartbeat, "message_type", "heartbeat");
cJSON_AddNumberToObject(heartbeat, "schema_version", 1);
cJSON_AddStringToObject(heartbeat, "serial", CameraID);
cJSON_AddStringToObject(heartbeat, "process_uuid", CameraID);
cJSON_AddStringToObject(heartbeat, "status", "running");
cJSON_AddNumberToObject(heartbeat, "timestamp", current_time_stamp());
int ret = post_json("127.0.0.1", UploadPort, "localhost", "/heartbeat", heartbeat);
detect_log_line("heartbeat_post camera=%s port=%d result=%d",
CameraID ? CameraID : "", UploadPort, ret);
cJSON_Delete(heartbeat);
release_heartbeat_upload();
return NULL;
}
void send_inference_heartbeat_if_needed()
{
if (UploadPort <= 0 || CameraID == NULL || CameraID[0] == '\0')
{
return;
}
time_t now_timestamp;
time(&now_timestamp);
if (now_timestamp - last_heartbeat_post < HEARTBEAT_SECS)
{
return;
}
if (!try_reserve_heartbeat_upload())
{
return;
}
last_heartbeat_post = now_timestamp;
pthread_t uploadth;
if (pthread_create(&uploadth, NULL, upload_heartbeat_info, NULL) == 0)
{
pthread_detach(uploadth);
}
else
{
detect_log_line("heartbeat_thread_create_failed camera=%s port=%d",
CameraID ? CameraID : "", UploadPort);
release_heartbeat_upload();
}
}
static int is_edge_touch(int left, int top, int right, int bottom)
{
return left <= 2 || top <= 2 || right >= DSTWidth - 3 || bottom >= DSTHeight - 3;
@@ -823,6 +901,8 @@ void *upload_info(void *arg)
return NULL;
}
cJSON *uploadinfo = cJSON_CreateObject();
cJSON_AddStringToObject(uploadinfo, "message_type", "inference_result");
cJSON_AddNumberToObject(uploadinfo, "schema_version", 1);
cJSON_AddStringToObject(uploadinfo, "serial", CameraID);
// 内部事件编码,因为没法表示多个,所以,即将废弃
cJSON_AddNumberToObject(uploadinfo, "type", TypeID);