-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cpp
More file actions
269 lines (251 loc) · 6.18 KB
/
plugin.cpp
File metadata and controls
269 lines (251 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* Fledge south plugin.
*
* This plugin is for the Advantech USB-4704 Portable Data Acquisition Module.
* It supports a number of digitial and analogue inputs on that device. The mapping
* of an input to a reading value within the JSON reading paylog is controlled
* via the plugin configuratio category.
*
* In order to build this plugin the Advantech biodaq library must be installed,
* to use it the kernel module for the USB-4704 must also be installed and
* enabled on your machine.
*
* Copyright (c) 2018 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Mark Riddoch
*/
#include <usb4704.h>
#include <plugin_api.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string>
#include <logger.h>
#include <plugin_exception.h>
#include <config_category.h>
#include <rapidjson/document.h>
#include <version.h>
using namespace std;
using namespace rapidjson;
#define DEFAULT_CONNECTIONS QUOTE({ \
"analogue_example" : { \
"type" : "analogue", \
"pin" : "AI0", \
"name" : "value1", \
"scale" : 0.1 \
}, \
"digital_example" : { \
"type" : "digital", \
"pins" : ["DI0", "DI1", "DI2", "DI3"], \
"name" : "value1", \
"scale" : 0.1 \
} \
})
/**
* Default configuration
*/
static const char *default_config = QUOTE({
"plugin" : {
"description" : "Advantech USB-4704 Data Acquisition Module",
"type" : "string",
"default" : "usb4704",
"readonly": "true"
},
"asset" : {
"description" : "Asset name to use for readings",
"type" : "string",
"default" : "usb4704",
"order": "1",
"displayName": "Asset Name",
"mandatory": "true"
},
"connections" : {
"description" : "Utilisation of connections on USB-4704",
"order": "2",
"displayName": "Connections",
"type" : "JSON",
"default" : DEFAULT_CONNECTIONS
}
});
/**
* The USB-4704 plugin interface
*/
extern "C" {
/**
* The plugin information structure
*/
static PLUGIN_INFORMATION info = {
"usb4704", // Name
VERSION, // Version
0, // Flags
PLUGIN_TYPE_SOUTH, // Type
"1.0.0", // Interface version
default_config // Default configuration
};
/**
* Return the information about this plugin
*/
PLUGIN_INFORMATION *plugin_info()
{
return &info;
}
/**
* Initialise the plugin, called to get the plugin handle
*/
PLUGIN_HANDLE plugin_init(ConfigCategory *config)
{
USB4704 *usb = 0;
usb = new USB4704();
if (config->itemExists("asset"))
{
usb->setAssetName(config->getValue("asset"));
}
else
{
usb->setAssetName("usb4704");
}
// Now process the Connections
string connections = config->getValue("connections");
rapidjson::Document doc;
doc.Parse(connections.c_str());
if (!doc.HasParseError())
{
for (rapidjson::Value::ConstMemberIterator itr = doc.MemberBegin();
itr != doc.MemberEnd(); ++itr)
{
const char *type = itr->value["type"].GetString();
if (strcmp(type, "analogue") == 0)
{
double scale = 1.0;
if (itr->value.HasMember("scale"))
{
scale = itr->value["scale"].GetFloat();
}
if (itr->value.HasMember("pin") && itr->value["pin"].IsString())
{
usb->addAnalogueConnection(itr->name.GetString(), itr->value["pin"].GetString(),
scale);
}
else
{
Logger::getLogger()->fatal("Analogue connection for USB-4704 is missing definition of pin");
throw exception();
}
}
else if (strcmp(type, "digital") == 0)
{
if (itr->value.HasMember("pins") && itr->value["pins"].IsArray())
{
vector<string> pins;
for (Value::ConstValueIterator pitr = itr->value["pins"].Begin();
pitr != itr->value["pins"].End(); ++pitr)
{
pins.push_back(string(pitr->GetString()));
}
usb->addDigitalConnection(itr->name.GetString(), pins);
}
else
{
Logger::getLogger()->fatal("Digital connection for USB-4704 is missing definition of pins, or pins is not an array");
}
}
else
{
Logger::getLogger()->fatal("Configuration type must have either an analogue or digitial. Type %s is not supported", type);
throw exception();
}
}
}
return (PLUGIN_HANDLE)usb;
}
/**
* Start the Async handling for the plugin
*/
void plugin_start(PLUGIN_HANDLE *handle)
{
if (!handle)
return;
}
/**
* Poll for a plugin reading
*/
Reading plugin_poll(PLUGIN_HANDLE *handle)
{
USB4704 *usb = (USB4704 *)handle;
if (!handle)
throw exception();
return usb->takeReading();
}
/**
* Reconfigure the plugin
*/
void plugin_reconfigure(PLUGIN_HANDLE *handle, string& newConfig)
{
ConfigCategory config("update", newConfig);
USB4704 *usb = (USB4704 *)*handle;
if (config.itemExists("asset"))
{
usb->setAssetName(config.getValue("asset"));
}
if (config.itemExists("connections"))
{
string connections = config.getValue("connections");
rapidjson::Document doc;
doc.Parse(connections.c_str());
if (!doc.HasParseError())
{
usb->clearConnections();
for (rapidjson::Value::ConstMemberIterator itr = doc.MemberBegin();
itr != doc.MemberEnd(); ++itr)
{
const char *type = itr->value["type"].GetString();
if (strcmp(type, "analogue") == 0)
{
double scale = 1.0;
if (itr->value.HasMember("scale"))
{
scale = itr->value["scale"].GetFloat();
}
if (itr->value.HasMember("pin") && itr->value["pin"].IsString())
{
usb->addAnalogueConnection(itr->name.GetString(), itr->value["pin"].GetString(),
scale);
}
else
{
Logger::getLogger()->error("Analogue connection for USB-4704 is missing definition of pin");
throw exception();
}
}
else if (strcmp(type, "digital") == 0)
{
if (itr->value.HasMember("pins") && itr->value["pins"].IsArray())
{
vector<string> pins;
for (Value::ConstValueIterator pitr = itr->value.Begin();
pitr != itr->value.End(); ++pitr)
{
pins.push_back(string(pitr->GetString()));
}
usb->addDigitalConnection(itr->name.GetString(), pins);
}
}
else
{
throw exception();
}
}
}
}
}
/**
* Shutdown the plugin
*/
void plugin_shutdown(PLUGIN_HANDLE *handle)
{
USB4704 *usb = (USB4704 *)handle;
delete usb;
}
};