服务端心跳和退网处理,设备ID共用处理

This commit is contained in:
cdyong 2015-11-07 09:04:36 +00:00
parent 8a11a5f210
commit d7faacab39
3 changed files with 43 additions and 13 deletions

View File

@ -32,10 +32,10 @@ struct TinyConfig
byte ServerKey[16]; // 服务端组网密码退网时使用。一般6字节 byte ServerKey[16]; // 服务端组网密码退网时使用。一般6字节
// 初始化各字段为0 //初始化各字段为0
//TinyConfig(); //TinyConfig();
//const TinyConfig& Default(); //const TinyConfig& Default();
byte StartSet; byte StartSet;
void LoadDefault(); void LoadDefault();

View File

@ -1,5 +1,6 @@
#include "Flash.h" #include "Flash.h"
#include "TinyServer.h" #include "TinyServer.h"
#include "Security\Crc.h"
#include "JoinMessage.h" #include "JoinMessage.h"
@ -300,11 +301,28 @@ bool TinyServer::OnDisjoin(const TinyMessage& msg)
return true; return true;
} }
bool TinyServer::Disjoin(TinyMessage& msg,uint crc)
{
msg.Code = 0x02;
Stream ms = msg.ToStream();
ms.Write(crc);
msg.Length = ms.Position();
Reply(msg);
return true;
}
// 心跳保持与对方的活动状态 // 心跳保持与对方的活动状态
bool TinyServer::OnPing(const TinyMessage& msg) bool TinyServer::OnPing(const TinyMessage& msg)
{ {
// 网关内没有相关节点信息时不鸟他 // 网关内没有相关节点信息时不鸟他
if(FindDevice(msg.Src) == NULL)return false; if(FindDevice(msg.Src) == NULL)return false;
// 准备一条响应指令
TinyMessage rs;
rs.Code = msg.Code;
rs.Dest = msg.Src;
rs.Sequence = msg.Sequence;
// 子操作码 // 子操作码
switch(msg.Data[0]) switch(msg.Data[0])
@ -312,11 +330,27 @@ bool TinyServer::OnPing(const TinyMessage& msg)
// 同步数据 // 同步数据
case 0x01: case 0x01:
{ {
Device* dv = Current; Device* dv = Current;
if(dv && msg.Length >= 4) byte ver = 0;
if(dv->Version > 1)
{
Stream ms(msg.Data, msg.Length);
ushort crc = ms.ReadUInt16();
ushort crc1 = Crc::Hash16(&dv->HardID, dv->HardID.Length());
if(crc ==crc1) ver = 2;
else
{
debug_printf("设备硬件Crc:%0x%08X,对比Crc%0x%08X \r\n",crc,crc1);
debug_printf("设备硬件ID");
dv->HardID.Show();
Disjoin(rs,crc);
return false;
}
}
if(dv && msg.Length >= ver + 4)
{ {
byte offset = msg.Data[1]; byte offset = msg.Data[ver + 1];
byte len = msg.Data[2]; byte len = msg.Data[ver + 2];
debug_printf("设备 0x%02X 同步数据(%d, %d到网关缓存 \r\n", dv->Address, offset, len); debug_printf("设备 0x%02X 同步数据(%d, %d到网关缓存 \r\n", dv->Address, offset, len);
int remain = dv->Store.Capacity() - offset; int remain = dv->Store.Capacity() - offset;
@ -330,12 +364,7 @@ bool TinyServer::OnPing(const TinyMessage& msg)
} }
} }
// 响应 Ping 指令,告诉客户端有多少指令需要等待执行
TinyMessage rs;
rs.Code = msg.Code;
rs.Dest = msg.Src;
rs.Sequence = msg.Sequence;
//todo。告诉客户端有多少待处理指令 //todo。告诉客户端有多少待处理指令
Reply(rs); Reply(rs);

View File

@ -62,7 +62,8 @@ public:
bool OnJoin(const TinyMessage& msg); bool OnJoin(const TinyMessage& msg);
bool ResetPassword(byte id); bool ResetPassword(byte id);
bool Disjoin(TinyMessage& msg,uint crc);
bool OnDisjoin(const TinyMessage& msg); bool OnDisjoin(const TinyMessage& msg);
// 心跳 // 心跳