以网关地址为基准,进行递增分配

This commit is contained in:
nnhy 2015-09-04 09:59:36 +00:00
parent 272b9c7bb6
commit cb888f2e82
6 changed files with 12 additions and 22 deletions

View File

@ -13,7 +13,7 @@ void TConfig::Init()
void TConfig::LoadDefault()
{
Type = Sys.Code;
Kind = Sys.Code;
//Server = 0x01;
PingTime = 15;

View File

@ -17,7 +17,7 @@ public:
byte HardVer; // 硬件版本
byte SoftVer; // 软件版本
ushort Type; // 类型
ushort Kind; // 类型
byte Address; // 分配得到的设备地址
byte Password[16]; // 通信密码
byte Server; // 网关ID

View File

@ -6,7 +6,7 @@
Device::Device() : HardID(0), Name(0), Pass(0)
{
Address = 0;
Type = 0;
Kind = 0;
LastTime = 0;
DataSize = 0;
ConfigSize = 0;
@ -22,7 +22,7 @@ Device::Device() : HardID(0), Name(0), Pass(0)
void Device::Write(Stream& ms) const
{
ms.Write(Address);
ms.Write(Type);
ms.Write(Kind);
ms.WriteArray(HardID);
ms.Write(LastTime);
ms.Write(DataSize);
@ -33,7 +33,7 @@ void Device::Write(Stream& ms) const
void Device::Read(Stream& ms)
{
Address = ms.Read<byte>();
Type = ms.Read<ushort>();
Kind = ms.Read<ushort>();
//ms.ReadArray(HardID);
HardID = ms.ReadArray();
LastTime= ms.Read<ulong>();
@ -46,7 +46,7 @@ void Device::Read(Stream& ms)
String& Device::ToStr(String& str) const
{
str = str + "Address=0x" + Address;
str = str + " Type=" + (byte)(Type >> 8) + (byte)(Type & 0xFF);
str = str + " Kind=" + (byte)(Kind >> 8) + (byte)(Kind & 0xFF);
str = str + " Name=" + Name;
str = str + " HardID=" + HardID;

View File

@ -11,7 +11,7 @@ class Device : public Object
{
public:
byte Address; // 节点地址
ushort Type; // 类型
ushort Kind; // 类型
ByteArray HardID; // 硬件编码
ulong LastTime; // 活跃时间
ushort Version; // 版本

View File

@ -145,29 +145,21 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
byte id = msg.Src;
if(!id) return false;
// 根据硬件编码找设备
Device* dv = FindDevice(dm.HardID);
if(!dv)
{
// 以网关地址为基准,进行递增分配
byte addr = Config->Address;
// 查找该ID是否存在如果不同设备有相同ID则从0x02开始主动分配
if(FindDevice(id) != NULL)
{
id = addr;
while(FindDevice(++id) != NULL && id < 0xFF);
debug_printf("发现ID=0x%02X已分配为当前节点分配 0x%02X\r\n", msg.Src, id);
}
else
{
id = Devices.Count() + addr;
// 注意,网关可能来不及添加
if(id <= addr) id = addr + 1;
debug_printf("发现节点设备 0x%02X ,为其分配 0x%02X\r\n", msg.Src, id);
debug_printf("发现节点设备 0x%02X ,为其分配 0x%02X\r\n", dm.Kind, id);
}
dv = new Device();
dv->Address = id;
dv->Address = id;
Devices.Add(dv);
@ -179,11 +171,9 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
{
Current = dv;
dv->Type = dm.Kind;
dv->Kind = dm.Kind;
dv->HardID = dm.HardID;
dv->Version = dm.Version;
//dv->Switchs = dm.Switchs;
//dv->Analogs = dm.Analogs;
// 如果最后活跃时间超过60秒则认为是设备上线
if(dv->LastTime == 5 || dv->LastTime + 6000000 < Time.Current())

View File

@ -51,7 +51,7 @@ void Gateway::Start()
{
Device* dv = new Device();
dv->Address = Server->Config->Address;
dv->Type = Sys.Code;
dv->Kind = Sys.Code;
dv->HardID.SetLength(16);
dv->HardID = Sys.ID;
dv->LastTime = Time.Current();