AlarmConfig 数据结构
This commit is contained in:
parent
5081dedb7c
commit
c4203186d7
111
App/Alarm.cpp
111
App/Alarm.cpp
|
@ -1,6 +1,115 @@
|
||||||
#include "Alarm.h"
|
#include "Alarm.h"
|
||||||
|
|
||||||
|
#define Int_Max 2147483647
|
||||||
|
|
||||||
|
class AlarmConfig :public ConfigBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
byte Length;
|
||||||
|
AlarmDataType Data[20];
|
||||||
|
byte TagEnd;
|
||||||
|
|
||||||
|
AlarmConfig();
|
||||||
|
virtual void Init();
|
||||||
|
};
|
||||||
|
|
||||||
|
AlarmConfig::AlarmConfig()
|
||||||
|
{
|
||||||
|
_Name = "AlarmCf";
|
||||||
|
_Start = &Length;
|
||||||
|
_End = &TagEnd;
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AlarmConfig::Init()
|
||||||
|
{
|
||||||
|
Buffer(Data, sizeof(Data)).Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/************************************************/
|
||||||
|
|
||||||
Alarm::Alarm()
|
Alarm::Alarm()
|
||||||
{
|
{
|
||||||
|
AlarmTaskId = 0;
|
||||||
|
AfterAlarmId = 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Alarm::SetCfg(byte id, AlarmDataType& data)
|
||||||
|
{
|
||||||
|
AlarmConfig cfg;
|
||||||
|
cfg.Load();
|
||||||
|
|
||||||
|
Buffer bf(&data, sizeof(AlarmDataType));
|
||||||
|
Buffer bf2(&cfg.Data[id].Enable, sizeof(AlarmDataType));
|
||||||
|
bf2 = bf;
|
||||||
|
|
||||||
|
cfg.Save();
|
||||||
|
// 修改过后要检查一下Task的时间
|
||||||
|
// xxxx
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Alarm::GetCfg(byte id, AlarmDataType& data)
|
||||||
|
{
|
||||||
|
AlarmConfig cfg;
|
||||||
|
cfg.Load();
|
||||||
|
|
||||||
|
Buffer bf(&data, sizeof(AlarmDataType));
|
||||||
|
Buffer bf2(&cfg.Data[id].Enable, sizeof(AlarmDataType));
|
||||||
|
bf = bf2;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Alarm::CalcNextTime(AlarmDataType& data)
|
||||||
|
{
|
||||||
|
auto dt = DateTime::Now();
|
||||||
|
if (data.Type.ToByte() & 1 << dt.DayOfWeek())
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
return Int_Max;
|
||||||
|
}
|
||||||
|
|
||||||
|
byte Alarm::FindAfter()
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Alarm::AlarmTask()
|
||||||
|
{
|
||||||
|
// 执行动作
|
||||||
|
|
||||||
|
// 找到下一个定时器动作的时间
|
||||||
|
AfterAlarmId = FindAfter();
|
||||||
|
if (AfterAlarmId != 0xff)
|
||||||
|
{
|
||||||
|
Sys.SetTask(AlarmTaskId, true, NextAlarmMs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Sys.SetTask(AlarmTaskId, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Alarm::Start()
|
||||||
|
{
|
||||||
|
if (!AlarmTaskId)AlarmTaskId = Sys.AddTask(&Alarm::AlarmTask, this, -1, -1, "AlarmTask");
|
||||||
|
Sys.SetTask(AlarmTaskId,true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
48
App/Alarm.h
48
App/Alarm.h
|
@ -4,14 +4,60 @@
|
||||||
#include "Sys.h"
|
#include "Sys.h"
|
||||||
#include "Timer.h"
|
#include "Timer.h"
|
||||||
#include "Port.h"
|
#include "Port.h"
|
||||||
|
#include "..\Config.h"
|
||||||
|
|
||||||
|
class ByteStruct2 // 位域基类
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Init(byte data = 0) { *(byte*)this = data; }
|
||||||
|
byte ToByte() { return *(byte*)this; }
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct:ByteStruct2
|
||||||
|
{
|
||||||
|
byte Sunday : 1;
|
||||||
|
byte Monday : 1;
|
||||||
|
byte Tuesday : 1;
|
||||||
|
byte Wednesday : 1;
|
||||||
|
byte Thursday : 1;
|
||||||
|
byte Friday : 1;
|
||||||
|
byte Saturday : 1;
|
||||||
|
byte Again : 1;
|
||||||
|
}AlarmType;
|
||||||
|
|
||||||
|
// 必须设定为1字节对齐,否则offsetof会得到错误的位置
|
||||||
|
#pragma pack(push) // 保存对齐状态
|
||||||
|
// 强制结构体紧凑分配空间
|
||||||
|
#pragma pack(1)
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
byte Enable;
|
||||||
|
AlarmType Type;
|
||||||
|
byte Hour;
|
||||||
|
byte Minutes;
|
||||||
|
byte Seconds;
|
||||||
|
byte Data[11];
|
||||||
|
}AlarmDataType;
|
||||||
|
#pragma pack(pop) // 恢复对齐状态
|
||||||
|
|
||||||
class Alarm
|
class Alarm
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Alarm();
|
Alarm();
|
||||||
|
|
||||||
// bool AddLine(byte id,byte* data);
|
bool SetCfg(byte id, AlarmDataType& data);
|
||||||
|
bool GetCfg(byte id, AlarmDataType& data);
|
||||||
|
|
||||||
|
void Start();
|
||||||
|
|
||||||
|
private:
|
||||||
|
byte AfterAlarmId; // 0xff 无效
|
||||||
|
|
||||||
|
uint AlarmTaskId;
|
||||||
|
int NextAlarmMs; // 下次闹钟时间
|
||||||
|
void AlarmTask();
|
||||||
|
byte FindAfter();
|
||||||
|
int CalcNextTime(AlarmDataType& data);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue