-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
231 lines (199 loc) · 7.59 KB
/
index.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
if (process.env.APPDYNAMICS_CONTROLLER_HOST_NAME)
require("appdynamics").profile({
controllerHostName: process.env.APPDYNAMICS_CONTROLLER_HOST_NAME,
controllerPort: process.env.APPDYNAMICS_CONTROLLER_PORT,
controllerSslEnabled: process.env.APPDYNAMICS_CONTROLLER_SSL_ENABLED,
accountName: process.env.APPDYNAMICS_AGENT_ACCOUNT_NAME,
accountAccessKey: process.env.APPDYNAMICS_AGENT_ACCOUNT_ACCESS_KEY,
applicationName: process.env.APP_NAME || 'firefly-monitor',
tierName: process.env.APP_ID,
nodeName: process.env.INSTANCE_ID // The controller will automatically append the node name with a unique number
});
if (process.env.ELASTIC_APM_SERVER_URLS)
require("elastic-apm-node").start()
const express = require("express");
const bodyParser = require("body-parser");
const fetch = require('node-fetch');
const Discover = require('firefly-consumer').Discover
const pgp = require('pg-promise')();
const servicesUrl = process.env.SERVICES_URL || `http://localhost:8080/services`
const discover = new Discover({servicesUrl})
const port = process.env.PORT || 9090;
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
function onNotification(data) {
console.log('Received Payload:', data.payload);
if (data.payload != 'lifeline') {
io.emit('pgnotif', data.payload);
}
}
function setListeners(client) {
client.on('notification', onNotification);
return connection.none('LISTEN $1~', 'my-channel')
.catch(error => {
console.log(error); // unlikely to ever happen
});
}
function removeListeners(client) {
client.removeListener('notification', onNotification);
}
function onConnectionLost(err, e) {
console.log('Connectivity Problem:', err);
connection = null; // prevent use of the broken connection
removeListeners(e.client);
reconnect(5000, 100) // retry 10 times, with 5-second intervals
.then(() => {
console.log('Successfully Reconnected');
})
.catch(() => {
// failed after 10 attempts
console.log('Connection Lost Permanently');
process.exit(); // exiting the process
});
}
function reconnect(delay, maxAttempts) {
delay = delay > 0 ? parseInt(delay) : 0;
maxAttempts = maxAttempts > 0 ? parseInt(maxAttempts) : 1;
return new Promise((resolve, reject) => {
setTimeout(() => {
db.connect({direct: true, onLost: onConnectionLost})
.then(obj => {
connection = obj; // global connection is now available
resolve(obj);
return setListeners(obj.client);
})
.catch(error => {
console.log('Error Connecting:', error);
if (--maxAttempts) {
reconnect(delay, maxAttempts)
.then(resolve)
.catch(reject);
} else {
reject(error);
}
});
}, delay);
});
}
function sendNotifications() {
// send a notification to our listener every second:
setInterval(() => {
if (connection) {
connection.none('NOTIFY $1~, $2', ['my-channel', 'lifeline'])
.catch(error => {
console.log('Failed to Notify:', error); // unlikely to ever happen
})
}
}, 1000);
}
let connection; // global connection for permanent event listeners
const cn = {
connectionString: process.env.POSTGRESQL_ADDON_URI,
max: 5
};
const db = pgp(cn); // Database Object
reconnect() // = same as reconnect(0, 1)
.then(obj => {
console.log('Successful Initial Connection');
// obj.done(); - releases the connection
sendNotifications();
})
.catch(error => {
console.log('Failed Initial Connection:', error);
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static('public'));
// Expose env data about the current application
app.get('/env/vars', (req, res) => {
const d = new Date();
res.send({
APP_ID: process.env.APP_ID || "APP_ID",
INSTANCE_ID: process.env.INSTANCE_ID || "INSTANCE_ID",
INSTANCE_TYPE: process.env.INSTANCE_TYPE || "INSTANCE_TYPE",
COMMIT_ID: process.env.COMMIT_ID || "COMMIT_ID",
INSTANCE_NUMBER: process.env.INSTANCE_NUMBER || "INSTANCE_NUMBER",
WHOAMI: process.env.WHOAMI || "🤖"
})
});
// Get some informations about my discovery server
// So you can update some variables on the discovery server side for the demonstration
app.get('/discovery-server/informations', (req, res) => {
let urlOrigin = servicesUrl.split("/").slice(0,3).reduce((tmp, part)=> tmp + (part=="" ? "//" : part))
fetch(`${urlOrigin}/informations`, {
method: 'GET',
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(informations => {
res.send(informations)
}).catch(error => {
res.send(error)
})
});
// list of connected gateways
// http://localhost:9090/gateways/all
// fetch("http://localhost:9090/gateways/all").then(res=>res.json()).then(data=> console.log(data))
app.get('/gateways/all', (req, res) => {
discover.services().then(services => {
res.send(services.filter(serviceKey => serviceKey.startsWith('sensors')))
}).catch(error => {
res.send([])
})
});
// list of connected gateways and details
// http://localhost:9090/gateways/details
// fetch("http://localhost:9090/gateways/details").then(res=>res.json()).then(data=> console.log(data))
app.get('/gateways/details', (req, res) => {
discover.servicesDetails().then(services => {
res.send(services.filter(service => service.name.startsWith('sensors')))
}).catch(error => {
res.send([])
})
});
// get temperature values of a gateway
//http://localhost:9090/gateways/sensors_quarter_b:42/sensors/temperature/values
// fetch("http://localhost:9090/gateways/sensors_quarter_b:42/sensors/temperature/values").then(res=>res.json()).then(data=> console.log(data))
app.get('/gateways/:gateway_id/sensors/temperature/values', (req, res) => {
let serviceKey = req.params.gateway_id
discover.operations({serviceKey}).then(operations => {
operations.temperatureValues().then(data => {
res.send(data)
})
}).catch(error => {
res.send([])
})
});
// get humidity values of a gateway
//http://localhost:9090/gateways/sensors_quarter_b:42/sensors/humidity/values
// fetch("http://localhost:9090/gateways/sensors_quarter_b:42/sensors/humidity/values").then(res=>res.json()).then(data=> console.log(data))
app.get('/gateways/:gateway_id/sensors/humidity/values', (req, res) => {
let serviceKey = req.params.gateway_id
discover.operations({serviceKey}).then(operations => {
operations.humidityValues().then(data => {
res.send(data)
})
}).catch(error => {
res.send([])
})
});
server.listen(port);
console.log(`🌍 Web Server is started - listening on ${port}`);
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
// tools
console.panda = function(...args) { console.log("🐼", ...args) }
console.bear = function(...args) { console.log("🐻", ...args) }
console.fox = function(...args) { console.log("🦊", ...args) }
console.pig = function(...args) { console.log("🐷", ...args) }
console.happy = function(...args) { console.log("😀", ...args) }
console.angry = function(...args) { console.log("😡", ...args) }
console.good = function(...args) { console.log("👍", ...args) }
console.bad = function(...args) { console.log("👎", ...args) }