-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
155 lines (130 loc) · 3.65 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
// call the packages that genesis needs
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var request = require('request');
var HashMap = require('hashmap');
var schedule = require('node-schedule');
mongoose.connect('mongodb://localhost/api', function(err) {
if(err) {
console.log('connection error', err);
} else {
console.log('connection successful');
}
});
var nucleusSchema = mongoose.Schema({
name : String,
timeStamp : Number
});
var Nucleus = mongoose.model('Nucleus', nucleusSchema);
var timeMap = new HashMap();
Nucleus.find(function (err, storedPoints){
if (err) return console.error(err);
if( storedPoints.length > 0)
{
console.log("Retrieving points from database...");
}
else
{
return console.log("No points found in the database");
}
for(var i = 0; i < storedPoints.length; i++){
timeMap.set(storedPoints[i].name, storedPoints[i].timeStamp);
}
});
var subscriptionBody = {
"entities": [
{
"type": "Nucleus",
"isPattern": "false",
"id": "NucleusAlpha"
}
],
"attributes": [
"level"
],
"reference": "http://localhost:8080/genesis/subscription",
"duration": "P1M", /* <- 1 month subscription*/
"notifyConditions": [
{
"type": "ONCHANGE",
"condValues": [ "level" ]
}
],
"throttling": "PT5S"
};
var subscriptionOptions = {
url: "http://130.206.119.206:1026/v1/subscribeContext",
method: "POST",
json: true,
body: subscriptionBody
};
function callback(error,response,body)
{
console.log(body);
}
var checkPoints = schedule.scheduleJob('10 * * * * *', function(){
console.log('Schedule started');
var currentTimeMillis = Date.now();
var keys = timeMap.keys();
for(var i = 0; i < keys.length; i++ )
{
var lastTimeUpdated = timeMap.get(keys[i]);
var status = "not_broken";
if(currentTimeMillis - lastTimeUpdated > (3600 * 1000 * 4))
{
status = "broken";
}
var updateBody = {
"contextElements": [
{
"type": "Nucleus",
"isPattern": "false",
"id": keys[i],
"attributes": [
{
"name": "status",
"type": "String",
"value": status
}
]
}
],
"updateAction": "APPEND"
}
var updateOptions = {
url: "http://130.206.119.206:1026/v1/updateContext",
method: "POST",
headers: { 'Content-Length': updateBody.length},
json: true,
body: updateBody
};
request(updateOptions,callback);
console.log(keys[i]);
console.log(status);
console.log('Sent');
}
console.log('Schedule over');
});
request(subscriptionOptions,callback);
app.use(bodyParser.urlencoded({ extended : true}));
app.use(bodyParser.json());
var port = process.env.PORT || 8080;
var router = express.Router();
router.get('/get', function(request, response){
response.json({message : 'hooray! welcome to our api!'});
});
router.post('/subscription', function(request,response){
var id = request.body.contextResponses[0].contextElement.id;
var timeMillis = Date.now();
timeMap.set(id, timeMillis);
var currentNucleus = new Nucleus({name : id, timeStamp: timeMillis});
currentNucleus.save(function (err, currentNucleus) {
if (err) return console.error(err);
return console.log(id + " updated");
});
});
app.use('/genesis', router);
app.listen(port);
console.log('Magic happens on port ' + port);