-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscan.js
executable file
·108 lines (98 loc) · 2.68 KB
/
scan.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
#!/usr/bin/env node
var request = require('request');
var MCrypt = require('mcrypt').MCrypt;
var buffertrim = require('buffertrim');
var config = require('yargs')
.env('KM200')
.usage('Scans a KM200 API\n\nUsage: $0 [options]')
.describe('p', 'KM200 passcode')
.describe('k', 'KM200 host')
.describe('h', 'show help')
.alias({
'h': 'help',
'k': 'km200',
'p': 'passcode'
})
.default({
})
.version()
.help('help')
.argv;
var Table = require('cli-table');
var table = new Table({
head: ['id', 'type', 'value', 'unit', 'Rec', 'Write', 'min', 'max', 'values'],
colWidths: [60, 16, 25, 10, 6, 6, 6, 12, 80]
});
var key = Buffer.from(config.passcode, 'hex');
var host = config.km200;
var APIs = [
'/gateway',
'/system',
'/heatSources',
'/recordings',
'/notifications',
'/heatingCircuits',
'/solarCircuits',
'/dhwCircuits'
];
var desEcb = new MCrypt('rijndael-128', 'ecb');
desEcb.open(key);
function getKM200 (host, api, done) {
var options = {
url: 'http://' + host + api,
headers: {
'Content-type': 'application/json',
'User-Agent': 'TeleHeater/2.2.3'
}
};
request.get(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
try {
var bodyBuffer = Buffer.from(body, 'base64');
var dataBuffer = buffertrim.trimEnd(desEcb.decrypt(bodyBuffer, 'base64'));
var result = JSON.parse(dataBuffer.toString());
done(dataBuffer.toString(), result);
} catch (e) {
done({ error: e });
}
} else {
done({ error: error });
}
});
}
function requestKM200 (host, APIs) {
var API = APIs.shift();
getKM200(host, API, function (result, json) {
console.error(API, APIs.length);
if (json !== undefined) analyzeResult(json, APIs);
if (APIs.length) requestKM200(host, APIs);
else console.log(table.toString());
});
}
function analyzeResult (json, APIs) {
if (json.type === 'refEnum') {
json.references.forEach(function (e) {
APIs.unshift(e.id);
});
} else buildTableEntry(json);
}
function buildTableEntry (json) {
var entry = [
optionalTableString(json.id),
optionalTableString(json.type),
optionalTableString(json.value),
optionalTableString(json.unitOfMeasure),
optionalTableString(json.recordable),
optionalTableString(json.writeable),
optionalTableString(json.minValue),
optionalTableString(json.maxValue),
optionalTableString(json.allowedValues)
];
table.push(entry);
}
function optionalTableString (element) {
if (element === undefined) return '';
else if (Array.isArray(element)) return JSON.stringify(element);
else return JSON.stringify(element);
}
requestKM200(host, APIs);