forked from fledge-iot/fledge-filter-python35
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.cpp
More file actions
191 lines (174 loc) · 4.89 KB
/
plugin.cpp
File metadata and controls
191 lines (174 loc) · 4.89 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
/*
* Fledge "Python 3.5" filter plugin.
*
* Copyright (c) 2018 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Massimiliano Pinto
*/
#define FILTER_NAME "python35"
#include <utils.h>
#include <plugin_api.h>
#include <config_category.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string>
#include <iostream>
#include <filter_plugin.h>
#include <filter.h>
#include <version.h>
#include <pyruntime.h>
#include "python35.h"
static void* libpython_handle = NULL;
/**
* The Python 3.5 script module to load is set in
* 'script' config item and it doesn't need the trailing .py
*
* Example:
* if filename is 'readings_filter.py', just set 'readings_filter'
* via Fledge configuration managewr
*
* Note:
* Python 3.5 filter code needs two methods.
*
* One is the filtering method to call which must have
* the same as the script name: it can not be changed.
* The second one is the configuration entry point
* method 'set_filter_config': it can not be changed
*
* Example: readings_filter.py
*
* expected two methods:
* - set_filter_config(configuration) // Input is a string
* It sets the configuration internally as dict
*
* - readings_filter(readings) // Input is a dict
* It returns a dict with filtered input data
*/
// Filter default configuration
static const char *default_config = QUOTE({
"plugin" : {
"description" : "Python 3.5 filter plugin",
"type" : "string",
"readonly": "true",
"default" : FILTER_NAME
},
"enable": {
"description": "A switch that can be used to enable or disable execution of the Python 3.5 filter.",
"type": "boolean",
"displayName": "Enabled",
"default": "false"
},
"config" : {
"description" : "Python 3.5 filter configuration.",
"type" : "JSON",
"order": "2",
"displayName" : "Configuration",
"default" : "{}"
},
"script" : {
"description" : "Python 3.5 module to load.",
"type": "script",
"order": "1",
"displayName" : "Python script",
"default": ""
},
"encode_attribute_names" : {
"description" : "Whether to encode/decode attribute names",
"type": "boolean",
"displayName": "Encode attribute names",
"default": "true"
}
});
using namespace std;
/**
* The Filter plugin interface
*/
extern "C" {
/**
* The plugin information structure
*/
static PLUGIN_INFORMATION info = {
FILTER_NAME, // Name
VERSION, // Version
0, // Flags
PLUGIN_TYPE_FILTER, // Type
"1.0.0", // Interface version
default_config // Default plugin configuration
};
/**
* Return the information about this plugin
*/
PLUGIN_INFORMATION *plugin_info()
{
return &info;
}
/**
* Initialise the plugin, called to get the plugin handle and setup the
* output handle that will be passed to the output stream. The output stream
* is merely a function pointer that is called with the output handle and
* the new set of readings generated by the plugin.
* (*output)(outHandle, readings);
* Note that the plugin may not call the output stream if the result of
* the filtering is that no readings are to be sent onwards in the chain.
* This allows the plugin to discard data or to buffer it for aggregation
* with data that follows in subsequent calls
*
* @param config The configuration category for the filter
* @param outHandle A handle that will be passed to the output stream
* @param output The output stream (function pointer) to which data is passed
* @return An opaque handle that is used in all subsequent calls to the plugin
*/
PLUGIN_HANDLE plugin_init(ConfigCategory* config,
OUTPUT_HANDLE *outHandle,
OUTPUT_STREAM output)
{
Python35Filter *filter = new Python35Filter(FILTER_NAME,
*config,
outHandle,
output);
filter->init();
// return NULL aborts the filter pipeline set up
return (PLUGIN_HANDLE)filter;
}
/**
* Ingest a set of readings into the plugin for processing
*
* NOTE: in case of any error no data will be passed onwards in the pipeline
*
* @param handle The plugin handle returned from plugin_init
* @param readingSet The readings to process
*/
void plugin_ingest(PLUGIN_HANDLE *handle,
READINGSET *readingSet)
{
Python35Filter *filter = (Python35Filter *)handle;
filter->ingest(readingSet);
}
/**
* Call the shutdown method in the plugin
*
* @param handle The plugin handle returned from plugin_init
*/
void plugin_shutdown(PLUGIN_HANDLE *handle)
{
Python35Filter* filter = (Python35Filter *)handle;
filter->shutdown();
// Remove filter object
delete filter;
}
/**
* Apply filter plugin reconfiguration
*
* @param handle The plugin handle returned from plugin_init
* @param newConfig The new configuration to apply.
*/
void plugin_reconfigure(PLUGIN_HANDLE *handle, const string& newConfig)
{
Python35Filter* filter = (Python35Filter *)handle;
filter->reconfigure(newConfig);
}
// End of extern "C"
};