微网配置独立

This commit is contained in:
Stone 2015-10-24 03:08:03 +00:00
parent 51549bcb03
commit 61aac14449
5 changed files with 17 additions and 76 deletions

View File

@ -218,31 +218,3 @@ HotConfig& HotConfig::Current()
return *(HotConfig*)dat;
}
// 初始化
TConfig::TConfig()
{
// 实际内存大小,减去头部大小
uint len = sizeof(this) - ((int)&Length - (int)this);
//memset(&Length, 0, len);
Length = len;
}
void TConfig::LoadDefault()
{
Kind = Sys.Code;
//Server = 0x01;
PingTime = 15;
OfflineTime = 60;
}
void TConfig::Write(Stream& ms)const
{
ms.Write((byte *)this, 0, sizeof(this[0]));
}
void TConfig::Read(Stream& ms)
{
memcpy((byte *)this, ms.GetBuffer(), sizeof(this[0]));
}

View File

@ -59,39 +59,8 @@ public:
};
// 配置信息
class TConfig
{
public:
ushort Length; // 数据长度
byte HardVer; // 硬件版本
byte SoftVer; // 软件版本
ushort Kind; // 类型
byte Address; // 分配得到的设备地址
byte Password[16]; // 通信密码
byte Server; // 网关ID
byte Channel; // 通道
ushort Speed; // 传输速度
byte ServerKey[16]; // 服务端组网密码退网时使用。一般6字节
ushort PingTime; // 心跳时间。秒
ushort OfflineTime; // 离线阀值时间。秒
ushort SleepTime; // 睡眠时间。秒
// 初始化各字段为0
TConfig();
void LoadDefault();
// 序列化到消息数据流
void Write(Stream& ms) const;
void Read(Stream& ms);
};
#pragma pack(pop) // 恢复对齐状态
//extern TConfig Config;
/*
1

View File

@ -1,6 +1,5 @@
#include "Time.h"
#include "Task.h"
#include "Config.h"
#include "TinyClient.h"
@ -26,7 +25,7 @@ TinyClient::TinyClient(TinyController* control)
Received = NULL;
Param = NULL;
Config = NULL;
Cfg = NULL;
_TaskID = 0;
@ -45,12 +44,12 @@ void TinyClient::Open()
_TaskID = Sys.AddTask(TinyClientTask, this, 0, 5000, "客户端服务");
if(Config->Address > 0 && Config->Server > 0)
if(Cfg->Address > 0 && Cfg->Server > 0)
{
Control->Address = Config->Address;
Server = Config->Server;
Control->Address = Cfg->Address;
Server = Cfg->Server;
Password.Load(Config->Password, ArrayLength(Config->Password));
Password.Load(Cfg->Password, ArrayLength(Cfg->Password));
}
}
@ -332,23 +331,23 @@ bool TinyClient::OnJoin(const TinyMessage& msg)
Control->Address = dm.Address;
Password = dm.Password;
Password.Save(Config->Password, ArrayLength(Config->Password));
Password.Save(Cfg->Password, ArrayLength(Cfg->Password));
// 记住服务端地址
Server = dm.Server;
Config->Channel = dm.Channel;
Config->Speed = dm.Speed == 0 ? 250 : (dm.Speed == 1 ? 1000 : 2000);
Cfg->Channel = dm.Channel;
Cfg->Speed = dm.Speed == 0 ? 250 : (dm.Speed == 1 ? 1000 : 2000);
// 服务端组网密码,退网使用
Config->ServerKey[0] = dm.HardID.Length();
dm.HardID.Save(Config->ServerKey, ArrayLength(Config->ServerKey));
Cfg->ServerKey[0] = dm.HardID.Length();
dm.HardID.Save(Cfg->ServerKey, ArrayLength(Cfg->ServerKey));
debug_printf("组网成功!\r\n");
//debug_printf("组网成功!由网关 0x%02X 分配得到节点地址 0x%02X ,频道:%d传输速率%dkbps密码", Server, dm.Address, dm.Channel, Config->Speed);
//debug_printf("组网成功!由网关 0x%02X 分配得到节点地址 0x%02X ,频道:%d传输速率%dkbps密码", Server, dm.Address, dm.Channel, Cfg->Speed);
//Password.Show(true); // 这里出错 问题未知
// 取消Join任务启动Ping任务
ushort time = Config->PingTime;
ushort time = Cfg->PingTime;
if(time < 5) time = 5;
if(time > 60) time = 60;
Sys.SetTaskPeriod(_TaskID, time * 1000);
@ -368,7 +367,7 @@ bool TinyClient::OnDisjoin(const TinyMessage& msg)
// 心跳
void TinyClient::Ping()
{
ushort off = Config->OfflineTime;
ushort off = Cfg->OfflineTime;
if(off < 10) off = 10;
if(LastActive > 0 && LastActive + off * 1000 < Time.Current())
{

View File

@ -2,8 +2,9 @@
#define __TinyClient_H__
#include "Sys.h"
#include "Config.h"
#include "TinyMessage.h"
#include "TinyConfig.h"
#include "Message\DataStore.h"
@ -15,7 +16,7 @@ private:
public:
TinyController* Control;
TConfig* Config;
TinyConfig* Cfg;
byte Server; // 服务端地址
ushort Type; // 设备类型。两个字节可做二级分类

View File

@ -4,9 +4,9 @@
#include "Sys.h"
#include "TinyMessage.h"
#include "TinyConfig.h"
#include "TinyNet\Device.h"
#include "TinyNet\TinyConfig.h"
/******************************** TinyServer ********************************/