使用指针来操作配置,而不是全局对象

This commit is contained in:
nnhy 2015-09-04 09:30:55 +00:00
parent 9b7f339b32
commit 272b9c7bb6
7 changed files with 28 additions and 25 deletions

View File

@ -1,6 +1,6 @@
#include "Config.h"
TConfig Config;
//TConfig Config;
// 初始化各字段为0
void TConfig::Init()

View File

@ -36,6 +36,6 @@ public:
#pragma pack(pop) // 恢复对齐状态
extern TConfig Config;
//extern TConfig Config;
#endif

View File

@ -6,9 +6,9 @@
#include "JoinMessage.h"
bool OnClientReceived(Message& msg, void* param);
static bool OnClientReceived(Message& msg, void* param);
void TinyClientTask(void* param);
static void TinyClientTask(void* param);
/******************************** 初始化和开关 ********************************/
@ -26,6 +26,8 @@ TinyClient::TinyClient(TinyController* control)
Received = NULL;
Param = NULL;
Config = NULL;
_TaskID = 0;
}
@ -40,12 +42,12 @@ void TinyClient::Open()
_TaskID = Sys.AddTask(TinyClientTask, this, 0, 5000000, "客户端服务");
if(Config.Address > 0 && Config.Server > 0)
if(Config->Address > 0 && Config->Server > 0)
{
Control->Address = Config.Address;
Server = Config.Server;
Control->Address = Config->Address;
Server = Config->Server;
Password.Load(Config.Password, ArrayLength(Config.Password));
Password.Load(Config->Password, ArrayLength(Config->Password));
}
}
@ -268,22 +270,22 @@ bool TinyClient::OnJoin(const TinyMessage& msg)
Control->Address = dm.Address;
Password = dm.Password;
Password.Save(Config.Password, ArrayLength(Config.Password));
Password.Save(Config->Password, ArrayLength(Config->Password));
// 记住服务端地址
Server = dm.Server;
Config.Channel = dm.Channel;
Config.Speed = dm.Speed == 0 ? 250 : (dm.Speed == 1 ? 1000 : 2000);
Config->Channel = dm.Channel;
Config->Speed = dm.Speed == 0 ? 250 : (dm.Speed == 1 ? 1000 : 2000);
// 服务端组网密码,退网使用
Config.ServerKey[0] = dm.HardID.Length();
dm.HardID.Save(Config.ServerKey, ArrayLength(Config.ServerKey));
Config->ServerKey[0] = dm.HardID.Length();
dm.HardID.Save(Config->ServerKey, ArrayLength(Config->ServerKey));
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, Config->Speed);
Password.Show(true);
// 取消Join任务启动Ping任务
ushort time = Config.PingTime;
ushort time = Config->PingTime;
if(time < 5) time = 5;
Sys.SetTaskPeriod(_TaskID, time * 1000000);
@ -302,7 +304,7 @@ bool TinyClient::OnDisjoin(const TinyMessage& msg)
// 心跳
void TinyClient::Ping()
{
ushort off = Config.OfflineTime;
ushort off = Config->OfflineTime;
if(off < 10) off = 10;
if(LastActive > 0 && LastActive + off * 1000000 < Time.Current())
{

View File

@ -14,6 +14,7 @@ private:
public:
TinyController* Control;
TConfig* Config;
byte Server; // 服务端地址
ushort Type; // 设备类型。两个字节可做二级分类

View File

@ -9,12 +9,12 @@
/******************************** TinyServer ********************************/
bool OnServerReceived(Message& msg, void* param);
static bool OnServerReceived(Message& msg, void* param);
TinyServer::TinyServer(TinyController* control)
{
Control = control;
Config = NULL;
DeviceType = Sys.Code;
Control->Received = OnServerReceived;
@ -149,7 +149,7 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
if(!dv)
{
// 以网关地址为基准,进行递增分配
byte addr = Config.Address;
byte addr = Config->Address;
// 查找该ID是否存在如果不同设备有相同ID则从0x02开始主动分配
if(FindDevice(id) != NULL)
{
@ -215,9 +215,9 @@ bool TinyServer::OnJoin(const TinyMessage& msg)
//JoinMessage dm;
dm.Reply = true;
dm.Server = Config.Address;
dm.Channel = Config.Channel;
dm.Speed = Config.Speed == 250 ? 0 : (Config.Speed == 1000 ? 1 : 2);;
dm.Server = Config->Address;
dm.Channel = Config->Channel;
dm.Speed = Config->Speed == 250 ? 0 : (Config->Speed == 1000 ? 1 : 2);;
dm.Address = dv->Address;
dm.Password = dv->Pass;

View File

@ -2,6 +2,7 @@
#define __TinyServer_H__
#include "Sys.h"
#include "Config.h"
#include "TinyMessage.h"
@ -16,6 +17,7 @@ private:
public:
TinyController* Control;
TConfig* Config;
ushort DeviceType; // 设备类型。两个字节可做二级分类

View File

@ -50,15 +50,13 @@ void Gateway::Start()
if(Server->Devices.Count() == 0)
{
Device* dv = new Device();
dv->Address = Config.Address;
dv->Address = Server->Config->Address;
dv->Type = Sys.Code;
dv->HardID.SetLength(16);
dv->HardID = Sys.ID;
dv->LastTime = Time.Current();
dv->Name = Sys.Name;
Config.Address = dv->Address;
Server->Devices.Add(dv);
}