245 lines
8.0 KiB
C
245 lines
8.0 KiB
C
#include "service_sleep.h"
|
|
#include "cJSON.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define JSON_FILE_PATH "/system/service_sleep.json"
|
|
static const char *day_names[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
|
|
|
static char *g_cur_week_day = NULL;
|
|
static cJSON *g_cur_day_json_array = NULL;
|
|
|
|
// 0-6 周日~周六
|
|
static const char *get_day_name(uint8_t week_day)
|
|
{
|
|
if (week_day > 6) {
|
|
printf("error week_day value!\n");
|
|
return NULL;
|
|
}
|
|
return day_names[week_day];
|
|
}
|
|
|
|
void service_sleep_set_cur_week_day(uint8_t week_day) { g_cur_week_day = get_day_name(week_day); }
|
|
|
|
int32_t service_sleep_save_sleep_data_to_json(service_sleep_time_t time, sleep_status_t status)
|
|
{
|
|
if (!g_cur_day_json_array) {
|
|
g_cur_day_json_array = cJSON_CreateArray();
|
|
if (!g_cur_day_json_array) {
|
|
printf("cJSON_CreateArray fail, g_cur_day_json_array is null!\n");
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
cJSON *json_object = cJSON_CreateObject();
|
|
if (!json_object) {
|
|
printf("cJSON_CreateObject fail, json_object is null!\n");
|
|
return -1;
|
|
}
|
|
cJSON_AddNumberToObject(json_object, "year", time.year);
|
|
cJSON_AddNumberToObject(json_object, "month", time.mon);
|
|
cJSON_AddNumberToObject(json_object, "day", time.day);
|
|
cJSON_AddNumberToObject(json_object, "hour", time.hour);
|
|
cJSON_AddNumberToObject(json_object, "minute", time.min);
|
|
cJSON_AddNumberToObject(json_object, "second", time.sec);
|
|
cJSON_AddNumberToObject(json_object, "status", status);
|
|
cJSON_AddItemToArray(g_cur_day_json_array, json_object);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int32_t service_sleep_save_sleep_json_to_file(void)
|
|
{
|
|
if (g_cur_week_day == NULL) {
|
|
printf("[%s]g_cur_week_day is null!\n", __func__);
|
|
return -1;
|
|
}
|
|
if (g_cur_day_json_array == NULL) {
|
|
printf("[%s]g_cur_day_json_array is null!\n", __func__);
|
|
return -1;
|
|
}
|
|
|
|
// 读取 JSON 文件
|
|
char *file_content = NULL;
|
|
long file_size;
|
|
FILE *file = fopen(JSON_FILE_PATH, "rb");
|
|
if (file == NULL) {
|
|
printf("[%s]Failed to open JSON file:%s\n", __func__, JSON_FILE_PATH);
|
|
return -1;
|
|
}
|
|
|
|
// 获取文件大小
|
|
fseek(file, 0, SEEK_END);
|
|
file_size = ftell(file);
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
// 读取文件内容
|
|
file_content = (char *)malloc(file_size + 1);
|
|
fread(file_content, 1, file_size, file);
|
|
file_content[file_size] = '\0';
|
|
fclose(file);
|
|
|
|
// 解析 JSON
|
|
cJSON *root = cJSON_Parse(file_content);
|
|
free(file_content);
|
|
if (root == NULL) {
|
|
printf("[%s]Failed to parse JSON\n", __func__);
|
|
return -1;
|
|
}
|
|
|
|
// 查找并更新指定日期的值
|
|
cJSON *item = cJSON_GetObjectItem(root, g_cur_week_day);
|
|
if (item != NULL && cJSON_IsArray(item)) {
|
|
// 如果已经存在且是数组,先删除再添加新的数组(如果需要完全替换)
|
|
cJSON_DeleteItemFromObject(root, g_cur_week_day);
|
|
}
|
|
// 添加或更新键的值为新的数组
|
|
cJSON_AddItemToObject(root, g_cur_week_day, g_cur_day_json_array);
|
|
g_cur_day_json_array = NULL;
|
|
|
|
// 写回文件
|
|
char *output = cJSON_Print(root);
|
|
file = fopen(JSON_FILE_PATH, "wb");
|
|
if (file == NULL) {
|
|
printf("[%s]Failed to open JSON file for writing\n", __func__);
|
|
return -1;
|
|
}
|
|
fwrite(output, strlen(output), 1, file);
|
|
fclose(file);
|
|
|
|
// 清理内存
|
|
cJSON_Delete(root);
|
|
free(output);
|
|
return 0;
|
|
}
|
|
|
|
// 0-6 周日~周六
|
|
int32_t load_sleep_data_from_json(sleep_status_dada_t *null_head, uint8_t week_day)
|
|
{
|
|
if (null_head == NULL || week_day > 6) {
|
|
printf("[%s]Parameter is not valid value\n", __func__);
|
|
return -1;
|
|
}
|
|
FILE *file = fopen(JSON_FILE_PATH, "rb");
|
|
if (file == NULL) {
|
|
printf("[%s]Failed to open %s for reading!\n",__func__, JSON_FILE_PATH);
|
|
return -1;
|
|
}
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
uint32_t file_size = ftell(file);
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
char *file_content = (char *)malloc(file_size + 1);
|
|
if (file_content == NULL) {
|
|
printf("[%s]Failed to allocate memory for file content",__func__);
|
|
fclose(file);
|
|
return -1;
|
|
}
|
|
fread(file_content, 1, file_size, file);
|
|
file_content[file_size] = '\0';
|
|
fclose(file);
|
|
|
|
cJSON *root = cJSON_Parse(file_content);
|
|
free(file_content);
|
|
|
|
if (root == NULL || !cJSON_IsObject(root)) {
|
|
printf("[%s]Failed to parse JSON data\n",__func__);
|
|
if (root) cJSON_Delete(root);
|
|
return -1;
|
|
}
|
|
|
|
const char *day_name = get_day_name(week_day);
|
|
cJSON *data = cJSON_GetObjectItemCaseSensitive(root, day_name);
|
|
sleep_status_dada_t *p = null_head;
|
|
if (cJSON_IsArray(data)) {
|
|
cJSON *item = NULL;
|
|
cJSON_ArrayForEach(item, data) {
|
|
if (!cJSON_IsObject(item))
|
|
continue;
|
|
service_sleep_time_t time;
|
|
sleep_status_t status;
|
|
|
|
// 解析时间字段
|
|
cJSON *cjson_status = cJSON_GetObjectItemCaseSensitive(item, "status");
|
|
cJSON *cjson_year = cJSON_GetObjectItemCaseSensitive(item, "year");
|
|
cJSON *cjson_mon = cJSON_GetObjectItemCaseSensitive(item, "month");
|
|
cJSON *cjson_day = cJSON_GetObjectItemCaseSensitive(item, "day");
|
|
cJSON *cjson_hour = cJSON_GetObjectItemCaseSensitive(item, "hour");
|
|
cJSON *cjson_min = cJSON_GetObjectItemCaseSensitive(item, "minute");
|
|
cJSON *cjson_sec = cJSON_GetObjectItemCaseSensitive(item, "second");
|
|
|
|
if (!cJSON_IsNumber(cjson_sec) || !cJSON_IsNumber(cjson_min) || !cJSON_IsNumber(cjson_hour) ||
|
|
!cJSON_IsNumber(cjson_day) || !cJSON_IsNumber(cjson_mon) || !cJSON_IsNumber(cjson_year) || !cJSON_IsNumber(cjson_status)) {
|
|
printf("[%s]Failed to parse time fields for %s\n", __func__, day_name);
|
|
continue;
|
|
}
|
|
|
|
time.sec = (uint8_t)cjson_sec->valueint;
|
|
time.min = (uint8_t)cjson_min->valueint;
|
|
time.hour = (uint8_t)cjson_hour->valueint;
|
|
time.day = (uint8_t)cjson_day->valueint;
|
|
time.mon = (uint8_t)cjson_mon->valueint;
|
|
time.year = (uint16_t)cjson_year->valueint;
|
|
status = (sleep_status_t)(cjson_status->valueint);
|
|
// printf("%d %d-%2d-%2d %2d:%2d:%2d\n", status, time.year, time.mon, time.day, time.hour, time.min, time.sec);
|
|
|
|
// 创建新节点并链接到链表
|
|
sleep_status_dada_t *new_node = (sleep_status_dada_t *)malloc(sizeof(sleep_status_dada_t));
|
|
if (!new_node) {
|
|
printf("[%s]Failed to allocate memory for new node", __func__);
|
|
cJSON_Delete(root);
|
|
return -1;
|
|
}
|
|
|
|
new_node->time = time;
|
|
new_node->status = status;
|
|
new_node->next = NULL;
|
|
|
|
while(p->next != NULL) {
|
|
p = p->next;
|
|
}
|
|
p->next = new_node;
|
|
}
|
|
}
|
|
|
|
cJSON_Delete(root);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void service_sleep_delete_json_file(void)
|
|
{
|
|
remove(JSON_FILE_PATH);
|
|
}
|
|
|
|
void service_sleep_init(void)
|
|
{
|
|
FILE *file = fopen(JSON_FILE_PATH, "r");
|
|
cJSON *json_data = NULL;
|
|
|
|
if (!file) {
|
|
printf("%s is not exist!\n", JSON_FILE_PATH);
|
|
json_data = cJSON_CreateObject();
|
|
|
|
for (int i = 0; i < 7; i++) {
|
|
cJSON_AddItemToObject(json_data, get_day_name(i), cJSON_CreateArray());
|
|
}
|
|
|
|
char *json_string = cJSON_Print(json_data);
|
|
file = fopen(JSON_FILE_PATH, "w");
|
|
if (file != NULL) {
|
|
fputs(json_string, file);
|
|
fclose(file);
|
|
} else {
|
|
printf("Failed to open %s for writing!\n", JSON_FILE_PATH);
|
|
return;
|
|
}
|
|
|
|
cJSON_Delete(json_data);
|
|
free(json_string);
|
|
printf("%s create succ!\n", JSON_FILE_PATH);
|
|
}
|
|
printf("service_sleep_init succ!\n");
|
|
} |