From e64effc8ed04913ef67ef956ad5a6731e7f72bd7 Mon Sep 17 00:00:00 2001 From: kyz Date: Fri, 7 Nov 2025 14:30:30 +0800 Subject: [PATCH] add pcs_dtu_d --- CMakeLists.txt | 5 +- fs_overlay/app/html/test.html | 1363 +++++++++++++++++++++++++++++ fs_overlay/app/log4crc | 33 + fs_overlay/app/pcs_dtu_d | 56 ++ fs_overlay/etc/init.d/S99_pcs_dtu | 19 + fs_overlay/etc/network/interfaces | 15 + fs_overlay/etc/nginx/nginx.conf | 15 +- src/main.c | 51 +- src/user_bms.c | 22 +- src/user_mb.c | 1 + src/user_web_server.c | 4 +- test-login.html | 880 ------------------- test.html | 497 ----------- 13 files changed, 1576 insertions(+), 1385 deletions(-) create mode 100644 fs_overlay/app/html/test.html create mode 100644 fs_overlay/app/log4crc create mode 100755 fs_overlay/app/pcs_dtu_d create mode 100755 fs_overlay/etc/init.d/S99_pcs_dtu create mode 100644 fs_overlay/etc/network/interfaces delete mode 100644 test-login.html delete mode 100644 test.html diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ff617b..68137bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,4 +46,7 @@ target_link_libraries( ) install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION /app) -install(FILES ${PROJECT_SOURCE_DIR}/log4crc DESTINATION /app) +#install(FILES ${PROJECT_SOURCE_DIR}/log4crc DESTINATION /app) +#install(PROGRAMS ${PROJECT_SOURCE_DIR}/pcs_dtu_d DESTINATION /app) +#install(FILES ${PROJECT_SOURCE_DIR}/log4crc DESTINATION /app) +#install(PROGRAMS ${PROJECT_SOURCE_DIR}/S99_pcs_dtu DESTINATION /etc/init.d) diff --git a/fs_overlay/app/html/test.html b/fs_overlay/app/html/test.html new file mode 100644 index 0000000..2a6deaf --- /dev/null +++ b/fs_overlay/app/html/test.html @@ -0,0 +1,1363 @@ + + + + + + HTTP & WebSocket 综合测试页面 + + + +
+

HTTP & WebSocket 综合测试页面

+ + +
+

用户认证

+
未登录
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
认证结果
+

+                
+
+ + + +
+ + +
+

GET请求测试

+
+ + +
+
+ + +
+
+
响应结果
+

+            
+
+ + +
+

PCS信息测试

+
+ + +
+
+
响应结果
+

+            
+
+ + +
+

BMS信息测试

+
+ + +
+
+
响应结果
+

+            
+
+ + +
+

WebSocket测试

+
+ + +
+
+ + + +
+
+ + +
+
+ +
+
+
WebSocket消息记录
+
+
全部消息
+
发送消息
+
接收消息
+
+
+

+                
+
+

+                
+
+

+                
+
+
+ + +

+

+
+

文件上传测试

