-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
236 lines (220 loc) · 7.13 KB
/
server.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
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
//Requirements
const app = require("express")();
const request = require("request");
const http = require("http").Server(app);
const fs = require("fs")
const path = require("path")
//we add the socket package
const io = require('socket.io')(http);
const bodyParser = require("body-parser");
const deviceID = "xxxxxxxxxxx"
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
const googleMapsClient = require('@google/maps').createClient({
key: '' //replace with the API key you got from Google
});
const dir = process.env.DATA_DIR || "."
var downlink_link = ""
file = "data.json"
fs.openSync(file, 'a')
//Read the json
const read = function () {
return new Promise(function (resolve, reject) {
// start reading the file
fs.readFile(file, "utf8", function (err, data) {
if (err) {
return reject(err)
}
// return the parsed data
return resolve(JSON.parse(data || "[]"))
})
})
}
// special reading function to extract the data from the JSON file and transform them into something usable for our graph.
const readchartTemperature = function () {
return new Promise(function (resolve, reject) {
// start reading the file again
fs.readFile(file, "utf8", function (err, data) {
if (err) {
return reject(err)
}
// put the data into a variable
var datajson = JSON.parse(data || "[]")
// initialize an empty array
var tab = []
//for each element of our json file we get the temperature and the date
for (var i=0; i < datajson.length; i++){
var obj = datajson[i]
var temp = obj.temperature
var date = obj.date
// and we push it to the previously empty array
tab.push([date, temp])
}
// return the full array with the "graph-ready" data
return resolve(tab || "[]")
})
})
}
const readchartPressure = function () {
return new Promise(function (resolve, reject) {
// start reading the file again
fs.readFile(file, "utf8", function (err, data) {
if (err) {
return reject(err)
}
// put the data into a variable
var datajson = JSON.parse(data || "[]")
// initialize an empty array
var tab = []
//for each element of our json file we get the temperature and the date
for (var i=0; i < datajson.length; i++){
var obj = datajson[i]
var temp = obj.pressure
var date = obj.date
// and we push it to the previously empty array
tab.push([date, temp])
}
// return the full array with the "graph-ready" data
return resolve(tab || "[]")
})
})
}
const readchartHumidity = function () {
return new Promise(function (resolve, reject) {
// start reading the file again
fs.readFile(file, "utf8", function (err, data) {
if (err) {
return reject(err)
}
// put the data into a variable
var datajson = JSON.parse(data || "[]")
// initialize an empty array
var tab = []
//for each element of our json file we get the temperature and the date
for (var i=0; i < datajson.length; i++){
var obj = datajson[i]
var temp = obj.humidity
var date = obj.date
// and we push it to the previously empty array
tab.push([date, temp])
}
// return the full array with the "graph-ready" data
return resolve(tab || "[]")
})
})
}
const readGPS = function (){
return new Promise(function (resolve, reject) {
// start reading the file again
fs.readFile(file, "utf8", function (err, data) {
if (err) {
return reject(err)
}
// put the data into a variable
var datajson = JSON.parse(data || "[]")
// initialize an empty array
var tab = []
//for each element of our json file we get the temperature and the date
for (var i=0; i < datajson.length; i++){
var obj = datajson[i]
var lng = obj.longitude
var lat = obj.latitude
var date = obj.date
// and we push it to the previously empty array
tab.push([date, lng, lat])
}
// return the full array with the "graph-ready" data
return resolve(tab[tab.length-1] || "[]")
})
})
}
// function writing the data to the JSON file.
const write = function (data) {
return new Promise(function (resolve, reject) {
fs.writeFile(file, JSON.stringify(data), "utf8", function (err) {
if (err) {
return reject(err)
}
return resolve()
})
})
}
// write the temperature of the uplink message to our JSON file.
function writeFields(value) {
return read().then(function(data){
var now = new Date().toISOString()
var newEntry = {'date':now, 'temperature':value.temperature, 'altitude':value.altitude*100,'humidity':value.humidity,'pressure':value.pressure,'longitude':value.lon,'latitude':value.lat*0.0566935837}
data.push(newEntry)
return write(data)
})
}
//code
//What we see
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html")
})
app.get('/chartdata/temperature', function(req, res) {
console.log('GET CHARTDATA TEMPERATURE')
return readchartTemperature().then(res.json.bind(res))
})
app.get('/chartdata/humidity', function(req, res) {
console.log('GET CHARTDATA HUMIDITY')
return readchartHumidity().then(res.json.bind(res))
})
app.get('/chartdata/pressure', function(req, res) {
console.log('GET CHARTDATA PRESSURE')
return readchartPressure().then(res.json.bind(res))
})
app.get("/generator", function(req, res) {
console.log('GET LAT/LONG')
return readGPS().then(res.json.bind(res))
});
//the treament of the POST request made to the /on URL
app.post("/on", function(req, res) {
// we create the message to be sent
var msg = {
"dev_id": deviceID,
"port": 1,
"payload_raw": "AQ=="
}
// if the downlink link is defined we send the request
if (downlink_link != "") {
request({
url: downlink_link,
method: "POST",
json: msg
})
} else { // if not we print an error message
console.log("Error, wait for an uplink message to come in to get the downlink url")
}
})
// the treament of the POST request made to the /off URL
app.post("/off", function(req, res) {
//this is our downlink message
var msg = {
"dev_id": deviceID,
"port": 1,
"payload_raw": "AA=="
}
// if the downlink url is set, we send the request
if (downlink_link != "") {
request({
url: downlink_link,
method: "POST",
json: msg
})
} else { // if not we print the error message to the console
console.log("Error, wait for an uplink message to come in to get the downlink url")
}
})
//How we receive data from the things network
app.post("/endpoint", function(req, res) {
console.log(req.body)
writeFields(req.body.payload_fields)
downlink_link = req.body.downlink_url
io.emit('message', req.body);
res.sendFile(__dirname + "/index.html")
})
http.listen(8000, function(){
console.log('listening on *:8000')
});