-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
279 lines (241 loc) · 10.3 KB
/
main.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
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const axios = require('axios').default;
let callReadPrinter;
let ip = '';
const baselevel = 50; // bedeutet: in der Webseite wird ein Balken von 100% Höhe 50px hoch gezeichnet.
// Also entspricht ein gezeigtes Tintenlevel von 25 (px) dann 50% und eines von 10 (px) dann 20%
let sync = 10;
let isUnloaded = false;
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: 'epson_ecotank_et_2750',
useFormatDate: true,
unload: function (callback) {
try {
stopReadPrinter();
isUnloaded = true;
callback();
} catch (e) {
callback();
}
},
ready: function () {
readSettings();
main();
adapter.setState('info.connection', false, true);
}
});
adapter = new utils.Adapter(options);
return adapter;
}
const ink = {
'cyan': {
'state': 'cyan',
'name': 'Cyan',
'inklvl_rx': "IMAGE\\/Ink_C\\.PNG\\' height=\\'([0-9]{1,2})\\'",
'cartridge_rx': '\\(C\\) \\:<\\/span><\\/dt><dd class=\\"value clearfix\\"><div class=\\"preserve-white-space\\">([a-zA-Z0-9\\/]*)<\\/div>'
},
'yellow': {
'state': 'yellow',
'name': 'Yellow',
'inklvl_rx': "IMAGE\\/Ink_Y\\.PNG\\' height=\\'([0-9]{1,2})\\'",
'cartridge_rx': '\\(Y\\) \\:<\\/span><\\/dt><dd class="value clearfix\\"><div class=\\"preserve-white-space\\">([a-zA-Z0-9\\/]*)<\\/div>'
},
'black': {
'state': 'black',
'name': 'Black',
'inklvl_rx': "IMAGE\\/Ink_K\\.PNG\\' height=\\'([0-9]{1,2})\\'",
'cartridge_rx': '\\(BK\\) \\:<\\/span><\\/dt><dd class=\\"value clearfix\\"><div class=\\"preserve-white-space\\">([a-zA-Z0-9\\/]*)<\\/div>'
},
'magenta': {
'state': 'magenta',
'name': 'Magenta',
'inklvl_rx': "IMAGE\\/Ink_M\\.PNG\\' height=\\'([0-9]{1,2})\\'",
'cartridge_rx': '\\(M\\) \\:<\\/span><\\/dt><dd class=\\"value clearfix\\"><div class=\\"preserve-white-space\\">([a-zA-Z0-9\\/]*)<\\/div>'
}
};
function readSettings() {
//check if IP is entered in settings
if (!adapter.config.printerip) {
adapter.log.warn('No IP adress of printer set up. Adapter will be stopped.');
}
else { // ip entered
ip = (adapter.config.printerport.length > 0) ? adapter.config.printerip + ':' + adapter.config.printerport : adapter.config.printerip; // if port is set then ip+port else ip only
adapter.log.debug('IP: ' + ip);
//check if sync time is entered in settings
sync = (!adapter.config.synctime) ? 10 : parseInt(adapter.config.synctime, 10);
adapter.log.debug('ioBroker reads printer every ' + sync + ' minutes');
} // end ip entered
}
async function readPrinterStatus() {
// Check if unload triggerted
if (isUnloaded) {
return;
}
const link = 'http://' + ip + '/PRESENTATION/ADVANCED/INFO_PRTINFO/TOP';
const resp = await axios.get(link);
if (resp.status === 200) {
adapter.setState('info.ip', {
val: ip,
ack: true
});
let match, rx;
// MAC ADRESSE EINLESEN
rx = new RegExp(/(?:MAC-Adresse|Printer Name|Adresse MAC Wi-Fi\/R.seau|Indirizzo MAC Wi-Fi\/rete|Dirección MAC de Wi-Fi\/Red|Endereço MAC de Wi-Fi\/Rede) :<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">([a-zA-Z0-9:]*)<\/div>/g);
let mac_string;
while ((match = rx.exec(resp.data)) != null) {
mac_string = match[1];
}
adapter.log.debug('mac_string: ' + mac_string);
adapter.setState('info.mac', { val: mac_string, ack: true });
// read firmware version
rx = new RegExp(/(?:Firmware|Firmware-Version) :<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">([a-zA-Z0-9 äöüÄÖÜ\-\_\.]*)<\/div>/g);
let firmware_string;
while ((match = rx.exec(resp.data)) != null) {
firmware_string = match[1];
}
adapter.log.debug('firmware_string: ' + firmware_string);
adapter.setState('info.firmware', { val: firmware_string, ack: true });
// read serial number
rx = new RegExp(/(?:Seriennummer|Serial Number|Numéro de série|Numero di serie|Número de serie|Número de série) :<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">([a-zA-Z0-9]*)<\/div>/g);
let serial_string;
while ((match = rx.exec(resp.data)) != null) {
serial_string = match[1];
}
adapter.log.debug('serial_string: ' + serial_string);
adapter.setState('info.serial', { val: serial_string, ack: true });
for (const i in ink) {
await adapter.setObjectNotExists(`inks.${ink[i].state}`, {
type: 'state',
common: {
role: 'level.volume',
name: 'Level of ' + ink[i].name,
desc: 'Level of ' + ink[i].name,
type: 'number',
unit: '%',
read: true,
write: false
},
native: {}
});
// read levels
rx = new RegExp(ink[i].inklvl_rx, 'g');
let level_string;
while ((match = rx.exec(resp.data)) != null) {
level_string = match[1];
}
adapter.log.debug(ink[i].name + ' Levelstring: ' + level_string + 'px');
const level = parseInt(level_string, 10) * 100 / baselevel;
adapter.setState(`inks.${ink[i].state}`, { val: level, ack: true });
adapter.log.debug(ink[i].name + ' Level: ' + level + '%');
} // end for
adapter.log.debug('Channels and states created/read');
} else {
adapter.log.warn('Cannot connect to Printer:');
}
adapter.log.debug('finished reading printer status data');
}
async function readPrinterNetwork() {
// Check if unload triggerted
if (isUnloaded) {
return;
}
const link = 'http://' + ip + '/PRESENTATION/ADVANCED/INFO_NWINFO/TOP';
const resp = await axios.get(link);
if (resp.status === 200) {
adapter.setState('info.ip', {
val: ip,
ack: true
});
let match, rx, name_string, model_string;
// NAME EINLESEN
rx = new RegExp(/(?:Gerätename|Druckername) :<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">([a-zA-Z0-9 äöüÄÖÜ\-\_]*)<\/div>/g);
while ((match = rx.exec(resp.data)) != null) {
name_string = match[1];
}
adapter.log.debug('name_string: ' + name_string);
adapter.setState('info.name', { val: name_string, ack: true });
// MODELL EINLESEN
rx = new RegExp(/<title>([a-zA-Z0-9 äöüÄÖÜ\-\_]*)<\/title>/g);
while ((match = rx.exec(resp.data)) != null) {
model_string = match[1];
}
adapter.log.debug('model_string: ' + model_string);
adapter.setState('info.model', { val: model_string, ack: true });
adapter.log.debug('Channels and states created/read');
} else {
adapter.log.warn('Cannot connect to Printer');
}
adapter.log.debug('finished reading printer network data');
}
async function readPrinterMaintenance() {
// Check if unload triggerted
if (isUnloaded) {
return;
}
const link = 'http://' + ip + '/PRESENTATION/ADVANCED/INFO_MENTINFO/TOP';
const resp = await axios.get(link);
if (resp.status === 200) {
adapter.setState('info.ip', {
val: ip,
ack: true
});
let match, rx, first_print_string, printed_pages_string;
// ERSTDRUCKDATUM EINLESEN
rx = new RegExp(/(?:Erstdruckdatum|First Printing Date|Date de première impression|Data prima stampa|Primera fecha de impresión|Data da primeira impressão) \:<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">((\d\d\-\d\d\-\d\d\d\d)|(\d\d\d\d\-\d\d\-\d\d))<\/div>/g);
while ((match = rx.exec(resp.data)) != null) {
first_print_string = match[1];
}
adapter.log.debug('first_print_string: ' + first_print_string);
adapter.setState('info.first_print_date', { val: first_print_string, ack: true });
// GESAMTZAHL SEITEN
rx = new RegExp(/(?:Gesamtanzahl Seiten) \:<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">(\d*)<\/div>/g);
while ((match = rx.exec(resp.data)) != null) {
printed_pages_string = match[1];
}
adapter.log.debug('printed_pages_string: ' + printed_pages_string);
const page_count = parseInt(printed_pages_string, 10);
adapter.log.debug('page_count: ' + page_count);
adapter.setState('info.page_count', { val: page_count, ack: true });
adapter.log.debug('Channels and states created/read');
} else {
adapter.log.warn('Cannot connect to Printer');
}
adapter.log.debug('finished reading printer maintenance data');
}
function stopReadPrinter() {
clearTimeout(callReadPrinter);
adapter.setState('info.connection', false, true);
adapter.log.info('Epson EcoTank ET-2750 adapter stopped');
}
async function main() {
// Check if unload triggerted
if (isUnloaded) {
return;
}
try {
adapter.log.debug('Request printer stats...');
await readPrinterNetwork();
await readPrinterStatus();
await readPrinterMaintenance();
adapter.setState('info.connection', true, true);
} catch (err) {
if (err.message.includes('EHOSTUNREACH') || err.message.includes('ETIMEDOUT')) {
adapter.log.debug(`Printer offline, next try in ${sync} minutes...`);
}
else {
adapter.log.error(JSON.stringify(err));
}
adapter.setState('info.connection', false, true);
}
callReadPrinter = setTimeout(main, sync * 1000 * 60);
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}