mirror of
https://github.com/ad1217/PrinterStatus
synced 2024-11-24 08:23:48 -05:00
Don't break when printers are not accessible during server startup
This commit is contained in:
parent
8c18460995
commit
51d3cbf41e
@ -69,6 +69,7 @@ class PrinterStatus {
|
|||||||
|
|
||||||
webcamURL?: URL;
|
webcamURL?: URL;
|
||||||
name?: string;
|
name?: string;
|
||||||
|
session_key?: string;
|
||||||
|
|
||||||
websocket?: WebSocket;
|
websocket?: WebSocket;
|
||||||
lastStatus?: messages.ExtendedMessage;
|
lastStatus?: messages.ExtendedMessage;
|
||||||
@ -78,34 +79,42 @@ class PrinterStatus {
|
|||||||
this.address = address;
|
this.address = address;
|
||||||
this.apikey = apikey;
|
this.apikey = apikey;
|
||||||
|
|
||||||
try {
|
// async init
|
||||||
this.init(); // async init
|
this.connect_websocket().catch((e) => {
|
||||||
} catch (e) {
|
console.error('Failed to initialize "${this.slug}"');
|
||||||
throw 'Failed to Init' + e;
|
throw e;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async init() {
|
|
||||||
// TODO: error handling (try/catch)
|
|
||||||
const settings = await this.api_get('settings');
|
|
||||||
this.webcamURL = new URL(settings.webcam.streamUrl, this.address);
|
|
||||||
this.name = settings.appearance.name;
|
|
||||||
|
|
||||||
// do passive login to get a session key from the API key
|
|
||||||
const login: octoprint.LoginResponse = await this.api_post('login', {
|
|
||||||
passive: 'true',
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.connect_websocket(login.name + ':' + login.session);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connect_websocket(authToken: string): void {
|
async connect_websocket() {
|
||||||
|
// initial setup
|
||||||
|
if (!this.session_key) {
|
||||||
|
try {
|
||||||
|
const settings = await this.api_get('settings');
|
||||||
|
this.webcamURL = new URL(settings.webcam.streamUrl, this.address);
|
||||||
|
this.name = settings.appearance.name;
|
||||||
|
|
||||||
|
// do passive login to get a session key from the API key
|
||||||
|
const login: octoprint.LoginResponse = await this.api_post('login', {
|
||||||
|
passive: 'true',
|
||||||
|
});
|
||||||
|
this.session_key = login.name + ':' + login.session;
|
||||||
|
} catch {
|
||||||
|
console.log(
|
||||||
|
`Failed to connect to "${this.slug}" attempting reconnection in 5 seconds`
|
||||||
|
);
|
||||||
|
setTimeout(() => this.connect_websocket(), 5000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const url = new URL('/sockjs/websocket', this.address);
|
const url = new URL('/sockjs/websocket', this.address);
|
||||||
url.protocol = 'ws';
|
url.protocol = 'ws';
|
||||||
this.websocket = new WebSocket(url.toString());
|
this.websocket = new WebSocket(url.toString());
|
||||||
this.websocket
|
this.websocket
|
||||||
.on('open', () => {
|
.on('open', () => {
|
||||||
this.websocket!.send(JSON.stringify({ auth: authToken }));
|
console.log(`Connected to "${this.slug}"`);
|
||||||
|
this.websocket!.send(JSON.stringify({ auth: this.session_key }));
|
||||||
})
|
})
|
||||||
.on('message', (data: WebSocket.Data) => {
|
.on('message', (data: WebSocket.Data) => {
|
||||||
const event: octoprint.Message = JSON.parse(data as string);
|
const event: octoprint.Message = JSON.parse(data as string);
|
||||||
@ -122,8 +131,10 @@ class PrinterStatus {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on('close', () => {
|
.on('close', () => {
|
||||||
console.log('Lost connection to ' + this.slug + ' reconnecting...');
|
console.log(
|
||||||
setTimeout(() => this.connect_websocket(authToken), 5000);
|
`Lost connection to "${this.slug}" attempting reconnection in 5 seconds`
|
||||||
|
);
|
||||||
|
setTimeout(() => this.connect_websocket(), 5000);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user