mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-14 14:08:19 +08:00
断电保留定时任务 v2.1.2
This commit is contained in:
@@ -335,7 +335,14 @@ static int HttpAddTask(httpd_request_t *req)
|
|||||||
err = httpd_get_data(req, buf, 16);
|
err = httpd_get_data(req, buf, 16);
|
||||||
require_noerr(err, exit);
|
require_noerr(err, exit);
|
||||||
|
|
||||||
pTimedTask task = (pTimedTask)malloc(sizeof(struct TimedTask));
|
pTimedTask task = NewTask();
|
||||||
|
if (task == NULL)
|
||||||
|
{
|
||||||
|
http_log("NewTask() error, max task num = %d!", MAX_TASK_NUM);
|
||||||
|
char* mess = "NO SPACE";
|
||||||
|
send_http(mess, strlen(mess), exit, &err);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
int re = sscanf(buf, "%ld %d %d %d", &task->prs_time, &task->socket_idx, &task->on, &task->weekday);
|
int re = sscanf(buf, "%ld %d %d %d", &task->prs_time, &task->socket_idx, &task->on, &task->weekday);
|
||||||
http_log("AddTask buf[%s] re[%d] (%ld %d %d %d)",
|
http_log("AddTask buf[%s] re[%d] (%ld %d %d %d)",
|
||||||
buf, re, task->prs_time, task->socket_idx, task->on, task->weekday);
|
buf, re, task->prs_time, task->socket_idx, task->on, task->weekday);
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ void appRestoreDefault_callback(void * const user_config_data, uint32_t size)
|
|||||||
userConfigDefault->mqtt_port = 0;
|
userConfigDefault->mqtt_port = 0;
|
||||||
userConfigDefault->mqtt_user[0] = 0;
|
userConfigDefault->mqtt_user[0] = 0;
|
||||||
userConfigDefault->mqtt_password[0] = 0;
|
userConfigDefault->mqtt_password[0] = 0;
|
||||||
|
userConfigDefault->task_top = NULL;
|
||||||
|
userConfigDefault->task_count = 0;
|
||||||
userConfigDefault->version = USER_CONFIG_VERSION;
|
userConfigDefault->version = USER_CONFIG_VERSION;
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
@@ -38,6 +40,10 @@ void appRestoreDefault_callback(void * const user_config_data, uint32_t size)
|
|||||||
{
|
{
|
||||||
userConfigDefault->socket_status[i] = 1;
|
userConfigDefault->socket_status[i] = 1;
|
||||||
}
|
}
|
||||||
|
for (i = 0; i < MAX_TASK_NUM; i++)
|
||||||
|
{
|
||||||
|
user_config->timed_tasks[i].on_use = false;
|
||||||
|
}
|
||||||
//mico_system_context_update(sys_config);
|
//mico_system_context_update(sys_config);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,7 +132,7 @@ int application_start(void)
|
|||||||
UserMqttHassPower();
|
UserMqttHassPower();
|
||||||
|
|
||||||
time_t now = time(NULL);
|
time_t now = time(NULL);
|
||||||
if (task_top && now >= task_top->prs_time)
|
if (user_config->task_top && now >= user_config->task_top->prs_time)
|
||||||
{
|
{
|
||||||
ProcessTask();
|
ProcessTask();
|
||||||
}
|
}
|
||||||
|
|||||||
10
TC1/main.h
10
TC1/main.h
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "mico.h"
|
#include "mico.h"
|
||||||
#include "micokit_ext.h"
|
#include "micokit_ext.h"
|
||||||
|
#include "timed_task/timed_task.h"
|
||||||
|
|
||||||
#define app_log(M, ...) do { custom_log("APP", M, ##__VA_ARGS__); web_log("APP", M, ##__VA_ARGS__); } while(0)
|
#define app_log(M, ...) do { custom_log("APP", M, ##__VA_ARGS__); web_log("APP", M, ##__VA_ARGS__); } while(0)
|
||||||
#define key_log(M, ...) do { custom_log("KEY", M, ##__VA_ARGS__); web_log("KEY", M, ##__VA_ARGS__); } while(0)
|
#define key_log(M, ...) do { custom_log("KEY", M, ##__VA_ARGS__); web_log("KEY", M, ##__VA_ARGS__); } while(0)
|
||||||
@@ -15,14 +16,14 @@
|
|||||||
#define wifi_log(M, ...) do { custom_log("WIFI", M, ##__VA_ARGS__); web_log("WIFI", M, ##__VA_ARGS__); } while(0)
|
#define wifi_log(M, ...) do { custom_log("WIFI", M, ##__VA_ARGS__); web_log("WIFI", M, ##__VA_ARGS__); } while(0)
|
||||||
#define power_log(M, ...) do { custom_log("POWER", M, ##__VA_ARGS__); web_log("POWER", M, ##__VA_ARGS__); } while(0)
|
#define power_log(M, ...) do { custom_log("POWER", M, ##__VA_ARGS__); web_log("POWER", M, ##__VA_ARGS__); } while(0)
|
||||||
|
|
||||||
#define VERSION "v2.1.1"
|
#define VERSION "v2.1.2"
|
||||||
|
|
||||||
#define TYPE 1
|
#define TYPE 1
|
||||||
#define TYPE_NAME "zTC1"
|
#define TYPE_NAME "zTC1"
|
||||||
|
|
||||||
#define ZTC1_NAME "zTC1-%s"
|
#define ZTC1_NAME "zTC1-%s"
|
||||||
|
|
||||||
#define USER_CONFIG_VERSION 6
|
#define USER_CONFIG_VERSION 8
|
||||||
#define SETTING_MQTT_STRING_LENGTH_MAX 32 //必须4字节对齐。
|
#define SETTING_MQTT_STRING_LENGTH_MAX 32 //必须4字节对齐。
|
||||||
|
|
||||||
#define SOCKET_NAME_LENGTH 32
|
#define SOCKET_NAME_LENGTH 32
|
||||||
@@ -43,6 +44,8 @@
|
|||||||
#define Relay_5 MICO_GPIO_18
|
#define Relay_5 MICO_GPIO_18
|
||||||
#define Relay_NUM SOCKET_NUM
|
#define Relay_NUM SOCKET_NUM
|
||||||
|
|
||||||
|
#define MAX_TASK_NUM 128
|
||||||
|
|
||||||
//用户保存参数结构体
|
//用户保存参数结构体
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@@ -56,6 +59,9 @@ typedef struct
|
|||||||
WiFiEvent last_wifi_status;
|
WiFiEvent last_wifi_status;
|
||||||
char ap_name[32];
|
char ap_name[32];
|
||||||
char ap_key[32];
|
char ap_key[32];
|
||||||
|
int task_count;
|
||||||
|
pTimedTask task_top;
|
||||||
|
struct TimedTask timed_tasks[MAX_TASK_NUM];
|
||||||
} user_config_t;
|
} user_config_t;
|
||||||
|
|
||||||
extern char rtc_init;
|
extern char rtc_init;
|
||||||
|
|||||||
@@ -9,28 +9,40 @@
|
|||||||
#include"timed_task/timed_task.h"
|
#include"timed_task/timed_task.h"
|
||||||
#include"http_server/web_log.h"
|
#include"http_server/web_log.h"
|
||||||
|
|
||||||
pTimedTask task_top = NULL;
|
|
||||||
int task_count = 0;
|
|
||||||
int day_sec = 86400;
|
int day_sec = 86400;
|
||||||
|
|
||||||
|
pTimedTask NewTask()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < MAX_TASK_NUM; i++)
|
||||||
|
{
|
||||||
|
pTimedTask task = &user_config->timed_tasks[i];
|
||||||
|
if (!task->on_use)
|
||||||
|
{
|
||||||
|
task->on_use = true;
|
||||||
|
return task;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
bool AddTaskSingle(pTimedTask task)
|
bool AddTaskSingle(pTimedTask task)
|
||||||
{
|
{
|
||||||
task_count++;
|
user_config->task_count++;
|
||||||
if (task_top == NULL)
|
if (user_config->task_top == NULL)
|
||||||
{
|
{
|
||||||
task->next = NULL;
|
task->next = NULL;
|
||||||
task_top = task;
|
user_config->task_top = task;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (task->prs_time <= task_top->prs_time)
|
if (task->prs_time <= user_config->task_top->prs_time)
|
||||||
{
|
{
|
||||||
task->next = task_top;
|
task->next = user_config->task_top;
|
||||||
task_top = task;
|
user_config->task_top = task;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pTimedTask tmp = task_top;
|
pTimedTask tmp = user_config->task_top;
|
||||||
while (tmp)
|
while (tmp)
|
||||||
{
|
{
|
||||||
if (tmp->next == NULL
|
if (tmp->next == NULL
|
||||||
@@ -43,7 +55,7 @@ bool AddTaskSingle(pTimedTask task)
|
|||||||
}
|
}
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
}
|
}
|
||||||
task_count--;
|
user_config->task_count--;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,14 +80,14 @@ bool AddTask(pTimedTask task)
|
|||||||
|
|
||||||
bool DelFirstTask()
|
bool DelFirstTask()
|
||||||
{
|
{
|
||||||
if (task_top)
|
if (user_config->task_top)
|
||||||
{
|
{
|
||||||
pTimedTask tmp = task_top;
|
pTimedTask tmp = user_config->task_top;
|
||||||
task_top = task_top->next;
|
user_config->task_top = user_config->task_top->next;
|
||||||
task_count--;
|
user_config->task_count--;
|
||||||
if (tmp->weekday == 0)
|
if (tmp->weekday == 0)
|
||||||
{
|
{
|
||||||
free(tmp);
|
tmp->on_use = false;
|
||||||
}
|
}
|
||||||
else if (tmp->weekday == 8) //8代表每日任务
|
else if (tmp->weekday == 8) //8代表每日任务
|
||||||
{
|
{
|
||||||
@@ -94,33 +106,33 @@ bool DelFirstTask()
|
|||||||
|
|
||||||
bool DelTask(int time)
|
bool DelTask(int time)
|
||||||
{
|
{
|
||||||
if (task_top == NULL)
|
if (user_config->task_top == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (time == task_top->prs_time)
|
if (time == user_config->task_top->prs_time)
|
||||||
{
|
{
|
||||||
pTimedTask tmp = task_top;
|
pTimedTask tmp = user_config->task_top;
|
||||||
task_top = task_top->next;
|
user_config->task_top = user_config->task_top->next;
|
||||||
free(tmp);
|
tmp->on_use = false;
|
||||||
task_count--;
|
user_config->task_count--;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if (task_top->next == NULL)
|
else if (user_config->task_top->next == NULL)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pTimedTask pre_tsk = task_top;
|
pTimedTask pre_tsk = user_config->task_top;
|
||||||
pTimedTask tmp_tsk = task_top->next;
|
pTimedTask tmp_tsk = user_config->task_top->next;
|
||||||
while (tmp_tsk)
|
while (tmp_tsk)
|
||||||
{
|
{
|
||||||
if (time == tmp_tsk->prs_time)
|
if (time == tmp_tsk->prs_time)
|
||||||
{
|
{
|
||||||
pre_tsk->next = tmp_tsk->next;
|
pre_tsk->next = tmp_tsk->next;
|
||||||
free(tmp_tsk);
|
tmp_tsk->on_use = false;
|
||||||
task_count--;
|
user_config->task_count--;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
tmp_tsk = tmp_tsk->next;
|
tmp_tsk = tmp_tsk->next;
|
||||||
@@ -131,15 +143,15 @@ bool DelTask(int time)
|
|||||||
void ProcessTask()
|
void ProcessTask()
|
||||||
{
|
{
|
||||||
task_log("process task time[%ld] socket_idx[%d] on[%d]",
|
task_log("process task time[%ld] socket_idx[%d] on[%d]",
|
||||||
task_top->prs_time, task_top->socket_idx, task_top->on);
|
user_config->task_top->prs_time, user_config->task_top->socket_idx, user_config->task_top->on);
|
||||||
UserRelaySet(task_top->socket_idx, task_top->on);
|
UserRelaySet(user_config->task_top->socket_idx, user_config->task_top->on);
|
||||||
DelFirstTask();
|
DelFirstTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
char* GetTaskStr()
|
char* GetTaskStr()
|
||||||
{
|
{
|
||||||
char* str = (char*)malloc(sizeof(char)*(task_count*89+2));
|
char* str = (char*)malloc(sizeof(char)*(user_config->task_count*89+2));
|
||||||
pTimedTask tmp_tsk = task_top;
|
pTimedTask tmp_tsk = user_config->task_top;
|
||||||
char* tmp_str = str;
|
char* tmp_str = str;
|
||||||
tmp_str[0] = '[';
|
tmp_str[0] = '[';
|
||||||
tmp_str[2] = 0;
|
tmp_str[2] = 0;
|
||||||
@@ -157,7 +169,7 @@ char* GetTaskStr()
|
|||||||
tmp_str += strlen(tmp_str);
|
tmp_str += strlen(tmp_str);
|
||||||
tmp_tsk = tmp_tsk->next;
|
tmp_tsk = tmp_tsk->next;
|
||||||
}
|
}
|
||||||
if (task_count > 0) --tmp_str;
|
if (user_config->task_count > 0) --tmp_str;
|
||||||
*tmp_str = ']';
|
*tmp_str = ']';
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,15 @@ struct TimedTask;
|
|||||||
typedef struct TimedTask* pTimedTask;
|
typedef struct TimedTask* pTimedTask;
|
||||||
struct TimedTask
|
struct TimedTask
|
||||||
{
|
{
|
||||||
|
bool on_use; //正在使用
|
||||||
time_t prs_time; //被执行的格林尼治时间戳
|
time_t prs_time; //被执行的格林尼治时间戳
|
||||||
int socket_idx; //要控制的插孔
|
int socket_idx; //要控制的插孔
|
||||||
int on; //开或者关
|
int on; //开或者关
|
||||||
int weekday; //星期重复 0代表不重复
|
int weekday; //星期重复 0代表不重复 8代表每日重复
|
||||||
pTimedTask next; //下一个任务(按之间排序)
|
pTimedTask next; //下一个任务(按之间排序)
|
||||||
};
|
};
|
||||||
|
|
||||||
extern pTimedTask task_top;
|
pTimedTask NewTask();
|
||||||
extern int task_count;
|
|
||||||
|
|
||||||
bool AddTask(pTimedTask task);
|
bool AddTask(pTimedTask task);
|
||||||
bool DelTask(int time);
|
bool DelTask(int time);
|
||||||
bool DelFirstTask();
|
bool DelFirstTask();
|
||||||
|
|||||||
Reference in New Issue
Block a user