+   +
+
+ +
+
+
+
+ + + diff --git a/fs_overlay/app/log4crc b/fs_overlay/app/log4crc new file mode 100644 index 0000000..efc4a0f --- /dev/null +++ b/fs_overlay/app/log4crc @@ -0,0 +1,33 @@ + + + + + + + 0 + + 0 + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/fs_overlay/app/pcs_dtu_d b/fs_overlay/app/pcs_dtu_d new file mode 100755 index 0000000..50ff634 --- /dev/null +++ b/fs_overlay/app/pcs_dtu_d @@ -0,0 +1,56 @@ +#!/bin/bash + +mkdir -p /var/run/pcs_dtu + +# 定义守护进程的名称和运行路径 +DAEMON_NAME="pcs_dtu" +DAEMON_PATH="/app/" +#DAEMON_PATH="/tmp/" ##debug path + + +# 检查守护进程是否已经在运行 +is_running() { + pid=$(cat /var/run/pcs_dtu/pcs_dtu.pid) + ps -p $pid > /dev/null +} + + +# 启动守护进程 +start() { + if is_running; then + echo "守护进程 $DAEMON_NAME 已经在运行。" + fi + + sleep 10 + + echo "启动守护进程 $DAEMON_NAME ..." + cd "$DAEMON_PATH" + ./"$DAEMON_NAME" & + echo "守护进程 $DAEMON_NAME 启动成功。" +} + +# 监测守护进程并重新启动 +monitor() { + while true; do + if ! is_running; then + echo "守护进程 $DAEMON_NAME 未在运行,重新启动..." + start + fi + + ###TODO: add ota check + + sleep 10 + done +} + +# 检查命令行参数 +case "$1" in + start) + start + monitor + ;; + *) + echo "error" + exit 1 + ;; +esac diff --git a/fs_overlay/etc/init.d/S99_pcs_dtu b/fs_overlay/etc/init.d/S99_pcs_dtu new file mode 100755 index 0000000..d7c2237 --- /dev/null +++ b/fs_overlay/etc/init.d/S99_pcs_dtu @@ -0,0 +1,19 @@ +#!/bin/sh +# +# Start init +# + +case "$1" in + start) + /app/pcs_dtu_d start & + ;; + stop) + ;; + restart|reload) + ;; + *) + echo "Usage: $0 {start|stop|restart}" + exit 1 +esac + +exit $? diff --git a/fs_overlay/etc/network/interfaces b/fs_overlay/etc/network/interfaces new file mode 100644 index 0000000..20a7a4d --- /dev/null +++ b/fs_overlay/etc/network/interfaces @@ -0,0 +1,15 @@ +# interface file auto-generated by buildroot + +auto lo +iface lo inet loopback + +auto eth0 +iface eth0 inet dhcp + +auto eth0:1 +iface eth0:1 inet static + address 192.168.1.8 + netmask 255.255.255.0 + + +source-directory /etc/network/interfaces.d diff --git a/fs_overlay/etc/nginx/nginx.conf b/fs_overlay/etc/nginx/nginx.conf index b7cc4c1..37a012a 100755 --- a/fs_overlay/etc/nginx/nginx.conf +++ b/fs_overlay/etc/nginx/nginx.conf @@ -42,7 +42,7 @@ http { location / { root /app/html; - index index.html index.htm; + index index.html index.htm test.html; } #error_page 404 /404.html; @@ -79,7 +79,7 @@ http { # HTTP API 反向代理 location /api/ { - proxy_pass http://localhost:7681; + proxy_pass http://localhost:12345; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; @@ -91,7 +91,7 @@ http { # 修改WebSocket代理配置 location /ws { - proxy_pass http://localhost:7681/; # 添加尾部斜杠,确保路径正确映射 + proxy_pass http://localhost:12345/; # 添加尾部斜杠,确保路径正确映射 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; @@ -104,6 +104,15 @@ http { proxy_send_timeout 86400; } + client_max_body_size 100m; + location /upload { + proxy_pass http://localhost:12345; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } } diff --git a/src/main.c b/src/main.c index 7f5287a..74fb403 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,17 @@ #include #include "user.h" +#ifdef __x86_64__ + +#define PID_FILE "/tmp/pcs_dtu.pid" + +#else + +#define PID_FILE "/var/run/pcs_dtu/pcs_dtu.pid" + +#endif + + int bms_start(void); int pcs_start(void); int webserver_start(void); @@ -10,6 +21,40 @@ extern bool user_bms_send_heartbeat(uint16_t heartbeat_value); board_info_t g_board_info; +static int alone_runnind(void) +{ + int fd; + char buf[16]; + struct flock fl; + + fd = open(PID_FILE, O_RDWR | O_CREAT, 0666); + if (fd < 0){ + perror("open"); + exit(1); + } + + fl.l_type = F_WRLCK; + fl.l_start = 0; + fl.l_whence = SEEK_SET; + fl.l_len = 0; + + if(fcntl(fd, F_SETLK, &fl)<0){ + if (errno == EACCES || errno == EAGAIN){ + close(fd); + return -1; + } + } + + if( ftruncate(fd, 0) ){ //设置文件的大小为0 + } + + sprintf(buf, "%ld\n", (long)getpid()); + if( write(fd, buf, strlen(buf) )){ + } + + //这里不关闭文件, 保持锁定状态 + return 0; +} int main(void) { @@ -18,6 +63,10 @@ int main(void) uint16_t heartbeat_value = 0x1234; uint32_t n = 0; + if( alone_runnind()<0){ + printf( "pcs_dtu already running ...exit!\n " ); + exit(-1); + } user_log_init(); printf("---PCS DTU, Version:1.0\n"); @@ -56,7 +105,7 @@ int main(void) printf( "\n\n bms data: %s \n\n", bms_data_to_str( &bms_data ) ); memcpy( &g_board_info.bms_data, &bms_data, sizeof( bms_data ) ); } - if( n++ > 10 ){ + if( n++ > 5 ){ n = 0; user_bms_send_heartbeat( heartbeat_value++ ); } diff --git a/src/user_bms.c b/src/user_bms.c index 45b77bb..c0248b6 100644 --- a/src/user_bms.c +++ b/src/user_bms.c @@ -9,9 +9,13 @@ #include #include #include +#include #include "user_bms.h" +#define BMS_CAN "can0" +#define BMS_CAN_DOWN "ip link set can0 down" +#define BMS_CAN_UP "ip link set can0 up" static struct sockaddr_can addr; @@ -28,6 +32,16 @@ static int16_t le16s_to_host(const uint8_t *data) { return (int16_t)(data[0] | (data[1] << 8)); } +void bms_can_reset( void ) +{ + printf( "can0 down...\n" ); + system( BMS_CAN_DOWN ); + usleep(100); + printf( "can0 up...\n" ); + system( BMS_CAN_UP ); + +} + /* 获取BMS设备信息 */ int user_bms_data_get(bms_data_t *bms_data) { @@ -58,6 +72,7 @@ bool user_bms_init(const char *can_interface) // 初始化BMS数据 memset(&g_bms, 0, sizeof(g_bms)); + g_bms.can_socket = -1; pthread_mutex_init(&g_bms.mutex, NULL); // 创建CAN socket @@ -110,8 +125,13 @@ bool user_bms_send_heartbeat(uint16_t heartbeat_value) memset(&frame.data[2], 0, 6); // 发送CAN帧 + printf( "bms write ... \n" ); if (write(g_bms.can_socket, &frame, sizeof(struct can_frame)) != sizeof(struct can_frame)) { - perror("write"); + perror("bsm write error"); + if (errno == ENOBUFS) { + printf("write faile [ENOBUFS]: reset can interface!\b"); + bms_can_reset(); + } return false; } diff --git a/src/user_mb.c b/src/user_mb.c index 1d440b4..ec69278 100644 --- a/src/user_mb.c +++ b/src/user_mb.c @@ -262,6 +262,7 @@ static void *user_mb_thread_func(void *arg) { } /* 等待1秒 */ + printf( "mb read sleep(%d s);\n", handler->read_interval ); if( modbus_read_input_bits > 0 && handler->read_interval < 30 ) sleep(handler->read_interval); else diff --git a/src/user_web_server.c b/src/user_web_server.c index 0a34fb5..6cd0d08 100644 --- a/src/user_web_server.c +++ b/src/user_web_server.c @@ -755,10 +755,10 @@ static void* webserver_task_thread(void* arg) int thread_create_result; lws_set_log_level(logs, NULL); - lwsl_user("LWS minimal HTTP & WebSocket combined server | visit http://localhost:7681\n"); + lwsl_user("LWS minimal HTTP & WebSocket combined server | visit http://localhost:12345\n"); memset(&info, 0, sizeof info); /* 否则是未初始化的垃圾值 */ - info.port = 7681; + info.port = 12345; info.protocols = protocols; info.mounts = &mount; info.pvo = &pvo; diff --git a/test-login.html b/test-login.html deleted file mode 100644 index f555d4f..0000000 --- a/test-login.html +++ /dev/null @@ -1,880 +0,0 @@ - - - - - - HTTP & WebSocket 综合测试页面 - - - -
- 未登录 -
- -

HTTP & WebSocket 综合测试页面

- - -
-

用户认证

-
-
- - -
-
- - -
- - -
- -
- 等待认证操作... -
-
- - -
-

GET 请求测试

-

点击按钮发送 GET 请求到服务器,并查看返回的 JSON 数据。

- - - -
- 等待响应... -
-
- - -
-

获取PCS信息测试

-

点击按钮发送 POST 请求获取PCS信息,服务器将返回包含PCS详细数据的JSON响应。

- - - - -
- 等待响应... -
-
- - -
-

WebSocket 测试

-

连接到 WebSocket 服务器,并发送/接收消息。

- -
- - -
- -
- - - 未连接 -
- - -
- - - - \ No newline at end of file diff --git a/test.html b/test.html deleted file mode 100644 index 6959cc2..0000000 --- a/test.html +++ /dev/null @@ -1,497 +0,0 @@ - - - - - - HTTP & WebSocket 综合测试页面 - - - -

HTTP & WebSocket 综合测试页面

- - -
-

GET 请求测试

-

点击按钮发送 GET 请求到服务器,并查看返回的 JSON 数据。

- - - -
- 等待响应... -
-
- - -
-

获取PCS信息测试

-

点击按钮发送 POST 请求获取PCS信息,服务器将返回包含PCS详细数据的JSON响应。

- - - - -
- 等待响应... -
-
- - -
-

WebSocket 测试

-

连接到 WebSocket 服务器,并发送/接收消息。

- -
- - -
- -
- - - 未连接 -
- - -
- - - - -- 2.34.1