-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
459 lines (377 loc) · 14.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
'use strict';
/*
* Created with @iobroker/create-adapter v2.3.0
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const WebSocket = require('ws'); // Lib to handle Websocket
let ws;
const stateDef = require('./lib/stateDef.js'); // Load attribute library
const warnMessages = {}; // Array containing warn messages to avoid duplicates
const wsConnection = {
connectionActive : false,
connectionNeeded : true
}; // Array containing websocket connection data
const printers = [];
const repServerConfig = {};
// Load your modules here, e.g.:
// const fs = require("fs");
class RepetierServer extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'repetier-server',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('message', this.onMessage.bind(this));
this.on('unload', this.onUnload.bind(this));
this.createdStatesDetails = {}; // Array of created states to avoid object overwrites
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Reset the connection indicator during startup
this.setState('info.connection', false, true);
// Read configuration from config
repServerConfig.ip = this.config.ip;
repServerConfig.apiKey = this.decrypt(this.config.token);
repServerConfig.port = this.config.port;
//ToDo: Consider to have this as option for advance mode only
// Create State on root level to send custom messages
await this.localeStateSetCreate('sendMessage', 'Send Custom Message', '');
// Start connection handler to created & monitor websocket connection
await this.connectionHandler();
this.setState('info.connection', true, true);
}
/**
* `Connect websocket, keep connection alive and update values not included in regular websocket feed`
*/
async connectionHandler(){
// Reset timer (if running)
if (wsConnection && wsConnection.reconnectTimer ) clearTimeout(wsConnection.reconnectTimer );
// Start websocket connection if required but not active
if (wsConnection.connectionNeeded && !wsConnection.connectionActive) {
// Consider writing message only 1 time if connection fails at first attempt
this.log.info('Trying to connect to websocket');
await this.webSocketHandler();
} else if (wsConnection.connectionNeeded && wsConnection.connectionActive){ // If connection is active, request value updates for defined functions
// Prepare ping message to keep websocket connection alive
this.log.debug(`Send ping to server`);
const messageArray = {
'action': 'ping',
'data': {},
'callback_id': 800
};
// Send message to websocket connection
ws.send(JSON.stringify(messageArray));
// Request state updates for values not updated by live websocket feed (like time left & % of print)
this.requestData('getPrinterInfo');
}
// Run connection handler every 5 seconds
wsConnection.reconnectTimer = setTimeout(() => {
this.connectionHandler();
}, (5000));
}
/**
* `Connect to websocket & listen to events
*/
async webSocketHandler(){
try {
// Open websocket connection
ws = new WebSocket(`ws://${repServerConfig.ip}:${repServerConfig.port}/socket`);
// Event if connection is opened
ws.on('open', () => {
wsConnection.connectionActive = true;
this.log.info(`Connected with Repetier Server`);
// Request Printer details
this.requestData(`getPrinterInfo`);
});
// Event if connection is closed, show warning if noe expected
ws.on('close', () => {
// Confirm inactive connection to memory
wsConnection.connectionActive = false;
// Avoid log message if connection is closed at adapter stop
if (wsConnection.connectionNeeded === true) this.log.warn(`Connection with Repetier Server closed, will try to reconnect`);
});
// Handle errors on socket connection
ws.on('error', (error) => {
this.log.error(error);
});
// Handle messages received from socket connection
ws.on('message', async (data) => {
// Confirm active connection to memory
wsConnection.connectionActive = true;
const messageObject = JSON.parse(data.toString());
console.log(`[Callback ID] ${messageObject.callback_id}`);
// Message id possibilities
// -1 : Regular message by Repetier Server
// 545 : Response to custom command
// 800 : Response to "Keep alive ping"
// 900 : List all printers and values
if (messageObject.callback_id != '-1') {
if (messageObject.callback_id == null){
console.error(`undefined found`);
}
this.log.debug(`${JSON.stringify(messageObject)}`);
if (messageObject.callback_id == '900') {
await this.updatePrinterValues(messageObject);
}
} else {
//ToDo: Seperate between unknown messages and event type = temperature
this.log.debug(`${JSON.stringify(messageObject)}`);
await this.updateTemperatures(messageObject);
}
});
} catch (e) {
this.log.error(`[ webSocketHandler ] ${e}`);
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
// End timer if running
if (wsConnection && wsConnection.reconnectTimer ) {
clearTimeout(wsConnection.reconnectTimer );
}
// Close web socket if connected
if (wsConnection.connectionNeeded || wsConnection.connectionActive){
wsConnection.connectionNeeded = false;
wsConnection.connectionActive = false;
ws.close();
this.log.info(`Connection to Repetier Server closed`);
}
callback();
} catch (e) {
callback();
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state) {
if (state.ack) return; // Ignore state change if value is acknowledged
// The state was changed
this.log.debug(`state ${id} changed: ${state.val} (ack = ${state.ack})`);
// Store state root in workable format
const printer = id.split('.');
// Send gcode Command to specific printer
if (id === `${this.namespace}.${printer[2]}.commands.send-gCode-Command` && state.val){
// Prepare message to send g-code command
const commandData = {
'action': 'send',
'data': {
'cmd': state.val
},
'printer': printer[2],
'callback_id': 545
};
// Send message to websocket
ws.send(JSON.stringify(commandData));
// Handle custom message, see https://www.repetier-server.com/manuals/programming/API/index.html
} else if (id === `${this.namespace}.sendMessage`){
ws.send(JSON.stringify(state.val));
}
} else {
// The state was deleted
this.log.debug(`state ${id} deleted`);
}
}
/**
* Request data from Repetier Server
*/
requestData(requestType){
switch (requestType) {
case ('getPrinterInfo'):
ws.send('{"callback_id":900,"data":{},"action":"listPrinter"}');
break;
default:
console.error(`[ requestData ] Unknown request type ${requestType}`);
}
}
/**
* Handle state updates received from websocket
* {boolean} ini If function call needs to initialize device
*/
async updatePrinterValues(data){
// console.log(JSON.stringify(data));
const dataObject = data.data;
for (const printer in dataObject){
if (printers[printer] == null) printers.push(dataObject[printer].slug);
// Create folder root structure
// await this.customStates(dataObject[printer].slug);
await this.localExtendObject(dataObject[printer].slug, 'device', printer);
await this.localExtendObject(`${dataObject[printer].slug}.commands`, 'channel', `Printer commands`);
// Create state for custom g-code command
await this.localeStateSetCreate(`${dataObject[printer].slug}.commands.send-gCode-Command`, 'send gCode Command (M256 B0)', '');
// Create state for each value
for (const printerState in dataObject[printer]){
let stateValue = dataObject[printer][printerState];
// Recalculate epoch time to Human Readable format for time values
if (printerState === 'start' || printerState === 'printTime'|| printerState === 'printStart' || printerState === 'printedTimeComp') stateValue = this.reCalcSeconds(stateValue);
await this.localeStateSetCreate(`${dataObject[printer].slug}.${printerState}`, printerState, stateValue);
}
// Calculate remaining print time
const calculatedTimeRemaining = this.reCalcSeconds(dataObject[printer].printTime - dataObject[printer].printedTimeComp);
await this.localeStateSetCreate(`${dataObject[printer].slug}.printTimeRemaining`, 'Remaining Print time', calculatedTimeRemaining);
}
console.log(printers);
}
async updateTemperatures(data){
// Handle all data content
for (const device in data.data){
if (data.data[device].event === `temp`){ // Verify if message contains updates for temperatures
// Create root structure
await this.localExtendObject(`${data.data[device].printer}.temperatures`, 'channel', data.data[device].printer);
for (const tempStates in data.data[device].data){
let channelNR = data.data[device].data.id;
let temptype = 'Extruder';
// Recalculate bed sensor number starting from 1
if (channelNR >= 1000) {
channelNR = 1000 - data.data[device].data.id;
temptype = 'Bed';
}
await this.localExtendObject(`${data.data[device].printer}.temperatures.${temptype}`, 'channel', `${temptype} Temperatures`);
await this.localExtendObject(`${data.data[device].printer}.temperatures.${temptype}.${channelNR}`, 'channel', `Sensor ${channelNR}`);
await this.localeStateSetCreate(`${data.data[device].printer}.temperatures.${temptype}.${channelNR}.${tempStates}`, tempStates, data.data[device].data[tempStates]);
}
}
}
}
reCalcSeconds(allSeconds){
let totalSeconds = Math.round(allSeconds);
const hours = ('00' + Math.floor(totalSeconds / 3600)).slice(-2);
totalSeconds %= 3600;
const minutes = ('00' + Math.floor(totalSeconds / 60)).slice(-2);
const seconds = ('00' + totalSeconds % 60).slice(-2);
return (hours + ':' + minutes + ':' + seconds);
}
// Handle messages from adapter settings showing available current printers tp passing custom states
async onMessage(obj) {
if (obj) {
switch (obj.command) {
case 'getPrinterList':
if (obj.callback) {
// Return array of all printer including option all
const printerSelect = ['all'];
for (const printer in printers) {
printerSelect.push(printers[printer]);
}
this.sendTo(obj.from, obj.command, printerSelect, obj.callback);
}
break;
}
}
}
/**
* Generic function to create objects
* @param {string} id
* @param {'channel' | 'device'} type
* @param {string} name
*/
async localExtendObject(id, type, name) {
try {
const objectDefinition = {
type: type,
common: {
name: name
}
};
if (!this.createdStatesDetails[id]){
await this.extendObjectAsync(id, objectDefinition);
this.createdStatesDetails[id] = objectDefinition;
}
} catch (e) {
this.log.error(`[ localExtendObject ] ${e}`);
}
}
/**
* State create and value update handler
/**
* Generic function to create states & write value updates
* @param {string} stateName ID of state to create
* @param {string} name Name of object
* @param {object} value Value
*/
async localeStateSetCreate(stateName, name, value) {
this.log.debug('Create_state called for : ' + stateName + ' with value : ' + value);
try {
// Try to get details from state lib, if not use defaults. throw warning is states is not known in attribute list
const common = {};
if (!stateDef[name]) {
const warnMessage = `State attribute definition missing for : ${name}`;
if (warnMessages[stateName] !== warnMessage) {
warnMessages[stateName] = warnMessage;
console.info(`No specific state attributes assigned for : ${name} with value : ${value}`);
}
}
if (stateDef[name] != null && stateDef[name].min != null){
common.min = stateDef[name].min;
}
if (stateDef[name] != null && stateDef[name].max != null){
common.max = stateDef[name].max;
}
common.name = stateDef[name] != null ? stateDef[name].name || name : name;
common.type = stateDef[name] != null ? stateDef[name].type || typeof (value) : typeof (value) ;
common.role = stateDef[name] != null ? stateDef[name].role || 'state' : 'state';
common.read = true;
common.unit = stateDef[name] != null ? stateDef[name].unit || '' : '';
common.write = stateDef[name] != null ? stateDef[name].write || false : false;
if ((!this.createdStatesDetails[stateName])
|| (this.createdStatesDetails[stateName]
&& (
common.name !== this.createdStatesDetails[stateName].name
|| common.name !== this.createdStatesDetails[stateName].name
|| common.type !== this.createdStatesDetails[stateName].type
|| common.role !== this.createdStatesDetails[stateName].role
|| common.read !== this.createdStatesDetails[stateName].read
|| common.unit !== this.createdStatesDetails[stateName].unit
|| common.write !== this.createdStatesDetails[stateName].write
)
)) {
this.log.debug(`An attribute has changed : ${stateName} | old ${this.createdStatesDetails[stateName]} | new ${JSON.stringify(common)}`);
await this.extendObjectAsync(stateName, {
type: 'state',
common
});
// Store current object definition to memory
this.createdStatesDetails[stateName] = common;
// Subscribe on state changes if writable
common.write && this.subscribeStates(stateName);
} else {
// console.log(`Nothing changed do not update object`);
}
// Set value to state
if (value != null) {
await this.setStateChangedAsync(stateName, {
val: typeof value === 'object' ? JSON.stringify(value) : value, // real objects are not allowed
ack: true,
});
}
} catch (error) {
// this.errorHandler(`[create_state]`, error);
this.log.error(`[create_state] ${error}`);
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new RepetierServer(options);
} else {
// otherwise start the instance directly
new RepetierServer();
}