-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
executable file
·154 lines (131 loc) · 4.03 KB
/
handler.js
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
'use strict';
var _ = require('underscore')
module.exports.fetch = require('node-fetch') // Use export so that it can be stubbed
function isBlankString(s) {
return (s==null || _.isUndefined(s) || s=="");
}
function relayPostComplete(method, url, header, authorization, data, callback) {
if (isBlankString(url)) {
callback(new Error(`Invalid URL query parameter '${url}'`));
}
var headers = {
'Accept': "application/json, text/plain, */*",
'Content-Type': "application/json"
}
headers[header] = authorization;
module.exports.fetch(url, {
method: method,
headers: headers,
body: JSON.stringify(data)
}).then(function(res) {
if (res.status==200) {
return res.json();
}
console.log(res);
console.log(res.body);
throw new Error(res.body);
})
.then(function(res) {
console.log(res);
const response = {
statusCode: 200,
body: JSON.stringify(res)
};
callback(null, response);
})
.catch(function (err) {
console.error(err);
callback(err);
});
}
function relayPost(url, authorization, data, callback) {
relayPostComplete('post', url, 'Authorization', authorization, data, callback);
}
function makeWithModulator(mod) {
return function(event, context, callback) {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
var data = JSON.parse(event.body);
data = _.extend({}, data);
const url = event.queryStringParameters.url;
var authorization = null;
if (event.headers) {
authorization = event.headers["Authorization"];
}
// console.log("Before:", data);
mod(event, context, data, function(err, moddata) {
// console.log("After:", moddata);
relayPost(url, authorization, moddata, callback);
});
};
}
function wrapData(data, fieldname) {
var newdata = {};
newdata[fieldname] = data;
return newdata;
}
var handlers = {}
handlers.rename = makeWithModulator(function (event, context, data, cb) {
const rename = event.queryStringParameters.dataname;
if (rename) {
cb(null, wrapData(data, rename));
}
else {
cb(new Error("Invalid 'dataname' query parameter '" + rename + "'."));
}
});
handlers.opensensors = makeWithModulator(function (event, context, data, cb) {
// OpenSensors.io required a text "data" field and NO other fields in JSON
const newdata = {
data: JSON.stringify(data.payload_fields)
};
cb(null, newdata);
});
handlers.slicingdice = makeWithModulator(function (event, context, data, cb) {
const entity = data.hardware_serial;
var newdata = {
"auto-create": ["table", "column"]
};
newdata[entity] = {
"device-name": data.dev_id,
"temperature": [
{
"value": data.payload_fields.temperature,
"date": data.metadata.time
}
],
"table": "sensor-data"
};
cb(null, newdata);
});
handlers.pyroclast = makeWithModulator(function (event, context, data, cb) {
// Pyroclast will take the whole message under "value"
cb(null, wrapData(data, "value"))
});
handlers.adafruit = function(event, context, callback) {
console.log(event); // Contains incoming request data (e.g., query params, headers and more)
var data = JSON.parse(event.body);
const url = event.queryStringParameters.url;
const field = event.queryStringParameters.field;
var authorization = null;
if (event.headers) {
authorization = event.headers["Authorization"];
}
// console.log("Before:", data);
const moddata = {
"created_at": data.metadata.time,
"value": "" + data.payload_fields[field]
}
// console.log("After:", moddata);
relayPostComplete('post', url, 'X-AIO-Key', authorization, moddata, callback);
}
module.exports.dispatch = function(event, context, callback) {
const path = event.path;
const handler = path.substring(1);
if (handler in handlers) {
const fn = handlers[handler];
fn(event, context, callback);
}
else {
callback(new Error("Unknown path '" + path + "'"));
}
}