除去测试代码
This commit is contained in:
parent
6c94113e59
commit
6970d22f31
104
App/Alarm.cpp
104
App/Alarm.cpp
|
@ -1,4 +1,5 @@
|
||||||
#include "Alarm.h"
|
#include "Alarm.h"
|
||||||
|
#include "Kernel\TTime.h"
|
||||||
|
|
||||||
#define Int_Max 2147483647
|
#define Int_Max 2147483647
|
||||||
|
|
||||||
|
@ -90,6 +91,7 @@ byte Alarm::SetCfg(const AlarmItem& item) const
|
||||||
if (!id) return 0; // 查找失败
|
if (!id) return 0; // 查找失败
|
||||||
|
|
||||||
auto& dst = cfg.Items[id - 1];
|
auto& dst = cfg.Items[id - 1];
|
||||||
|
|
||||||
if (item.Hour < 0xFF)
|
if (item.Hour < 0xFF)
|
||||||
{
|
{
|
||||||
Buffer::Copy(&dst, &item, sizeof(item));
|
Buffer::Copy(&dst, &item, sizeof(item));
|
||||||
|
@ -113,7 +115,7 @@ byte Alarm::SetCfg(const AlarmItem& item) const
|
||||||
cfg.Save();
|
cfg.Save();
|
||||||
|
|
||||||
// 马上调度一次
|
// 马上调度一次
|
||||||
Sys.SetTask(_taskid, true, 0);
|
//Sys.SetTask(_taskid, true, 0);
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
@ -122,19 +124,23 @@ byte Alarm::SetCfg(const AlarmItem& item) const
|
||||||
static int CheckTime(const AlarmItem& item)
|
static int CheckTime(const AlarmItem& item)
|
||||||
{
|
{
|
||||||
// 判断有效
|
// 判断有效
|
||||||
if(!item.Enable) return -100;
|
if (!item.Enable) return -100;
|
||||||
|
|
||||||
// 判断星期,0~6表示星期天到星期六
|
// 判断星期,0~6表示星期天到星期六
|
||||||
auto now = DateTime::Now();
|
auto now = DateTime::Now();
|
||||||
auto dt = now.Date();
|
auto dt = now.Date();
|
||||||
auto week = now.DayOfWeek();
|
auto week = now.DayOfWeek();
|
||||||
|
|
||||||
|
//now.Show();
|
||||||
|
|
||||||
int type = item.Type.ToByte();
|
int type = item.Type.ToByte();
|
||||||
|
|
||||||
// 今天星期是否配对
|
// 今天星期是否配对
|
||||||
if((type & (1 << week)) == 0)
|
if ((type & (1 << week)) == 0)
|
||||||
{
|
{
|
||||||
// 明天星期是否配对
|
// 明天星期是否配对
|
||||||
if(++week >= 7) week -= 7;
|
if (++week >= 7) week -= 7;
|
||||||
if((type & (1 << week)) == 0) return -100;
|
if ((type & (1 << week)) == 0) return -100;
|
||||||
|
|
||||||
// 明天时间减去现在时间,避免漏了刚好跨天的闹铃
|
// 明天时间减去现在时间,避免漏了刚好跨天的闹铃
|
||||||
dt.Day++;
|
dt.Day++;
|
||||||
|
@ -148,26 +154,40 @@ static int CheckTime(const AlarmItem& item)
|
||||||
// 需要特别小心时间的偏差
|
// 需要特别小心时间的偏差
|
||||||
return (dt - now).TotalSeconds();
|
return (dt - now).TotalSeconds();
|
||||||
}
|
}
|
||||||
|
static bool test = false;
|
||||||
void Alarm::AlarmTask()
|
void Alarm::AlarmTask()
|
||||||
{
|
{
|
||||||
// 如果系统年份不对,则不执行任何动作
|
// 如果系统年份不对,则不执行任何动作
|
||||||
auto now = DateTime::Now();
|
auto now = DateTime::Now();
|
||||||
if(now.Year < 2010) return;
|
if (now.Year < 2010) return;
|
||||||
|
|
||||||
|
//if (!test)
|
||||||
|
//{
|
||||||
|
// Test();
|
||||||
|
//}
|
||||||
AlarmConfig cfg;
|
AlarmConfig cfg;
|
||||||
cfg.Load();
|
cfg.Load();
|
||||||
|
|
||||||
bool flag = false;
|
bool flag = false;
|
||||||
int next = -1;
|
int next = 60000;
|
||||||
// 遍历所有闹钟
|
// 遍历所有闹钟
|
||||||
for (int i = 0; i < ArrayLength(cfg.Items); i++)
|
for (int i = 0; i < ArrayLength(cfg.Items); i++)
|
||||||
{
|
{
|
||||||
auto& item = cfg.Items[i];
|
auto& item = cfg.Items[i];
|
||||||
|
if (item.Index == 0) continue;
|
||||||
|
|
||||||
// 检查闹钟还有多少秒到期
|
// 检查闹钟还有多少秒到期
|
||||||
int sec = CheckTime(item);
|
int sec = CheckTime(item);
|
||||||
if (sec < 3 || sec > -3)
|
|
||||||
|
debug_printf("\r\n");
|
||||||
|
debug_printf("sec = %d\r\n", sec);
|
||||||
|
if (sec < 3 && sec > -3)
|
||||||
{
|
{
|
||||||
|
debug_printf("闹钟执行系统时间:");
|
||||||
|
now.Show();
|
||||||
|
debug_printf(" 星期:%d", now.DayOfWeek());
|
||||||
|
debug_printf("闹钟时间 %d:%d:%d\r\n", item.Hour, item.Minutes, item.Seconds);
|
||||||
|
|
||||||
// 1长度 + 1类型 + n数据
|
// 1长度 + 1类型 + n数据
|
||||||
byte len = item.Data[0];
|
byte len = item.Data[0];
|
||||||
if (len <= 10)
|
if (len <= 10)
|
||||||
|
@ -188,22 +208,33 @@ void Alarm::AlarmTask()
|
||||||
debug_printf("无效数据\r\n");
|
debug_printf("无效数据\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 非重复闹铃需要禁用
|
debug_printf("\r\n");
|
||||||
if(item.Type.Repeat)
|
//// 非重复闹铃需要禁用
|
||||||
{
|
//if(item.Type.Repeat)
|
||||||
item.Enable = false;
|
//{
|
||||||
flag = true;
|
// item.Enable = false;
|
||||||
}
|
// flag = true;
|
||||||
|
//}
|
||||||
|
|
||||||
|
//auto dt = now.Date();
|
||||||
|
//dt.Hour = 23;
|
||||||
|
//dt.Minute = 59;
|
||||||
|
//dt.Second = 50;
|
||||||
|
//dt.Day++;
|
||||||
|
|
||||||
|
//((TTime&)Time).SetTime(dt.TotalSeconds() - 3);
|
||||||
|
//debug_printf("设定系统时间:");
|
||||||
|
//DateTime::Now().Show();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算最近一次将要执行的时间
|
// 计算最近一次将要执行的时间
|
||||||
if(sec > 3 && sec < next) next = sec;
|
if (sec >= 3 && sec < next) next = sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(flag) cfg.Save();
|
debug_printf("下次执行时间%d\r\n", next);
|
||||||
|
if (flag) cfg.Save();
|
||||||
// 设置下次执行的时间
|
if (next > 0)
|
||||||
if(next > 0) Sys.SetTask(_taskid, true, next);
|
Sys.SetTask(_taskid, true, next*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Alarm::Start()
|
void Alarm::Start()
|
||||||
|
@ -211,7 +242,7 @@ void Alarm::Start()
|
||||||
debug_printf("Alarm::Start\r\n");
|
debug_printf("Alarm::Start\r\n");
|
||||||
|
|
||||||
// 创建任务
|
// 创建任务
|
||||||
if (!_taskid) _taskid = Sys.AddTask(&Alarm::AlarmTask, this, 1000, 60000, "AlarmTask");
|
if (!_taskid) _taskid = Sys.AddTask(&Alarm::AlarmTask, this, 1000, 20000, "AlarmTask");
|
||||||
|
|
||||||
// 马上调度一次
|
// 马上调度一次
|
||||||
Sys.SetTask(_taskid, true, 0);
|
Sys.SetTask(_taskid, true, 0);
|
||||||
|
@ -221,3 +252,34 @@ void Alarm::Register(byte type, AlarmExecutor act)
|
||||||
{
|
{
|
||||||
dic.Add((int)type, act);
|
dic.Add((int)type, act);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Alarm::Test()
|
||||||
|
{
|
||||||
|
debug_printf("闹钟测试...\r\n");
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 7; i++)
|
||||||
|
{
|
||||||
|
AlarmItem item;
|
||||||
|
item.Enable = true;
|
||||||
|
item.Hour = 23;
|
||||||
|
item.Index = 0;
|
||||||
|
item.Minutes = 59;
|
||||||
|
item.Seconds = 57;
|
||||||
|
item.Type.Init((byte)(1 << i));
|
||||||
|
SetCfg(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto now = DateTime::Now();
|
||||||
|
auto dt = now.Date();
|
||||||
|
|
||||||
|
dt.Hour = 23;
|
||||||
|
dt.Minute = 59;
|
||||||
|
dt.Second = 50;
|
||||||
|
|
||||||
|
//时间设定好
|
||||||
|
((TTime&)Time).SetTime(dt.TotalSeconds() - 60);
|
||||||
|
|
||||||
|
debug_printf("初始化设定时间\r\n");
|
||||||
|
DateTime::Now().Show(true);
|
||||||
|
test = true;
|
||||||
|
}
|
||||||
|
|
|
@ -76,6 +76,8 @@ public:
|
||||||
// 注册各种类型的执行动作
|
// 注册各种类型的执行动作
|
||||||
void Register(byte type, AlarmExecutor act);
|
void Register(byte type, AlarmExecutor act);
|
||||||
|
|
||||||
|
void Test();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Dictionary<int, AlarmExecutor> dic;// AlarmItem.Data[1] 表示动作类型,由此字典进行匹配动作执行器
|
Dictionary<int, AlarmExecutor> dic;// AlarmItem.Data[1] 表示动作类型,由此字典进行匹配动作执行器
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue