0801,0802,pandora 底板 默认启用定时策略

This commit is contained in:
cdyong 2016-12-15 11:19:06 +08:00
parent 97727ccc5d
commit 79cdde44a1
4 changed files with 138 additions and 105 deletions

View File

@ -192,7 +192,7 @@ void AP0801::InitClient()
Client = tc; Client = tc;
tc->MaxNotActive = 480000; tc->MaxNotActive = 480000;
InitAlarm();
// 重启 // 重启
tc->Register("Gateway/Restart", &TokenClient::InvokeRestart, tc); tc->Register("Gateway/Restart", &TokenClient::InvokeRestart, tc);
// 重置 // 重置

View File

@ -14,36 +14,37 @@
#include "..\App\FlushPort.h" #include "..\App\FlushPort.h"
AP0802 * AP0802::Current = nullptr; AP0802 * AP0802::Current = nullptr;
static TokenClient* Client = nullptr; // 令牌客户端
AP0802::AP0802() AP0802::AP0802()
{ {
Host = nullptr; Host = nullptr;
HostAP = nullptr; HostAP = nullptr;
Client = nullptr; Client = nullptr;
Data = nullptr; Data = nullptr;
Size = 0; Size = 0;
// Control 打开情况标识 // Control 打开情况标识
NetMaster = false; NetMaster = false;
NetBra = false; NetBra = false;
EspMaster = false; EspMaster = false;
EspBra = false; EspBra = false;
Current = this; Current = this;
HardwareVer = HardwareVerLast; HardwareVer = HardwareVerLast;
} }
void AP0802::Init(ushort code, cstring name, COM message) void AP0802::Init(ushort code, cstring name, COM message)
{ {
auto& sys = (TSys&)Sys; auto& sys = (TSys&)Sys;
sys.Code = code; sys.Code = code;
sys.Name = (char*)name; sys.Name = (char*)name;
// 初始化系统 // 初始化系统
sys.Init(); sys.Init();
#if DEBUG #if DEBUG
sys.MessagePort = message; // 指定printf输出的串口 sys.MessagePort = message; // 指定printf输出的串口
Sys.ShowInfo(); Sys.ShowInfo();
WatchDog::Start(20000, 10000); WatchDog::Start(20000, 10000);
#else #else
@ -51,7 +52,7 @@ void AP0802::Init(ushort code, cstring name, COM message)
#endif #endif
// Flash最后一块作为配置区 // Flash最后一块作为配置区
Config::Current = &Config::CreateFlash(); Config::Current = &Config::CreateFlash();
if (HardwareVer == HardwareVerLast) if (HardwareVer == HardwareVerLast)
{ {
@ -77,7 +78,7 @@ void AP0802::Init(ushort code, cstring name, COM message)
void* AP0802::InitData(void* data, int size) void* AP0802::InitData(void* data, int size)
{ {
// 启动信息 // 启动信息
auto hot = &HotConfig::Current(); auto hot = &HotConfig::Current();
hot->Times++; hot->Times++;
data = hot->Next(); data = hot->Next();
@ -88,17 +89,17 @@ void* AP0802::InitData(void* data, int size)
ds[0] = size; ds[0] = size;
} }
Data = data; Data = data;
Size = size; Size = size;
return data; return data;
} }
void AP0802::InitLeds() void AP0802::InitLeds()
{ {
for(int i=0; i<LedPins.Count(); i++) for (int i = 0; i < LedPins.Count(); i++)
{ {
auto port = new OutputPort(LedPins[i]); auto port = new OutputPort(LedPins[i]);
port->Open(); port->Open();
Leds.Add(port); Leds.Add(port);
} }
@ -110,14 +111,43 @@ void ButtonOnpress(InputPort* port, bool down, void* param)
AP0802::OnLongPress(port, down); AP0802::OnLongPress(port, down);
} }
static void OnAlarm(AlarmItem& item)
{
// 1长度n + 1类型 + 1偏移 + (n-2)数据
auto bs = item.GetData();
debug_printf("OnAlarm ");
bs.Show(true);
if (bs[1] == 0x06)
{
auto client = Client;
client->Store.Write(bs[2], bs.Sub(3, bs[0] - 2));
// 主动上报状态
client->ReportAsync(bs[2], bs[0] - 2);
}
}
void AP0802::InitAlarm()
{
if (!Client)return;
if (!AlarmObj)AlarmObj = new Alarm();
Client->Register("Policy/AlarmSet", &Alarm::Set, AlarmObj);
Client->Register("Policy/AlarmGet", &Alarm::Get, AlarmObj);
AlarmObj->OnAlarm = OnAlarm;
AlarmObj->Start();
}
void AP0802::InitButtons(const Delegate2<InputPort&, bool>& press) void AP0802::InitButtons(const Delegate2<InputPort&, bool>& press)
{ {
for(int i=0; i<ButtonPins.Count(); i++) for (int i = 0; i < ButtonPins.Count(); i++)
{ {
auto port = new InputPort(ButtonPins[i]); auto port = new InputPort(ButtonPins[i]);
port->Invert = true; port->Invert = true;
port->State = i; port->State = i;
port->Press = press; port->Press = press;
port->UsePress(); port->UsePress();
port->Open(); port->Open();
Buttons.Add(port); Buttons.Add(port);
@ -128,7 +158,7 @@ ISocketHost* AP0802::Create5500()
{ {
debug_printf("\r\nW5500::Create \r\n"); debug_printf("\r\nW5500::Create \r\n");
auto host = new W5500(Spi2, PE1, PD13); auto host = new W5500(Spi2, PE1, PD13);
host->NetReady.Bind(&AP0802::OpenClient, this); host->NetReady.Bind(&AP0802::OpenClient, this);
return host; return host;
@ -136,7 +166,7 @@ ISocketHost* AP0802::Create5500()
ISocketHost* AP0802::Create8266(bool apOnly) ISocketHost* AP0802::Create8266(bool apOnly)
{ {
auto host = new Esp8266(COM4, PE0, PD3); auto host = new Esp8266(COM4, PE0, PD3);
// 初次需要指定模式 否则为 Wire // 初次需要指定模式 否则为 Wire
bool join = host->SSID && *host->SSID; bool join = host->SSID && *host->SSID;
@ -165,16 +195,18 @@ ISocketHost* AP0802::Create8266(bool apOnly)
void AP0802::InitClient() void AP0802::InitClient()
{ {
if(Client) return; if (Client) return;
auto tk = TokenConfig::Current; auto tk = TokenConfig::Current;
// 创建客户端 // 创建客户端
auto client = new TokenClient(); auto client = new TokenClient();
client->Cfg = tk; client->Cfg = tk;
// 需要使用本地连接 // 需要使用本地连接
//client->UseLocal(); //client->UseLocal();
//配置闹钟
InitAlarm();
Client = client; Client = client;
Client->MaxNotActive = 480000; Client->MaxNotActive = 480000;
@ -189,30 +221,30 @@ void AP0802::InitClient()
// 获取所有Ivoke命令 // 获取所有Ivoke命令
Client->Register("Api/All", &TokenClient::InvokeGetAllApi, Client); Client->Register("Api/All", &TokenClient::InvokeGetAllApi, Client);
if(Data && Size > 0) if (Data && Size > 0)
{ {
auto& ds = Client->Store; auto& ds = Client->Store;
ds.Data.Set(Data, Size); ds.Data.Set(Data, Size);
} }
// 如果若干分钟后仍然没有打开令牌客户端,则重启系统 // 如果若干分钟后仍然没有打开令牌客户端,则重启系统
Sys.AddTask( Sys.AddTask(
[](void* p){ [](void* p) {
auto& client = *(TokenClient*)p; auto& client = *(TokenClient*)p;
if(!client.Opened) if (!client.Opened)
{ {
debug_printf("联网超时,准备重启系统!\r\n\r\n"); debug_printf("联网超时,准备重启系统!\r\n\r\n");
Sys.Reboot(); Sys.Reboot();
} }
}, },
client, 8 * 60 * 1000, -1, "联网检查"); client, 8 * 60 * 1000, -1, "联网检查");
} }
void AP0802::Register(int index, IDataPort& dp) void AP0802::Register(int index, IDataPort& dp)
{ {
if(!Client) return; if (!Client) return;
auto& ds = Client->Store; auto& ds = Client->Store;
ds.Register(index, dp); ds.Register(index, dp);
} }
@ -234,17 +266,17 @@ void AP0802::OpenClient(ISocketHost& host)
debug_printf("\r\n OpenClient \r\n"); debug_printf("\r\n OpenClient \r\n");
// 网络就绪后,打开指示灯 // 网络就绪后,打开指示灯
auto net = dynamic_cast<W5500*>(&host); auto net = dynamic_cast<W5500*>(&host);
if(net && !net->Led) net->SetLed(*Leds[0]); if (net && !net->Led) net->SetLed(*Leds[0]);
auto esp = dynamic_cast<Esp8266*>(&host); auto esp = dynamic_cast<Esp8266*>(&host);
if(esp && !esp->Led) esp->SetLed(*Leds[1]); if (esp && !esp->Led) esp->SetLed(*Leds[1]);
auto tk = TokenConfig::Current; auto tk = TokenConfig::Current;
NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355); NetUri uri(NetType::Udp, IPAddress::Broadcast(), 3355);
// 避免重复打开 // 避免重复打开
if(Host) if (Host)
{ {
if (Host == esp) // 8266 作为Host的时候 使用 Master 和广播端口两个 HostAP 为空 if (Host == esp) // 8266 作为Host的时候 使用 Master 和广播端口两个 HostAP 为空
{ {
@ -317,21 +349,21 @@ void AP0802::OpenClient(ISocketHost& host)
TokenController* AP0802::AddControl(ISocketHost& host, const NetUri& uri, ushort localPort) TokenController* AP0802::AddControl(ISocketHost& host, const NetUri& uri, ushort localPort)
{ {
// 创建连接服务器的Socket // 创建连接服务器的Socket
auto socket = host.CreateRemote(uri); auto socket = host.CreateRemote(uri);
// 创建连接服务器的控制器 // 创建连接服务器的控制器
auto ctrl = new TokenController(); auto ctrl = new TokenController();
//ctrl->Port = dynamic_cast<ITransport*>(socket); //ctrl->Port = dynamic_cast<ITransport*>(socket);
ctrl->Socket = socket; ctrl->Socket = socket;
// 创建客户端 // 创建客户端
auto client = Client; auto client = Client;
if(localPort == 0) if (localPort == 0)
client->Master = ctrl; client->Master = ctrl;
else else
{ {
socket->Local.Port = localPort; socket->Local.Port = localPort;
ctrl->ShowRemote = true; ctrl->ShowRemote = true;
client->Controls.Add(ctrl); client->Controls.Add(ctrl);
} }
@ -357,17 +389,17 @@ Host为空 或 AP/STA_AP
*/ */
void OnInitNet(void* param) void OnInitNet(void* param)
{ {
auto& bsp = *(AP0802*)param; auto& bsp = *(AP0802*)param;
// 检查是否连接网线 // 检查是否连接网线
auto host = (W5500*)bsp.Create5500(); auto host = (W5500*)bsp.Create5500();
// 软路由的DHCP要求很严格必须先把自己IP设为0 // 软路由的DHCP要求很严格必须先把自己IP设为0
host->IP = IPAddress::Any(); host->IP = IPAddress::Any();
if(host->Open()) if (host->Open())
{ {
host->EnableDNS(); host->EnableDNS();
host->EnableDHCP(); host->EnableDHCP();
bsp.Host = host; bsp.Host = host;
} }
else else
{ {
@ -375,25 +407,25 @@ void OnInitNet(void* param)
} }
// 没有接网线需要完整WiFi通道 // 没有接网线需要完整WiFi通道
if(!bsp.Host) if (!bsp.Host)
{ {
auto esp = bsp.Create8266(false); auto esp = bsp.Create8266(false);
if(esp) if (esp)
{ {
// 未组网时主机留空仅保留AP主机 // 未组网时主机留空仅保留AP主机
bool join = esp->SSID && *esp->SSID; bool join = esp->SSID && *esp->SSID;
if(join && esp->IsStation()) if (join && esp->IsStation())
bsp.Host = esp; bsp.Host = esp;
else else
bsp.HostAP = esp; bsp.HostAP = esp;
} }
} }
// 接了网线同时需要AP // 接了网线同时需要AP
else if(host->IsAP()) else if (host->IsAP())
{ {
// 如果Host已存在则8266仅作为AP // 如果Host已存在则8266仅作为AP
auto esp = bsp.Create8266(true); auto esp = bsp.Create8266(true);
if(esp) bsp.HostAP = esp; if (esp) bsp.HostAP = esp;
} }
// 打开DHCP完成时会打开客户端 // 打开DHCP完成时会打开客户端
@ -412,8 +444,8 @@ int Fix2401(const Buffer& bs)
{ {
//auto& bs = *(Buffer*)param; //auto& bs = *(Buffer*)param;
// 微网指令特殊处理长度 // 微网指令特殊处理长度
uint rs = bs.Length(); uint rs = bs.Length();
if(rs >= 8) if (rs >= 8)
{ {
rs = bs[5] + 8; rs = bs[5] + 8;
//if(rs < bs.Length()) bs.SetLength(rs); //if(rs < bs.Length()) bs.SetLength(rs);
@ -429,29 +461,29 @@ ITransport* AP0802::Create2401(SPI spi_, Pin ce, Pin irq, Pin power, bool powerI
static NRF24L01 nrf; static NRF24L01 nrf;
nrf.Init(&spi, ce, irq, power); nrf.Init(&spi, ce, irq, power);
auto tc = TinyConfig::Create(); auto tc = TinyConfig::Create();
if(tc->Channel == 0) if (tc->Channel == 0)
{ {
tc->Channel = 120; tc->Channel = 120;
tc->Speed = 250; tc->Speed = 250;
} }
if(tc->Interval == 0) if (tc->Interval == 0)
{ {
tc->Interval= 40; tc->Interval = 40;
tc->Timeout = 1000; tc->Timeout = 1000;
} }
nrf.AutoAnswer = false; nrf.AutoAnswer = false;
nrf.DynPayload = false; nrf.DynPayload = false;
nrf.Channel = tc->Channel; nrf.Channel = tc->Channel;
//nrf.Channel = 120; //nrf.Channel = 120;
nrf.Speed = tc->Speed; nrf.Speed = tc->Speed;
nrf.FixData = Fix2401; nrf.FixData = Fix2401;
//if(WirelessLed) net->Led = CreateFlushPort(WirelessLed); //if(WirelessLed) net->Led = CreateFlushPort(WirelessLed);
nrf.Master = true; nrf.Master = true;
return &nrf; return &nrf;
} }
@ -460,12 +492,12 @@ ITransport* AP0802::Create2401()
{ {
auto port = new FlushPort(); auto port = new FlushPort();
port->Port = Leds[2]; port->Port = Leds[2];
return Create2401(Spi3,PD12,PE3,PE6,true,port); return Create2401(Spi3, PD12, PE3, PE6, true, port);
} }
void AP0802::Restore() void AP0802::Restore()
{ {
if(Client) Client->Reset("按键重置"); if (Client) Client->Reset("按键重置");
} }
void AP0802::OnLongPress(InputPort* port, bool down) void AP0802::OnLongPress(InputPort* port, bool down)
@ -475,14 +507,14 @@ void AP0802::OnLongPress(InputPort* port, bool down)
debug_printf("Press P%c%d Time=%d ms\r\n", _PIN_NAME(port->_Pin), port->PressTime); debug_printf("Press P%c%d Time=%d ms\r\n", _PIN_NAME(port->_Pin), port->PressTime);
ushort time = port->PressTime; ushort time = port->PressTime;
auto client = AP0802::Current->Client; auto client =Client;
if (time >= 5000 && time < 10000) if (time >= 5000 && time < 10000)
{ {
if(client) client->Reset("按键重置"); if (client) client->Reset("按键重置");
} }
else if (time >= 3000) else if (time >= 3000)
{ {
if(client) client->Reboot("按键重启"); if (client) client->Reboot("按键重启");
Sys.Reboot(1000); Sys.Reboot(1000);
} }
} }
@ -514,14 +546,14 @@ Host为空 或 AP/STA_AP
*/ */
/* /*
NRF24L01+ (SPI3) | W5500 (SPI2) NRF24L01+ (SPI3) | W5500 (SPI2)
NSS | NSS NSS | NSS
CLK | SCK CLK | SCK
MISO | MISO MISO | MISO
MOSI | MOSI MOSI | MOSI
PE3 IRQ | PE1 INT(IRQ) PE3 IRQ | PE1 INT(IRQ)
PD12 CE | PD13 NET_NRST PD12 CE | PD13 NET_NRST
PE6 POWER | POWER PE6 POWER | POWER
ESP8266 (COM4) ESP8266 (COM4)
TX TX

View File

@ -6,6 +6,7 @@
#include "TokenNet\TokenClient.h" #include "TokenNet\TokenClient.h"
#include "Device\Port.h" #include "Device\Port.h"
#include "App\Alarm.h"
#define HardwareVerFist 0 #define HardwareVerFist 0
#define HardwareVerAt160712 1 #define HardwareVerAt160712 1
@ -25,7 +26,6 @@ public:
ISocketHost* Host; // 网络主机 ISocketHost* Host; // 网络主机
ISocketHost* HostAP; // 网络主机 ISocketHost* HostAP; // 网络主机
TokenClient* Client; // 令牌客户端
byte HardwareVer; byte HardwareVer;
AP0802(); AP0802();
@ -37,7 +37,7 @@ public:
// 设置数据区 // 设置数据区
void* InitData(void* data, int size); void* InitData(void* data, int size);
void Register(int index, IDataPort& dp); void Register(int index, IDataPort& dp);
void InitAlarm();
void InitLeds(); void InitLeds();
void InitButtons(const Delegate2<InputPort&, bool>& press); void InitButtons(const Delegate2<InputPort&, bool>& press);
void InitPort(); void InitPort();
@ -54,7 +54,7 @@ public:
void InitClient(); void InitClient();
void InitNet(); void InitNet();
void Restore(); void Restore();
static void OnLongPress(InputPort* port, bool down); static void OnLongPress(InputPort* port, bool down);
private: private:
@ -65,7 +65,7 @@ private:
bool NetBra; bool NetBra;
bool EspMaster; bool EspMaster;
bool EspBra; bool EspBra;
Alarm* AlarmObj;
// uint Flag; // 内部使用 标识Controller创建情况 // uint Flag; // 内部使用 标识Controller创建情况
void OpenClient(ISocketHost& host); void OpenClient(ISocketHost& host);

View File

@ -102,6 +102,7 @@ void PA0903::InitClient()
Client = client; Client = client;
InitAlarm();
// 重启 // 重启
Client->Register("Gateway/Restart", &TokenClient::InvokeRestart, Client); Client->Register("Gateway/Restart", &TokenClient::InvokeRestart, Client);
// 重置 // 重置