Skip to content

Commit 7755c5d

Browse files
完善插件信息
1 parent 6fb8d47 commit 7755c5d

File tree

4 files changed

+90
-66
lines changed

4 files changed

+90
-66
lines changed

inc/dlms_error.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// Created by andrew on 2022/6/11.
3+
//
4+
5+
#ifndef DLMS_DLMS_ERROR_H
6+
#define DLMS_DLMS_ERROR_H
7+
8+
#endif //DLMS_DLMS_ERROR_H

inc/plugin.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,22 @@
1313
* 实际项目中随时可以替换成动态库插件形式,这里为demo代码采用直接链接编译的方式使用
1414
* */
1515

16-
enum PluginType
16+
// 服务函数根据类型进行参数和返回值的确认
17+
enum PluginServiceType
1718
{
18-
/// \brief A World plugin
19-
WORLD_PLUGIN,
20-
/// \brief A Model plugin
21-
MODEL_PLUGIN,
22-
/// \brief A Sensor plugin
23-
SENSOR_PLUGIN,
24-
/// \brief A System plugin
25-
SYSTEM_PLUGIN,
26-
/// \brief A Visual plugin
27-
VISUAL_PLUGIN,
28-
/// \brief A GUI plugin
29-
GUI_PLUGIN
19+
String = 0x1,
20+
JSON = 0x2,
21+
Binary = 0x4,
22+
Integer = 0x8,
3023
};
3124

25+
enum NotifyTypes
26+
{
27+
NetBroken = 0,
28+
Quit = 1,
29+
};
3230

