-
Notifications
You must be signed in to change notification settings - Fork 2
/
router-data.ts
58 lines (42 loc) · 1.67 KB
/
router-data.ts
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
import * as fs from 'fs';
import * as path from 'path';
import * as express from 'express';
import { handleError, RouterError, BadRequestError, AuthenticationError } from './router-error';
import { FroniusMeter } from '../devices/fronius-meter';
import * as debugsx from 'debug-sx';
const debug: debugsx.IFullLogger = debugsx.createFullLogger('routers:RouterData');
export class RouterData {
public static get Instance(): express.Router {
if (!this._instance) {
this._instance = new RouterData;
}
return this._instance._router;
}
private static _instance: RouterData;
// ******************************************************
private _router: express.Router;
private constructor () {
this._router = express.Router();
this._router.get('/froniusmeter', (req, res, next) => this.getFroniusMeterJson(req, res, next));
this._router.get('/*', (req, res, next) => this.getAll(req, res, next));
}
private async getAll (req: express.Request, res: express.Response, next: express.NextFunction) {
try {
res.send('OK');
} catch (err) {
handleError(err, req, res, next, debug);
}
}
private async getFroniusMeterJson (req: express.Request, res: express.Response, next: express.NextFunction) {
try {
const fd = FroniusMeter.getInstance(req.query.address ? req.query.address : 1);
if (!fd) {
res.json({ error: 'device not found' });
} else {
res.json(fd.toValuesObject());
}
} catch (err) {
handleError(err, req, res, next, debug);
}
}
}