-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
121 lines (97 loc) · 2.94 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
const restify = require('restify');
const moment = require('moment');
const logger = require('./logger');
const { parseList, time, parseUpsVars } = require('./helpers');
const NUT = require('./node-nut');
const config = require('./env');
logger.setLevel(config.LOG_LEVEL);
logger.info('environment variables:\n', config);
// nut settings
const nut = new NUT(config.NUT_PORT, config.NUT_ADDRESS);
// https://networkupstools.org/docs/man/genericups.html
const statusses = Object.freeze({
OL: 1, // online
'OL CHRG': 2, // online & charging
'OL CHRG LB': 2, // online low battery
'OB DISCHRG': 3, // on battery
LB: 4, // low battery,
SD: 5 // shutdown load
});
// api settings
const server = restify.createServer();
server.server.setTimeout(config.SERVER_TIMEOUT);
server.use(restify.plugins.queryParser({ mapParams: false }));
let LOCK = 0;
// some handling
nut.on('connect', () => {
logger.info(`nut connection established to ${config.NUT_ADDRESS}:${config.NUT_PORT}`);
});
nut.on('error', err => {
logger.error('nut encountered an error:', err);
});
nut.on('close', () => {
logger.info('nut connection closed');
});
server.on('error', (err) => {
logger.error('server encountered an error', err);
nut.disconnect();
process.exit(1); // if in docker it should be restarted automatically
});
// router
server.get('/devices', (req, res) => {
const startDateTime = moment();
logger.logRequest('nut-http.devices.list');
connectNut();
nut
.getUpsList()
.then((list) => {
if (req.query.parsed && req.query.parsed === 'true') list = parseList(list);
const endDateTime = moment();
logger.debug(`processing took ${time(startDateTime, endDateTime)}`);
res.send(list);
return list;
})
.catch((err) => {
logger.error(err);
res.send(500, { code: 500, message: `an internal error occurred ${err}` });
return err;
});
});
server.get('/devices/:name', (req, res) => {
const startDateTime = moment();
const ups = req.params.name;
logger.logRequest('nut-http.devices.getUps', ups);
setTimeout((rq, rs) => {
connectNut();
nut
.getUpsVars(ups)
.then((vars) => {
if (rq.query.parsed && rq.query.parsed === 'true') {
vars = parseUpsVars(vars);
vars.ups.statusnum = statusses[vars.ups.status];
}
const endDateTime = moment();
logger.debug(`processing took ${time(startDateTime, endDateTime)}`);
rs.send(vars);
if (LOCK > 0) LOCK--;
return vars;
})
.catch((err) => {
if (err === 'UNKNOWN-UPS') {
logger.error('unknown ups requested');
rs.send(404, { code: 404, message: `UPS '${ups}' is not unknown at ${config.NUT_ADDRESS}` });
return err;
}
logger.error(err);
rs.send(500, { code: 500, message: `an internal error occurred ${err}` });
return err;
});
}, LOCK * config.LOCK_TIMEOUT, req, res);
LOCK++;
});
server.listen(config.SERVER_PORT, () => {
logger.info(`nut-http listening on port ${config.SERVER_PORT}`);
});
function connectNut() {
if (!nut.connected) nut.connect();
}