homebridge-brother-printer 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +22 -3
- package/dist/accessory.js +595 -0
- package/{bin → dist/bin}/brother-printer-probe.js +12 -14
- package/dist/decode.js +326 -0
- package/dist/index.js +6 -0
- package/dist/platform.js +89 -0
- package/dist/settings.js +5 -0
- package/dist/snmp.js +183 -0
- package/package.json +16 -9
- package/index.js +0 -8
- package/lib/accessory.js +0 -474
- package/lib/decode.js +0 -299
- package/lib/platform.js +0 -87
- package/lib/settings.js +0 -6
- package/lib/snmp.js +0 -152
|
@@ -1,27 +1,25 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
"use strict";
|
|
4
3
|
/*
|
|
5
4
|
* Quick check from the command line:
|
|
6
5
|
* npx brother-printer-probe 10.0.4.150 [community]
|
|
7
6
|
* Prints what the plugin would read from the printer.
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const snmp_1 = require("../snmp");
|
|
12
10
|
const [target, community = 'public'] = process.argv.slice(2);
|
|
13
11
|
const [host, port] = (target || '').split(':');
|
|
14
12
|
if (!host) {
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
console.error('Usage: brother-printer-probe <printer-ip-or-hostname[:port]> [community]');
|
|
14
|
+
process.exit(2);
|
|
17
15
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
.then((d) => {
|
|
16
|
+
(0, snmp_1.query)({ host, port: Number(port) || 161, community })
|
|
17
|
+
.then((d) => {
|
|
21
18
|
console.log(JSON.stringify(d, null, 2));
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
})
|
|
20
|
+
.catch((err) => {
|
|
21
|
+
const e = err;
|
|
22
|
+
console.error(`Couldn't read ${host}: ${e?.message || err}`);
|
|
25
23
|
console.error('Check the address, and that SNMP is enabled on the printer (Network > Protocol).');
|
|
26
24
|
process.exit(1);
|
|
27
|
-
|
|
25
|
+
});
|
package/dist/decode.js
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Brother printers pack maintenance data into OctetString "blobs" of records:
|
|
4
|
+
* [code(1)] [0x01] [length(1)] [value(length bytes, big-endian)]
|
|
5
|
+
* terminated by 0xFF. Older models use length 2 with [current][max], read as
|
|
6
|
+
* current / max %.
|
|
7
|
+
*
|
|
8
|
+
* Record codes follow the bieniu/brother library (Apache-2.0), which Home
|
|
9
|
+
* Assistant's Brother integration uses, and were checked against a real
|
|
10
|
+
* MFC-L2750DW.
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.COLORS = void 0;
|
|
14
|
+
exports.toBuffer = toBuffer;
|
|
15
|
+
exports.parseRecords = parseRecords;
|
|
16
|
+
exports.decodeMaintenance = decodeMaintenance;
|
|
17
|
+
exports.decodeCounters = decodeCounters;
|
|
18
|
+
exports.decodeText = decodeText;
|
|
19
|
+
exports.modelFromDeviceId = modelFromDeviceId;
|
|
20
|
+
exports.printerType = printerType;
|
|
21
|
+
exports.supplyList = supplyList;
|
|
22
|
+
exports.decodeErrorState = decodeErrorState;
|
|
23
|
+
exports.summarizeStatus = summarizeStatus;
|
|
24
|
+
exports.COLORS = ['black', 'cyan', 'magenta', 'yellow'];
|
|
25
|
+
// Maintenance blob (brInfoMaintenance)
|
|
26
|
+
const MAINTENANCE = {
|
|
27
|
+
0x81: { group: 'level', color: 'black' },
|
|
28
|
+
0x82: { group: 'level', color: 'cyan' },
|
|
29
|
+
0x83: { group: 'level', color: 'magenta' },
|
|
30
|
+
0x84: { group: 'level', color: 'yellow' },
|
|
31
|
+
0x6f: { group: 'fine', color: 'black', percent: true },
|
|
32
|
+
0x70: { group: 'fine', color: 'cyan', percent: true },
|
|
33
|
+
0x71: { group: 'fine', color: 'magenta', percent: true },
|
|
34
|
+
0x72: { group: 'fine', color: 'yellow', percent: true },
|
|
35
|
+
0xa1: { group: 'fine', color: 'black', percent: true },
|
|
36
|
+
0xa2: { group: 'fine', color: 'cyan', percent: true },
|
|
37
|
+
0xa3: { group: 'fine', color: 'magenta', percent: true },
|
|
38
|
+
0xa4: { group: 'fine', color: 'yellow', percent: true },
|
|
39
|
+
0x31: { group: 'supplyStatus', color: 'black' },
|
|
40
|
+
0x32: { group: 'supplyStatus', color: 'cyan' },
|
|
41
|
+
0x33: { group: 'supplyStatus', color: 'magenta' },
|
|
42
|
+
0x34: { group: 'supplyStatus', color: 'yellow' },
|
|
43
|
+
0x41: { group: 'drum', color: 'black', percent: true },
|
|
44
|
+
0x80: { group: 'drum', color: 'black', percent: true },
|
|
45
|
+
0x79: { group: 'drum', color: 'cyan', percent: true },
|
|
46
|
+
0x7a: { group: 'drum', color: 'magenta', percent: true },
|
|
47
|
+
0x7b: { group: 'drum', color: 'yellow', percent: true },
|
|
48
|
+
0x11: { group: 'drumPagesUsed', color: 'black' },
|
|
49
|
+
0x7e: { group: 'drumPagesUsed', color: 'black' },
|
|
50
|
+
0x73: { group: 'drumPagesUsed', color: 'cyan' },
|
|
51
|
+
0x74: { group: 'drumPagesUsed', color: 'magenta' },
|
|
52
|
+
0x75: { group: 'drumPagesUsed', color: 'yellow' },
|
|
53
|
+
0x63: { group: 'misc', key: 'drumStatus' },
|
|
54
|
+
0x69: { group: 'parts', key: 'belt', percent: true },
|
|
55
|
+
0x6a: { group: 'parts', key: 'fuser', percent: true },
|
|
56
|
+
0x6b: { group: 'parts', key: 'laser', percent: true },
|
|
57
|
+
0x6c: { group: 'parts', key: 'paperFeedMP', percent: true },
|
|
58
|
+
0x6d: { group: 'parts', key: 'paperFeed1', percent: true },
|
|
59
|
+
0x85: { group: 'parts', key: 'inkWasteBox' },
|
|
60
|
+
};
|
|
61
|
+
// Counter blob (brInfoCounter)
|
|
62
|
+
const COUNTERS = {
|
|
63
|
+
0x00: 'totalPages',
|
|
64
|
+
0x01: 'monoPages',
|
|
65
|
+
0x02: 'colorPages',
|
|
66
|
+
0x06: 'duplexPages',
|
|
67
|
+
};
|
|
68
|
+
// Next-care blob (brInfoNextCare), laser only: pages left
|
|
69
|
+
const NEXT_CARE = {
|
|
70
|
+
0x82: { group: 'drumPagesRemaining', color: 'black' },
|
|
71
|
+
0xa4: { group: 'drumPagesRemaining', color: 'black' },
|
|
72
|
+
0xa5: { group: 'drumPagesRemaining', color: 'cyan' },
|
|
73
|
+
0xa6: { group: 'drumPagesRemaining', color: 'magenta' },
|
|
74
|
+
0xa7: { group: 'drumPagesRemaining', color: 'yellow' },
|
|
75
|
+
0x73: { group: 'partPagesRemaining', key: 'laser' },
|
|
76
|
+
0x77: { group: 'partPagesRemaining', key: 'paperFeed1' },
|
|
77
|
+
0x86: { group: 'partPagesRemaining', key: 'paperFeedMP' },
|
|
78
|
+
0x88: { group: 'partPagesRemaining', key: 'belt' },
|
|
79
|
+
0x89: { group: 'partPagesRemaining', key: 'fuser' },
|
|
80
|
+
};
|
|
81
|
+
function toBuffer(value) {
|
|
82
|
+
if (value === null || value === undefined) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
if (Buffer.isBuffer(value)) {
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
if (Array.isArray(value)) {
|
|
89
|
+
return toBuffer(value.join(''));
|
|
90
|
+
}
|
|
91
|
+
if (typeof value === 'string') {
|
|
92
|
+
const hex = value.replace(/[^0-9a-f]/gi, '');
|
|
93
|
+
if (hex.length && hex.length % 2 === 0) {
|
|
94
|
+
return Buffer.from(hex, 'hex');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
/** Parse a Brother record blob into [{ code, value, legacy }] */
|
|
100
|
+
function parseRecords(input) {
|
|
101
|
+
const buf = toBuffer(input);
|
|
102
|
+
const records = [];
|
|
103
|
+
if (!buf) {
|
|
104
|
+
return records;
|
|
105
|
+
}
|
|
106
|
+
let i = 0;
|
|
107
|
+
while (i < buf.length) {
|
|
108
|
+
const code = buf[i];
|
|
109
|
+
if (code === 0xff) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
if (i + 3 > buf.length) {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
const len = buf[i + 2];
|
|
116
|
+
const start = i + 3;
|
|
117
|
+
const end = start + len;
|
|
118
|
+
if (end > buf.length || len === 0 || len > 6) {
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
if (len === 2) {
|
|
122
|
+
// Legacy format: [current][max]
|
|
123
|
+
const [cur, max] = [buf[start], buf[start + 1]];
|
|
124
|
+
if (max > 0) {
|
|
125
|
+
records.push({ code, value: Math.round((cur / max) * 100), legacy: true, len });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
let value = 0;
|
|
130
|
+
for (let j = start; j < end; j++) {
|
|
131
|
+
value = value * 256 + buf[j];
|
|
132
|
+
}
|
|
133
|
+
records.push({ code, value, legacy: false, len });
|
|
134
|
+
}
|
|
135
|
+
i = end;
|
|
136
|
+
}
|
|
137
|
+
return records;
|
|
138
|
+
}
|
|
139
|
+
// Percent records are hundredths of a % in 4-byte form; short records with a
|
|
140
|
+
// percent code (seen on old models) aren't levels, so they're skipped.
|
|
141
|
+
const pct = (rec, def) => {
|
|
142
|
+
if (!def.percent || rec.legacy) {
|
|
143
|
+
return rec.value;
|
|
144
|
+
}
|
|
145
|
+
return rec.len >= 4 ? Math.round(rec.value / 100) : null;
|
|
146
|
+
};
|
|
147
|
+
const finite = (v) => (typeof v === 'number' && Number.isFinite(v) ? v : null);
|
|
148
|
+
function emptySupplies() {
|
|
149
|
+
return Object.fromEntries(exports.COLORS.map((c) => [c, { level: null, fine: null, status: null }]));
|
|
150
|
+
}
|
|
151
|
+
/** Decode the maintenance and next-care blobs */
|
|
152
|
+
function decodeMaintenance(maintenance, nextCare) {
|
|
153
|
+
const supplies = emptySupplies();
|
|
154
|
+
const drums = {};
|
|
155
|
+
const parts = {};
|
|
156
|
+
const misc = {};
|
|
157
|
+
const unknown = {};
|
|
158
|
+
for (const rec of parseRecords(maintenance)) {
|
|
159
|
+
if (rec.legacy) {
|
|
160
|
+
misc.legacy = true;
|
|
161
|
+
}
|
|
162
|
+
const def = MAINTENANCE[rec.code];
|
|
163
|
+
if (!def) {
|
|
164
|
+
unknown[`0x${rec.code.toString(16).padStart(2, '0')}`] = rec.value;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const v = pct(rec, def);
|
|
168
|
+
if (v === null) {
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
switch (def.group) {
|
|
172
|
+
case 'level':
|
|
173
|
+
supplies[def.color].level = v;
|
|
174
|
+
break;
|
|
175
|
+
case 'fine':
|
|
176
|
+
supplies[def.color].fine = v;
|
|
177
|
+
break;
|
|
178
|
+
case 'supplyStatus':
|
|
179
|
+
supplies[def.color].status = v;
|
|
180
|
+
break;
|
|
181
|
+
case 'drum':
|
|
182
|
+
(drums[def.color] = drums[def.color] || {}).percent = v;
|
|
183
|
+
break;
|
|
184
|
+
case 'drumPagesUsed':
|
|
185
|
+
(drums[def.color] = drums[def.color] || {}).pagesUsed = v;
|
|
186
|
+
break;
|
|
187
|
+
case 'parts':
|
|
188
|
+
(parts[def.key] = parts[def.key] || {}).percent = v;
|
|
189
|
+
break;
|
|
190
|
+
default:
|
|
191
|
+
misc[def.key] = v;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
for (const rec of parseRecords(nextCare)) {
|
|
195
|
+
const def = NEXT_CARE[rec.code];
|
|
196
|
+
if (!def) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
if (def.group === 'drumPagesRemaining') {
|
|
200
|
+
(drums[def.color] = drums[def.color] || {}).pagesRemaining = rec.value;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
(parts[def.key] = parts[def.key] || {}).pagesRemaining = rec.value;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// Drum entries that only carry a page count aren't useful on their own
|
|
207
|
+
for (const [color, d] of Object.entries(drums)) {
|
|
208
|
+
if (d.percent === undefined && d.pagesRemaining === undefined) {
|
|
209
|
+
delete drums[color];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return { supplies, drums, parts, misc, unknown };
|
|
213
|
+
}
|
|
214
|
+
function decodeCounters(input) {
|
|
215
|
+
const out = {};
|
|
216
|
+
for (const { code, value } of parseRecords(input)) {
|
|
217
|
+
const key = COUNTERS[code];
|
|
218
|
+
if (key) {
|
|
219
|
+
out[key] = value;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
/** prtLocalizationCharacterSet (IANA MIBenum) to a TextDecoder label */
|
|
225
|
+
const CHARSETS = {
|
|
226
|
+
3: 'ascii', 4: 'latin1', 5: 'iso-8859-2', 8: 'iso-8859-5', 12: 'iso-8859-9', 106: 'utf-8',
|
|
227
|
+
};
|
|
228
|
+
/** Decode an SNMP OctetString that holds text */
|
|
229
|
+
function decodeText(input, charset) {
|
|
230
|
+
const buf = toBuffer(input);
|
|
231
|
+
if (!buf) {
|
|
232
|
+
return typeof input === 'string' ? input.trim() : '';
|
|
233
|
+
}
|
|
234
|
+
let text;
|
|
235
|
+
try {
|
|
236
|
+
text = new TextDecoder((charset && CHARSETS[charset]) || 'latin1', { fatal: true }).decode(buf);
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
text = buf.toString('latin1');
|
|
240
|
+
}
|
|
241
|
+
// eslint-disable-next-line no-control-regex
|
|
242
|
+
return text.replace(/[\x00-\x1f]+/g, ' ').replace(/\s+/g, ' ').trim();
|
|
243
|
+
}
|
|
244
|
+
/** Model from the PJL device ID, e.g. "MFG:Brother;CMD:PJL;MDL:DCP-L3550CDW;CLS:PRINTER;" */
|
|
245
|
+
function modelFromDeviceId(text) {
|
|
246
|
+
const m = /MDL:([^;]+)/.exec(text || '');
|
|
247
|
+
return m ? m[1].trim() : null;
|
|
248
|
+
}
|
|
249
|
+
/** "laser" or "ink", from what the printer reports */
|
|
250
|
+
function printerType(model, decoded, deviceId) {
|
|
251
|
+
if (decoded && decoded.parts.inkWasteBox) {
|
|
252
|
+
return 'ink';
|
|
253
|
+
}
|
|
254
|
+
if (/CID:[^;]*Laser/i.test(deviceId || '')) {
|
|
255
|
+
return 'laser';
|
|
256
|
+
}
|
|
257
|
+
if (/\b(?:DCP|MFC|HL)-[JT]/i.test(model || '')) {
|
|
258
|
+
return 'ink';
|
|
259
|
+
}
|
|
260
|
+
// The current/max record format is only used by older inkjets
|
|
261
|
+
if (decoded && decoded.misc.legacy) {
|
|
262
|
+
return 'ink';
|
|
263
|
+
}
|
|
264
|
+
return 'laser';
|
|
265
|
+
}
|
|
266
|
+
/** One entry per supply the printer reports a level for */
|
|
267
|
+
function supplyList(supplies) {
|
|
268
|
+
const list = [];
|
|
269
|
+
for (const color of exports.COLORS) {
|
|
270
|
+
const s = supplies[color];
|
|
271
|
+
const level = finite(s.level) ?? finite(s.fine);
|
|
272
|
+
if (level === null) {
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
list.push({ color, level: Math.max(0, Math.min(100, level)), fine: finite(s.fine) });
|
|
276
|
+
}
|
|
277
|
+
return list;
|
|
278
|
+
}
|
|
279
|
+
/** hrPrinterDetectedErrorState bit names (RFC 3805 / RFC 2790) */
|
|
280
|
+
const ERROR_BITS = [
|
|
281
|
+
'lowPaper', 'noPaper', 'lowToner', 'noToner',
|
|
282
|
+
'doorOpen', 'jammed', 'offline', 'serviceRequested',
|
|
283
|
+
'inputTrayMissing', 'outputTrayMissing', 'markerSupplyMissing', 'outputNearFull',
|
|
284
|
+
'outputFull', 'inputTrayEmpty', 'overduePreventMaint',
|
|
285
|
+
];
|
|
286
|
+
function decodeErrorState(input) {
|
|
287
|
+
const buf = toBuffer(input);
|
|
288
|
+
const errors = [];
|
|
289
|
+
if (!buf) {
|
|
290
|
+
return errors;
|
|
291
|
+
}
|
|
292
|
+
for (let bit = 0; bit < ERROR_BITS.length; bit++) {
|
|
293
|
+
const byte = buf[bit >> 3];
|
|
294
|
+
if (byte === undefined) {
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
if (byte & (0x80 >> (bit & 7))) {
|
|
298
|
+
errors.push(ERROR_BITS[bit]);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return errors;
|
|
302
|
+
}
|
|
303
|
+
/** Errors that stop the printer and deserve the Problem sensor */
|
|
304
|
+
const FAULT_ERRORS = new Set([
|
|
305
|
+
'noPaper', 'noToner', 'doorOpen', 'jammed', 'serviceRequested',
|
|
306
|
+
'inputTrayMissing', 'outputTrayMissing', 'markerSupplyMissing', 'outputFull',
|
|
307
|
+
]);
|
|
308
|
+
// Display text is in the printer's language; English patterns are only a
|
|
309
|
+
// fallback for printers that don't set the error bits.
|
|
310
|
+
const FAULT_TEXT = /jam|cover is open|open|no paper|paper empty|replace|no toner|toner end|drum end|drum stop|error|call service|unable|cannot|no tray|size mismatch|out of memory/i;
|
|
311
|
+
const WARNING_TEXT = /\blow\b|end soon|soon/i;
|
|
312
|
+
const PRINTING_TEXT = /printing|receiving|copying/i;
|
|
313
|
+
/** hrPrinterStatus: 1 other, 2 unknown, 3 idle, 4 printing, 5 warmup */
|
|
314
|
+
function summarizeStatus({ statusText = '', printerStatus = null, errors = [] }) {
|
|
315
|
+
const text = statusText || '';
|
|
316
|
+
const faults = errors.filter((e) => FAULT_ERRORS.has(e));
|
|
317
|
+
// "Toner Low" / "Drum End Soon" are warnings handled by the battery services
|
|
318
|
+
const textFault = FAULT_TEXT.test(text) && !WARNING_TEXT.test(text);
|
|
319
|
+
const printing = printerStatus === 4 || (PRINTING_TEXT.test(text) && !textFault);
|
|
320
|
+
return {
|
|
321
|
+
printing,
|
|
322
|
+
fault: faults.length > 0 || textFault,
|
|
323
|
+
faults,
|
|
324
|
+
sleeping: /sleep/i.test(text),
|
|
325
|
+
};
|
|
326
|
+
}
|
package/dist/index.js
ADDED
package/dist/platform.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BrotherPrinterPlatform = void 0;
|
|
4
|
+
const settings_1 = require("./settings");
|
|
5
|
+
const accessory_1 = require("./accessory");
|
|
6
|
+
// Resolved from dist/ at runtime, like the published package
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
8
|
+
const schema = require('../config.schema.json');
|
|
9
|
+
const DEFAULTS = Object.fromEntries(Object.entries(schema.schema.properties.printers.items.properties)
|
|
10
|
+
.filter(([, v]) => 'default' in v)
|
|
11
|
+
.map(([k, v]) => [k, v.default]));
|
|
12
|
+
class BrotherPrinterPlatform {
|
|
13
|
+
log;
|
|
14
|
+
config;
|
|
15
|
+
api;
|
|
16
|
+
cached = new Map();
|
|
17
|
+
printers = [];
|
|
18
|
+
constructor(log, config, api) {
|
|
19
|
+
this.log = log;
|
|
20
|
+
this.config = config || {};
|
|
21
|
+
this.api = api;
|
|
22
|
+
this.api.on('didFinishLaunching', () => {
|
|
23
|
+
try {
|
|
24
|
+
this.setup();
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
const e = err;
|
|
28
|
+
this.log.error(`Setup failed: ${e?.stack || err}`);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
this.api.on('shutdown', () => this.printers.forEach((p) => p.stop()));
|
|
32
|
+
}
|
|
33
|
+
configureAccessory(accessory) {
|
|
34
|
+
this.cached.set(accessory.UUID, accessory);
|
|
35
|
+
}
|
|
36
|
+
setup() {
|
|
37
|
+
const entries = Array.isArray(this.config.printers) ? this.config.printers : [];
|
|
38
|
+
const active = new Set();
|
|
39
|
+
entries.forEach((rawEntry, idx) => {
|
|
40
|
+
if (!rawEntry || !String(rawEntry.host || '').trim()) {
|
|
41
|
+
// The settings screen can save a blank row; only warn when it was filled in
|
|
42
|
+
const filled = !!rawEntry && Object.entries(rawEntry).some(([k, v]) => k !== 'host' && v !== DEFAULTS[k] && v !== '');
|
|
43
|
+
const msg = `Printer #${idx + 1} has no host set, skipping it.`;
|
|
44
|
+
if (filled) {
|
|
45
|
+
this.log.warn(msg);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
this.log.debug(msg);
|
|
49
|
+
}
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const host = String(rawEntry.host).trim();
|
|
53
|
+
const name = String(rawEntry.name || 'Printer').trim();
|
|
54
|
+
const uuid = this.api.hap.uuid.generate(`${settings_1.PLUGIN_NAME}:${name}`);
|
|
55
|
+
if (active.has(uuid)) {
|
|
56
|
+
this.log.warn(`Two printers are named "${name}". Give each a unique name; skipping the second.`);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
active.add(uuid);
|
|
60
|
+
let accessory = this.cached.get(uuid);
|
|
61
|
+
const isNew = !accessory;
|
|
62
|
+
if (!accessory) {
|
|
63
|
+
accessory = new this.api.platformAccessory(name, uuid, 10 /* this.api.hap.Categories.SENSOR */);
|
|
64
|
+
}
|
|
65
|
+
accessory.context.host = host;
|
|
66
|
+
try {
|
|
67
|
+
this.printers.push(new accessory_1.PrinterAccessory(this, accessory, { ...rawEntry, host, name }));
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
const e = err;
|
|
71
|
+
this.log.error(`[${name}] Couldn't set up this printer: ${e?.message || err}`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (isNew) {
|
|
75
|
+
this.api.registerPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, [accessory]);
|
|
76
|
+
this.log.info(`Added printer "${name}" (${host}).`);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this.api.updatePlatformAccessories([accessory]);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
const stale = [...this.cached.values()].filter((a) => !active.has(a.UUID));
|
|
83
|
+
if (stale.length) {
|
|
84
|
+
this.api.unregisterPlatformAccessories(settings_1.PLUGIN_NAME, settings_1.PLATFORM_NAME, stale);
|
|
85
|
+
stale.forEach((a) => this.log.info(`Removed printer "${a.displayName}", no longer in config.`));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.BrotherPrinterPlatform = BrotherPrinterPlatform;
|
package/dist/settings.js
ADDED
package/dist/snmp.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.OID = void 0;
|
|
37
|
+
exports.query = query;
|
|
38
|
+
exports.parse = parse;
|
|
39
|
+
const snmp = __importStar(require("net-snmp"));
|
|
40
|
+
const decode = __importStar(require("./decode"));
|
|
41
|
+
exports.OID = {
|
|
42
|
+
maintenance: '1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.8.0', // brInfoMaintenance
|
|
43
|
+
counters: '1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.10.0', // brInfoCounter
|
|
44
|
+
nextCare: '1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.11.0', // brInfoNextCare
|
|
45
|
+
statusText: '1.3.6.1.4.1.2435.2.3.9.4.2.1.5.4.5.2.0', // LCD status line
|
|
46
|
+
deviceId: '1.3.6.1.4.1.2435.2.3.9.1.1.7.0', // PJL device ID (MDL:...)
|
|
47
|
+
brSerial: '1.3.6.1.4.1.2435.2.3.9.4.2.1.5.5.1.0', // brInfoSerialNumber
|
|
48
|
+
model: '1.3.6.1.2.1.25.3.2.1.3.1', // hrDeviceDescr
|
|
49
|
+
serial: '1.3.6.1.2.1.43.5.1.1.17.1', // prtGeneralSerialNumber
|
|
50
|
+
charset: '1.3.6.1.2.1.43.7.1.1.4.1.1', // prtLocalizationCharacterSet
|
|
51
|
+
pageCount: '1.3.6.1.2.1.43.10.2.1.4.1.1', // prtMarkerLifeCount
|
|
52
|
+
printerStatus: '1.3.6.1.2.1.25.3.5.1.1.1', // hrPrinterStatus
|
|
53
|
+
errorState: '1.3.6.1.2.1.25.3.5.1.2.1', // hrPrinterDetectedErrorState
|
|
54
|
+
};
|
|
55
|
+
const NAMES = Object.keys(exports.OID);
|
|
56
|
+
const VERSIONS = { '2c': snmp.Version2c, 1: snmp.Version1 };
|
|
57
|
+
function get(session, oids) {
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
session.get(oids, (err, varbinds) => (err ? reject(err) : resolve(varbinds || [])));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* One request. Under SNMP v1 an OID the printer doesn't have fails the whole
|
|
64
|
+
* request (noSuchName), so that OID is dropped and the request repeated.
|
|
65
|
+
*/
|
|
66
|
+
async function getAll(options, version) {
|
|
67
|
+
const session = snmp.createSession(options.host, options.community || 'public', {
|
|
68
|
+
port: options.port || 161,
|
|
69
|
+
version: VERSIONS[version],
|
|
70
|
+
timeout: options.timeout ?? 5000,
|
|
71
|
+
retries: options.retries ?? 1,
|
|
72
|
+
idBitsSize: 32,
|
|
73
|
+
});
|
|
74
|
+
session.on('error', () => { });
|
|
75
|
+
const names = [...NAMES];
|
|
76
|
+
const raw = {};
|
|
77
|
+
try {
|
|
78
|
+
while (names.length) {
|
|
79
|
+
try {
|
|
80
|
+
const varbinds = await get(session, names.map((n) => exports.OID[n]));
|
|
81
|
+
varbinds.forEach((vb, idx) => {
|
|
82
|
+
raw[names[idx]] = snmp.isVarbindError(vb) ? null : vb.value;
|
|
83
|
+
});
|
|
84
|
+
return raw;
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
const e = err;
|
|
88
|
+
const missing = e && e.name === 'RequestFailedError' && /NoSuchName: ([\d.]+)/.exec(e.message);
|
|
89
|
+
const idx = missing ? names.findIndex((n) => exports.OID[n] === missing[1]) : -1;
|
|
90
|
+
if (idx < 0) {
|
|
91
|
+
throw err;
|
|
92
|
+
}
|
|
93
|
+
raw[names[idx]] = null;
|
|
94
|
+
names.splice(idx, 1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return raw;
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
try {
|
|
101
|
+
session.close();
|
|
102
|
+
}
|
|
103
|
+
catch { /* already closed */ }
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Query the printer once and return decoded values. Rejects only when the
|
|
108
|
+
* printer can't be reached at all.
|
|
109
|
+
*
|
|
110
|
+
* options.version: '2c', '1' or 'auto' (default). With 'auto', v2c is tried
|
|
111
|
+
* first and v1 when v2c gets no answer; options.state remembers which worked.
|
|
112
|
+
*/
|
|
113
|
+
async function query(options) {
|
|
114
|
+
const state = options.state || {};
|
|
115
|
+
const wanted = options.version && options.version !== 'auto' ? [String(options.version)] : null;
|
|
116
|
+
const order = wanted || (state.version ? [state.version] : ['2c', '1']);
|
|
117
|
+
let lastErr;
|
|
118
|
+
for (const version of order) {
|
|
119
|
+
try {
|
|
120
|
+
const raw = await getAll(options, version);
|
|
121
|
+
state.version = version;
|
|
122
|
+
return { ...parse(raw), snmpVersion: version };
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
lastErr = err;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
throw lastErr;
|
|
129
|
+
}
|
|
130
|
+
/** Some inkjets only report mono and color page counts */
|
|
131
|
+
function sumPages(c) {
|
|
132
|
+
return typeof c.monoPages === 'number' && typeof c.colorPages === 'number' ? c.monoPages + c.colorPages : null;
|
|
133
|
+
}
|
|
134
|
+
function parse(raw) {
|
|
135
|
+
const charset = typeof raw.charset === 'number' ? raw.charset : Number(decode.decodeText(raw.charset)) || null;
|
|
136
|
+
const statusText = decode.decodeText(raw.statusText, charset);
|
|
137
|
+
const printerStatus = typeof raw.printerStatus === 'number' ? raw.printerStatus : null;
|
|
138
|
+
const errors = decode.decodeErrorState(raw.errorState);
|
|
139
|
+
const deviceId = decode.decodeText(raw.deviceId);
|
|
140
|
+
const model = decode.decodeText(raw.model) || decode.modelFromDeviceId(deviceId) || '';
|
|
141
|
+
const maint = decode.decodeMaintenance(raw.maintenance, raw.nextCare);
|
|
142
|
+
const counters = decode.decodeCounters(raw.counters);
|
|
143
|
+
const supplies = decode.supplyList(maint.supplies);
|
|
144
|
+
const type = decode.printerType(model, maint, deviceId);
|
|
145
|
+
const pageCount = typeof raw.pageCount === 'number' ? raw.pageCount : null;
|
|
146
|
+
// Inkjets have no drum; their 0x41 record means something else
|
|
147
|
+
if (type === 'ink') {
|
|
148
|
+
maint.drums = {};
|
|
149
|
+
}
|
|
150
|
+
// Lowest supply; when coarse levels tie (they move in 10% steps), the finer
|
|
151
|
+
// reading decides, so a 2% yellow beats a 10% cyan
|
|
152
|
+
const rank = (s) => [s.level, s.fine ?? s.level];
|
|
153
|
+
const lowest = supplies.length
|
|
154
|
+
? supplies.reduce((a, b) => {
|
|
155
|
+
const [al, af] = rank(a);
|
|
156
|
+
const [bl, bf] = rank(b);
|
|
157
|
+
return bl < al || (bl === al && bf < af) ? b : a;
|
|
158
|
+
})
|
|
159
|
+
: null;
|
|
160
|
+
const black = maint.drums.black || {};
|
|
161
|
+
return {
|
|
162
|
+
model: model || null,
|
|
163
|
+
serial: decode.decodeText(raw.serial) || decode.decodeText(raw.brSerial) || null,
|
|
164
|
+
type,
|
|
165
|
+
statusText,
|
|
166
|
+
printerStatus,
|
|
167
|
+
errors,
|
|
168
|
+
...decode.summarizeStatus({ statusText, printerStatus, errors }),
|
|
169
|
+
supplies,
|
|
170
|
+
lowestSupply: lowest,
|
|
171
|
+
drums: maint.drums,
|
|
172
|
+
parts: maint.parts,
|
|
173
|
+
totalPages: counters.totalPages ?? pageCount ?? sumPages(counters),
|
|
174
|
+
colorPages: counters.colorPages ?? null,
|
|
175
|
+
duplexPages: counters.duplexPages ?? null,
|
|
176
|
+
tonerPercent: supplies.find((s) => s.color === 'black')?.level ?? null,
|
|
177
|
+
tonerPercentFine: supplies.find((s) => s.color === 'black')?.fine ?? null,
|
|
178
|
+
drumPercent: black.percent ?? null,
|
|
179
|
+
drumPagesUsed: black.pagesUsed ?? null,
|
|
180
|
+
drumPagesRemaining: black.pagesRemaining ?? null,
|
|
181
|
+
unknown: maint.unknown,
|
|
182
|
+
};
|
|
183
|
+
}
|