Commit 438d9a5e authored by 957dd's avatar 957dd

3

parent b0ceaf43
...@@ -296,6 +296,8 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o ...@@ -296,6 +296,8 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o
/usr/include/aarch64-linux-gnu/bits/local_lim.h /usr/include/aarch64-linux-gnu/bits/local_lim.h
/usr/include/linux/limits.h /usr/include/linux/limits.h
/usr/include/aarch64-linux-gnu/bits/posix2_lim.h /usr/include/aarch64-linux-gnu/bits/posix2_lim.h
/usr/include/aarch64-linux-gnu/bits/xopen_lim.h
/usr/include/aarch64-linux-gnu/bits/uio_lim.h
/usr/include/aarch64-linux-gnu/sys/time.h /usr/include/aarch64-linux-gnu/sys/time.h
/usr/include/aarch64-linux-gnu/curl/easy.h /usr/include/aarch64-linux-gnu/curl/easy.h
/usr/include/aarch64-linux-gnu/curl/multi.h /usr/include/aarch64-linux-gnu/curl/multi.h
...@@ -311,7 +313,6 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o ...@@ -311,7 +313,6 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o
/home/orangepi/car/master/include/log.h /home/orangepi/car/master/include/log.h
/home/orangepi/car/master/include/delay.h /home/orangepi/car/master/include/delay.h
/home/orangepi/car/master/device_judg/device/device_change.h /home/orangepi/car/master/device_judg/device/device_change.h
/home/orangepi/car/master/include/download.h
CMakeFiles/main.dir/device_judg/device/pao.c.o CMakeFiles/main.dir/device_judg/device/pao.c.o
/home/orangepi/car/master/device_judg/device/pao.c /home/orangepi/car/master/device_judg/device/pao.c
...@@ -2040,6 +2041,7 @@ CMakeFiles/main.dir/src/mqtt.c.o ...@@ -2040,6 +2041,7 @@ CMakeFiles/main.dir/src/mqtt.c.o
/usr/include/cjson/cJSON.h /usr/include/cjson/cJSON.h
/home/orangepi/car/master/system/sensor/warm.h /home/orangepi/car/master/system/sensor/warm.h
/home/orangepi/car/master/include/fileopen.h /home/orangepi/car/master/include/fileopen.h
/home/orangepi/car/master/device_judg/device/device_change.h
/home/orangepi/car/master/device_judg/judg/judg.h /home/orangepi/car/master/device_judg/judg/judg.h
CMakeFiles/main.dir/src/opensh.c.o CMakeFiles/main.dir/src/opensh.c.o
......
...@@ -294,6 +294,8 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o: ../device_judg/device/ ...@@ -294,6 +294,8 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o: ../device_judg/device/
/usr/include/aarch64-linux-gnu/bits/local_lim.h \ /usr/include/aarch64-linux-gnu/bits/local_lim.h \
/usr/include/linux/limits.h \ /usr/include/linux/limits.h \
/usr/include/aarch64-linux-gnu/bits/posix2_lim.h \ /usr/include/aarch64-linux-gnu/bits/posix2_lim.h \
/usr/include/aarch64-linux-gnu/bits/xopen_lim.h \
/usr/include/aarch64-linux-gnu/bits/uio_lim.h \
/usr/include/aarch64-linux-gnu/sys/time.h \ /usr/include/aarch64-linux-gnu/sys/time.h \
/usr/include/aarch64-linux-gnu/curl/easy.h \ /usr/include/aarch64-linux-gnu/curl/easy.h \
/usr/include/aarch64-linux-gnu/curl/multi.h \ /usr/include/aarch64-linux-gnu/curl/multi.h \
...@@ -308,8 +310,7 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o: ../device_judg/device/ ...@@ -308,8 +310,7 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o: ../device_judg/device/
../system/gpio/gpio_common.h \ ../system/gpio/gpio_common.h \
../include/log.h \ ../include/log.h \
../include/delay.h \ ../include/delay.h \
../device_judg/device/device_change.h \ ../device_judg/device/device_change.h
../include/download.h
CMakeFiles/main.dir/device_judg/device/pao.c.o: ../device_judg/device/pao.c \ CMakeFiles/main.dir/device_judg/device/pao.c.o: ../device_judg/device/pao.c \
/usr/include/stdc-predef.h \ /usr/include/stdc-predef.h \
...@@ -2027,6 +2028,7 @@ CMakeFiles/main.dir/src/mqtt.c.o: ../src/mqtt.c \ ...@@ -2027,6 +2028,7 @@ CMakeFiles/main.dir/src/mqtt.c.o: ../src/mqtt.c \
/usr/include/cjson/cJSON.h \ /usr/include/cjson/cJSON.h \
../system/sensor/warm.h \ ../system/sensor/warm.h \
../include/fileopen.h \ ../include/fileopen.h \
../device_judg/device/device_change.h \
../device_judg/judg/judg.h ../device_judg/judg/judg.h
CMakeFiles/main.dir/src/opensh.c.o: ../src/opensh.c \ CMakeFiles/main.dir/src/opensh.c.o: ../src/opensh.c \
...@@ -6632,10 +6634,6 @@ CMakeFiles/main.dir/zlog/src/zlog.c.o: ../zlog/src/zlog.c \ ...@@ -6632,10 +6634,6 @@ CMakeFiles/main.dir/zlog/src/zlog.c.o: ../zlog/src/zlog.c \
../device_judg/device/device_change.h: ../device_judg/device/device_change.h:
../include/download.h:
../system/sensor/heat.h:
../device_judg/device/pao.c: ../device_judg/device/pao.c:
../include/ip.h: ../include/ip.h:
...@@ -6652,6 +6650,10 @@ CMakeFiles/main.dir/zlog/src/zlog.c.o: ../zlog/src/zlog.c \ ...@@ -6652,6 +6650,10 @@ CMakeFiles/main.dir/zlog/src/zlog.c.o: ../zlog/src/zlog.c \
../src/download.c: ../src/download.c:
../include/download.h:
../system/sensor/heat.h:
/usr/include/aarch64-linux-gnu/bits/getopt_posix.h: /usr/include/aarch64-linux-gnu/bits/getopt_posix.h:
../src/fileopen.c: ../src/fileopen.c:
......
...@@ -116,6 +116,8 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o: \ ...@@ -116,6 +116,8 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o: \
/usr/include/aarch64-linux-gnu/bits/local_lim.h \ /usr/include/aarch64-linux-gnu/bits/local_lim.h \
/usr/include/linux/limits.h \ /usr/include/linux/limits.h \
/usr/include/aarch64-linux-gnu/bits/posix2_lim.h \ /usr/include/aarch64-linux-gnu/bits/posix2_lim.h \
/usr/include/aarch64-linux-gnu/bits/xopen_lim.h \
/usr/include/aarch64-linux-gnu/bits/uio_lim.h \
/usr/include/aarch64-linux-gnu/sys/time.h \ /usr/include/aarch64-linux-gnu/sys/time.h \
/usr/include/aarch64-linux-gnu/curl/easy.h \ /usr/include/aarch64-linux-gnu/curl/easy.h \
/usr/include/aarch64-linux-gnu/curl/multi.h \ /usr/include/aarch64-linux-gnu/curl/multi.h \
...@@ -129,5 +131,4 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o: \ ...@@ -129,5 +131,4 @@ CMakeFiles/main.dir/device_judg/device/device_change.c.o: \
/home/orangepi/car/master/system/gpio/gpio_common.h \ /home/orangepi/car/master/system/gpio/gpio_common.h \
/home/orangepi/car/master/include/log.h \ /home/orangepi/car/master/include/log.h \
/home/orangepi/car/master/include/delay.h \ /home/orangepi/car/master/include/delay.h \
/home/orangepi/car/master/device_judg/device/device_change.h \ /home/orangepi/car/master/device_judg/device/device_change.h
/home/orangepi/car/master/include/download.h
...@@ -141,4 +141,5 @@ CMakeFiles/main.dir/src/mqtt.c.o: /home/orangepi/car/master/src/mqtt.c \ ...@@ -141,4 +141,5 @@ CMakeFiles/main.dir/src/mqtt.c.o: /home/orangepi/car/master/src/mqtt.c \
/usr/include/cjson/cJSON.h \ /usr/include/cjson/cJSON.h \
/home/orangepi/car/master/system/sensor/warm.h \ /home/orangepi/car/master/system/sensor/warm.h \
/home/orangepi/car/master/include/fileopen.h \ /home/orangepi/car/master/include/fileopen.h \
/home/orangepi/car/master/device_judg/device/device_change.h \
/home/orangepi/car/master/device_judg/judg/judg.h /home/orangepi/car/master/device_judg/judg/judg.h
...@@ -2,5 +2,5 @@ ...@@ -2,5 +2,5 @@
#define PROJECT_VERSION_MINOR 1 #define PROJECT_VERSION_MINOR 1
#define PROJECT_VERSION_PATCH 0 #define PROJECT_VERSION_PATCH 0
#define GIT_HASH "" #define GIT_HASH ""
#define BUILD_TIMESTAMP "2025-05-08T12:02:23" #define BUILD_TIMESTAMP "2025-05-10T08:31:49"
#define BUILD_USER "orangepi" #define BUILD_USER "orangepi"
No preview for this file type
#define _XOPEN_SOURCE 700
#define _DEFAULT_SOURCE
#include "common.h" #include "common.h"
#include "device_change.h" #include "device_change.h"
#include "download.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
char *DEVICE_NAME_FILE="/home/orangepi/car/master/Deviceld.txt"; // 判断文件是否存在
int file_exists(const char *path) {
// 检查并创建日志目录 return access(path, F_OK) == 0;
static int create_devicename_directory_if_not_exists(const char *path) { }
char dir_path[256] = {0};
const char *p = path; // 删除文件
char *q = dir_path; int remove_file(const char *path) {
if (remove(path) == 0) {
// 提取目录部分 return 1;
while (*p != '\0') { } else {
*q = *p; my_zlog_warn("删除文件失败: %s", path);
if (*p == '/' && *(p+1) != '\0') { return 0;
// 检查并创建每一级目录 }
if (access(dir_path, F_OK) != 0) { }
if (mkdir(dir_path, 0755) != 0) {
fprintf(stderr, "Failed to create directory: %s\n", dir_path); // 递归创建目录
my_zlog_error("创建目录失败"); int create_directory_if_not_exists(const char *path) {
char temp[1024];
size_t len = strlen(path);
if (len >= sizeof(temp)) {
my_zlog_error("路径过长: %s", path);
return -1;
}
// 拷贝路径并保证以 '/' 结尾
strncpy(temp, path, sizeof(temp));
if (temp[len - 1] != '/') {
temp[len] = '/';
temp[len + 1] = '\0';
len++;
}
for (size_t i = 1; i < len; i++) {
if (temp[i] == '/') {
temp[i] = '\0';
if (access(temp, F_OK) != 0) {
if (mkdir(temp, 0755) != 0) {
my_zlog_error("创建目录失败: %s (%s)", temp, strerror(errno));
return -1; return -1;
} else {
my_zlog_debug("已创建目录: %s", temp);
} }
} }
temp[i] = '/';
} }
p++;
q++;
} }
return 0; return 0;
} }
// 复制文件函数 // 复制文件内容
int copy_file(const char *source, const char *destination) { int copy_file(const char *src, const char *dst) {
FILE *src_file = fopen(source, "rb"); FILE *fsrc = fopen(src, "rb");
if (src_file == NULL) { if (!fsrc) {
my_zlog_warn("无法打开源文件"); my_zlog_warn("无法打开源文件: %s", src);
return -1; return -1;
} }
FILE *dst_file = fopen(destination, "wb"); FILE *fdst = fopen(dst, "wb");
if (dst_file == NULL) { if (!fdst) {
my_zlog_warn("无法打开目标文件"); my_zlog_warn("无法打开目标文件: %s", dst);
fclose(src_file); fclose(fsrc);
return -1; return -1;
} }
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
size_t bytes_read; size_t bytes;
while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, src_file)) > 0) { while ((bytes = fread(buffer, 1, BUFFER_SIZE, fsrc)) > 0) {
if (fwrite(buffer, 1, bytes_read, dst_file) != bytes_read) { if (fwrite(buffer, 1, bytes, fdst) != bytes) {
my_zlog_warn("写入目标文件时出错"); my_zlog_warn("写入文件出错: %s", dst);
fclose(src_file); fclose(fsrc);
fclose(dst_file); fclose(fdst);
return -1; return -1;
} }
} }
fclose(src_file); fclose(fsrc);
fclose(dst_file); fclose(fdst);
return 0; return 0;
} }
// 改名并移动文件函数 // 重命名并移动文件
int rename_and_move_file(const char *source_file, const char *new_name, const char *destination_dir) { int rename_and_move_file(const char *src, const char *new_name, const char *dst_dir) {
char destination_file[BUFFER_SIZE]; char dst_path[BUFFER_SIZE];
// 构建目标文件的完整路径 snprintf(dst_path, sizeof(dst_path), "%s/%s", dst_dir, new_name);
snprintf(destination_file, sizeof(destination_file), "%s/%s", destination_dir, new_name);
my_zlog_debug("尝试移动文件: %s -> %s", src, dst_path);
// 尝试使用 rename 函数移动文件 if (rename(src, dst_path) == 0) {
if (rename(source_file, destination_file) == 0) { my_zlog_debug("文件已移动至 %s", dst_path);
my_zlog_debug("文件已成功改名并移动到 %s\n", destination_file);
return 0; return 0;
} }
// 如果 rename 失败,可能是跨文件系统,尝试复制文件 my_zlog_warn("rename()失败: %s -> %s, 错误: %s", src, dst_path, strerror(errno));
if (errno == EXDEV) {
if (copy_file(source_file, destination_file) == 0) { if (errno == EXDEV) { // 跨文件系统移动
// 复制成功后删除原文件 if (copy_file(src, dst_path) == 0) {
if (remove(source_file) == 0) { if (remove(src) == 0) {
my_zlog_debug("文件已成功改名并移动到 %s\n", destination_file); my_zlog_debug("跨文件系统移动成功: %s", dst_path);
return 0; return 0;
} else { } else {
my_zlog_warn("删除原文件时出错"); my_zlog_warn("删除源文件失败: %s", src);
return -1; return -1;
} }
} else { } else {
my_zlog_warn("复制文件时出错"); my_zlog_warn("复制文件失败: %s -> %s", src, dst_path);
return -1; return -1;
} }
} }
my_zlog_warn("移动文件时出错"); my_zlog_warn("移动文件失败: %s", strerror(errno));
return -1; return -1;
} }
// 从文件名中提取时间戳 // 提取文件名中的时间戳(例如 Deviceld.txt.202405100930)
time_t extract_timestamp(const char *filename) { time_t extract_timestamp(const char *filename) {
char timestamp_str[20]; char ts[20];
const char *dot = strrchr(filename, '.'); const char *dot = strrchr(filename, '.');
if (dot != NULL) { if (dot) {
strncpy(timestamp_str, dot + 1, sizeof(timestamp_str) - 1); strncpy(ts, dot + 1, sizeof(ts) - 1);
timestamp_str[sizeof(timestamp_str) - 1] = '\0'; ts[sizeof(ts) - 1] = '\0';
struct tm tm = {0}; struct tm tm = {0};
if (strptime(timestamp_str, "%Y%m%d%H%M", &tm) != NULL) { if (strptime(ts, "%Y%m%d%H%M", &tm)) {
return mktime(&tm); return mktime(&tm);
} }
} }
return 0; return 0;
} }
// 查找最新的文件 // 查找目录中最新的设备备份文件
const char *find_latest_file(const char *dir_path) { char *find_latest_file(const char *dir_path) {
DIR *dir = opendir(dir_path); DIR *dir = opendir(dir_path);
if (dir == NULL) { if (!dir) {
perror("无法打开目录"); my_zlog_error("无法打开目录: %s", dir_path);
return NULL; return NULL;
} }
struct dirent *entry; struct dirent *entry;
time_t latest_time = 0; time_t latest_time = 0;
const char *latest_filename = NULL; char *latest_file = NULL;
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) { if (entry->d_type == DT_REG) {
time_t timestamp = extract_timestamp(entry->d_name); time_t ts = extract_timestamp(entry->d_name);
if (timestamp > latest_time) { if (ts > latest_time) {
latest_time = timestamp; latest_time = ts;
if (latest_filename != NULL) { free(latest_file);
free((char *)latest_filename); latest_file = strdup(entry->d_name);
}
latest_filename = strdup(entry->d_name);
} }
} }
} }
closedir(dir); closedir(dir);
return latest_filename; return latest_file;
} }
int device_changename_back() {//备份文件 // 写入设备 ID 到文件
if(file_exists(DEVICE_NAME_FILE) == 0) {//先检测文件是否存在 int write_device_id(const char *path, const char *device_id) {
if(create_devicename_directory_if_not_exists(DEVICE_NAME_DIR) != 0) { FILE *f = fopen(path, "w");
my_zlog_error("创建文件目录失败"); if (!f) {
return -1; my_zlog_error("无法写入文件: %s", path);
} return -1;
}
char *DEVICE_NAME_NEW = sprintf(DEVICE_NAME_FILE,".%s",); fprintf(f, "%s", device_id);
fclose(f);
my_zlog_debug("成功写入设备ID到 %s", path);
return 0;
}
if(rename_and_move_file(DEVICE_NAME_FILE,DEVICE_NAME_NEW,DEVICE_NAME_DIR) !=0 ){ // 改名并备份设备文件
my_zlog_warn("移动文件失败"); int device_changename_back(const char *device_date, const char *device_id) {
// 调试输出:源文件是否存在
if (!file_exists(DEVICE_NAME_FILE)) {
my_zlog_warn("设备文件不存在: %s", DEVICE_NAME_FILE);
return 0;
} }
my_zlog_debug("设备文件存在,准备备份");
if (create_directory_if_not_exists(DEVICE_NAME_DIR) != 0) {
my_zlog_error("创建备份目录失败");
return -1;
} }
return 0;
char new_name[256];
snprintf(new_name, sizeof(new_name), "Deviceld.txt.%s", device_date);
my_zlog_debug("新文件名: %s", new_name);
// 移动文件(可能失败)
if (rename_and_move_file(DEVICE_NAME_FILE, new_name, DEVICE_NAME_DIR) != 0) {
my_zlog_warn("设备文件备份失败");
return -1;
}
return write_device_id(DEVICE_NAME_FILE, device_id);
} }
char *device_changename_back_fail() {//改设备号密码失败回退版本,需要释放 // 获取最近的设备备份文件路径(回退使用)
char *device_file=find_latest_file(DEVICE_NAME_DIR); char *device_changename_back_fail(void) {
const char *device_file_dir= "/home/orangepi/car/master/deviceback" ; char *latest_file = find_latest_file(DEVICE_NAME_DIR);
// 计算拼接后字符串的长度 if (!latest_file) {
size_t size = strlen(device_file_dir) + strlen(device_file) + 1 + 1; // 加上'/'和字符串结束符'\0' my_zlog_warn("未找到任何备份文件");
char *full_path = (char *)malloc(size); return NULL;
snprintf(full_path, size, "%s/%s", device_file_dir, device_file); }
my_zlog_debug("拼接后的完整路径: %s\n", full_path);
free(device_file); size_t size = strlen(DEVICE_NAME_DIR) + strlen(latest_file) + 2;
char *full_path = malloc(size);
if (!full_path) {
free(latest_file);
my_zlog_error("内存分配失败");
return NULL;
}
snprintf(full_path, size, "%s/%s", DEVICE_NAME_DIR, latest_file);
my_zlog_debug("回退路径为: %s", full_path);
free(latest_file);
return full_path; return full_path;
} }
\ No newline at end of file
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
#define DEVICE_NAME_DIR "/home/orangepi/car/master/deviceback" #define DEVICE_NAME_DIR "/home/orangepi/car/master/deviceback"
int device_changename_back(); #define DEVICE_NAME_FILE "/home/orangepi/car/master/Deviceld.txt"
int device_changename_back(const char *device_date, const char *device_id);
const char *find_latest_file(const char *dir_path);
#endif #endif
\ No newline at end of file
00010200000000
\ No newline at end of file
...@@ -3,6 +3,4 @@ ...@@ -3,6 +3,4 @@
int download_file(const char *url,const char *target_dir ,const char *filename) ;//下载文件 int download_file(const char *url,const char *target_dir ,const char *filename) ;//下载文件
int file_exists(const char *path);//检查文件是否存在
int remove_file(const char *path); // 删除文件
#endif #endif
\ No newline at end of file
...@@ -27,44 +27,39 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi ...@@ -27,44 +27,39 @@ static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, voi
} }
// 检查文件是否存在 // 检查并创建日志目录
int file_exists(const char *path) { int create_directory_if_not_exists(const char *path) {
return access(path, F_OK) == 0; char temp[1024];
} size_t len = strlen(path);
// 删除文件 if (len >= sizeof(temp)) {
int remove_file(const char *path) { my_zlog_error("路径过长: %s", path);
if (remove(path) == 0) { return -1;
return 1;
} else {
my_zlog_warn("删除文件失败");
return 0;
} }
}
// 检查并创建日志目录 // 拷贝路径并保证以 '/' 结尾
static int create_directory_if_not_exists(const char *path) { strncpy(temp, path, sizeof(temp));
char dir_path[256] = {0}; if (temp[len - 1] != '/') {
const char *p = path; temp[len] = '/';
char *q = dir_path; temp[len + 1] = '\0';
len++;
// 提取目录部分 }
while (*p != '\0') {
*q = *p; for (size_t i = 1; i < len; i++) {
if (*p == '/' && *(p+1) != '\0') { if (temp[i] == '/') {
// 检查并创建每一级目录 temp[i] = '\0';
if (access(dir_path, F_OK) != 0) { if (access(temp, F_OK) != 0) {
if (mkdir(dir_path, 0755) != 0) { if (mkdir(temp, 0755) != 0) {
fprintf(stderr, "Failed to create directory: %s\n", dir_path); my_zlog_error("创建目录失败: %s (%s)", temp, strerror(errno));
my_zlog_error("创建目录失败");
return -1; return -1;
} else {
my_zlog_debug("已创建目录: %s", temp);
} }
} }
temp[i] = '/';
} }
p++;
q++;
} }
return 0; return 0;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "mqtt.h" #include "mqtt.h"
#include "warm.h" #include "warm.h"
#include "fileopen.h" #include "fileopen.h"
#include "device_change.h"
#include "judg.h" #include "judg.h"
struct mosquitto *mosq; struct mosquitto *mosq;
...@@ -43,9 +44,6 @@ char *glon=NULL;//加入gps后删除,心跳预留,不更改 ...@@ -43,9 +44,6 @@ char *glon=NULL;//加入gps后删除,心跳预留,不更改
time_t gStart;//时间戳 time_t gStart;//时间戳
char *gdevice_change_id=NULL;
char *gdevice_change_date=NULL;
int mqtt_init() { int mqtt_init() {
// 初始化 mosquitto 库 // 初始化 mosquitto 库
mosquitto_lib_init(); mosquitto_lib_init();
...@@ -176,7 +174,7 @@ void message_4(cJSON *body, cJSON *pin_setctrl){//message 为4时候 ...@@ -176,7 +174,7 @@ void message_4(cJSON *body, cJSON *pin_setctrl){//message 为4时候
pwm_value(gvalt[1],gvalt[2]); pwm_value(gvalt[1],gvalt[2]);
} }
int device_message(cJSON *json){ int device_message(cJSON *json){//接收到的控制设备的mqtt消息
cJSON *head = cJSON_GetObjectItem(json, "head"); cJSON *head = cJSON_GetObjectItem(json, "head");
cJSON *message_type = cJSON_GetObjectItem(head, "message_type"); cJSON *message_type = cJSON_GetObjectItem(head, "message_type");
...@@ -221,7 +219,7 @@ int device_message(cJSON *json){ ...@@ -221,7 +219,7 @@ int device_message(cJSON *json){
return 0; return 0;
} }
int download_message(cJSON *root){ int download_message(cJSON *root){//接收的下载的mqtt消息
cJSON *down = cJSON_GetObjectItem(root, "down"); cJSON *down = cJSON_GetObjectItem(root, "down");
if (cJSON_IsObject(down)) { if (cJSON_IsObject(down)) {
// 获取 site 对象 // 获取 site 对象
...@@ -254,14 +252,16 @@ int download_message(cJSON *root){ ...@@ -254,14 +252,16 @@ int download_message(cJSON *root){
return 0; return 0;
} }
int device_mqttchange_name(cJSON *device_id){ int device_mqttchange_name(cJSON *device_id){
cJSON *id = cJSON_GetObjectItem(device_id, "id");// 提取ID cJSON *id = cJSON_GetObjectItem(device_id, "id");// 提取ID
cJSON *date = cJSON_GetObjectItem(device_id, "date");// 提取日期 cJSON *date = cJSON_GetObjectItem(device_id, "date");// 提取日期
*gdevice_change_id=id->valuestring; char *device_change_id=id->valuestring;
*gdevice_change_date=date->valuestring; char *device_change_date=date->valuestring;
device_changename_back(device_change_date,device_change_id);
// 输出提取的数据 // 输出提取的数据
printf("设备ID: %s\n", id->valuestring); my_zlog_debug("设备ID: %s", device_change_id);
printf("日期: %s\n", date->valuestring); my_zlog_debug("日期: %s", device_change_date);
return 0; return 0;
} }
...@@ -293,7 +293,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -293,7 +293,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
} else if(cJSON_GetObjectItem(json, "device_id") != NULL) { } else if(cJSON_GetObjectItem(json, "device_id") != NULL) {
cJSON *device_id=cJSON_GetObjectItem(json, "device_id"); cJSON *device_id=cJSON_GetObjectItem(json, "device_id");
device_mqttchange_name(device_id); device_mqttchange_name(device_id);
my_zlog_debug("接收到设备更改"); my_zlog_debug("接收到设备更改");
} else {// 提取 head 对象 } else {// 提取 head 对象
if(download_message(json) != 1) my_zlog_debug("接收到dowwnload下载"); if(download_message(json) != 1) my_zlog_debug("接收到dowwnload下载");
if(device_message(json) != 0) my_zlog_debug("没有接收到控制信息,代码编号:%d",device_message(json)); if(device_message(json) != 0) my_zlog_debug("没有接收到控制信息,代码编号:%d",device_message(json));
......
...@@ -14,7 +14,7 @@ void car0102_calculate_L_R(int angle) {//将角度转化为对应的舵机pwm值 ...@@ -14,7 +14,7 @@ void car0102_calculate_L_R(int angle) {//将角度转化为对应的舵机pwm值
pwmWrite(PWM_PIN_CHANGE,val); pwmWrite(PWM_PIN_CHANGE,val);
} }
void car0102_speed_stop() { void car0102_speed_stop() {
pin_value(26,0); //pin_value(26,0);
pwmWrite(PWM_PIN_SPEED,0); pwmWrite(PWM_PIN_SPEED,0);
car0102_calculate_L_R(90); car0102_calculate_L_R(90);
} }
......
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