Commit 6c0e0a2e authored by 957dd's avatar 957dd

Merge branch 'feature/change_http_mqtt_config' into 'master'

Feature/change http mqtt config See merge request !70
parents 98155083 47699a2f
...@@ -91,6 +91,7 @@ target_link_libraries(main PRIVATE ...@@ -91,6 +91,7 @@ target_link_libraries(main PRIVATE
crypto crypto
pthread pthread
dl dl
uuid
) )
# 安装规则 # 安装规则
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "common.h" #include "common.h"
#include "delay.h" #include "delay.h"
#include "device_wifi_change.h" #include "device_wifi_change.h"
#include "http_config_mqtt.h"
char g_current_ssid[SSID_MAX_LEN] = {0};//用于存现在已连接WiFi char g_current_ssid[SSID_MAX_LEN] = {0};//用于存现在已连接WiFi
char wifi_Last_ssid[SSID_MAX_LEN]= {0} ; char wifi_Last_ssid[SSID_MAX_LEN]= {0} ;
...@@ -108,7 +109,11 @@ void scan_wifi_json() { ...@@ -108,7 +109,11 @@ void scan_wifi_json() {
char *json_str = cJSON_Print(root); char *json_str = cJSON_Print(root);
if (json_str) { if (json_str) {
my_zlog_debug("%s", json_str); my_zlog_debug("%s", json_str);
mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(json_str), json_str, 0, false);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(json_str), json_str, 0, false);
}
free(json_str); free(json_str);
} }
cJSON_Delete(root); cJSON_Delete(root);
...@@ -383,7 +388,10 @@ void wifichange_sendmqtt(int wifi_status) { //改WiFi发送 ...@@ -383,7 +388,10 @@ void wifichange_sendmqtt(int wifi_status) { //改WiFi发送
char *payload = cJSON_PrintUnformatted(root); char *payload = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",payload); my_zlog_debug("%s",payload);
mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false);
}
cJSON_Delete(root); // 释放 cJSON 对象 cJSON_Delete(root); // 释放 cJSON 对象
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "delay.h" #include "delay.h"
#include "device_wifi_change.h" #include "device_wifi_change.h"
#include "device_wifi_manager.h" #include "device_wifi_manager.h"
#include "http_config_mqtt.h"
#define MAX_WIFI_ENTRIES 50 #define MAX_WIFI_ENTRIES 50
#define SSID_MAX_LENGTH 64 #define SSID_MAX_LENGTH 64
...@@ -24,7 +25,9 @@ void device_wifi_public_sendmqtt(int wifi_status) { ...@@ -24,7 +25,9 @@ void device_wifi_public_sendmqtt(int wifi_status) {
char *payload = cJSON_PrintUnformatted(root); char *payload = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",payload); my_zlog_debug("%s",payload);
mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false); for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false);
}
cJSON_Delete(root); // 释放 cJSON 对象 cJSON_Delete(root); // 释放 cJSON 对象
} }
...@@ -170,7 +173,11 @@ void device_send_saved_wifi(){ ...@@ -170,7 +173,11 @@ void device_send_saved_wifi(){
char *json_str = cJSON_Print(root); char *json_str = cJSON_Print(root);
if (json_str) { if (json_str) {
my_zlog_debug("%s", json_str); my_zlog_debug("%s", json_str);
mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(json_str), json_str, 0, false);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(json_str), json_str, 0, false);
}
free(json_str); free(json_str);
} }
cJSON_Delete(root); cJSON_Delete(root);
......
...@@ -10,7 +10,7 @@ char g_dev2app_topic[23]; ...@@ -10,7 +10,7 @@ char g_dev2app_topic[23];
char g_pure_topic[15]; char g_pure_topic[15];
char g_ser2dev_topic[23]; char g_ser2dev_topic[23];
bool g_device_name_exists=0; bool g_device_name_exists=0;//查找设备号状态机,如果id是不属于任何类型
/*提早声明*/ /*提早声明*/
int device_mqtt_topic_init(); int device_mqtt_topic_init();
......
...@@ -122,38 +122,20 @@ void *thread_open_browser(void *arg) { ...@@ -122,38 +122,20 @@ void *thread_open_browser(void *arg) {
//mqtt异常处理,断开自动重连,简单粗暴 //mqtt异常处理,断开自动重连,简单粗暴
void *thread_mqtt_reconnect(void *arg) { void *thread_mqtt_reconnect(void *arg) {
if (mqtt_init() != 0) { while(1){
my_zlog_fatal("mqtt_init failed"); if(mqtt_init() == 0){
return NULL; my_zlog_warn("mqtt success");
} break;
while (1) { }else {
if (mosq == NULL) { delay_ms(300);
mqtt_init();
if (!mosq) {
mqtt_clean(mosq);
my_zlog_error("Failed to create mosquitto client");
continue;
}
}
g_mqtt_grc = mqtt_create(mosq);//创建mqtt
if (g_mqtt_grc != 0) {
my_zlog_warn("mqtt fail ...");
mqtt_clean(mosq);
mosq = NULL;
continue; continue;
} }
g_mqtt_grc = mqtt_cycle(mosq); // 阻塞直到断开
if(g_mqtt_grc !=0 ){
my_zlog_warn("mqtt fail ...");
mqtt_clean(mosq);
mosq = NULL;
}
my_zlog_warn("mqtt disconnected ...");
} }
mqtt_clean(mosq);
mqtt_cycle();
mqtt_clean();
return NULL; return NULL;
} }
......
No preview for this file type
#ifndef TANK0202_CONTROL_H__ #ifndef TANK0202_CONTROL_H__
#define TANK0202_CONTROL_H__ #define TANK0202_CONTROL_H__
void tank0202_middle(); void tank0202_middle();
void tank0202_change(unsigned char *buf); void tank0202_change(unsigned char *buf);
......
...@@ -131,22 +131,22 @@ void tank_angle_limit_function(void *arg_gpio){ ...@@ -131,22 +131,22 @@ void tank_angle_limit_function(void *arg_gpio){
if (arg_gpio != NULL) { if (arg_gpio != NULL) {
free(arg_gpio); free(arg_gpio);
} }
my_zlog_debug("limit task started."); my_zlog_info("limit task started.");
while(1){ while(1){
int limit_status = angle_limit(); int limit_status = angle_limit();
if(limit_status==1) { if(limit_status==1) {
device_gpio_control(g_device_type,5,0); device_gpio_control(g_device_type,5,0);
my_zlog_debug("lift limit stop"); my_zlog_info("lift limit stop");
} }
else if(limit_status==2) { else if(limit_status==2) {
device_gpio_control(g_device_type,7,0); device_gpio_control(g_device_type,7,0);
my_zlog_debug("right limit stop"); my_zlog_info("right limit stop");
} }
else if(limit_status==0) { else if(limit_status==0) {
delay_ms(5); delay_ms(5);
limit_log_count++; limit_log_count++;
if(limit_log_count>=400){ if(limit_log_count>=400){
my_zlog_debug("limit stop"); my_zlog_info("limit stop");
limit_log_count=0; limit_log_count=0;
} }
...@@ -268,10 +268,10 @@ void public_pin_value(int pin,int value) { //引脚控制 ...@@ -268,10 +268,10 @@ void public_pin_value(int pin,int value) { //引脚控制
} }
if(value==1) { if(value==1) {
digitalWrite(pin, HIGH); digitalWrite(pin, HIGH);
my_zlog_debug("pin:%d,%d",pin,HIGH); my_zlog_info("pin:%d,%d",pin,HIGH);
}else if(value==0) { }else if(value==0) {
digitalWrite(pin, LOW); digitalWrite(pin, LOW);
my_zlog_debug("pin:%d,%d",pin,LOW); my_zlog_info("pin:%d,%d",pin,LOW);
} }
} }
...@@ -325,17 +325,17 @@ void public_pwm_value(int pin ,int value){ ...@@ -325,17 +325,17 @@ void public_pwm_value(int pin ,int value){
if(pin == 27){ if(pin == 27){
softPwmWrite(pin, shot_speed); softPwmWrite(pin, shot_speed);
my_zlog_debug("pwm:%d,%d",pin,shot_speed); my_zlog_info("pwm:%d,%d",pin,shot_speed);
} else { } else {
softPwmWrite(pin, shot_speed); softPwmWrite(pin, shot_speed);
my_zlog_debug("pwm:%d,%d",pin,shot_speed); my_zlog_info("pwm:%d,%d",pin,shot_speed);
} }
}else if(value==0) { }else if(value==0) {
softPwmWrite(pin, 0); softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin); my_zlog_info("pwm:%d,0",pin);
} }
my_zlog_debug("public pwm"); my_zlog_info("public pwm");
} }
...@@ -354,14 +354,14 @@ void tank0202_pwm_value(int pin,int value) { //软件陪我们控制调速 ...@@ -354,14 +354,14 @@ void tank0202_pwm_value(int pin,int value) { //软件陪我们控制调速
device_shoting_check(27,30); device_shoting_check(27,30);
} else { } else {
softPwmWrite(pin, 40); softPwmWrite(pin, 40);
my_zlog_debug("pwm:%d",pin); my_zlog_info("pwm:%d",pin);
} }
}else if(value==0) { }else if(value==0) {
softPwmWrite(pin, 0); softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin); my_zlog_info("pwm:%d,0",pin);
} }
my_zlog_debug("tank0202 pwm"); my_zlog_info("tank0202 pwm");
} }
void tank0203_pwm_value(int pin,int value) { //软件陪我们控制调速 void tank0203_pwm_value(int pin,int value) { //软件陪我们控制调速
...@@ -379,14 +379,14 @@ void tank0203_pwm_value(int pin,int value) { //软件陪我们控制调速 ...@@ -379,14 +379,14 @@ void tank0203_pwm_value(int pin,int value) { //软件陪我们控制调速
device_shoting_check(27,30); device_shoting_check(27,30);
} else { } else {
softPwmWrite(pin, 35); softPwmWrite(pin, 35);
my_zlog_debug("pwm:%d",pin); my_zlog_info("pwm:%d",pin);
} }
}else if(value==0) { }else if(value==0) {
softPwmWrite(pin, 0); softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin); my_zlog_info("pwm:%d,0",pin);
} }
my_zlog_debug("tank0203 pwm"); my_zlog_info("tank0203 pwm");
} }
void tank0204_pwm_value(int pin,int value){ void tank0204_pwm_value(int pin,int value){
...@@ -404,14 +404,14 @@ void tank0204_pwm_value(int pin,int value){ ...@@ -404,14 +404,14 @@ void tank0204_pwm_value(int pin,int value){
device_shoting_check(27,30); device_shoting_check(27,30);
} else { } else {
softPwmWrite(pin, 35); softPwmWrite(pin, 35);
my_zlog_debug("pwm:%d",pin); my_zlog_info("pwm:%d",pin);
} }
}else if(value==0) { }else if(value==0) {
softPwmWrite(pin, 0); softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin); my_zlog_info("pwm:%d,0",pin);
} }
my_zlog_debug("tank0204 pwm"); my_zlog_info("tank0204 pwm");
} }
void tank0206_pwm_value(int pin,int value) { //软件陪我们控制调速 void tank0206_pwm_value(int pin,int value) { //软件陪我们控制调速
...@@ -429,15 +429,15 @@ void tank0206_pwm_value(int pin,int value) { //软件陪我们控制调速 ...@@ -429,15 +429,15 @@ void tank0206_pwm_value(int pin,int value) { //软件陪我们控制调速
softPwmWrite(26, 35); softPwmWrite(26, 35);
} else { } else {
softPwmWrite(pin, 35); softPwmWrite(pin, 35);
my_zlog_debug("pwm:%d",pin); my_zlog_info("pwm:%d",pin);
} }
}else if(value==0) { }else if(value==0) {
if(pin == 27) softPwmWrite(26, 0); if(pin == 27) softPwmWrite(26, 0);
softPwmWrite(pin, 0); softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin); my_zlog_info("pwm:%d,0",pin);
} }
my_zlog_debug("tank0206 pwm"); my_zlog_info("tank0206 pwm");
} }
void ship0301_pwm_value(int pin,int value) { //软件陪我们控制调速 void ship0301_pwm_value(int pin,int value) { //软件陪我们控制调速
...@@ -455,15 +455,15 @@ void ship0301_pwm_value(int pin,int value) { //软件陪我们控制调速 ...@@ -455,15 +455,15 @@ void ship0301_pwm_value(int pin,int value) { //软件陪我们控制调速
softPwmWrite(26, 35); softPwmWrite(26, 35);
} else { } else {
softPwmWrite(pin, 35); softPwmWrite(pin, 35);
my_zlog_debug("pwm:%d",pin); my_zlog_info("pwm:%d",pin);
} }
}else if(value==0) { }else if(value==0) {
if(pin == 27) softPwmWrite(26, 0); if(pin == 27) softPwmWrite(26, 0);
softPwmWrite(pin, 0); softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin); my_zlog_info("pwm:%d,0",pin);
} }
my_zlog_debug("tank0301 pwm"); my_zlog_info("tank0301 pwm");
} }
...@@ -479,17 +479,17 @@ void dog0501_pwm_value(int pin,int value) { //软件陪我们控制调速 ...@@ -479,17 +479,17 @@ void dog0501_pwm_value(int pin,int value) { //软件陪我们控制调速
if(value==1) { if(value==1) {
if(pin == 27){ if(pin == 27){
softPwmWrite(pin, 50); softPwmWrite(pin, 90);
} else { } else {
softPwmWrite(pin, 35); softPwmWrite(pin, 35);
my_zlog_debug("pwm:%d",pin); my_zlog_info("pwm:%d",pin);
} }
}else if(value==0) { }else if(value==0) {
softPwmWrite(pin, 0); softPwmWrite(pin, 0);
my_zlog_debug("pwm:%d,0",pin); my_zlog_info("pwm:%d,0",pin);
} }
my_zlog_debug("dog0501 pwm"); my_zlog_info("dog0501 pwm");
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "audioplay.h" #include "audioplay.h"
#include "device_identity.h" #include "device_identity.h"
#include "mqtt_init.h" #include "mqtt_init.h"
#include "http_config_mqtt.h"
int audio_status=7; int audio_status=7;
char urlbuf[512]; char urlbuf[512];
...@@ -71,7 +72,10 @@ void audioplay_send_mqtt() { ...@@ -71,7 +72,10 @@ void audioplay_send_mqtt() {
// 将 JSON 对象转换为字符串 // 将 JSON 对象转换为字符串
char* json_string = cJSON_PrintUnformatted(root); char* json_string = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",json_string); my_zlog_debug("%s",json_string);
mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(json_string), json_string, 0, false);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(json_string), json_string, 0, false);
}
// 释放 JSON 对象内存 // 释放 JSON 对象内存
cJSON_Delete(root); cJSON_Delete(root);
} }
......
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
#include <time.h> // 时间处理 #include <time.h> // 时间处理
#include <stddef.h> #include <stddef.h>
#include <uuid/uuid.h>
#include "mosquitto.h" #include "mosquitto.h"
#include "cjson/cJSON.h" #include "cjson/cJSON.h"
......
...@@ -6,41 +6,11 @@ ...@@ -6,41 +6,11 @@
char gwebcam[2048];//存放启动火狐网站命令 char gwebcam[2048];//存放启动火狐网站命令
int opencamsh_china(){ int opencamsh() {
const char* url = "https://jywy.yd-ss.com?dev=";
char urls[50];
sprintf(urls,"%s%s",url,mqtt_topic_pure_number());
//setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo
sprintf(gwebcam,"su - orangepi -c \"chromium-browser --use-fake-ui-for-media-stream %s\"",urls);
system(gwebcam);
my_zlog_debug("%s",gwebcam);
my_zlog_debug("open cam");
return 0;
}
int opencamsh_abroad(){
const char* url = "https://video.luckycar.top/?dev=";
char urls[50];
sprintf(urls,"%s%s",url,mqtt_topic_pure_number());
//setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo
sprintf(gwebcam,"su - orangepi -c \"chromium-browser --use-fake-ui-for-media-stream %s\"",urls);
system(gwebcam);
my_zlog_debug("%s",gwebcam);
my_zlog_debug("open cam");
return 0;
} char urls[512];
int opencamsh_zd(){
const char* url = "https://video.yzwlkj2025.com?dev=";
char urls[50];
sprintf(urls,"%s%s",url,mqtt_topic_pure_number()); sprintf(urls,"%s%s",g_mqtt_cam_config_t->videourl,mqtt_topic_pure_number());
//setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo //setenv("DISPLAY", ":0", 1);//设置环境变量https://jywy.yd-ss.com?dev=controcar0004 --new-window sudo
sprintf(gwebcam,"su - orangepi -c \"chromium-browser --use-fake-ui-for-media-stream %s\"",urls); sprintf(gwebcam,"su - orangepi -c \"chromium-browser --use-fake-ui-for-media-stream %s\"",urls);
...@@ -48,26 +18,6 @@ int opencamsh_zd(){ ...@@ -48,26 +18,6 @@ int opencamsh_zd(){
my_zlog_debug("%s",gwebcam); my_zlog_debug("%s",gwebcam);
my_zlog_debug("open cam"); my_zlog_debug("open cam");
return 0; return 0;
}
int opencamsh() {
#if BROWSER_MODE == 1
// 当MODE为1时的代码
my_zlog_info("Mode 1: 执行代码A");
return opencamsh_china();
#elif BROWSER_MODE == 2
// 当MODE为2时的代码
my_zlog_info("Mode 2: 执行代码B");
return opencamsh_abroad();
#elif BROWSER_MODE == 3
// 当MODE为2时的代码
my_zlog_info("Mode 3: 执行代码C");
return opencamsh_zd();
#else
my_zlog_error("未知的 WARM_MODE: %d", BROWSER_MODE);
return -1;
#endif
} }
......
...@@ -7,6 +7,16 @@ ...@@ -7,6 +7,16 @@
DEVICE_MQTTCONFIG g_mqtt_cam_config; DEVICE_MQTTCONFIG g_mqtt_cam_config;
DEVICE_MQTTCONFIG *g_mqtt_cam_config_t = &g_mqtt_cam_config; DEVICE_MQTTCONFIG *g_mqtt_cam_config_t = &g_mqtt_cam_config;
/*使用默认的mqtt服务器地址和WEBRTC的信令服务器*/
void default_device_mqtt_cam(){
strncpy(g_mqtt_cam_config_t->mqtt_servers[0], DEFAULT_MQTT_BROKER_ADDRESS , sizeof(g_mqtt_cam_config_t->mqtt_servers[0]) - 1);
g_mqtt_cam_config_t->mqtt_servers[0][sizeof(g_mqtt_cam_config_t->mqtt_servers[0]) - 1] = '\0';
g_mqtt_cam_config_t->mqtt_count =1;
strncpy(g_mqtt_cam_config_t->videourl, DEVICE_DEFAULT_VIDEO , sizeof(g_mqtt_cam_config_t->videourl) - 1);
g_mqtt_cam_config_t->videourl[sizeof(g_mqtt_cam_config_t->videourl) - 1] = '\0';
}
int parse_device_config(const char *json_str) { int parse_device_config(const char *json_str) {
// 解析 JSON // 解析 JSON
cJSON *root = cJSON_Parse(json_str); cJSON *root = cJSON_Parse(json_str);
...@@ -24,6 +34,9 @@ int parse_device_config(const char *json_str) { ...@@ -24,6 +34,9 @@ int parse_device_config(const char *json_str) {
my_zlog_debug("状态码: %d", code->valueint); my_zlog_debug("状态码: %d", code->valueint);
if(code->valueint!=200){ if(code->valueint!=200){
my_zlog_error("code error"); my_zlog_error("code error");
default_device_mqtt_cam();
return -1; return -1;
} }
} }
...@@ -39,13 +52,13 @@ int parse_device_config(const char *json_str) { ...@@ -39,13 +52,13 @@ int parse_device_config(const char *json_str) {
int i = 0; int i = 0;
cJSON_ArrayForEach(item, mqtt) { cJSON_ArrayForEach(item, mqtt) {
if (cJSON_IsString(item)) { if (cJSON_IsString(item)) {
my_zlog_debug(" [%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); 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 // 4. 提取 api 和 videoUrl
...@@ -73,12 +86,15 @@ int deviceconfig_mqtt_video_init() { ...@@ -73,12 +86,15 @@ int deviceconfig_mqtt_video_init() {
if (!curl) { if (!curl) {
fprintf(stderr, "Failed to initialize CURL\n"); fprintf(stderr, "Failed to initialize CURL\n");
my_zlog_error("Failed to initialize CURL"); my_zlog_error("Failed to initialize CURL");
default_device_mqtt_cam();
return -1; return -1;
} }
// 构造请求 URL // 构造请求 URL
char url[512]; char url[512];
snprintf(url, sizeof(url), "http://192.168.0.112:8111/device/getConfig?deviceNo=%s", mqtt_topic_pure_number()); snprintf(url, sizeof(url), "https://fcrs-api.yd-ss.com/device/getConfig?deviceNo=%s", mqtt_topic_pure_number());
my_zlog_info("url:%s",url); my_zlog_info("url:%s",url);
// 设置 CURL 选项 // 设置 CURL 选项
...@@ -92,6 +108,9 @@ int deviceconfig_mqtt_video_init() { ...@@ -92,6 +108,9 @@ int deviceconfig_mqtt_video_init() {
if (res != CURLE_OK) { if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
my_zlog_error("curl_easy_perform() failed"); my_zlog_error("curl_easy_perform() failed");
default_device_mqtt_cam();
return -1; return -1;
} else { } else {
// 解析返回的 JSON 数据 // 解析返回的 JSON 数据
......
...@@ -3,14 +3,16 @@ ...@@ -3,14 +3,16 @@
#include "common.h" #include "common.h"
#define DEFAULT_MQTT_BROKER_ADDRESS "119.45.167.177"
#define DEVICE_DEFAULT_VIDEO "https://jywy.yd-ss.com?dev="
typedef struct{ typedef struct{
char videourl[512]; char videourl[512];
char mqtt_servers[10][256]; char mqtt_servers[10][256];
int mqtt_count; int mqtt_count;
}DEVICE_MQTTCONFIG; }DEVICE_MQTTCONFIG;
extern DEVICE_MQTTCONFIG *g_mqtt_cam_config_t ; extern DEVICE_MQTTCONFIG *g_mqtt_cam_config_t;
/*mqtt服务器和视频流地址初始化*/ /*mqtt服务器和视频流地址初始化*/
int http_mqtt_video_init(); int http_mqtt_video_init();
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "tank_angle.h" #include "tank_angle.h"
#include "gpio_control.h" #include "gpio_control.h"
#include "http_consolepush.h" #include "http_consolepush.h"
#include "http_config_mqtt.h"
int g_heartbeat_count=0; int g_heartbeat_count=0;
...@@ -77,8 +78,11 @@ void heartbeat_send() { ...@@ -77,8 +78,11 @@ void heartbeat_send() {
char *payload = cJSON_PrintUnformatted(root); char *payload = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",payload); my_zlog_debug("%s",payload);
mosquitto_publish(mosq, NULL, mqtt_topic_dev2app_number(), strlen(payload), payload, 0, false); for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false); mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_dev2app_number(), strlen(payload), payload, 0, false);
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false);
}
cJSON_Delete(root); // 释放 cJSON 对象 cJSON_Delete(root); // 释放 cJSON 对象
} }
...@@ -100,7 +104,9 @@ void angle_mqtt_send() { ...@@ -100,7 +104,9 @@ void angle_mqtt_send() {
angle_i=0; angle_i=0;
} }
if(fabs(rounded_angle - record_angle_t) >3){ if(fabs(rounded_angle - record_angle_t) >3){
mosquitto_publish(mosq, NULL, TOPIC_send_angle, strlen(payload), payload, 0, false); for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, TOPIC_send_angle, strlen(payload), payload, 0, false);
}
} }
record_angle_t=rounded_angle; record_angle_t=rounded_angle;
......
...@@ -6,22 +6,31 @@ ...@@ -6,22 +6,31 @@
#include "mylog.h" #include "mylog.h"
#include "http_config_mqtt.h" #include "http_config_mqtt.h"
struct mosquitto *mosq; 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);
//struct mosquitto *mosq1;
//struct mosquitto *mosq2;
//mqtt初始化 //mqtt初始化
int mqtt_init() { int mqtt_init() {
mosquitto_lib_init(); mosquitto_lib_init();
// 这里创建mosq可以保留,也可以删除这一行,让创建放到 Mqtt_onnect 里 // 这里创建mosq可以保留,也可以删除这一行,让创建放到 Mqtt_onnect 里
mosq = mosquitto_new(NULL, true, NULL); for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
if (!mosq) { int erg= add_mqtt_create(g_mqtt_cam_config_t->mqtt_servers[i],BROKER_PORT,i);
my_zlog_fatal("Failed to create Mosquitto client"); if(erg !=0){
return -1;
}
}
if (!g_clients_t) {
my_zlog_error("Failed to create Mosquitto client");
return -1; return -1;
} }
mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); //mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
//mosquitto_int_option(mosq1, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); //mosquitto_int_option(mosq1, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
//mosquitto_int_option(mosq2, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); //mosquitto_int_option(mosq2, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
return 0; return 0;
...@@ -29,13 +38,20 @@ int mqtt_init() { ...@@ -29,13 +38,20 @@ int mqtt_init() {
//回调函数 //回调函数
void on_connect(struct mosquitto *mosq, void *obj, int rc) { void on_connect(struct mosquitto *mosq, void *obj, int rc) {
mqttclient *client_t = (mqttclient*)obj;
if (rc == 0) { if (rc == 0) {
my_zlog_debug("Connected to broker"); my_zlog_info("Connected to broker");
my_zlog_info("[Connected] %s:%d (%s)",
client_t->host, client_t->port, client_t->client_id);
mosquitto_subscribe(mosq, NULL, mqtt_topic_app2dev_number(), 0); mosquitto_subscribe(mosq, NULL, mqtt_topic_app2dev_number(), 0);
mosquitto_subscribe(mosq, NULL, mqtt_topic_ser2dev_number(), 0); mosquitto_subscribe(mosq, NULL, mqtt_topic_ser2dev_number(), 0);
} else { } else {
fprintf(stderr, "Connection failed with code %d\n", rc); fprintf(stderr, "Connection failed with code %d\n", rc);
my_zlog_fatal("Connection failed"); my_zlog_fatal("[Connection Failed] %s:%d rc=%d",
client_t->host, client_t->port, rc);
} }
} }
...@@ -43,6 +59,12 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) { ...@@ -43,6 +59,12 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) {
//消息回环函数 //消息回环函数
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) { void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) {
mqttclient *client_t = (mqttclient*)obj;
my_zlog_info("[Message from %s:%d] topic=%s payload=%s",
client_t->host, client_t->port,
message->topic, (char*)message->payload);
// 确保消息有效 // 确保消息有效
if (message->payload && message->payloadlen) { // 将消息内容转换为字符串 if (message->payload && message->payloadlen) { // 将消息内容转换为字符串
char *payload_str = (char *)malloc(message->payloadlen + 1); char *payload_str = (char *)malloc(message->payloadlen + 1);
...@@ -56,7 +78,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -56,7 +78,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
// 解析 JSON // 解析 JSON
cJSON *json = cJSON_Parse(payload_str); cJSON *json = cJSON_Parse(payload_str);
if(json != NULL){ if(json != NULL){
if(device_message_receive(json)==0) my_zlog_debug("进入设备接收mqtt"); if(device_message_receive(json)==0) my_zlog_info("进入设备接收mqtt");
} }
// 释放 payload 字符串 // 释放 payload 字符串
...@@ -65,48 +87,107 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -65,48 +87,107 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
} }
} }
// 返回连接结果,0成功,非0失败 // 返回连接结果,0成功,非0失败,单个使用函数
int mqtt_create(struct mosquitto *mosq) { // int mqtt_create(struct mosquitto *mosq) {
mosquitto_reconnect_delay_set(mosq, 2, 10, true); // mosquitto_reconnect_delay_set(mosq, 2, 10, true);
mosquitto_connect_callback_set(mosq, on_connect); // mosquitto_connect_callback_set(mosq, on_connect);
mosquitto_message_callback_set(mosq, on_message); // mosquitto_message_callback_set(mosq, on_message);
mosquitto_username_pw_set(mosq, USERNAME, PASSWORD); // mosquitto_username_pw_set(mosq, USERNAME, PASSWORD);
int rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60); // int rc = mosquitto_connect(mosq, BROKER_ADDRESS, BROKER_PORT, 60);
if (rc != MOSQ_ERR_SUCCESS) { // if (rc != MOSQ_ERR_SUCCESS) {
my_zlog_warn("Failed to connect to broker: %s", mosquitto_strerror(rc)); // my_zlog_warn("Failed to connect to broker: %s", mosquitto_strerror(rc));
// 不销毁 mosq,等待调用者处理重连 // // 不销毁 mosq,等待调用者处理重连
return rc; // return rc;
// }
// return 0; // 成功
// }
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;
} }
return 0; // 成功 mqttclient *client_t = &g_clients_t[clients_count];
} strncpy(client_t->host, host, sizeof(client_t->host)-1);
client_t->port = port;
//阻塞型 uuid_t uuid;
int mqtt_cycle(struct mosquitto *mosq) { uuid_generate_time(uuid); // 获取UUIDv1
// 这个函数会阻塞直到连接断开或错误返回
int rc = mosquitto_loop_forever(mosq, -1, 1); unsigned short time_part = (uuid[0] << 8) | uuid[1];
if (rc != MOSQ_ERR_SUCCESS) { unsigned short mac_part = (uuid[10] << 8) | uuid[11];
my_zlog_warn("mqtt loop error: %s", mosquitto_strerror(rc));
return rc; // 组合成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");
return -1;
} }
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);
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;
} }
// int mqtt_cycle(struct mosquitto *mosq) {//非阻塞型 //阻塞型
// int rc; // int mqtt_cycle(struct mosquitto *mosq) {
// while (1) { // // 这个函数会阻塞直到连接断开或错误返回
// rc = mosquitto_loop(mosq, 100, 1); // 每秒检查一次 // int rc = mosquitto_loop_forever(mosq, -1, 1);
// if (rc != MOSQ_ERR_SUCCESS) { // if (rc != MOSQ_ERR_SUCCESS) {
// my_zlog_warn("mosquitto loop error: %s,(code: %d)", mosquitto_strerror(rc),rc); // my_zlog_warn("mqtt loop error: %s", mosquitto_strerror(rc));
// return -1; // 跳出循环,重新连接 // return rc;
// }
// Delay_Ms(0, 3); // 防止空转
// } // }
// } // }
//mqtt清理 //mqtt清理
void mqtt_clean(struct mosquitto *mosq){ void mqtt_clean(){
// 清理 // 清理
mosquitto_destroy(mosq); for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_disconnect(g_clients_t[i].mosq);
mosquitto_destroy(g_clients_t[i].mosq);
}
mosquitto_lib_cleanup(); mosquitto_lib_cleanup();
my_zlog_notice("清理成功"); my_zlog_notice("清理成功");
} }
\ No newline at end of file
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, 100, 1); // 每秒检查一次
if(rc != MOSQ_ERR_SUCCESS){
my_zlog_warn("服务器 %s:%d 断开,尝试重连...",
g_clients_t[i].host, g_clients_t[i].port);
mosquitto_reconnect(g_clients_t[i].mosq);
}
}
delay_ms(300); // 防止空转
}
return 0;
}
...@@ -6,35 +6,32 @@ ...@@ -6,35 +6,32 @@
//extern ThreadPool *pool; //extern ThreadPool *pool;
/*2为泰国1为国内,3为老中东,4为中东最新mqtt地址*/ #define MAX_SERVERS 10
#define MQTT_IPMODE 1 // 或通过编译选项 -DMODE=1 指定
typedef struct {
#if MQTT_IPMODE == 1 struct mosquitto *mosq;
#define BROKER_ADDRESS "119.45.167.177" char host[128];
#elif MQTT_IPMODE == 2 int port;
#define BROKER_ADDRESS "mqtt.luckycar.top" char client_id[64];
#elif MQTT_IPMODE == 3 } mqttclient;
#define BROKER_ADDRESS "47.107.64.23"
#elif MQTT_IPMODE == 4 extern mqttclient g_clients_t[MAX_SERVERS];
#define BROKER_ADDRESS "me-mqtt.yzwlkj2025.com"
#else
#define BROKER_ADDRESS "127.0.0.1" // 默认地址
#endif
//#define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883 #define BROKER_PORT 1883
#define USERNAME "admin" // 替换为你的用户名 #define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码 #define PASSWORD "admin" // 替换为你的密码
extern struct mosquitto *mosq;//创建客服端 extern struct mosquitto *mosq;//创建客服端
extern int gPwmCount; // 计数 extern int gPwmCount; // 计数
extern int gmessage_type;//message消息值 extern int gmessage_type;//message消息值
int mqtt_init();//mqtt初始化 int mqtt_init();//mqtt初始化
int mqtt_create(struct mosquitto *mosq);//创建mqtt客服端 //void on_connect(struct mosquitto *mosq, void *obj, int rc);//回调函数
void on_connect(struct mosquitto *mosq, void *obj, int rc);//回调函数 //void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message);//消息回调函数
void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message);//消息回调函数
int mqtt_cycle(struct mosquitto *mosq);//循环 int mqtt_cycle();//循环
void mqtt_clean(struct mosquitto *mosq);//清理
//mqtt清理
void mqtt_clean();
#endif #endif
\ No newline at end of file
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "device_identity.h" #include "device_identity.h"
#include "mqtt_init.h" #include "mqtt_init.h"
#include "mylog.h" #include "mylog.h"
#include "http_config_mqtt.h"
/*TRUE为打开验证,FALSE为关闭验证*/ /*TRUE为打开验证,FALSE为关闭验证*/
bool VERIFIED_MODE=FALSE; bool VERIFIED_MODE=FALSE;
...@@ -70,7 +71,10 @@ int send_jwtser(const char *token){ ...@@ -70,7 +71,10 @@ int send_jwtser(const char *token){
char *payload = cJSON_PrintUnformatted(root); char *payload = cJSON_PrintUnformatted(root);
my_zlog_debug("%s",payload); my_zlog_debug("%s",payload);
mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false);
for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false);
}
free(only_Id); free(only_Id);
cJSON_Delete(root); // 释放 cJSON 对象 cJSON_Delete(root); // 释放 cJSON 对象
...@@ -204,7 +208,7 @@ int message_sendopen_verify(){ ...@@ -204,7 +208,7 @@ int message_sendopen_verify(){
cJSON *body = cJSON_CreateObject(); cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject(); cJSON *head = cJSON_CreateObject();
cJSON_AddStringToObject(body, "verify_status", 0); cJSON_AddNumberToObject(body, "verify_status", 0);
cJSON_AddNumberToObject(head, "message_type",3013); cJSON_AddNumberToObject(head, "message_type",3013);
...@@ -220,7 +224,9 @@ int message_sendopen_verify(){ ...@@ -220,7 +224,9 @@ int message_sendopen_verify(){
my_zlog_debug("%s",payload); my_zlog_debug("%s",payload);
mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false); for(int i=0;i<g_mqtt_cam_config_t->mqtt_count;i++){
mosquitto_publish(g_clients_t[i].mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false);
}
cJSON_Delete(root); // 释放 cJSON 对象 cJSON_Delete(root); // 释放 cJSON 对象
......
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