31+
class IDlms;
3332
class Plugin {
3433
public:
3534
enum PluginStatus : uint8_t {
@@ -49,7 +48,7 @@ class Plugin {
4948
*
5049
* @param lpIDlms 主框架对象指针
5150
*/
52-
virtual int32_t Init(IDlms *lpIDlms) = 0;
51+
virtual int32_t Init(IDlms *lpIDlms, std::string& pluginName) = 0;
5352

5453
virtual int32_t Start() = 0;
5554

@@ -60,11 +59,17 @@ class Plugin {
6059
// 获取插件名字
6160
virtual std::string GetHandleName() = 0;
6261

62+
virtual int32_t SendAndReceive(PluginServiceType in, void *lpInData, PluginServiceType out, void *lpOutData) = 0;
63+
64+
virtual int32_t Send(PluginServiceType in, void *lpInData) = 0;
65+
66+
virtual int32_t Notify(PluginServiceType in, void *lpInData) = 0;
6367

68+
virtual bool ServiceSupport(PluginServiceType serviceType) = 0;
6469

6570
protected:
66-
volatile PluginStatus m_pluginStatus{PLUGIN_UNKNOWN};
67-
PluginType type;
71+
volatile PluginStatus pluginStatus{PLUGIN_UNKNOWN};
72+
uint32_t serviceSupportList;
6873
std::string handleName; // name of the plugin
6974
};
7075

src/dlms/dlms.cpp

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ void JsonTest() {
3838

3939

4040

41-
42-
4341
// 也可以通过直接赋值方式创建json对象,两种方式创建结果相同
4442
json j2 = {
4543
{"pi", 3.141},
@@ -68,14 +66,18 @@ int main(int argc, char* argv[]) {
6866

6967
google::InitGoogleLogging(argv[0]);
7068
FLAGS_log_dir = "./log";
71-
7269
LOG(INFO) << "Hello, world!";
7370

74-
std::string data;
75-
data = 'a';
71+
CDlms dlms;
72+
// 1. 解析命令行参数
73+
dlms.ParseCommandLine(argc, argv);
74+
75+
76+
77+
78+
79+
7680

77-
data = 65;
78-
std::cout << data << std::endl;
7981

8082
//...... DoSomething
8183
//Shutdown google's logging library.
@@ -118,49 +120,59 @@ Plugin *CDlms::GetPlugin(std::string &pluginName) {
118120
return nullptr;
119121
}
120122

121-
//#define GET_CASE_INFO(X) \
122-
//do { \
123-
// case X: \
124-
// \
125-
// \
126-
// };
127-
int32_t CConfig::ParseCommandLine(int32_t argc, char **argv) {
123+
int32_t CDlms::ParseCommandLine(int32_t argc, char **argv) {
124+
return config.ParseCommandLine(argc, argv);
125+
}
128126

127+
#define POINTER_SAFE(lp) ((nullptr == lp) ? "null" : lp)
129128

129+
#define GET_CASE_INFO(X) \
130+
case X: \
131+
configMap.emplace(std::make_pair(X, POINTER_SAFE(optarg))); \
132+
break;
130133

131-
int ch;
134+
int32_t CConfig::ParseCommandLine(int32_t argc, char *argv[]) {
132135

133-
opterr = 0; //使getopt不行stderr输出错误信息
136+
int ch;
134137
std::string opts = "a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z:"
135138
"A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z:";
136139
while( (ch = getopt(argc, argv, opts.c_str())) != -1 )
137140
{
138141
switch(ch)
139142
{
140-
case 'a':
141-
printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg);
142-
break;
143-
case 'b':
144-
printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg);
145-
break;
146-
case 'c':
147-
printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg);
148-
break;
143+
GET_CASE_INFO('a');
144+
GET_CASE_INFO('b');
145+
GET_CASE_INFO('c');
146+
GET_CASE_INFO('d');
147+
GET_CASE_INFO('e');
148+
GET_CASE_INFO('f');
149+
GET_CASE_INFO('g');
150+
GET_CASE_INFO('h');
151+
GET_CASE_INFO('i');
152+
GET_CASE_INFO('j');
153+
GET_CASE_INFO('k');
154+
GET_CASE_INFO('l');
155+
GET_CASE_INFO('m');
156+
GET_CASE_INFO('n');
157+
GET_CASE_INFO('o');
158+
GET_CASE_INFO('p');
159+
GET_CASE_INFO('q');
160+
GET_CASE_INFO('r');
161+
GET_CASE_INFO('s');
162+
GET_CASE_INFO('t');
163+
GET_CASE_INFO('u');
164+
GET_CASE_INFO('v');
165+
GET_CASE_INFO('w');
166+
GET_CASE_INFO('x');
167+
GET_CASE_INFO('y');
168+
GET_CASE_INFO('z');
149169
case '?':
150-
printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg);
170+
configMap['?'] = "Bad option!";
151171
break;
152172
default:
153173
printf("default, result=%c\n",ch);
154174
break;
155175
}
156-
printf("argv[%d]=%s\n", optind, argv[optind]);
157176
}
158-
printf("result=-1, optind=%d\n", optind); //看看最后optind的位置
159-
160-
161177
return 0;
162-
163-
164-
165-
166178
}

src/dlms/dlms.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414
#include "json_proxy.h"
1515

1616

17+
18+
class CConfig : public IConfig {
19+
public:
20+
~CConfig() override = default;
21+
22+
int32_t ParseCommandLine(int32_t argc, char* argv[]);
23+
24+
25+
private:
26+
std::unordered_map<char, std::string> configMap;
27+
JsonProxy* jsonProxy;
28+
};
29+
1730
class CDlms : public IDlms {
1831
public:
1932
~CDlms() override = default;
@@ -28,26 +41,12 @@ class CDlms : public IDlms {
2841

2942
Plugin* GetPlugin(std::string& pluginName) override;
3043

31-
32-
33-
private:
34-
PluginManager pluginManager;
35-
36-
37-
};
38-
39-
40-
41-
class CConfig : public IConfig {
42-
public:
43-
~CConfig() override = default;
44-
4544
int32_t ParseCommandLine(int32_t argc, char* argv[]);
4645

4746

4847
private:
49-
std::unordered_map<char, std::string> config;
50-
JsonProxy* jsonProxy;
48+
PluginManager pluginManager;
49+
CConfig config;
5150
};
5251

5352

0 commit comments

Comments
 (0)