Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
car-controlserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wenzhongjian
car-controlserver
Commits
47699a2f
Commit
47699a2f
authored
Sep 23, 2025
by
957dd
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
拉取接口绑定mqtt服务器,还需测试
parent
2f81009a
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
41 additions
and
24 deletions
+41
-24
CMakeLists.txt
CMakeLists.txt
+1
-0
pthread_open.c
app/main/pthread_open.c
+6
-10
main
build/main
+0
-0
common.h
include/common.h
+2
-0
browser_open.c
modules/browser/browser_open.c
+1
-1
http_config_mqtt.c
modules/http/http_config_mqtt.c
+4
-3
http_config_mqtt.h
modules/http/http_config_mqtt.h
+1
-2
mqtt_init.c
modules/mqtt/mqtt_init.c
+26
-8
No files found.
CMakeLists.txt
View file @
47699a2f
...
...
@@ -91,6 +91,7 @@ target_link_libraries(main PRIVATE
crypto
pthread
dl
uuid
)
# 安装规则
...
...
app/main/pthread_open.c
View file @
47699a2f
...
...
@@ -124,20 +124,16 @@ void *thread_open_browser(void *arg) {
void
*
thread_mqtt_reconnect
(
void
*
arg
)
{
while
(
1
){
if
(
mqtt_init
()
!=
0
)
{
my_zlog_fatal
(
"mqtt_init failed"
);
mqtt_clean
();
continue
;
}
if
(
g_clients_t
==
NULL
)
{
if
(
!
g_clients_t
)
{
my_zlog_error
(
"Failed to create mosquitto client"
);
if
(
mqtt_init
()
==
0
){
my_zlog_warn
(
"mqtt success"
);
break
;
}
else
{
delay_ms
(
300
);
continue
;
}
}
mqtt_cycle
();
}
mqtt_clean
();
return
NULL
;
...
...
build/main
View file @
47699a2f
No preview for this file type
include/common.h
View file @
47699a2f
...
...
@@ -29,6 +29,8 @@
#include <time.h> // 时间处理
#include <stddef.h>
#include <uuid/uuid.h>
#include "mosquitto.h"
#include "cjson/cJSON.h"
...
...
modules/browser/browser_open.c
View file @
47699a2f
...
...
@@ -8,7 +8,7 @@ char gwebcam[2048];//存放启动火狐网站命令
int
opencamsh
()
{
char
urls
[
5
0
];
char
urls
[
5
12
];
sprintf
(
urls
,
"%s%s"
,
g_mqtt_cam_config_t
->
videourl
,
mqtt_topic_pure_number
());
...
...
modules/http/http_config_mqtt.c
View file @
47699a2f
...
...
@@ -52,12 +52,13 @@ int parse_device_config(const char *json_str) {
int
i
=
0
;
cJSON_ArrayForEach
(
item
,
mqtt
)
{
if
(
cJSON_IsString
(
item
))
{
my_zlog_info
(
" [%d] %s"
,
i
++
,
item
->
valuestring
);
strncpy
(
g_mqtt_cam_config_t
->
mqtt_servers
[
i
],
item
->
valuestring
,
sizeof
(
g_mqtt_cam_config_t
->
mqtt_servers
[
i
])
-
1
);
g_mqtt_cam_config_t
->
mqtt_servers
[
i
][
sizeof
(
g_mqtt_cam_config_t
->
mqtt_servers
[
i
])
-
1
]
=
'\0'
;
g_mqtt_cam_config_t
->
mqtt_servers
[
i
][
sizeof
(
g_mqtt_cam_config_t
->
mqtt_servers
[
i
])
-
1
]
=
'\0'
;
my_zlog_info
(
" [%d] %s"
,
i
,
g_mqtt_cam_config_t
->
mqtt_servers
[
i
]);
i
=
i
+
1
;
}
g_mqtt_cam_config_t
->
mqtt_count
=
i
;
}
g_mqtt_cam_config_t
->
mqtt_count
=
i
;
}
// 4. 提取 api 和 videoUrl
...
...
modules/http/http_config_mqtt.h
View file @
47699a2f
...
...
@@ -12,8 +12,7 @@ typedef struct{
int
mqtt_count
;
}
DEVICE_MQTTCONFIG
;
extern
DEVICE_MQTTCONFIG
*
g_mqtt_cam_config_t
;
extern
DEVICE_MQTTCONFIG
*
g_mqtt_cam_config_t
;
/*mqtt服务器和视频流地址初始化*/
int
http_mqtt_video_init
();
...
...
modules/mqtt/mqtt_init.c
View file @
47699a2f
...
...
@@ -8,16 +8,17 @@
mqttclient
g_clients_t
[
MAX_SERVERS
];
char
g_uuid_mqtt_topic_id
[
MAX_SERVERS
][
56
];
// struct mosquitto *mosq;
int
add_mqtt_create
(
const
char
*
host
,
int
port
,
int
clients_count
);
//mqtt初始化
int
mqtt_init
()
{
mosquitto_lib_init
();
// 这里创建mosq可以保留,也可以删除这一行,让创建放到 Mqtt_onnect 里
for
(
int
i
=
0
;
i
<
g_mqtt_cam_config_t
->
mqtt_count
;
i
++
){
int
erg
=
add_mqtt_create
(
g_mqtt_cam_config_t
->
mqtt_servers
[
i
],
BROKER_PORT
,
i
);
if
(
erg
!=
0
){
...
...
@@ -103,6 +104,8 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
// }
int
add_mqtt_create
(
const
char
*
host
,
int
port
,
int
clients_count
){
static
int
uuid_index
=
0
;
if
(
clients_count
>=
MAX_SERVERS
)
{
my_zlog_error
(
"Max server limit reached."
);
return
-
1
;
...
...
@@ -110,9 +113,19 @@ int add_mqtt_create(const char *host, int port, int clients_count){
mqttclient
*
client_t
=
&
g_clients_t
[
clients_count
];
strncpy
(
client_t
->
host
,
host
,
sizeof
(
client_t
->
host
)
-
1
);
client_t
->
port
=
port
;
//strncpy(client_t->client_id, client_id, sizeof(client_t->client_id)-1);
client_t
->
mosq
=
mosquitto_new
(
NULL
,
true
,
client_t
);
uuid_t
uuid
;
uuid_generate_time
(
uuid
);
// 获取UUIDv1
unsigned
short
time_part
=
(
uuid
[
0
]
<<
8
)
|
uuid
[
1
];
unsigned
short
mac_part
=
(
uuid
[
10
]
<<
8
)
|
uuid
[
11
];
// 组合成32位值(8字符十六进制)
snprintf
(
g_uuid_mqtt_topic_id
[
uuid_index
],
9
,
"%04x%04x"
,
time_part
,
mac_part
);
client_t
->
mosq
=
mosquitto_new
(
g_uuid_mqtt_topic_id
[
uuid_index
],
true
,
client_t
);
uuid_index
++
;
if
(
!
client_t
->
mosq
)
{
my_zlog_error
(
"mosquitto_new failed"
);
...
...
@@ -121,15 +134,20 @@ int add_mqtt_create(const char *host, int port, int clients_count){
mosquitto_int_option
(
client_t
->
mosq
,
MOSQ_OPT_PROTOCOL_VERSION
,
MQTT_PROTOCOL_V5
);
mosquitto_reconnect_delay_set
(
client_t
->
mosq
,
2
,
10
,
true
);
mosquitto_connect_callback_set
(
client_t
->
mosq
,
on_connect
);
mosquitto_message_callback_set
(
client_t
->
mosq
,
on_message
);
mosquitto_username_pw_set
(
client_t
->
mosq
,
USERNAME
,
PASSWORD
);
if
(
mosquitto_connect
(
client_t
->
mosq
,
host
,
port
,
60
)
!=
MOSQ_ERR_SUCCESS
)
{
int
rc
=
mosquitto_connect_async
(
client_t
->
mosq
,
host
,
port
,
60
);
if
(
rc
!=
MOSQ_ERR_SUCCESS
)
{
my_zlog_error
(
"mosquitto_connect failed"
);
my_zlog_warn
(
"Failed to connect to broker: %s"
,
mosquitto_strerror
(
rc
));
return
-
1
;
}
my_zlog_info
(
"add %s mqttserver success"
,
host
);
return
0
;
}
...
...
@@ -159,11 +177,11 @@ void mqtt_clean(){
int
mqtt_cycle
()
{
//非阻塞型
while
(
1
)
{
for
(
int
i
=
0
;
i
<
g_mqtt_cam_config_t
->
mqtt_count
;
i
++
){
int
rc
=
mosquitto_loop
(
g_clients_t
[
i
].
mosq
,
0
,
1
);
// 每秒检查一次
int
rc
=
mosquitto_loop
(
g_clients_t
[
i
].
mosq
,
10
0
,
1
);
// 每秒检查一次
if
(
rc
!=
MOSQ_ERR_SUCCESS
){
my_zlog_warn
(
"服务器 %s:%d 断开,尝试重连...
\n
"
,
my_zlog_warn
(
"服务器 %s:%d 断开,尝试重连..."
,
g_clients_t
[
i
].
host
,
g_clients_t
[
i
].
port
);
mosquitto_reconnect
(
g_clients_t
[
i
].
mosq
);
mosquitto_reconnect
(
g_clients_t
[
i
].
mosq
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment