forked from rrov1/ioBroker.epson_workforce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
296 lines (261 loc) · 13.1 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* jshint -W097 */ // jshint strict:false
/*jslint node: true */
"use strict";
var utils = require('@iobroker/adapter-core'); // Get common adapter utils
var adapter = utils.Adapter('epson_workforce');
//var configChanged = false;
var request = require('request');
var lang = 'de';
var callReadPrinter;
var ip = '';
var 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%
var sync = 180;
// var adapter = utils.Adapter({
// name: 'epson_workforce',
// systemConfig: true,
// useFormatDate: true,
adapter.on('unload', function (callback) {
try {
adapter.log.info('terminating epson printer adapter');
stopReadPrinter();
callback();
} catch (e) {
callback();
}
});
adapter.on('ready', function () {
adapter.log.debug('initializing objects');
main();
});
var 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>"
},
'waste' : {
'state': 'waste',
'name': 'Waste',
'inklvl_rx': "IMAGE\\/Ink_Waste\\.PNG\\' height=\\'([0-9]{1,2})\\'",
'cartridge_rx': "Wartungsbox \\:<\\/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.');
stopReadPrinter();
}
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) ? 180 : parseInt(adapter.config.synctime,10);
adapter.log.debug('ioBroker reads printer every ' + sync + ' minutes');
} // end ip entered
}
function readPrinterStatus() {
var link = 'http://' + ip + '/PRESENTATION/ADVANCED/INFO_PRTINFO/TOP';
var unreach = true;
request({"rejectUnauthorized": false, "url": link, "method": "GET"}, async function(error, response, body) {
if (!error && response.statusCode === 200) {
unreach = false;
var match, rx;
// MAC ADRESSE EINLESEN
rx = new RegExp( /(?:MAC-Adresse|MAC Address|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 );
while((match = rx.exec(body)) != null) {
var mac_string = match[1];
}
adapter.log.debug('mac_string: ' + mac_string);
adapter.setState('mac', {val: mac_string, ack: true});
// read firmware version
rx = new RegExp( /(?:Firmware-Version|Firmware Version|Version firmware|Versione firmware|Versión del firmware|Versão do firmware) :<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">([a-zA-Z0-9 äöüÄÖÜ\-\_\.]*)<\/div>/g );
while((match = rx.exec(body)) != null) {
var firmware_string = match[1];
}
adapter.log.debug('firmware_string: ' + firmware_string);
adapter.setState('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 );
while((match = rx.exec(body)) != null) {
var serial_string = match[1];
}
adapter.log.debug('serial_string: ' + serial_string);
adapter.setState('serial', {val: serial_string, ack: true});
for (var i in ink) {
await adapter.setObjectNotExistsAsync('inks.' + ink[i].state + '.level', {
type: 'state',
common: {
name: 'Level of ' + ink[i].name,
desc: 'Level of ' + ink[i].name,
type: 'number',
role: 'value.fill',
unit: '%',
read: true,
write: false
},
native: {}
});
// create state with ink name + cartrigde
await adapter.setObjectNotExistsAsync('inks.' + ink[i].state + '.cartridge', {
type: 'state',
common: {
name: 'Cartridge name for ' + ink[i].name,
desc: 'Cartridge name for ' + ink[i].name,
type: 'string',
role: 'text',
def: 'unknown',
read: true,
write: false
},
native: {}
});
// read levels
rx = new RegExp(ink[i].inklvl_rx, "g");
while((match = rx.exec(body)) != null) {
var level_string = match[1];
}
adapter.log.debug(ink[i].name + ' Levelstring: ' + level_string + 'px');
var level = parseInt(level_string,10) * 100 / parseInt(baselevel,10);
adapter.setState('inks.' + ink[i].state + '.level', {val: level, ack: true});
adapter.log.debug(ink[i].name + ' Level: ' + level + '%');
// read cartridge
rx = new RegExp(ink[i].cartridge_rx, "g");
while((match = rx.exec(body)) != null) {
var cartridge_string = match[1];
}
adapter.log.debug(ink[i].name + ' cartridge_string: ' + cartridge_string);
adapter.setState('inks.' + ink[i].state + '.cartridge', {val: cartridge_string, ack: true});
} // end for
adapter.log.debug('Channels and states created/read');
} else {
adapter.log.warn('Cannot connect to Printer: ' + error);
unreach = true;
}
// Write connection status
adapter.setState('UNREACH', {val: unreach, ack: true});
}); // End request
adapter.log.debug('finished reading printer status data');
}
function readPrinterNetwork() {
var link = 'http://' + ip + '/PRESENTATION/ADVANCED/INFO_NWINFO/TOP';
var unreach = true;
adapter.setState('ip', {val: ip, ack: true});
request({"rejectUnauthorized": false, "url": link, "method": "GET"}, function(error, response, body) {
if (!error && response.statusCode === 200) {
unreach = false;
var match, rx;
// NAME EINLESEN
rx = new RegExp( /(?:Druckername|Printer Name|Nom de l\'imprimante|Nome stampante|Nombre de la impresora|Nome da impressora) :<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">([a-zA-Z0-9 äöüÄÖÜ\-\_]*)<\/div>/g );
while((match = rx.exec(body)) != null) {
var name_string = match[1];
}
adapter.log.debug('name_string: ' + name_string);
adapter.setState('name', {val: name_string, ack: true});
// MODELL EINLESEN
rx = new RegExp( /<title>([a-zA-Z0-9 äöüÄÖÜ\-\_]*)<\/title>/g );
while((match = rx.exec(body)) != null) {
var model_string = match[1];
}
adapter.log.debug('model_string: ' + model_string);
adapter.setState('model', {val: model_string, ack: true});
adapter.log.debug('states updated');
} else {
adapter.log.warn('Cannot connect to Printer: ' + error);
unreach = true;
}
// Write connection status
adapter.setState('UNREACH', {val: unreach, ack: true});
}); // End request
adapter.log.debug('finished reading printer network data');
}
function readPrinterMaintenance() {
var link = 'http://' + ip + '/PRESENTATION/ADVANCED/INFO_MENTINFO/TOP';
adapter.setState('ip', {val: ip, ack: true});
var unreach = true;
request({"rejectUnauthorized": false, "url": link, "method": "GET"}, function(error, response, body) {
if (!error && response.statusCode === 200) {
unreach = false;
var match, rx;
// 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)<\/div>/g );
while((match = rx.exec(body)) != null) {
var first_print_string = match[1];
}
adapter.log.debug('first_print_string: ' + first_print_string);
adapter.setState('first_print_date', {val: first_print_string, ack: true});
// GESAMTZAHL SEITEN
rx = new RegExp( /(?:Total Number of Pages|Gesamtanzahl Seiten) \:<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">(\d*)<\/div>/g );
while((match = rx.exec(body)) != null) {
var pages_string = match[1];
}
adapter.log.debug('pages_string: ' + pages_string);
var page_count = parseInt(pages_string, 10);
adapter.log.debug('page_count: ' + page_count);
adapter.setState('page_count', {val: page_count, ack: true});
// GESAMTZAHL SEITEN: S&W SCAN
rx = new RegExp( /(?:W-Scan) \:<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">(\d*)<\/div>/g );
while((match = rx.exec(body)) != null) {
var pages_string = match[1];
}
adapter.log.debug('b&w scanned pages string: ' + pages_string);
var page_count = parseInt(pages_string, 10);
adapter.log.debug('b&w scanned page_count: ' + page_count);
adapter.setState('scan_count_bw', {val: page_count, ack: true});
// GESAMTZAHL SEITEN: FARBE SCAN
rx = new RegExp( /(?:Color Scan|Farbe-Scan) \:<\/span><\/dt><dd class=\"value clearfix\"><div class=\"preserve-white-space\">(\d*)<\/div>/g );
while((match = rx.exec(body)) != null) {
var pages_string = match[1];
}
adapter.log.debug('color scanned pages string: ' + pages_string);
var page_count = parseInt(pages_string, 10);
adapter.log.debug('color scanned page_count: ' + page_count);
adapter.setState('scan_count_color', {val: page_count, ack: true});
adapter.log.debug('states updated');
} else {
adapter.log.warn('Cannot connect to Printer: ' + error);
unreach = true;
}
// Write connection status
adapter.setState('UNREACH', {val: unreach, ack: true});
}); // End request
adapter.log.debug('finished reading printer maintenance data');
}
function stopReadPrinter() {
clearInterval(callReadPrinter);
adapter.log.info('Epson Workforce adapter stopped');
}
function main() {
//adapter.subscribeStates('*');
readSettings();
adapter.log.debug('Epson Workforce adapter started...');
readPrinterNetwork();
readPrinterStatus();
readPrinterMaintenance();
callReadPrinter = setInterval(function() {
adapter.log.debug('connecting printer webserver ...');
readPrinterNetwork();
readPrinterStatus();
readPrinterMaintenance();
}, sync * 1000 * 60);
}