parent
790d1da913
commit
ce0aa67b49
|
@ -6,7 +6,8 @@
|
||||||
Proxy::Proxy()
|
Proxy::Proxy()
|
||||||
{
|
{
|
||||||
Cache = nullptr;
|
Cache = nullptr;
|
||||||
CacheSize = 0;
|
CacheSize = 10;
|
||||||
|
BufferSize = 256;
|
||||||
TimeStamp = 0;
|
TimeStamp = 0;
|
||||||
EnableStamp = false;
|
EnableStamp = false;
|
||||||
UploadTaskId= 0;
|
UploadTaskId= 0;
|
||||||
|
@ -28,6 +29,65 @@ bool Proxy::Close()
|
||||||
return true;
|
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()
|
void Proxy::UploadTask()
|
||||||
{
|
{
|
||||||
auto fac = ProxyFactory::Current;
|
auto fac = ProxyFactory::Current;
|
||||||
|
@ -67,6 +127,7 @@ bool Proxy::LoadConfig()
|
||||||
cfg.Load();
|
cfg.Load();
|
||||||
|
|
||||||
CacheSize = cfg.CacheSize;
|
CacheSize = cfg.CacheSize;
|
||||||
|
BufferSize = cfg.BufferSize;
|
||||||
EnableStamp = cfg.EnableStamp;
|
EnableStamp = cfg.EnableStamp;
|
||||||
AutoStart = cfg.AutoStart;
|
AutoStart = cfg.AutoStart;
|
||||||
|
|
||||||
|
@ -83,6 +144,7 @@ void Proxy::SaveConfig()
|
||||||
cfg.CacheSize = CacheSize;
|
cfg.CacheSize = CacheSize;
|
||||||
cfg.EnableStamp = EnableStamp;
|
cfg.EnableStamp = EnableStamp;
|
||||||
cfg.AutoStart = AutoStart;
|
cfg.AutoStart = AutoStart;
|
||||||
|
cfg.BufferSize = BufferSize;
|
||||||
|
|
||||||
Stream st(cfg.PortCfg, sizeof(cfg.PortCfg));
|
Stream st(cfg.PortCfg, sizeof(cfg.PortCfg));
|
||||||
OnSetConfig(st);
|
OnSetConfig(st);
|
||||||
|
@ -104,14 +166,15 @@ ComProxy::ComProxy(COM com) :port(com)
|
||||||
cfg.Load();
|
cfg.Load();
|
||||||
if (cfg.New) // 首次运行直接打开拿到默认配置后关闭
|
if (cfg.New) // 首次运行直接打开拿到默认配置后关闭
|
||||||
{
|
{
|
||||||
|
debug_printf("初次使用,打开端口获取默认配置:");
|
||||||
port.Open();
|
port.Open();
|
||||||
baudRate = port._baudRate;
|
baudRate = port._baudRate;
|
||||||
|
|
||||||
parity = port._parity; // USART_Parity_No;
|
parity = port._parity; // USART_Parity_No;
|
||||||
dataBits = port._dataBits; // USART_WordLength_8b;
|
dataBits = port._dataBits; // USART_WordLength_8b;
|
||||||
stopBits = port._stopBits; // USART_StopBits_1;
|
stopBits = port._stopBits; // USART_StopBits_1;
|
||||||
SaveConfig();
|
|
||||||
|
|
||||||
|
SaveConfig();
|
||||||
port.Close();
|
port.Close();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -144,7 +207,7 @@ bool ComProxy::OnClose()
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ComProxy::SetConfig(Dictionary<cstring, int>& config, String& str)
|
bool ComProxy::OnSetConfig(Dictionary<cstring, int>& config, String& str)
|
||||||
{
|
{
|
||||||
int value;
|
int value;
|
||||||
if (config.TryGetValue("baudrate", value))
|
if (config.TryGetValue("baudrate", value))
|
||||||
|
@ -152,7 +215,7 @@ bool ComProxy::SetConfig(Dictionary<cstring, int>& config, String& str)
|
||||||
port.SetBaudRate(value);
|
port.SetBaudRate(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
cstring ByteParam[] = { "parity","dataBits","stopBits" };
|
cstring const ByteParam[] = { "parity","dataBits","stopBits" };
|
||||||
ushort* ParamP[] = {&parity, &dataBits, &stopBits};
|
ushort* ParamP[] = {&parity, &dataBits, &stopBits};
|
||||||
bool haveChang = false;
|
bool haveChang = false;
|
||||||
for (int i = 0; i < ArrayLength(ByteParam); i++)
|
for (int i = 0; i < ArrayLength(ByteParam); i++)
|
||||||
|
@ -165,15 +228,20 @@ bool ComProxy::SetConfig(Dictionary<cstring, int>& config, String& str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (haveChang)port.Set(parity, dataBits, stopBits);
|
if (haveChang)port.Set(parity, dataBits, stopBits);
|
||||||
SaveConfig();
|
// SaveConfig();
|
||||||
|
|
||||||
return true;
|
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("baudrate",baudRate);
|
// 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);
|
config.Add("parity", parity);
|
||||||
config.Add("dataBits", dataBits);
|
config.Add("dataBits", dataBits);
|
||||||
|
|
|
@ -12,18 +12,19 @@ public:
|
||||||
MemoryStream* Cache; // 缓存空间
|
MemoryStream* Cache; // 缓存空间
|
||||||
uint UploadTaskId; // 上传任务的ID
|
uint UploadTaskId; // 上传任务的ID
|
||||||
uint AutoTaskId; // 自动任务ID,可以是定时Write数据
|
uint AutoTaskId; // 自动任务ID,可以是定时Write数据
|
||||||
|
|
||||||
int CacheSize; // 缓存大小
|
|
||||||
bool EnableStamp; // 时间戳开关
|
|
||||||
int TimeStamp; // 时间戳
|
int TimeStamp; // 时间戳
|
||||||
|
|
||||||
|
int CacheSize; // 缓存数据包个数
|
||||||
|
int BufferSize; // 缓冲区大小
|
||||||
|
bool EnableStamp; // 时间戳开关
|
||||||
bool AutoStart; // 自动启动
|
bool AutoStart; // 自动启动
|
||||||
|
|
||||||
Proxy();
|
Proxy();
|
||||||
bool Open();
|
bool Open();
|
||||||
bool Close();
|
bool Close();
|
||||||
|
|
||||||
virtual bool SetConfig(Dictionary<cstring, int>& config, String& str) = 0;
|
bool SetConfig(Dictionary<cstring, int>& config, String& str);
|
||||||
virtual bool GetConfig(Dictionary<cstring, int>& config) = 0;
|
bool GetConfig(Dictionary<char *, int>& config);
|
||||||
virtual int Write(Buffer& data) = 0;
|
virtual int Write(Buffer& data) = 0;
|
||||||
virtual int Read(Buffer& data, Buffer& input) = 0;
|
virtual int Read(Buffer& data, Buffer& input) = 0;
|
||||||
void UploadTask();
|
void UploadTask();
|
||||||
|
@ -37,6 +38,10 @@ private:
|
||||||
virtual bool OnOpen() = 0;
|
virtual bool OnOpen() = 0;
|
||||||
virtual bool OnClose() = 0;
|
virtual bool OnClose() = 0;
|
||||||
virtual bool OnAutoTask() { return true; };
|
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 OnGetConfig(Stream& cfg) { return true; };
|
||||||
virtual bool OnSetConfig(Stream& cfg) { return true; };
|
virtual bool OnSetConfig(Stream& cfg) { return true; };
|
||||||
};
|
};
|
||||||
|
@ -53,8 +58,8 @@ public:
|
||||||
ushort stopBits;
|
ushort stopBits;
|
||||||
int baudRate;
|
int baudRate;
|
||||||
|
|
||||||
virtual bool SetConfig(Dictionary<cstring, int>& config, String& str) override;
|
virtual bool OnSetConfig(Dictionary<cstring, int>& config, String& str) override;
|
||||||
virtual bool GetConfig(Dictionary<cstring, int>& config) override;
|
virtual bool OnGetConfig(Dictionary<char*, int>& config) override;
|
||||||
|
|
||||||
virtual int Write(Buffer& data) override;
|
virtual int Write(Buffer& data) override;
|
||||||
virtual int Read(Buffer& data, Buffer& input) override;
|
virtual int Read(Buffer& data, Buffer& input) override;
|
||||||
|
|
|
@ -9,8 +9,10 @@ class ProxyConfig : public ConfigBase
|
||||||
public:
|
public:
|
||||||
int Length;
|
int Length;
|
||||||
byte AutoStart; // 自动启动
|
byte AutoStart; // 自动启动
|
||||||
int CacheSize; // 缓存大小
|
byte EnableStamp; // 时间戳开关
|
||||||
bool EnableStamp; // 时间戳开关
|
int CacheSize; // 缓存数据包个数
|
||||||
|
int BufferSize; // 缓冲区大小
|
||||||
|
|
||||||
byte PortCfg[64]; // 子类自定义配置
|
byte PortCfg[64]; // 子类自定义配置
|
||||||
// byte FixedCmd[128]; // 定时任务的命令+数据
|
// byte FixedCmd[128]; // 定时任务的命令+数据
|
||||||
|
|
||||||
|
|
|
@ -168,9 +168,15 @@ bool ProxyFactory::GetConfig(const Pair& args, Stream& result)
|
||||||
// port->GetConfig(str); // 调用端口的函数处理内容
|
// port->GetConfig(str); // 调用端口的函数处理内容
|
||||||
// ms.Write(str);
|
// ms.Write(str);
|
||||||
|
|
||||||
Dictionary<cstring, int> cfg;
|
Dictionary<char *, int> cfg;
|
||||||
port->GetConfig(cfg); // 调用端口的函数处理内容
|
port->GetConfig(cfg); // 调用端口的函数处理内容
|
||||||
|
|
||||||
|
if (cfg.Count() < 1)
|
||||||
|
{
|
||||||
|
result.Write((byte)0);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// 数据先写进缓冲区ms2
|
// 数据先写进缓冲区ms2
|
||||||
MemoryStream ms2;
|
MemoryStream ms2;
|
||||||
auto name = cfg.Keys();
|
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());
|
// debug_printf("cfg count : %d value count : %d\t\t", name.Count(), value.Count());
|
||||||
String str;
|
String str;
|
||||||
|
|
||||||
|
debug_printf("config:\r\n");
|
||||||
for (int i = 0; i < cfg.Count(); i++)
|
for (int i = 0; i < cfg.Count(); i++)
|
||||||
{
|
{
|
||||||
|
debug_printf("%s %d\r\n",name[i],value[i]);
|
||||||
str = str + name[i] + '=' + value[i];
|
str = str + name[i] + '=' + value[i];
|
||||||
if (i < cfg.Count() - 1)str = str + '&';
|
if (i < cfg.Count() - 1)str = str + '&';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue