Proxy公共配置提取到基类里面

另发现字典类扩容有问题
This commit is contained in:
WangQiang 2016-08-22 07:46:52 +00:00
parent 790d1da913
commit ce0aa67b49
4 changed files with 103 additions and 20 deletions

View File

@ -6,7 +6,8 @@
Proxy::Proxy()
{
Cache = nullptr;
CacheSize = 0;
CacheSize = 10;
BufferSize = 256;
TimeStamp = 0;
EnableStamp = false;
UploadTaskId= 0;
@ -28,6 +29,65 @@ bool Proxy::Close()
return true;
}
bool Proxy::SetConfig(Dictionary<cstring, int>& config, String& str)
{
int value;
cstring const ByteParam[] = { "cache","buffersize" };
int* ParamP[] = { &CacheSize, &BufferSize };
for (int i = 0; i < ArrayLength(ByteParam); i++)
{
value = 0;
if (config.TryGetValue(ByteParam[i], value))
{
*(ParamP[i]) = value;
}
}
cstring const ByteParam2[] = { "auto","timestamp" };
bool* ParamP2[] = { &AutoStart, &EnableStamp };
for (int i = 0; i < ArrayLength(ByteParam2); i++)
{
value = 0;
if (config.TryGetValue(ByteParam2[i], value))
{
*(ParamP2[i]) = value;
}
}
OnSetConfig(config, str);
SaveConfig();
return true;
}
bool Proxy::GetConfig(Dictionary<char *, int>& config)
{
LoadConfig();
// cstring const str[] = { "cache" ,"buffersize","auto","timestamp" };
//
// config.Add(str[0], CacheSize);
// config.Add(str[1], BufferSize);
//
// config.Add(str[2], AutoStart);
// config.Add(str[3], EnableStamp);
config.Add("cache", CacheSize);
config.Add("buffersize", BufferSize);
config.Add("auto", AutoStart);
config.Add("timestamp", EnableStamp);
// debug_printf("基础配置条数%d\r\n",config.Count());
OnGetConfig(config);
debug_printf("一共%d跳配置",config.Count());
return true;
}
void Proxy::UploadTask()
{
auto fac = ProxyFactory::Current;
@ -67,6 +127,7 @@ bool Proxy::LoadConfig()
cfg.Load();
CacheSize = cfg.CacheSize;
BufferSize = cfg.BufferSize;
EnableStamp = cfg.EnableStamp;
AutoStart = cfg.AutoStart;
@ -83,6 +144,7 @@ void Proxy::SaveConfig()
cfg.CacheSize = CacheSize;
cfg.EnableStamp = EnableStamp;
cfg.AutoStart = AutoStart;
cfg.BufferSize = BufferSize;
Stream st(cfg.PortCfg, sizeof(cfg.PortCfg));
OnSetConfig(st);
@ -104,14 +166,15 @@ ComProxy::ComProxy(COM com) :port(com)
cfg.Load();
if (cfg.New) // 首次运行直接打开拿到默认配置后关闭
{
debug_printf("初次使用,打开端口获取默认配置:");
port.Open();
baudRate = port._baudRate;
parity = port._parity; // USART_Parity_No;
dataBits = port._dataBits; // USART_WordLength_8b;
stopBits = port._stopBits; // USART_StopBits_1;
SaveConfig();
SaveConfig();
port.Close();
}
else
@ -144,7 +207,7 @@ bool ComProxy::OnClose()
return true;
}
bool ComProxy::SetConfig(Dictionary<cstring, int>& config, String& str)
bool ComProxy::OnSetConfig(Dictionary<cstring, int>& config, String& str)
{
int value;
if (config.TryGetValue("baudrate", value))
@ -152,7 +215,7 @@ bool ComProxy::SetConfig(Dictionary<cstring, int>& config, String& str)
port.SetBaudRate(value);
}
cstring ByteParam[] = { "parity","dataBits","stopBits" };
cstring const ByteParam[] = { "parity","dataBits","stopBits" };
ushort* ParamP[] = {&parity, &dataBits, &stopBits};
bool haveChang = false;
for (int i = 0; i < ArrayLength(ByteParam); i++)
@ -165,14 +228,19 @@ bool ComProxy::SetConfig(Dictionary<cstring, int>& config, String& str)
}
}
if (haveChang)port.Set(parity, dataBits, stopBits);
SaveConfig();
// SaveConfig();
return true;
}
bool ComProxy::GetConfig(Dictionary<cstring, int>& config)
bool ComProxy::OnGetConfig(Dictionary<char *, int>& config)
{
LoadConfig();
// char * const str[] = {"baudrate","parity" ,"dataBits" ,"stopBits" };
// config.Add(str[0],baudRate);
//
// config.Add(str[1], parity);
// config.Add(str[2], dataBits);
// config.Add(str[3], stopBits);
config.Add("baudrate", baudRate);
config.Add("parity", parity);

View File

@ -12,18 +12,19 @@ public:
MemoryStream* Cache; // 缓存空间
uint UploadTaskId; // 上传任务的ID
uint AutoTaskId; // 自动任务ID可以是定时Write数据
int CacheSize; // 缓存大小
bool EnableStamp; // 时间戳开关
int TimeStamp; // 时间戳
int CacheSize; // 缓存数据包个数
int BufferSize; // 缓冲区大小
bool EnableStamp; // 时间戳开关
bool AutoStart; // 自动启动
Proxy();
bool Open();
bool Close();
virtual bool SetConfig(Dictionary<cstring, int>& config, String& str) = 0;
virtual bool GetConfig(Dictionary<cstring, int>& config) = 0;
bool SetConfig(Dictionary<cstring, int>& config, String& str);
bool GetConfig(Dictionary<char *, int>& config);
virtual int Write(Buffer& data) = 0;
virtual int Read(Buffer& data, Buffer& input) = 0;
void UploadTask();
@ -37,6 +38,10 @@ private:
virtual bool OnOpen() = 0;
virtual bool OnClose() = 0;
virtual bool OnAutoTask() { return true; };
virtual bool OnSetConfig(Dictionary<cstring, int>& config, String& str) = 0;
virtual bool OnGetConfig(Dictionary<char *, int>& config) = 0;
virtual bool OnGetConfig(Stream& cfg) { return true; };
virtual bool OnSetConfig(Stream& cfg) { return true; };
};
@ -53,8 +58,8 @@ public:
ushort stopBits;
int baudRate;
virtual bool SetConfig(Dictionary<cstring, int>& config, String& str) override;
virtual bool GetConfig(Dictionary<cstring, int>& config) override;
virtual bool OnSetConfig(Dictionary<cstring, int>& config, String& str) override;
virtual bool OnGetConfig(Dictionary<char*, int>& config) override;
virtual int Write(Buffer& data) override;
virtual int Read(Buffer& data, Buffer& input) override;

