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()
|
Alarm::Alarm()
|
||||||
{
|
{
|
||||||
AlarmTaskId = 0;
|
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)
|
bool Alarm::SetCfg(byte id, AlarmDataType& data)
|
||||||
|
@ -44,8 +75,9 @@ bool Alarm::SetCfg(byte id, AlarmDataType& data)
|
||||||
bf2 = bf;
|
bf2 = bf;
|
||||||
|
|
||||||
cfg.Save();
|
cfg.Save();
|
||||||
// 修改过后要检查一下Task的时间
|
// 修改过后要检查一下Task的时间 // 取消下次动作并重新计算
|
||||||
// xxxx
|
NextAlarmIds.Clear();
|
||||||
|
Start();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,61 +94,114 @@ bool Alarm::GetCfg(byte id, AlarmDataType& data)
|
||||||
|
|
||||||
int Alarm::CalcNextTime(AlarmDataType& data)
|
int Alarm::CalcNextTime(AlarmDataType& data)
|
||||||
{
|
{
|
||||||
auto dt = DateTime::Now();
|
debug_printf("CalcNextTime :");
|
||||||
|
auto now = DateTime::Now();
|
||||||
byte type = data.Type.ToByte();
|
byte type = data.Type.ToByte();
|
||||||
byte week = dt.DayOfWeek();
|
byte week = now.DayOfWeek();
|
||||||
if (type & 1 << week)
|
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;
|
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;
|
AlarmConfig cfg;
|
||||||
cfg.Load();
|
cfg.Load();
|
||||||
|
|
||||||
int nextTimeMs;
|
int miniTime = Int_Max;
|
||||||
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;
|
|
||||||
|
|
||||||
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()
|
void Alarm::AlarmTask()
|
||||||
{
|
{
|
||||||
|
debug_printf("AlarmTask");
|
||||||
// 获取定时的数据
|
// 获取定时的数据
|
||||||
AlarmDataType data;
|
AlarmDataType data;
|
||||||
if (AfterAlarmId != 0xff)
|
auto now = DateTime::Now();
|
||||||
GetCfg(AfterAlarmId, data);
|
now.Ms = 0;
|
||||||
// 执行动作 DoSomething(data);
|
for (int i = 0; i < NextAlarmIds.Count(); i++)
|
||||||
|
|
||||||
// 找到下一个定时器动作的时间
|
|
||||||
AfterAlarmId = FindAfter();
|
|
||||||
if (AfterAlarmId != 0xff)
|
|
||||||
{
|
{
|
||||||
Sys.SetTask(AlarmTaskId, true, NextAlarmMs);
|
byte NextAlarmId = NextAlarmIds[0];
|
||||||
return;
|
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()
|
void Alarm::Start()
|
||||||
{
|
{
|
||||||
|
debug_printf("Alarm::Start\r\n");
|
||||||
if (!AlarmTaskId)AlarmTaskId = Sys.AddTask(&Alarm::AlarmTask, this, -1, -1, "AlarmTask");
|
if (!AlarmTaskId)AlarmTaskId = Sys.AddTask(&Alarm::AlarmTask, this, -1, -1, "AlarmTask");
|
||||||
Sys.SetTask(AlarmTaskId,true);
|
Sys.SetTask(AlarmTaskId,true);
|
||||||
}
|
}
|
||||||
|
|
21
App/Alarm.h
21
App/Alarm.h
|
@ -5,6 +5,7 @@
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
#include "Port.h"
|
#include "Port.h"
|
||||||
#include "..\Config.h"
|
#include "..\Config.h"
|
||||||
|
#include "Message\BinaryPair.h"
|
||||||
|
|
||||||
class ByteStruct2 // 位域基类
|
class ByteStruct2 // 位域基类
|
||||||
{
|
{
|
||||||
|
@ -45,6 +46,11 @@ class Alarm
|
||||||
public:
|
public:
|
||||||
Alarm();
|
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 SetCfg(byte id, AlarmDataType& data);
|
||||||
bool GetCfg(byte id, AlarmDataType& data);
|
bool GetCfg(byte id, AlarmDataType& data);
|
||||||
|
|
||||||
|
@ -52,12 +58,21 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint AlarmTaskId;
|
uint AlarmTaskId;
|
||||||
|
List<int>NextAlarmIds; // 下次运行的编号,允许多组定时器定时时间相同
|
||||||
|
int NextAlarmMs; // 下次闹钟时间
|
||||||
|
|
||||||
byte AfterAlarmId; // 0xff 无效
|
|
||||||
int NextAlarmMs; // 下次闹钟时间
|
|
||||||
void AlarmTask();
|
void AlarmTask();
|
||||||
byte FindAfter();
|
byte FindNext(int& nextTime);
|
||||||
int CalcNextTime(AlarmDataType& data);
|
int CalcNextTime(AlarmDataType& data);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// class AlarmActivity
|
||||||
|
// {
|
||||||
|
// public:
|
||||||
|
// DataStore* store;
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// };
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -23,6 +23,7 @@ AP0801::AP0801()
|
||||||
HostAP = nullptr;
|
HostAP = nullptr;
|
||||||
Client = nullptr;
|
Client = nullptr;
|
||||||
ProxyFac = nullptr;
|
ProxyFac = nullptr;
|
||||||
|
AlarmObj = nullptr;
|
||||||
|
|
||||||
Data = nullptr;
|
Data = nullptr;
|
||||||
Size = 0;
|
Size = 0;
|
||||||
|
@ -377,16 +378,21 @@ void AP0801::InitProxy()
|
||||||
}
|
}
|
||||||
ProxyFac = ProxyFactory::Create();
|
ProxyFac = ProxyFactory::Create();
|
||||||
|
|
||||||
//ComProxy* proxyCom1 = new ComProxy(COM2);
|
|
||||||
ProxyFac->Register(new ComProxy(COM2));
|
ProxyFac->Register(new ComProxy(COM2));
|
||||||
ProxyFac->Register(new ComProxy(COM5));
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ProxyFac->Open(Client);
|
ProxyFac->Open(Client);
|
||||||
// ProxyFac->AutoStart(); // 自动启动的设备 需要保证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 ********************************/
|
/******************************** 2401 ********************************/
|
||||||
|
|
||||||
/*int Fix2401(const Buffer& bs)
|
/*int Fix2401(const Buffer& bs)
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include "TokenNet\TokenClient.h"
|
#include "TokenNet\TokenClient.h"
|
||||||
#include "Message\ProxyFactory.h"
|
#include "Message\ProxyFactory.h"
|
||||||
|
#include "App\Alarm.h"
|
||||||
|
|
||||||
// 阿波罗0801/0802
|
// 阿波罗0801/0802
|
||||||
class AP0801
|
class AP0801
|
||||||
|
@ -23,6 +24,7 @@ public:
|
||||||
ISocketHost* HostAP; // 网络主机
|
ISocketHost* HostAP; // 网络主机
|
||||||
TokenClient* Client; // 令牌客户端
|
TokenClient* Client; // 令牌客户端
|
||||||
ProxyFactory* ProxyFac; // 透传管理器
|
ProxyFactory* ProxyFac; // 透传管理器
|
||||||
|
Alarm* AlarmObj;
|
||||||
|
|
||||||
AP0801();
|
AP0801();
|
||||||
|
|
||||||
|
@ -48,6 +50,7 @@ public:
|
||||||
void InitClient();
|
void InitClient();
|
||||||
void InitNet();
|
void InitNet();
|
||||||
void InitProxy();
|
void InitProxy();
|
||||||
|
void InitAlarm();
|
||||||
|
|
||||||
void Restore();
|
void Restore();
|
||||||
void OnLongPress(InputPort* port, bool down);
|
void OnLongPress(InputPort* port, bool down);
|
||||||
|
|
|
@ -22,7 +22,7 @@ bool ProxyFactory::Open(TokenClient* client)
|
||||||
debug_printf("ProxyFac Open");
|
debug_printf("ProxyFac Open");
|
||||||
if (!client)return false;
|
if (!client)return false;
|
||||||
|
|
||||||
debug_printf(" Register");
|
// debug_printf(" Register");
|
||||||
Client = client;
|
Client = client;
|
||||||
Client->Register("Proxy/GetConfig", &ProxyFactory::GetConfig, this);
|
Client->Register("Proxy/GetConfig", &ProxyFactory::GetConfig, this);
|
||||||
Client->Register("Proxy/SetConfig", &ProxyFactory::SetConfig, this);
|
Client->Register("Proxy/SetConfig", &ProxyFactory::SetConfig, this);
|
||||||
|
@ -37,7 +37,7 @@ bool ProxyFactory::Open(TokenClient* client)
|
||||||
|
|
||||||
bool ProxyFactory::Register(Proxy* dev)
|
bool ProxyFactory::Register(Proxy* dev)
|
||||||
{
|
{
|
||||||
debug_printf("ProxyFac RegPort");
|
// debug_printf("ProxyFac RegPort");
|
||||||
String name = dev->Name;
|
String name = dev->Name;
|
||||||
name.Show(true);
|
name.Show(true);
|
||||||
Proxys.Add(dev->Name, dev);
|
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)
|
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 port = GetPort(args);
|
||||||
|
|
||||||
auto ms = (MemoryStream&)result;
|
auto ms = (MemoryStream&)result;
|
||||||
|
@ -152,7 +152,7 @@ bool ProxyFactory::Read(const Pair& args, Stream& result)
|
||||||
|
|
||||||
bool ProxyFactory::GetConfig(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);
|
Proxy* port = GetPort(args);
|
||||||
|
|
||||||
auto ms = (MemoryStream&)result;
|
auto ms = (MemoryStream&)result;
|
||||||
|
@ -176,7 +176,7 @@ bool ProxyFactory::GetConfig(const Pair& args, Stream& result)
|
||||||
auto name = cfg.Keys();
|
auto name = cfg.Keys();
|
||||||
auto value = cfg.Values();
|
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;
|
String str;
|
||||||
|
|
||||||
for (int i = 0; i < cfg.Count(); i++)
|
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)
|
bool ProxyFactory::SetConfig(const Pair& args, Stream& result)
|
||||||
{
|
{
|
||||||
debug_printf("ProxyFac SetConfig\r\n");
|
// debug_printf("ProxyFac SetConfig\r\n");
|
||||||
Proxy* port = GetPort(args);
|
Proxy* port = GetPort(args);
|
||||||
|
|
||||||
auto ms = (MemoryStream&)result;
|
auto ms = (MemoryStream&)result;
|
||||||
|
@ -239,8 +239,7 @@ bool ProxyFactory::SetConfig(const Pair& args, Stream& result)
|
||||||
|
|
||||||
bool ProxyFactory::QueryPorts(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();
|
auto portnames = Proxys.Keys();
|
||||||
String name;
|
String name;
|
||||||
for (int i = 0; i < Proxys.Count(); i++)
|
for (int i = 0; i < Proxys.Count(); i++)
|
||||||
|
|
Loading…
Reference in New Issue