除去测试代码

This commit is contained in:
cdyong 2016-11-09 07:12:29 +00:00
parent 6c94113e59
commit 6970d22f31
2 changed files with 111 additions and 47 deletions

View File

@ -1,4 +1,5 @@
#include "Alarm.h" #include "Alarm.h"
#include "Kernel\TTime.h"
#define Int_Max 2147483647 #define Int_Max 2147483647
@ -14,9 +15,9 @@ public:
AlarmConfig::AlarmConfig() AlarmConfig::AlarmConfig()
{ {
_Name = "AlarmCf"; _Name = "AlarmCf";
_Start = &Count; _Start = &Count;
_End = &TagEnd; _End = &TagEnd;
Init(); Init();
} }
@ -89,17 +90,18 @@ 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));
//重新分配 //重新分配
if (item.Index == 0) dst.Index = id; if (item.Index == 0) dst.Index = id;
} }
else else
{ {
// 删除 // 删除
dst.Index = 0; dst.Index = 0;
} }
// 重新计算个数 // 重新计算个数
@ -109,11 +111,11 @@ byte Alarm::SetCfg(const AlarmItem& item) const
if (cfg.Items[i].Index > 0) n++; if (cfg.Items[i].Index > 0) n++;
} }
cfg.Count = n; cfg.Count = n;
cfg.Save(); cfg.Save();
// 马上调度一次 // 马上调度一次
Sys.SetTask(_taskid, true, 0); //Sys.SetTask(_taskid, true, 0);
return id; return id;
} }
@ -122,58 +124,76 @@ 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();
int type = item.Type.ToByte();
//now.Show();
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++;
} }
// 判断时间有效性 // 判断时间有效性
dt.Hour = item.Hour; dt.Hour = item.Hour;
dt.Minute = item.Minutes; dt.Minute = item.Minutes;
dt.Second = item.Seconds; dt.Second = item.Seconds;
// 需要特别小心时间的偏差 // 需要特别小心时间的偏差
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)
{ {
// 取动作类型 // 取动作类型
auto type = (int)item.Data[1]; auto type = (int)item.Data[1];
AlarmExecutor acttor; AlarmExecutor acttor;
if (dic.TryGetValue(type, acttor)) if (dic.TryGetValue(type, acttor))
{ {
@ -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;
}

View File

@ -33,14 +33,14 @@ typedef void(*AlarmExecutor)(byte type, Buffer& bs);
typedef struct typedef struct
{ {
byte Sunday : 1; byte Sunday : 1;
byte Monday : 1; byte Monday : 1;
byte Tuesday : 1; byte Tuesday : 1;
byte Wednesday : 1; byte Wednesday : 1;
byte Thursday : 1; byte Thursday : 1;
byte Friday : 1; byte Friday : 1;
byte Saturday : 1; byte Saturday : 1;
byte Repeat : 1; byte Repeat : 1;
public: public:
void Init(byte data = 0) { *(byte*)this = data; } void Init(byte data = 0) { *(byte*)this = data; }
byte ToByte() const { return *(byte*)this; } byte ToByte() const { return *(byte*)this; }
@ -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] 表示动作类型,由此字典进行匹配动作执行器