View File

@ -9,8 +9,10 @@ class ProxyConfig : public ConfigBase
public:
int Length;
byte AutoStart; // 自动启动
int CacheSize; // 缓存大小
bool EnableStamp; // 时间戳开关
byte EnableStamp; // 时间戳开关
int CacheSize; // 缓存数据包个数
int BufferSize; // 缓冲区大小
byte PortCfg[64]; // 子类自定义配置
// byte FixedCmd[128]; // 定时任务的命令+数据

View File

@ -168,9 +168,15 @@ bool ProxyFactory::GetConfig(const Pair& args, Stream& result)
// port->GetConfig(str); // 调用端口的函数处理内容
// ms.Write(str);
Dictionary<cstring, int> cfg;
Dictionary<char *, int> cfg;
port->GetConfig(cfg); // 调用端口的函数处理内容
if (cfg.Count() < 1)
{
result.Write((byte)0);
return false;
}
// 数据先写进缓冲区ms2
MemoryStream ms2;
auto name = cfg.Keys();
@ -179,8 +185,10 @@ bool ProxyFactory::GetConfig(const Pair& args, Stream& result)
// debug_printf("cfg count : %d value count : %d\t\t", name.Count(), value.Count());
String str;
debug_printf("config:\r\n");
for (int i = 0; i < cfg.Count(); i++)
{
debug_printf("%s %d\r\n",name[i],value[i]);
str = str + name[i] + '=' + value[i];
if (i < cfg.Count() - 1)str = str + '&';
}