Commit c9eaeec3 authored by 957dd's avatar 957dd

实现了设备自控,将释放内存放到了根其他线程函数结束一起

parent 8d95ee6d
...@@ -20,21 +20,21 @@ char *device_inspect() { ...@@ -20,21 +20,21 @@ char *device_inspect() {
if(read_device_back_fail_middle!=NULL) {//回退文件 if(read_device_back_fail_middle!=NULL) {//回退文件
return read_device_back_fail_middle; return read_device_back_fail_middle;
} }
my_zlog_debug("文件 %s 打开失败,等待中...", FILENAME); my_zlog_info("文件 %s 打开失败,等待中...", FILENAME);
} }
else { else {
// 尝试读取文件内容 // 尝试读取文件内容
if (fgets(s_buffer_device, sizeof(s_buffer_device), file) != NULL) { if (fgets(s_buffer_device, sizeof(s_buffer_device), file) != NULL) {
// 如果文件内容不为空 // 如果文件内容不为空
fclose(file); fclose(file);
my_zlog_debug("读取到文件内容: %s", s_buffer_device); my_zlog_info("读取到文件内容: %s", s_buffer_device);
return s_buffer_device; return s_buffer_device;
} else { } else {
if(read_device_back_fail_middle!=NULL) { if(read_device_back_fail_middle!=NULL) {
fclose(file); fclose(file);
return read_device_back_fail(); return read_device_back_fail();
} }
my_zlog_debug("文件为空,等待中..."); my_zlog_info("文件为空,等待中...");
} }
fclose(file); fclose(file);
} }
...@@ -57,16 +57,16 @@ char *program_version() { ...@@ -57,16 +57,16 @@ char *program_version() {
static char s_buffer_version[30];// 用于存储版本号内容 static char s_buffer_version[30];// 用于存储版本号内容
file = fopen(FILE_VERSION, "r"); // 以只读模式打开文件 file = fopen(FILE_VERSION, "r"); // 以只读模式打开文件
if (file == NULL) { if (file == NULL) {
my_zlog_debug("版本文件无"); my_zlog_info("版本文件无");
} else { } else {
// 尝试读取文件内容 // 尝试读取文件内容
if (fgets(s_buffer_version, sizeof(s_buffer_version), file) != NULL) { if (fgets(s_buffer_version, sizeof(s_buffer_version), file) != NULL) {
// 如果文件内容不为空 // 如果文件内容不为空
fclose(file); fclose(file);
my_zlog_debug("读取到文件内容: %s", s_buffer_version); my_zlog_info("读取到文件内容: %s", s_buffer_version);
return s_buffer_version; return s_buffer_version;
} else { } else {
my_zlog_debug("文件为空,等待中..."); my_zlog_info("文件为空,等待中...");
} }
fclose(file); fclose(file);
} }
......
...@@ -56,6 +56,7 @@ int main(){ ...@@ -56,6 +56,7 @@ int main(){
thread_end_close(); thread_end_close();
device_end_close(g_device_type); device_end_close(g_device_type);
self_control_thread_close();//销毁自控的线程
my_log_close();//关闭日志 my_log_close();//关闭日志
my_zlog_info("程序关闭成功"); my_zlog_info("程序关闭成功");
return 0; return 0;
......
...@@ -46,6 +46,8 @@ void thread_end_close() ; ...@@ -46,6 +46,8 @@ void thread_end_close() ;
/*相关设备线程等关闭函数*/ /*相关设备线程等关闭函数*/
void device_end_close(int device_id); void device_end_close(int device_id);
void self_control_thread_close();//销毁自控的线程
/*关闭log日志记录*/ /*关闭log日志记录*/
void my_log_close(); void my_log_close();
......
...@@ -41,6 +41,7 @@ int thread_start_init(ThreadFunc thread_exit_time, ThreadFunc thread_mqtt_beat, ...@@ -41,6 +41,7 @@ int thread_start_init(ThreadFunc thread_exit_time, ThreadFunc thread_mqtt_beat,
void *thread_exit_time(void *arg) { void *thread_exit_time(void *arg) {
while(1){ while(1){
if(get_self_control_index()==false){ if(get_self_control_index()==false){
delay_ms(100); delay_ms(100);
pthread_mutex_lock(&g_exit_count_mutex); pthread_mutex_lock(&g_exit_count_mutex);
g_devcontrol_exit_count++; g_devcontrol_exit_count++;
...@@ -52,6 +53,7 @@ void *thread_exit_time(void *arg) { ...@@ -52,6 +53,7 @@ void *thread_exit_time(void *arg) {
}else if(get_self_control_index()==true){ }else if(get_self_control_index()==true){
delay_ms(20); delay_ms(20);
set_self_control_time_countfuntion(); set_self_control_time_countfuntion();
} }
} }
...@@ -83,6 +85,11 @@ void *thread_mqtt_beat(void *arg) { ...@@ -83,6 +85,11 @@ void *thread_mqtt_beat(void *arg) {
g_heartbeat_count++; g_heartbeat_count++;
if(g_mqtt_grc == 0){ 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; return NULL;
...@@ -91,12 +98,6 @@ void *thread_mqtt_beat(void *arg) { ...@@ -91,12 +98,6 @@ void *thread_mqtt_beat(void *arg) {
//打开游览器线程,让游览器一直在一个线程中打开并运行 //打开游览器线程,让游览器一直在一个线程中打开并运行
void *thread_open_browser(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"); system("pkill chromium");
delay_s(5); delay_s(5);
while(1){ while(1){
......
No preview for this file type
...@@ -40,12 +40,12 @@ void malloc_device_tasks_memory() { ...@@ -40,12 +40,12 @@ void malloc_device_tasks_memory() {
pthread_mutex_lock(&g_self_device_control_date_mutex); pthread_mutex_lock(&g_self_device_control_date_mutex);
if(g_self_device_control_date==NULL){ if(g_self_device_control_date==NULL){
g_self_device_control_date=(devicecontroltask_t*)malloc(sizeof(devicecontroltask_t)); 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"); my_zlog_info("malloc g_self_device_control_date");
} }
pthread_mutex_unlock(&g_self_device_control_date_mutex); pthread_mutex_unlock(&g_self_device_control_date_mutex);
} }
/* /*
* @brief 自控线程中的计时函数接口,每20ms加一次 * @brief 自控线程中的计时函数接口,每20ms加一次
* @param[in] self_control_time_count,计时次数 * @param[in] self_control_time_count,计时次数
...@@ -68,13 +68,16 @@ void self_device_control_task(){ ...@@ -68,13 +68,16 @@ void self_device_control_task(){
int time_now = g_self_control_time_count*20; int time_now = g_self_control_time_count*20;
if(i>g_self_device_control_date->id_run_count){ if(i>g_self_device_control_date->id_run_count){
if(g_self_control_interval>time_now){ if(g_self_control_interval>time_now){
return; return;
} }
pthread_mutex_lock(&g_self_control_time_count_mutex);
g_self_control_time_count=0;//将计时重置 g_self_control_time_count=0;//将计时重置
pthread_mutex_unlock(&g_self_control_time_count_mutex);
i=0; i=0;
} }
time_now = g_self_control_time_count*20; time_now = g_self_control_time_count*20;
...@@ -90,8 +93,6 @@ void self_device_control_task(){ ...@@ -90,8 +93,6 @@ void self_device_control_task(){
if(valt[2]>2) device_walk_control(g_device_type,valt); 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]); 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]);
} }
...@@ -107,7 +108,6 @@ void self_device_control_task(){ ...@@ -107,7 +108,6 @@ void self_device_control_task(){
++i; ++i;
} }
} }
...@@ -164,6 +164,7 @@ bool get_self_control_index(){ ...@@ -164,6 +164,7 @@ bool get_self_control_index(){
void self_control_thread_close(){ void self_control_thread_close(){
if(get_self_control_index()==true){ if(get_self_control_index()==true){
my_zlog_info("Destroy the thread of the automatic control device"); my_zlog_info("Destroy the thread of the automatic control device");
free_device_tasks_memory();
thread_pool_destroy(g_self_devicecontrol_task_t); thread_pool_destroy(g_self_devicecontrol_task_t);
} }
my_zlog_info("The thread of the automatic control device is not opened"); my_zlog_info("The thread of the automatic control device is not opened");
...@@ -241,31 +242,44 @@ void receive_self_contorl_mqtt(cJSON *body){ ...@@ -241,31 +242,44 @@ void receive_self_contorl_mqtt(cJSON *body){
} }
cJSON *switch_status = cJSON_GetObjectItem(body, "switch_status"); 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 int s_switch_status = 0;
static bool s_thread_index=0; static bool s_thread_index=0;
s_switch_status=switch_status->valueint; s_switch_status=switch_status->valueint;
if(s_switch_status == DEVICE_SELF_CONTROL_OPEN){ if(s_switch_status == DEVICE_SELF_CONTROL_OPEN){
if(s_thread_index==0) { if(s_thread_index==0) {
self_device_pthrpoll_task_init(); 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; g_self_control_time_count=0;
pthread_mutex_unlock(&g_self_control_time_count_mutex);
g_self_device_control_switch_index =true; g_self_device_control_switch_index =true;
s_thread_index=1; s_thread_index=1;
my_zlog_info("open device auto");
}else if(s_switch_status == DEVICE_SELF_CONTROL_CLOSE) { }else if(s_switch_status == DEVICE_SELF_CONTROL_CLOSE) {
g_self_device_control_switch_index =false; g_self_device_control_switch_index =false;
free_device_tasks_memory(); my_zlog_info("close device auto");
send_self_contorl_mqtt();
return ; return ;
} }
cJSON *interval=cJSON_GetObjectItem(body, "interval"); cJSON *interval=cJSON_GetObjectItem(body, "interval");
g_self_control_interval=interval->valueint; g_self_control_interval=interval->valueint;
malloc_device_tasks_memory();//分配内存
// 2️ id_run 数组 // 2️ id_run 数组
cJSON *id_run = cJSON_GetObjectItem(body, "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; g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(id_run)) { if (cJSON_IsArray(id_run)) {
int size = cJSON_GetArraySize(id_run); int size = cJSON_GetArraySize(id_run);
...@@ -278,28 +292,26 @@ void receive_self_contorl_mqtt(cJSON *body){ ...@@ -278,28 +292,26 @@ void receive_self_contorl_mqtt(cJSON *body){
// 3 run_cool 数组 // 3 run_cool 数组
cJSON *run_cool = cJSON_GetObjectItem(body, "run_cool"); cJSON *run_cool = cJSON_GetObjectItem(body, "run_cool");
//g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(run_cool)) { if (cJSON_IsArray(run_cool)) {
int size = cJSON_GetArraySize(run_cool); int size = cJSON_GetArraySize(run_cool);
for (int i = 0; i < size&& i < DEVICE_WALK_SIGN_MAX ; i++) { for (int i = 0; i < size&& i < DEVICE_WALK_SIGN_MAX ; i++) {
cJSON *item = cJSON_GetArrayItem(run_cool, i); cJSON *item = cJSON_GetArrayItem(run_cool, i);
g_self_device_control_date->run_cool[i] = item->valueint; g_self_device_control_date->run_cool[i] = item->valueint;
//g_self_device_control_date->id_run_count++;
} }
} }
// 4 timed_run 数组 // 4 timed_run 数组
cJSON *timed_run = cJSON_GetObjectItem(body, "timed_run"); cJSON *timed_run = cJSON_GetObjectItem(body, "timed_run");
//g_self_device_control_date->id_run_count = 0;
if (cJSON_IsArray(timed_run)) { if (cJSON_IsArray(timed_run)) {
int size = cJSON_GetArraySize(timed_run); int size = cJSON_GetArraySize(timed_run);
for (int i = 0; i < size && i < DEVICE_WALK_SIGN_MAX ; i++) { for (int i = 0; i < size && i < DEVICE_WALK_SIGN_MAX ; i++) {
cJSON *item = cJSON_GetArrayItem(timed_run, i); cJSON *item = cJSON_GetArrayItem(timed_run, i);
g_self_device_control_date->timed_run[i] = item->valueint; 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(); send_self_contorl_mqtt();
my_zlog_info("count:%d",g_self_device_control_date->id_run_count); 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){ ...@@ -353,8 +365,8 @@ void receive_self_contorl_date_mqtt(cJSON *body){
g_automatic_date_t[s_action_id].action_count++; 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_info("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("speed:%d",g_automatic_date_t[s_action_id].val[j]);
} }
} }
...@@ -372,3 +384,16 @@ void receive_self_contorl_date_mqtt(cJSON *body){ ...@@ -372,3 +384,16 @@ void receive_self_contorl_date_mqtt(cJSON *body){
return; 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{ ...@@ -30,7 +30,7 @@ typedef struct{
void set_self_control_time_countfuntion();//计时 void set_self_control_time_countfuntion();//计时
void self_control_thread_close(); void self_control_thread_close();//销毁自控的线程
void receive_self_contorl_mqtt(cJSON *body); void receive_self_contorl_mqtt(cJSON *body);
...@@ -39,4 +39,6 @@ void receive_self_contorl_date_mqtt(cJSON *body);//数据保存 ...@@ -39,4 +39,6 @@ void receive_self_contorl_date_mqtt(cJSON *body);//数据保存
bool get_self_control_index(); bool get_self_control_index();
void set_self_control_index(bool index); void set_self_control_index(bool index);
int get_device_self_control_configuration_initialization();//开机需要进行配置自控相关的初始化
#endif #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