This commit is contained in:
parent
15e1e2fab2
commit
0f81f9847b
155
App/Alarm.cpp
155
App/Alarm.cpp
|
@ -31,7 +31,38 @@ void AlarmConfig::Init()
|
|||
Alarm::Alarm()
|
||||
{
|
||||
AlarmTaskId = 0;
|
||||
AfterAlarmId = 0xff;
|
||||
}
|
||||
|
||||
bool Alarm::AlarmSet(const Pair& args, Stream& result)
|
||||
{
|
||||
AlarmDataType data;
|
||||
data.Enable = false;
|
||||
|
||||
byte Id = 0xff;
|
||||
args.Get("Number", Id); // 1/9
|
||||
if (Id > 20)return false;
|
||||
|
||||
args.Get("Enable",data.Enable); // 1/9
|
||||
byte type;
|
||||
args.Get("DayOfWeek",type); // 1/12
|
||||
data.Type.Init(type);
|
||||
args.Get("Hour", data.Hour); // 1/7
|
||||
args.Get("Minute", data.Minutes); // 1/9
|
||||
args.Get("Second", data.Seconds); // 1/9
|
||||
Buffer buf(data.Data, sizeof(data.Data));
|
||||
args.Get("Data",buf ); // 11/17
|
||||
|
||||
if (data.Hour > 23 || data.Minutes > 59 || data.Seconds > 59)return false;
|
||||
|
||||
return SetCfg(Id, data);
|
||||
}
|
||||
|
||||
bool Alarm::AlarmGet(const Pair& args, Stream& result)
|
||||
{
|
||||
// 不知道该如何序列化,暂时不提供
|
||||
BinaryPair bp(result);
|
||||
bp.Set("ErrorCode", (byte)0x01);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Alarm::SetCfg(byte id, AlarmDataType& data)
|
||||
|
@ -44,8 +75,9 @@ bool Alarm::SetCfg(byte id, AlarmDataType& data)
|
|||
bf2 = bf;
|
||||
|
||||
cfg.Save();
|
||||
// 修改过后要检查一下Task的时间
|
||||
// xxxx
|
||||
// 修改过后要检查一下Task的时间 // 取消下次动作并重新计算
|
||||
NextAlarmIds.Clear();
|
||||
Start();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -62,61 +94,114 @@ bool Alarm::GetCfg(byte id, AlarmDataType& data)
|
|||
|
||||
int Alarm::CalcNextTime(AlarmDataType& data)
|
||||
{
|
||||
auto dt = DateTime::Now();
|
||||
debug_printf("CalcNextTime :");
|
||||
auto now = DateTime::Now();
|
||||
byte type = data.Type.ToByte();
|
||||
byte week = dt.DayOfWeek();
|
||||
if (type & 1 << week)
|
||||
byte week = now.DayOfWeek();
|
||||
int time;
|
||||
if (type & 1 << week) // 今天
|
||||
{
|
||||
|
||||
DateTime dt(now.Year, now.Month, now.Day);
|
||||
dt.Hour = data.Hour;
|
||||
dt.Minute = data.Minutes;
|
||||
dt.Second = data.Seconds;
|
||||
if (dt > now)
|
||||
{
|
||||
time = (dt - now).Ms;
|
||||
debug_printf("%d\r\n", time);
|
||||
return time; // 今天闹钟还没响
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
debug_printf("max\r\n");
|
||||
return Int_Max;
|
||||
}
|
||||
|
||||
byte Alarm::FindAfter()
|
||||
int ToTomorrow()
|
||||
{
|
||||
auto dt = DateTime::Now();
|
||||
int time = (24 - dt.Hour - 1) * 3600000; // 时-1 -> ms
|
||||
time += ((60 - dt.Minute - 1) * 60000); // 分-1 -> ms
|
||||
time += ((60 - dt.Second) * 1000); // 秒 -> ms
|
||||
debug_printf("ToTomorrow : %d\r\n", time);
|
||||
return time;
|
||||
}
|
||||
|
||||
byte Alarm::FindNext(int& nextTime)
|
||||
{
|
||||
debug_printf("FindNext\r\n");
|
||||
AlarmConfig cfg;
|
||||
cfg.Load();
|
||||
|
||||
int nextTimeMs;
|
||||
byte id = 0xff;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
if (!cfg.Data[i].Enable)continue;
|
||||
int time = CalcNextTime(cfg.Data[i]);
|
||||
if (time < nextTimeMs)
|
||||
{
|
||||
nextTimeMs = time;
|
||||
id = i;
|
||||
}
|
||||
}
|
||||
if (id != 0xff)NextAlarmMs = nextTimeMs;
|
||||
int miniTime = Int_Max;
|
||||
|
||||
return id;
|
||||
int times[ArrayLength(cfg.Data)];
|
||||
for (int i = 0; i < ArrayLength(cfg.Data); i++)
|
||||
{
|
||||
times[i] = Int_Max;
|
||||
if (!cfg.Data[i].Enable)continue;
|
||||
int time = CalcNextTime(cfg.Data[i]); // 但凡有效的都计算出来
|
||||
times[i] = time;
|
||||
|
||||
if (time < miniTime)miniTime = time; // 找出最小时间
|
||||
}
|
||||
|
||||
if (miniTime != Int_Max)
|
||||
{
|
||||
for (int i = 0; i < ArrayLength(cfg.Data); i++)
|
||||
{
|
||||
if (times[i] == miniTime)
|
||||
NextAlarmIds.Add(i);
|
||||
}
|
||||
nextTime = miniTime;
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果最小值无效 直接明早再来算一次
|
||||
nextTime = ToTomorrow();
|
||||
NextAlarmIds.Clear();
|
||||
}
|
||||
|
||||
return NextAlarmIds.Count();
|
||||
}
|
||||
|
||||
void Alarm::AlarmTask()
|
||||
{
|
||||
debug_printf("AlarmTask");
|
||||
// 获取定时的数据
|
||||
AlarmDataType data;
|
||||
if (AfterAlarmId != 0xff)
|
||||
GetCfg(AfterAlarmId, data);
|
||||
// 执行动作 DoSomething(data);
|
||||
|
||||
// 找到下一个定时器动作的时间
|
||||
AfterAlarmId = FindAfter();
|
||||
if (AfterAlarmId != 0xff)
|
||||
auto now = DateTime::Now();
|
||||
now.Ms = 0;
|
||||
for (int i = 0; i < NextAlarmIds.Count(); i++)
|
||||
{
|
||||
Sys.SetTask(AlarmTaskId, true, NextAlarmMs);
|
||||
return;
|
||||
}
|
||||
byte NextAlarmId = NextAlarmIds[0];
|
||||
GetCfg(NextAlarmId, data);
|
||||
|
||||
Sys.SetTask(AlarmTaskId, false);
|
||||
DateTime dt(now.Year, now.Month, now.Day);
|
||||
dt.Hour = data.Hour;
|
||||
dt.Minute = data.Minutes;
|
||||
dt.Second = dt.Second;
|
||||
dt.Ms = 0;
|
||||
if (dt == now)
|
||||
{
|
||||
// 执行动作 DoSomething(data);
|
||||
debug_printf(" DoSomething: ");
|
||||
ByteArray bs((const void*)data.Data,sizeof(data.Data));
|
||||
bs.Show(true);
|
||||
}
|
||||
}
|
||||
NextAlarmIds.Clear();
|
||||
|
||||
// 找到下一个定时器动作的时间
|
||||
FindNext(NextAlarmMs);
|
||||
if (NextAlarmIds.Count() != 0)
|
||||
Sys.SetTask(AlarmTaskId, true, NextAlarmMs);
|
||||
else
|
||||
Sys.SetTask(AlarmTaskId, false);
|
||||
}
|
||||
|
||||
void Alarm::Start()
|
||||
{
|
||||
debug_printf("Alarm::Start\r\n");
|
||||
if (!AlarmTaskId)AlarmTaskId = Sys.AddTask(&Alarm::AlarmTask, this, -1, -1, "AlarmTask");
|
||||
Sys.SetTask(AlarmTaskId,true);
|
||||
}
|
||||
|
|
21
App/Alarm.h
21
App/Alarm.h
|
@ -5,6 +5,7 @@
|
|||
#include "Timer.h"
|
||||
#include "Port.h"
|
||||
#include "..\Config.h"
|
||||
#include "Message\BinaryPair.h"
|
||||
|
||||
class ByteStruct2 // 位域基类
|
||||
{
|
||||
|
@ -45,6 +46,11 @@ class Alarm
|
|||
public:
|
||||
Alarm();
|
||||
|
||||
/* 注册给 TokenClient 名称 Policy/AlarmSet */
|
||||
bool AlarmSet(const Pair& args, Stream& result);
|
||||
/* 注册给 TokenClient 名称 Policy/AlarmGet */
|
||||
bool AlarmGet(const Pair& args, Stream& result);
|
||||
|
||||
bool SetCfg(byte id, AlarmDataType& data);
|
||||
bool GetCfg(byte id, AlarmDataType& data);
|
||||
|
||||
|
@ -52,12 +58,21 @@ public:
|
|||
|
||||
private:
|
||||
uint AlarmTaskId;
|
||||
List<int>NextAlarmIds; // 下次运行的编号,允许多组定时器定时时间相同
|
||||
int NextAlarmMs; // 下次闹钟时间
|
||||
|
||||
byte AfterAlarmId; // 0xff 无效
|
||||
int NextAlarmMs; // 下次闹钟时间
|
||||
void AlarmTask();
|
||||
byte FindAfter();
|
||||
byte FindNext(int& nextTime);
|
||||
int CalcNextTime(AlarmDataType& data);
|
||||
};
|
||||
|
||||
// class AlarmActivity
|
||||
// {
|
||||
// public:
|
||||
// DataStore* store;
|
||||
//
|
||||
//
|
||||
//
|
||||
// };
|
||||
|
||||
#endif
|
||||
|
|
|
@ -23,6 +23,7 @@ AP0801::AP0801()
|
|||
HostAP = nullptr;
|
||||
Client = nullptr;
|
||||
ProxyFac = nullptr;
|
||||
AlarmObj = nullptr;
|
||||
|
||||
Data = nullptr;
|
||||
Size = 0;
|
||||
|
@ -377,16 +378,21 @@ void AP0801::InitProxy()
|
|||
}
|
||||
ProxyFac = ProxyFactory::Create();
|
||||
|
||||
//ComProxy* proxyCom1 = new ComProxy(COM2);
|
||||
ProxyFac->Register(new ComProxy(COM2));
|
||||
ProxyFac->Register(new ComProxy(COM5));
|
||||
|
||||
|
||||
|
||||
ProxyFac->Open(Client);
|
||||
// ProxyFac->AutoStart(); // 自动启动的设备 需要保证Client已经开启,否则没有意义
|
||||
}
|
||||
|
||||
void AP0801::InitAlarm()
|
||||
{
|
||||
if (!Client)return;
|
||||
|
||||
if(!AlarmObj)AlarmObj = new Alarm();
|
||||
Client->Register("Policy/AlarmSet", &Alarm::AlarmSet, AlarmObj);
|
||||
Client->Register("Policy/AlarmGet", &Alarm::AlarmGet, AlarmObj);
|
||||
}
|
||||
|
||||
/******************************** 2401 ********************************/
|
||||
|
||||
/*int Fix2401(const Buffer& bs)
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "TokenNet\TokenClient.h"
|
||||
#include "Message\ProxyFactory.h"
|
||||
#include "App\Alarm.h"
|
||||
|
||||
// 阿波罗0801/0802
|
||||
class AP0801
|
||||
|
@ -23,6 +24,7 @@ public:
|
|||
ISocketHost* HostAP; // 网络主机
|
||||
TokenClient* Client; // 令牌客户端
|
||||
ProxyFactory* ProxyFac; // 透传管理器
|
||||
Alarm* AlarmObj;
|
||||
|
||||
AP0801();
|
||||
|
||||
|
@ -48,6 +50,7 @@ public:
|
|||
void InitClient();
|
||||
void InitNet();
|
||||
void InitProxy();
|
||||
void InitAlarm();
|
||||
|
||||
void Restore();
|
||||
void OnLongPress(InputPort* port, bool down);
|
||||
|
|
|
@ -22,7 +22,7 @@ bool ProxyFactory::Open(TokenClient* client)
|
|||
debug_printf("ProxyFac Open");
|
||||
if (!client)return false;
|
||||
|
||||
debug_printf(" Register");
|
||||
// debug_printf(" Register");
|
||||
Client = client;
|
||||
Client->Register("Proxy/GetConfig", &ProxyFactory::GetConfig, this);
|
||||
Client->Register("Proxy/SetConfig", &ProxyFactory::SetConfig, this);
|
||||
|
@ -37,7 +37,7 @@ bool ProxyFactory::Open(TokenClient* client)
|
|||
|
||||
bool ProxyFactory::Register(Proxy* dev)
|
||||
{
|
||||
debug_printf("ProxyFac RegPort");
|
||||
// debug_printf("ProxyFac RegPort");
|
||||
String name = dev->Name;
|
||||
name.Show(true);
|
||||
Proxys.Add(dev->Name, dev);
|
||||
|
@ -102,7 +102,7 @@ bool ProxyFactory::PortClose(const Pair& args, Stream& result)
|
|||
|
||||
bool ProxyFactory::Write(const Pair& args, Stream& result)
|
||||
{
|
||||
debug_printf("ProxyFac Write\r\n");
|
||||
// debug_printf("ProxyFac Write\r\n");
|
||||
auto port = GetPort(args);
|
||||
|
||||
auto ms = (MemoryStream&)result;
|
||||
|
@ -152,7 +152,7 @@ bool ProxyFactory::Read(const Pair& args, Stream& result)
|
|||
|
||||
bool ProxyFactory::GetConfig(const Pair& args, Stream& result)
|
||||
{
|
||||
debug_printf("ProxyFac GetConfig\r\n");
|
||||
// debug_printf("ProxyFac GetConfig\r\n");
|
||||
Proxy* port = GetPort(args);
|
||||
|
||||
auto ms = (MemoryStream&)result;
|
||||
|
@ -176,7 +176,7 @@ bool ProxyFactory::GetConfig(const Pair& args, Stream& result)
|
|||
auto name = cfg.Keys();
|
||||
auto value = cfg.Values();
|
||||
|
||||
debug_printf("cfg count : %d value count : %d\t\t", name.Count(), value.Count());
|
||||
// debug_printf("cfg count : %d value count : %d\t\t", name.Count(), value.Count());
|
||||
String str;
|
||||
|
||||
for (int i = 0; i < cfg.Count(); i++)
|
||||
|
@ -205,7 +205,7 @@ bool ProxyFactory::GetConfig(const Pair& args, Stream& result)
|
|||
|
||||
bool ProxyFactory::SetConfig(const Pair& args, Stream& result)
|
||||
{
|
||||
debug_printf("ProxyFac SetConfig\r\n");
|
||||
// debug_printf("ProxyFac SetConfig\r\n");
|
||||
Proxy* port = GetPort(args);
|
||||
|
||||
auto ms = (MemoryStream&)result;
|
||||
|
@ -239,8 +239,7 @@ bool ProxyFactory::SetConfig(const Pair& args, Stream& result)
|
|||
|
||||
bool ProxyFactory::QueryPorts(const Pair& args, Stream& result)
|
||||
{
|
||||
debug_printf("ProxyFac QueryPorts\r\n");
|
||||
|
||||
// debug_printf("ProxyFac QueryPorts\r\n");
|
||||
auto portnames = Proxys.Keys();
|
||||
String name;
|
||||
for (int i = 0; i < Proxys.Count(); i++)
|
||||
|
|
Loading…
Reference in New Issue