-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenfloorplan.js
297 lines (258 loc) · 8.5 KB
/
genfloorplan.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-loop-func */
/* eslint-disable no-console */
const chalk = require('chalk');
const url = require('node:url');
const https = require('https');
const http = require('http');
const fs = require('fs');
const yaml = require('js-yaml');
const libxml = require('libxmljs2');
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');
const header = `
_____ _____ _____ _ _____ _ _____
| | || _ || __|| | ___ ___ ___ | _ || | ___ ___ | __| ___ ___
| || || __|| || . || . || _|| __|| || .'|| || | || -_|| |
|__|__||__|__||__| |_||___||___||_| |__| |_||__,||_|_||_____||___||_|_|
`;
const namespaces = {
inkscape: 'http://www.inkscape.org/namespaces/inkscape',
sodipodi: 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
svg: 'http://www.w3.org/2000/svg',
};
const optionDefinitions = [
{
name: 'svg',
alias: 's',
type: String,
multiple: true,
description: 'The svg floorplan file to process',
typeLabel: '<file>',
required: true,
},
{
name: 'rules',
alias: 'r',
type: String,
multiple: true,
description: 'The HA Floorplan rules to base from',
typeLabel: '<file>',
required: true,
},
{
name: 'url',
alias: 'u',
type: String,
description: 'The url to the Home Assistant server',
typeLabel: '<url>',
required: true,
},
{
name: 'token',
alias: 't',
type: String,
description: 'Long lived token to the Home Assistant server',
typeLabel: '<token>',
required: true,
},
// {
// name: "log",
// alias: "l",
// type: String,
// description: "info, warn or error",
// },
];
/** ************** Function declarations *********************** */
// generate a 8 character random string
const randomString = (length) => {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; // 62 characters
const charactersLength = characters.length;
for (let i = 0; i < length; i += 1) {
result += characters.charAt(Math.random() * charactersLength);
} // end for
return result;
}; // end randomString
let cmdOptions;
try {
cmdOptions = commandLineArgs(optionDefinitions);
} catch {
// noop
}
let allSet = (cmdOptions == true);
if (cmdOptions && cmdOptions.svg && cmdOptions.rules && cmdOptions.url && cmdOptions.token) {
allSet = true;
} else {
console.log(chalk.red('All command line options are required.'));
}
if (!allSet || cmdOptions.help) {
const usage = commandLineUsage([
{
content: chalk.blue(header),
raw: true,
},
{
header: 'Generator for HA Floorplan',
content: `A simple application to add entities to SVG and collect for rules.
- All command line options are required.
- Example SVG and rule fines are available in the examples folder.
- The SVG file will be backed up before processing.
- The rules file is a yaml file that contains the rules for the entities to be added to the SVG.
Created rules to be included in the HA Floorplan configuration are stored a ha_rules.yml file
in the working directory.
`,
},
{
header: 'Required parameters',
optionList: optionDefinitions,
},
{
content:
'Project home: {underline https://github.com/osfog/hafloorplangen}',
},
]);
console.log(usage);
} else {
const svgFileName = cmdOptions.svg[0];
console.log(`Using: ${svgFileName} as SVG reference`);
// copy file to backup
fs.copyFileSync(svgFileName, `${svgFileName}.${randomString(6)}.bak`);
// read svg file
const xmlFile = fs.readFileSync(svgFileName, 'utf8');
const svgDoc = libxml.parseXmlString(xmlFile);
// read rules file
const rulesFile = fs.readFileSync(cmdOptions.rules[0], 'utf8');
let rules = null;
// validate yaml
try {
rules = yaml.load(rulesFile);
} catch (err) {
console.error(`Error in rules: ${err.message}`);
}
const q = url.parse(cmdOptions.url, true);
// test if the url is valid
if (!q.hostname) {
console.error('Invalid URL');
}
// test if the protocol is http or https
if (q.protocol !== 'http:' && q.protocol !== 'https:') {
console.error('Invalid protocol');
}
const protocol = q.protocol === 'http:' ? http : https;
const requestOptions = {
path: '/api/states',
host: q.hostname,
port: q.port,
method: 'GET',
headers: {
Authorization: `Bearer ${cmdOptions.token}`,
},
};
let entities = [];
let data = '';
const haFloorplanRules = [];
const req = protocol.request(requestOptions, (resp) => {
console.log('Fetching entities from Home Assistant');
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received
resp.on('end', () => {
entities = JSON.parse(data);
const entityIDs = entities.map((e) => e.entity_id);
console.info(`Received: ${entities.length} entities`);
// iterate the types that there we want rules for
for (const rule of rules) {
const svgPrimitive = rule.svg_primitive || rule.type;
let layerSVGElement = svgDoc.get(
`//*[@inkscape:label='${svgPrimitive}']`,
namespaces,
);
// validate yaml
try {
yaml.load(rule.rule_snippet);
} catch (err) {
console.error(
`Error in rule snippet for ${rule.type}: ${err.message}`,
);
}
if (!layerSVGElement) {
console.info(`Layer ${rule.type} does not exist - creating it`);
layerSVGElement = svgDoc.root().node('g');
layerSVGElement.attr({
'inkscape:groupmode': 'layer',
id: `layer_${svgPrimitive}`,
'inkscape:label': svgPrimitive,
});
}
// filter entities
let ruleEntities = entityIDs.filter((e) => e.split('.')[0] === rule.type);
// filter by attribute
if (rule.attribute) {
ruleEntities = ruleEntities.filter(
(e) => entities.find((ee) => ee.entity_id === e).attributes
.device_class === rule.attribute.device_class,
);
}
// filter by friendly name
if (rule.friendly_name_includes) {
ruleEntities = ruleEntities.filter((e) => entities
.find((ee) => ee.entity_id === e)
.attributes.friendly_name.toLowerCase()
.includes(rule.friendly_name_includes));
}
console.info(
`Found ${ruleEntities.length} entities of type ${rule.type
}, attribute ${rule.attribute ? JSON.stringify(rule.attribute) : '<none>'
}, friendly_name_includes: ${rule.friendly_name_includes
? rule.friendly_name_includes
: '<none>'
}`,
);
// Generate the rule part
rule.rules.entities = ruleEntities;
haFloorplanRules.push(rule.rules);
let svgSnippets = svgDoc.find(
`//*[@inkscape:label='floorplan.${svgPrimitive}']`,
namespaces,
);
if (!svgSnippets || svgSnippets.length === 0) {
svgSnippets = svgDoc.find(
`//*[@id='floorplan.${svgPrimitive}']`,
namespaces,
);
}
if (svgSnippets.length > 1) {
console.warn('More than one svg snippet found');
}
if (svgSnippets.length === 0) {
console.error(`No svg snippet for ${rule.type} found`);
}
const svgSnippet = svgSnippets[0];
// Generate the svg part
ruleEntities.forEach((e) => {
if (!svgDoc.get(`//*[@id='${e}']`)) {
layerSVGElement.addChild(
svgSnippet.clone().attr({ id: e, 'inkscape:label': e }),
);
console.info(`Entity ${e} has been added to SVG`);
} else {
console.info(`Entity ${e} already exists in SVG`);
}
});
}
fs.writeFileSync(svgFileName, svgDoc.toString());
fs.writeFileSync(
`${__dirname}/ha_rules.yml`,
yaml.dump(haFloorplanRules, { lineWidth: 1000 }),
);
});
});
req.end();
req.on('error', (e) => {
console.error(
'Failed to get entities - please ensure that Home Assistant server is available and that the long lived token is correct.', e);
});
}