-
Notifications
You must be signed in to change notification settings - Fork 0
/
readings-create.js
181 lines (155 loc) · 4.77 KB
/
readings-create.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
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
'use strict';
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
//const uuid = require('uuid');
module.exports = (event, callback) => {
const data = JSON.parse(event.body);
//data.id = uuid.v1();
//var timeInMs = Date.now();
data.published_at = Date.now();
data.processed_at = Date.now();
// Special case for TH module
if(data.temperature_reading != undefined &&
data.temperature_reading != null)
{
// NEW CHANGE
var lambda = new AWS.Lambda({
region: 'ap-southeast-1' //change to your region
});
const event = {
"queryStringParameters": {
"rh": parseFloat(data.humidity_reading),
"ta": parseFloat(data.temperature_reading)
}
};
console.log(event);
var PMV = 0.0;
var APMV = 0.0;
var PPD = 0.0;
lambda.invoke({
FunctionName: 'pmv-dev-computePMV',
Payload: JSON.stringify(event, null, 2) // pass params
}, function(error, data2) {
if (error) {
//context.done('error', error);
console.log("in error");
console.log(error);
}
if(data2.Payload){
console.log("in succeed");
//context.succeed(data.Payload)
const temp = JSON.parse(data2.Payload);
const result = JSON.parse(temp.body);
PMV = parseFloat(result.PMV);
APMV = parseFloat(result.APMV);
PPD = parseFloat(result.PPD);
console.log(result);
// Split the single JSON to 2 JSONs
var data1 = {
sensor_id: 'TEMPERATURE-' + data.sensor_id,
device_id: data.device_id,
device_type: data.device_type,
sensor_type: 'temperature',
sensor_reading: data.temperature_reading,
published_at: data.published_at,
processed_at: data.processed_at
};
var data2 = {
sensor_id: 'HUMIDITY-' + data.sensor_id,
device_id: data.device_id,
device_type: data.device_type,
sensor_type: 'humidity',
sensor_reading: data.humidity_reading,
published_at: data.published_at,
processed_at: data.processed_at
};
var data3 = {
sensor_id: 'PMV-' + data.sensor_id,
device_id: data.device_id,
device_type: data.device_type,
sensor_type: 'PMV_NOT_A_SENSOR',
sensor_reading: PMV,
published_at: data.published_at,
processed_at: Date.now()
};
var data4 = {
sensor_id: 'APMV-' + data.sensor_id,
device_id: data.device_id,
device_type: data.device_type,
sensor_type: 'APMV_NOT_A_SENSOR',
sensor_reading: APMV,
published_at: data.published_at,
processed_at: Date.now()
};
var data5 = {
sensor_id: 'PPD-' + data.sensor_id,
device_id: data.device_id,
device_type: data.device_type,
sensor_type: 'PPD_NOT_A_SENSOR',
sensor_reading: PPD,
published_at: data.published_at,
processed_at: Date.now()
};
var params1 = {
TableName: 'readings',
Item: data1
};
var params2 = {
TableName: 'readings',
Item: data2
};
var params3 = {
TableName: 'readings',
Item: data3
};
var params4 = {
TableName: 'readings',
Item: data4
};
var params5 = {
TableName: 'readings',
Item: data5
};
dynamoDb.put(params1, function(err, data) {
if (err) console.log("Unable to update item. Error: ", JSON.stringify(err, null, 2));
else console.log("Updated item succeeded: ", JSON.stringify(data, null, 2));
//next() // modify for err handling
});
dynamoDb.put(params2, function(err, data) {
if (err) console.log("Unable to update item. Error: ", JSON.stringify(err, null, 2));
else console.log("Updated item succeeded: ", JSON.stringify(data, null, 2));
//next() // modify for err handling
});
dynamoDb.put(params3, function(err, data) {
if (err) console.log("Unable to update item. Error: ", JSON.stringify(err, null, 2));
else console.log("Updated item succeeded: ", JSON.stringify(data, null, 2));
//next() // modify for err handling
});
dynamoDb.put(params4, function(err, data) {
if (err) console.log("Unable to update item. Error: ", JSON.stringify(err, null, 2));
else console.log("Updated item succeeded: ", JSON.stringify(data, null, 2));
//next() // modify for err handling
});
return dynamoDb.put(params5, (error, data) => {
if (error) {
callback(error);
}
callback(error, params5.Item);
});
}
});
} // end of if
else
{
const params = {
TableName: 'readings',
Item: data
};
return dynamoDb.put(params, (error, data) => {
if (error) {
callback(error);
}
callback(error, params.Item);
});
}// end of else
};