-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathweb.js
77 lines (66 loc) · 2.98 KB
/
web.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
'use strict';
/**
* Proxy class
*
* Read files from localhost server
*
* @class
* @param {object} server http or https node.js object
* @param {object} webSettings settings of the web server, like <pre><code>{secure: settings.secure, port: settings.port}</code></pre>
* @param {object} adapter web adapter object
* @param {object} instanceSettings instance object with common and native
* @param {object} app express application
* @return {object} object instance
*/
class LaMetricWebExtension {
constructor(server, webSettings, adapter, instanceSettings, app) {
this.app = app;
this.adapter = adapter;
this.settings = webSettings;
this.config = instanceSettings ? instanceSettings.native : {};
this.namespace = instanceSettings ? instanceSettings._id.substring('system.adapter.'.length) : 'lametric';
this.appPath = `/${this.namespace}/`;
}
waitForReady(cb) {
if (this.settings.secure) {
this.adapter.log.error(`Unable to register ${this.namespace} web extension - HTTPS is not supported by LaMetric`);
} else {
if (this.app && this.config.type === 'poll') {
this.app.use(this.appPath, (req, res) => {
this.adapter.log.debug(`Received ${req.method} request on ${req.url}`);
res.setHeader('Content-Type', 'application/json; charset=utf-8');
if (req.method == 'GET') {
try {
this.adapter.getForeignState(`${this.namespace}.mydatadiy.obj`, (err, state) => {
if (!err && state && state.val) {
res.status(200).send(state.val);
} else {
res.status(500).send(JSON.stringify({ error: `unable to read state ${this.namespace}.mydatadiy.obj` }));
}
});
} catch (err) {
res.status(500).send(JSON.stringify({ error: err }));
}
} else {
res.status(500).send(JSON.stringify({ error: 'unsupported method' }));
}
});
this.adapter.log.info(`${this.namespace} server listening on port http://${this.adapter.config.bind}:${this.settings.port}/${this.namespace}/`);
}
}
cb();
}
unload() {
return new Promise((resolve) => {
this.adapter.log.debug(`${this.namespace} extension unloaded!`);
// unload app path
const middlewareIndex = this.app._router.stack.findIndex((layer) => layer && layer.route === this.appPath);
if (middlewareIndex !== -1) {
// Remove the matched middleware
this.app._router.stack.splice(middlewareIndex, 1);
}
resolve(true);
});
}
}
module.exports = LaMetricWebExtension;