-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
125 lines (109 loc) · 3.23 KB
/
app.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
const express = require('express')
const app = express()
var port = 4000
var mongojs = require('mongojs')
var Promise = require('promise')
var myiotdb = mongojs('test')
var dhtdb = mongojs('test')
var devid, data, datasize, dataset=''
var t, h
app.get('/', function (req, res) {
res.send("My IOT Protocol ready !")
})
app.get('/write/:data', function (req, res) {
var strParseWriteReq = JSON.stringify(req.params)
var strWriteReq = JSON.parse(strParseWriteReq)
data = strWriteReq.data
writedata(data, res)
})
app.get('/read/:datasize', function (req, res) {
var strParseReadReq = JSON.stringify(req.params)
var strReadReq = JSON.parse(strParseReadReq)
datasize = strReadReq.datasize
readdata(datasize, res)
})
/* For DHT write */
app.get('/writedht/:t/:h', function (req, res) {
var strParseWriteReq = JSON.stringify(req.params)
var strWriteReq = JSON.parse(strParseWriteReq)
t = strWriteReq.t
h = strWriteReq.h
writeDHT(t, h, res)
})
/* For DHT data read */
app.get('/readdht/:datasize', function (req, res) {
var strParseReadReq = JSON.stringify(req.params)
var strReadReq = JSON.parse(strParseReadReq)
datasize = strReadReq.datasize
readDHT(datasize, res)
})
app.listen(port, function () {
var nodeStartTime = new Date()
console.log('My IoT protocol running on port ' + port + ' start at ' + nodeStartTime)
})
/* -- ASYNC / AWAIT function -- */
async function writedata(_data, res){
await writeDataToMongo(_data, res)
}
function writeDataToMongo(_savedata, res){
return new Promise(function(resolve, reject){
var mywritecollection = myiotdb.collection('dataTest')
mywritecollection.insert({
data: (_savedata),
recordTime: new Date().getTime()
}, function(err){
if(err){
console.log(err)
res.send(String(err))
}else {
console.log('Reccord data ok')
res.send('Reccord data ok')
}
})
})
}
async function readdata(_datasize, res){
await readDataFromMongo(_datasize, res)
}
function readDataFromMongo(_readdatasize, res){
return new Promise(function(resolve,reject){
var myreadcollection = myiotdb.collection('dataTest')
myreadcollection.find({}).limit(Number(_readdatasize)).sort({recordTime: -1}, function(err, docs){
//console.log(JSON.stringify(docs))
res.jsonp(docs)
})
})
}
async function writeDHT(_t, _h, res){
await writeDHTtoMongo(_t, _h, res)
}
function writeDHTtoMongo(_saveT, _saveH, res){
return new Promise(function(resolve, reject){
var dhtwritecollection = dhtdb.collection('dht')
dhtwritecollection.insert({
t: Number(_saveT),
h: Number(_saveH),
recordTime: new Date().getTime()
}, function(err){
if(err){
console.log(err)
res.send(String(err))
}else {
console.log('record dht data ok')
res.send('record dht data ok')
}
})
})
}
async function readDHT(_datasize, res){
await readDHTFromMongo(_datasize, res)
}
function readDHTFromMongo(_readdatasize, res){
return new Promise(function(resolve,reject){
var dhtcollection = dhtdb.collection('dht')
dhtcollection.find({}).limit(Number(_readdatasize)).sort({recordTime: -1}, function(err, docs){
//console.log(JSON.stringify(docs))
res.jsonp(docs)
})
})
}