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