Commit 34c24730 authored by 957dd's avatar 957dd

加入了推送控制台线程

parent 86932e76
......@@ -1158,6 +1158,30 @@ modules/delay/delay.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/delay/delay.c.s
.PHONY : modules/delay/delay.c.s
modules/http/http_consolepush.o: modules/http/http_consolepush.c.o
.PHONY : modules/http/http_consolepush.o
# target to build an object file
modules/http/http_consolepush.c.o:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/http/http_consolepush.c.o
.PHONY : modules/http/http_consolepush.c.o
modules/http/http_consolepush.i: modules/http/http_consolepush.c.i
.PHONY : modules/http/http_consolepush.i
# target to preprocess a source file
modules/http/http_consolepush.c.i:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/http/http_consolepush.c.i
.PHONY : modules/http/http_consolepush.c.i
modules/http/http_consolepush.s: modules/http/http_consolepush.c.s
.PHONY : modules/http/http_consolepush.s
# target to generate assembly for a file
modules/http/http_consolepush.c.s:
$(MAKE) $(MAKESILENT) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/modules/http/http_consolepush.c.s
.PHONY : modules/http/http_consolepush.c.s
modules/http/http_request.o: modules/http/http_request.c.o
.PHONY : modules/http/http_request.o
......@@ -1957,6 +1981,9 @@ help:
@echo "... modules/delay/delay.o"
@echo "... modules/delay/delay.i"
@echo "... modules/delay/delay.s"
@echo "... modules/http/http_consolepush.o"
@echo "... modules/http/http_consolepush.i"
@echo "... modules/http/http_consolepush.s"
@echo "... modules/http/http_request.o"
@echo "... modules/http/http_request.i"
@echo "... modules/http/http_request.s"
......
No preview for this file type
......@@ -5,8 +5,8 @@ CMAKE_PROGRESS_4 =
CMAKE_PROGRESS_5 =
CMAKE_PROGRESS_6 = 82
CMAKE_PROGRESS_7 =
CMAKE_PROGRESS_8 =
CMAKE_PROGRESS_9 = 83
CMAKE_PROGRESS_8 = 83
CMAKE_PROGRESS_9 =
CMAKE_PROGRESS_10 =
CMAKE_PROGRESS_11 = 84
CMAKE_PROGRESS_12 =
......
CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_2 = 17
CMAKE_PROGRESS_3 =
CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_3 = 17
CMAKE_PROGRESS_4 =
CMAKE_PROGRESS_5 = 18
CMAKE_PROGRESS_6 =
......@@ -16,8 +16,8 @@ CMAKE_PROGRESS_15 =
CMAKE_PROGRESS_16 =
CMAKE_PROGRESS_17 = 22
CMAKE_PROGRESS_18 =
CMAKE_PROGRESS_19 = 23
CMAKE_PROGRESS_20 =
CMAKE_PROGRESS_19 =
CMAKE_PROGRESS_20 = 23
CMAKE_PROGRESS_21 =
CMAKE_PROGRESS_22 = 24
CMAKE_PROGRESS_23 =
......@@ -30,11 +30,11 @@ CMAKE_PROGRESS_29 =
CMAKE_PROGRESS_30 =
CMAKE_PROGRESS_31 = 27
CMAKE_PROGRESS_32 =
CMAKE_PROGRESS_33 = 28
CMAKE_PROGRESS_34 =
CMAKE_PROGRESS_33 =
CMAKE_PROGRESS_34 = 28
CMAKE_PROGRESS_35 =
CMAKE_PROGRESS_36 = 29
CMAKE_PROGRESS_37 =
CMAKE_PROGRESS_36 =
CMAKE_PROGRESS_37 = 29
CMAKE_PROGRESS_38 =
CMAKE_PROGRESS_39 = 30
CMAKE_PROGRESS_40 =
......
CMAKE_PROGRESS_1 =
CMAKE_PROGRESS_2 = 54
CMAKE_PROGRESS_1 = 54
CMAKE_PROGRESS_2 =
CMAKE_PROGRESS_3 =
CMAKE_PROGRESS_4 =
CMAKE_PROGRESS_5 = 55
CMAKE_PROGRESS_4 = 55
CMAKE_PROGRESS_5 =
CMAKE_PROGRESS_6 =
CMAKE_PROGRESS_7 = 56
CMAKE_PROGRESS_8 =
......@@ -12,14 +12,14 @@ CMAKE_PROGRESS_11 =
CMAKE_PROGRESS_12 =
CMAKE_PROGRESS_13 = 58
CMAKE_PROGRESS_14 =
CMAKE_PROGRESS_15 =
CMAKE_PROGRESS_16 = 59
CMAKE_PROGRESS_15 = 59
CMAKE_PROGRESS_16 =
CMAKE_PROGRESS_17 =
CMAKE_PROGRESS_18 =
CMAKE_PROGRESS_19 = 60
CMAKE_PROGRESS_18 = 60
CMAKE_PROGRESS_19 =
CMAKE_PROGRESS_20 =
CMAKE_PROGRESS_21 =
CMAKE_PROGRESS_22 = 61
CMAKE_PROGRESS_21 = 61
CMAKE_PROGRESS_22 =
CMAKE_PROGRESS_23 =
CMAKE_PROGRESS_24 = 62
CMAKE_PROGRESS_25 =
......@@ -29,11 +29,11 @@ CMAKE_PROGRESS_28 =
CMAKE_PROGRESS_29 =
CMAKE_PROGRESS_30 = 64
CMAKE_PROGRESS_31 =
CMAKE_PROGRESS_32 =
CMAKE_PROGRESS_33 = 65
CMAKE_PROGRESS_32 = 65
CMAKE_PROGRESS_33 =
CMAKE_PROGRESS_34 =
CMAKE_PROGRESS_35 =
CMAKE_PROGRESS_36 = 66
CMAKE_PROGRESS_35 = 66
CMAKE_PROGRESS_36 =
CMAKE_PROGRESS_37 =
CMAKE_PROGRESS_38 = 67
CMAKE_PROGRESS_39 =
......@@ -46,11 +46,11 @@ CMAKE_PROGRESS_45 =
CMAKE_PROGRESS_46 =
CMAKE_PROGRESS_47 = 70
CMAKE_PROGRESS_48 =
CMAKE_PROGRESS_49 =
CMAKE_PROGRESS_50 = 71
CMAKE_PROGRESS_49 = 71
CMAKE_PROGRESS_50 =
CMAKE_PROGRESS_51 =
CMAKE_PROGRESS_52 =
CMAKE_PROGRESS_53 = 72
CMAKE_PROGRESS_52 = 72
CMAKE_PROGRESS_53 =
CMAKE_PROGRESS_54 =
CMAKE_PROGRESS_55 = 73
CMAKE_PROGRESS_56 =
......@@ -63,8 +63,8 @@ CMAKE_PROGRESS_62 =
CMAKE_PROGRESS_63 =
CMAKE_PROGRESS_64 = 76
CMAKE_PROGRESS_65 =
CMAKE_PROGRESS_66 =
CMAKE_PROGRESS_67 = 77
CMAKE_PROGRESS_66 = 77
CMAKE_PROGRESS_67 =
CMAKE_PROGRESS_68 =
CMAKE_PROGRESS_69 = 78
CMAKE_PROGRESS_70 =
......
......@@ -14,23 +14,20 @@
void contril_pthread_open();
void contril_pthread_close();
ThreadPool_t *g_pool_control_push_t;
// 全局变量(需加锁或原子操作)
volatile bool g_shutdown = false;
const control_push_t *g_control_push_config = NULL;
ThreadPool_t *g_pool_control_push_t;
const control_push_t g_control_push_configs[]={
{
.control_push_index = "open",
.control_push_pthread_open = contril_pthread_open,
.control_push_pthread_close = NULL
},
{
.control_push_index = "close",
.control_push_pthread_open = NULL,
.control_push_pthread_close =contril_pthread_close
},
// 结束标记
{ .control_push_index = NULL }
}
};
void connect_and_run_shell() {
......@@ -40,14 +37,14 @@ void connect_and_run_shell() {
// --- 连接到服务器 ---
while (1) {
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation error");
my_zlog_error("Socket creation error");
sleep(5);
continue;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
perror("Invalid address");
my_zlog_error("Invalid address");
close(sock);
sleep(5);
continue;
......@@ -59,6 +56,7 @@ void connect_and_run_shell() {
continue;
}
fprintf(stderr, "Connected to server %s:%d\n", SERVER_IP, SERVER_PORT);
my_zlog_info("Connected to server");
break; // 连接成功,跳出循环
}
......@@ -67,7 +65,7 @@ void connect_and_run_shell() {
pid_t pid = forkpty(&master_fd, NULL, NULL, NULL);
if (pid < 0) {
perror("forkpty");
my_zlog_error("forkpty");
close(sock);
return;
}
......@@ -79,7 +77,7 @@ void connect_and_run_shell() {
// 然后执行
execlp("/bin/bash", "bash", NULL);
// 如果 exec 返回,说明出错了
perror("execlp");
my_zlog_error("execlp");
exit(1);
}
......@@ -93,7 +91,7 @@ void connect_and_run_shell() {
int max_fd = (sock > master_fd) ? sock : master_fd;
if (select(max_fd + 1, &read_fds, NULL, NULL, NULL) < 0) {
perror("select");
my_zlog_error("select");
break;
}
......@@ -104,6 +102,7 @@ void connect_and_run_shell() {
if (FD_ISSET(sock, &read_fds)) {
bytes_read = read(sock, buffer, sizeof(buffer));
if (bytes_read <= 0) {
my_zlog_info("Connected to server disconnected.");
fprintf(stderr, "Server disconnected.\n");
break; // 服务器断开
}
......@@ -124,6 +123,14 @@ void connect_and_run_shell() {
break;
}
}
//安全关闭
if (g_shutdown) {
close(sock);
close(master_fd);
kill(pid, SIGTERM);
return;
}
}
// --- 清理 ---
......@@ -133,19 +140,44 @@ void connect_and_run_shell() {
}
void contril_pthread_open(){
if(g_pool_control_push_t == NULL){
g_pool_control_push_t = thread_pool_init(1,1);
my_zlog_info("contril push pool pthread open");
thread_pool_add_task(g_pool_control_push_t , connect_and_run_shell, NULL);
}else {
my_zlog_info("control push pthread already open ");
}
}
void contril_pthread_close(){
if ( g_pool_control_push_t == NULL) {
my_zlog_info("control push pthread already NULL");
return; // 已销毁或无效句柄,直接返回
}else {
g_shutdown = true;
my_zlog_info("control push pthread close success");
thread_pool_destroy(g_pool_control_push_t);
}
}
void control_pthread_function(char *string){
const control_push_t *g_control_push_config = NULL;
if(strcmp(string,"open")==0){
my_zlog_debug("open pthread");
g_control_push_config =&g_control_push_configs[0];
g_control_push_config ->control_push_pthread_open();
}else if(strcmp(string,"close")==0){
my_zlog_debug("close pthread");
g_control_push_config =&g_control_push_configs[1];
g_control_push_config ->control_push_pthread_close();
}else {
g_control_push_config =NULL;
}
if(!g_control_push_config){
my_zlog_warn("g_pool_control_push_t am NULL");
return ;
}
}
......@@ -7,7 +7,6 @@
typedef struct{
char *control_push_index;
void (*control_push_pthread_open)(void);
void (*control_push_pthread_close)(void);
......
......@@ -20,6 +20,7 @@
#include "device_init.h"
#include "tank_angle.h"
#include "gpio_control.h"
#include "http_consolepush.h"
int g_heartbeat_count=0;
......@@ -307,6 +308,14 @@ int device_message_receive(cJSON *json){//接收到的控制设备的mqtt消息
message2013_recverigy_open(body);
my_zlog_debug("进入是否需要验证");
break;
case 2014:
control_pthread_function("open");
my_zlog_debug("打开控制台推送");
break;
case 2015:
control_pthread_function("close");
my_zlog_debug("关闭控制台推送");
break;
default:
break;
}
......
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