Commit cfddd0e7 authored by 957dd's avatar 957dd

Merge branch 'feature/self_control' into 'master'

实现了设备自控,将释放内存放到了根其他线程函数结束一起 See merge request !76
parents 066bebf9 c9eaeec3
......@@ -20,21 +20,21 @@ char *device_inspect() {
if(read_device_back_fail_middle!=NULL) {//回退文件
return read_device_back_fail_middle;
}
my_zlog_debug("文件 %s 打开失败,等待中...", FILENAME);
my_zlog_info("文件 %s 打开失败,等待中...", FILENAME);
}
else {
// 尝试读取文件内容
if (fgets(s_buffer_device, sizeof(s_buffer_device), file) != NULL) {
// 如果文件内容不为空
fclose(file);
my_zlog_debug("读取到文件内容: %s", s_buffer_device);
my_zlog_info("读取到文件内容: %s", s_buffer_device);
return s_buffer_device;
} else {
if(read_device_back_fail_middle!=NULL) {
fclose(file);
return read_device_back_fail();
}
my_zlog_debug("文件为空,等待中...");
my_zlog_info("文件为空,等待中...");
}
fclose(file);
}
......@@ -57,16 +57,16 @@ char *program_version() {
static char s_buffer_version[30];// 用于存储版本号内容
file = fopen(FILE_VERSION, "r"); // 以只读模式打开文件
if (file == NULL) {
my_zlog_debug("版本文件无");
my_zlog_info("版本文件无");
} else {
// 尝试读取文件内容
if (fgets(s_buffer_version, sizeof(s_buffer_version), file) != NULL) {
// 如果文件内容不为空
fclose(file);
my_zlog_debug("读取到文件内容: %s", s_buffer_version);
my_zlog_info("读取到文件内容: %s", s_buffer_version);
return s_buffer_version;
} else {
my_zlog_debug("文件为空,等待中...");
my_zlog_info("文件为空,等待中...");
}
fclose(file);
}
......
......@@ -54,10 +54,11 @@ int main(){
return -7;
}
thread_end_close();
device_end_close(g_device_type);
my_log_close();//关闭日志
my_zlog_info("程序关闭成功");
thread_end_close();
device_end_close(g_device_type);
self_control_thread_close();//销毁自控的线程
my_log_close();//关闭日志
my_zlog_info("程序关闭成功");
return 0;
}
\ No newline at end of file
......@@ -46,6 +46,8 @@ void thread_end_close() ;
/*相关设备线程等关闭函数*/
void device_end_close(int device_id);
void self_control_thread_close();//销毁自控的线程
/*关闭log日志记录*/
void my_log_close();
......
......@@ -41,6 +41,7 @@ int thread_start_init(ThreadFunc thread_exit_time, ThreadFunc thread_mqtt_beat,
void *thread_exit_time(void *arg) {
while(1){
if(get_self_control_index()==false){
delay_ms(100);
pthread_mutex_lock(&g_exit_count_mutex);
g_devcontrol_exit_count++;
......@@ -52,6 +53,7 @@ void *thread_exit_time(void *arg) {
}else if(get_self_control_index()==true){
delay_ms(20);
set_self_control_time_countfuntion();
}
}
......@@ -82,7 +84,12 @@ void *thread_mqtt_beat(void *arg) {
g_heartbeat_count++;
if(g_mqtt_grc == 0){
mqtt_beat_wirte();//心跳,3s一个
mqtt_beat_wirte();//心跳,3s一个
if(get_device_self_control_configuration_initialization()==0){
my_zlog_warn("自控配置初始化成功");
}
}
}
return NULL;
......@@ -91,12 +98,6 @@ void *thread_mqtt_beat(void *arg) {
//打开游览器线程,让游览器一直在一个线程中打开并运行
void *thread_open_browser(void *arg) {
//如果为相关的不需要心跳或者游览器设备,提早结束线程
if(g_device_type==DEVICE_ROBOT_DOG0501){
my_zlog_info("device am robot and no c/c++");
return NULL;
}
system("pkill chromium");
delay_s(5);
while(1){
......
No preview for this file type
......@@ -24,11 +24,11 @@ static device_automatic_date_t g_automatic_date_t[DEVICE_WALK_SIGN_MAX]={0};
*/
void free_device_tasks_memory() {
if(g_self_device_control_date!=NULL){
pthread_mutex_lock(&g_self_device_control_date_mutex);
free(g_self_device_control_date);
g_self_device_control_date=NULL;
my_zlog_info("free g_self_device_control_date");
pthread_mutex_unlock(&g_self_device_control_date_mutex);
pthread_mutex_lock(&g_self_device_control_date_mutex);
free(g_self_device_control_date);
g_self_device_control_date=NULL;
my_zlog_info("free g_self_device_control_date");
pthread_mutex_unlock(&g_self_device_control_date_mutex);
} else return;
}
......@@ -40,12 +40,12 @@ void malloc_device_tasks_memory() {
pthread_mutex_lock(&g_self_device_control_date_mutex);
if(g_self_device_control_date==NULL){
g_self_device_control_date=(devicecontroltask_t*)malloc(sizeof(devicecontroltask_t));
memset(g_self_device_control_date, 0, sizeof(devicecontroltask_t)); // 清零内存
my_zlog_info("malloc g_self_device_control_date");
}
pthread_mutex_unlock(&g_self_device_control_date_mutex);
}
/*
* @brief 自控线程中的计时函数接口,每20ms加一次
* @param[in] self_control_time_count,计时次数
......@@ -68,13 +68,16 @@ void self_device_control_task(){
int time_now = g_self_control_time_count*20;
if(i>g_self_device_control_date->id_run_count){
if(g_self_control_interval>time_now){
return;
}
pthread_mutex_lock(&g_self_control_time_count_mutex);
g_self_control_time_count=0;//将计时重置
pthread_mutex_unlock(&g_self_control_time_count_mutex);
i=0;
}
time_now = g_self_control_time_count*20;
......@@ -90,8 +93,6 @@ void self_device_control_task(){
if(valt[2]>2) device_walk_control(g_device_type,valt);
else if(valt[2]<=2) device_gpio_control(g_device_type,valt[1],valt[2]);
my_zlog_debug("m:%d,v:%d",valt[1],valt[2]);
}
......@@ -106,7 +107,6 @@ void self_device_control_task(){
pthread_mutex_unlock(&g_self_control_time_count_mutex);
++i;
}
}
......@@ -164,6 +164,7 @@ bool get_self_control_index(){
void self_control_thread_close(){
if(get_self_control_index()==true){
my_zlog_info("Destroy the thread of the automatic control device");
free_device_tasks_memory();
thread_pool_destroy(g_self_devicecontrol_task_t);
}
my_zlog_info("The thread of the automatic control device is not opened");
......@@ -241,31 +242,44 @@ void receive_self_contorl_mqtt(cJSON *body){
}
cJSON *switch_status = cJSON_GetObjectItem(body, "switch_status");
if (switch_status == NULL || !cJSON_IsNumber(switch_status)) {
my_zlog_error("Invalid switch_status!");
return;
}
static int s_switch_status = 0;
static bool s_thread_index=0;
s_switch_status=switch_status->valueint;
if(s_switch_status == DEVICE_SELF_CONTROL_OPEN){
if(s_thread_index==0) {
self_device_pthrpoll_task_init();
send_self_contorl_date_mqtt();//找后端要动作组合
malloc_device_tasks_memory();//分配内存
}
pthread_mutex_lock(&g_self_control_time_count_mutex);
g_self_control_time_count=0;
pthread_mutex_unlock(&g_self_control_time_count_mutex);
g_self_device_control_switch_index =true;
s_thread_index=1;
my_zlog_info("open device auto");
}else if(s_switch_status == DEVICE_SELF_CONTROL_CLOSE) {
g_self_device_control_switch_index =false;
free_device_tasks_memory();
send_self_contorl_mqtt();
my_zlog_info("close device auto");
return ;
}
cJSON *interval=cJSON_GetObjectItem(body, "interval");
g_self_control_interval=interval->valueint;
malloc_device_tasks_memory();//分配内存
// 2️ id_run 数组
cJSON *id_run = cJSON_GetObjectItem(body, "id_run");
pthread_mutex_lock(&g_self_device_control_date_mutex);//加锁
g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(id_run)) {
int size = cJSON_GetArraySize(id_run);
......@@ -278,28 +292,26 @@ void receive_self_contorl_mqtt(cJSON *body){
// 3 run_cool 数组
cJSON *run_cool = cJSON_GetObjectItem(body, "run_cool");
//g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(run_cool)) {
int size = cJSON_GetArraySize(run_cool);
for (int i = 0; i < size&& i < DEVICE_WALK_SIGN_MAX ; i++) {
cJSON *item = cJSON_GetArrayItem(run_cool, i);
g_self_device_control_date->run_cool[i] = item->valueint;
//g_self_device_control_date->id_run_count++;
}
}
// 4 timed_run 数组
cJSON *timed_run = cJSON_GetObjectItem(body, "timed_run");
//g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(timed_run)) {
int size = cJSON_GetArraySize(timed_run);
for (int i = 0; i < size && i < DEVICE_WALK_SIGN_MAX ; i++) {
cJSON *item = cJSON_GetArrayItem(timed_run, i);
g_self_device_control_date->timed_run[i] = item->valueint;
//g_self_device_control_date->id_run_count++;
}
}
pthread_mutex_unlock(&g_self_device_control_date_mutex);//解锁
send_self_contorl_mqtt();
my_zlog_info("count:%d",g_self_device_control_date->id_run_count);
......@@ -353,8 +365,8 @@ void receive_self_contorl_date_mqtt(cJSON *body){
g_automatic_date_t[s_action_id].action_count++;
my_zlog_debug("combination:%d",g_automatic_date_t[s_action_id].mode[j]);
my_zlog_debug("speed:%d",g_automatic_date_t[s_action_id].val[j]);
my_zlog_info("combination:%d",g_automatic_date_t[s_action_id].mode[j]);
my_zlog_info("speed:%d",g_automatic_date_t[s_action_id].val[j]);
}
}
......@@ -372,3 +384,16 @@ void receive_self_contorl_date_mqtt(cJSON *body){
return;
}
/*
* @brief 开机需要进行配置自控相关的初始化
*/
int get_device_self_control_configuration_initialization(){
static bool index=0;
if(index == 0){
send_self_contorl_date_mqtt();
index = 1;
return 0;
}else return -1;
}
\ No newline at end of file
......@@ -30,7 +30,7 @@ typedef struct{
void set_self_control_time_countfuntion();//计时
void self_control_thread_close();
void self_control_thread_close();//销毁自控的线程
void receive_self_contorl_mqtt(cJSON *body);
......@@ -39,4 +39,6 @@ void receive_self_contorl_date_mqtt(cJSON *body);//数据保存
bool get_self_control_index();
void set_self_control_index(bool index);
int get_device_self_control_configuration_initialization();//开机需要进行配置自控相关的初始化
#endif
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment