-
Notifications
You must be signed in to change notification settings - Fork 8
/
squeezeboxrpc.js
executable file
·87 lines (78 loc) · 2.52 KB
/
squeezeboxrpc.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
"use strict";
/*
* Created with @iobroker/create-adapter v2.6.3
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require("@iobroker/adapter-core");
let squeezeboxServer;
const IoSbServerRequire = require(__dirname + "/lib/iosbserver");
class Squeezeboxrpc extends utils.Adapter {
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: "squeezeboxrpc",
});
this.on("ready", this.onReady.bind(this));
this.on("stateChange", this.onStateChange.bind(this));
this.on("unload", this.onUnload.bind(this));
this.on("message", this.onMessage.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Initialize your adapter here
// Reset the connection indicator during startup
this.setState("info.connection", false, true);
// In order to get state updates, you need to subscribe to them. The following line adds a subscription for our variable we have created above.
this.subscribeStates("*");
// Initialize your adapter here
if (!squeezeboxServer) {
this.log.debug("main onReady open squeezeboxrpc");
squeezeboxServer = new IoSbServerRequire(this);
this.subscribeStates("*");
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
squeezeboxServer.closeConnections();
callback();
} catch (e) {
callback();
}
}
onMessage(obj) {
if (typeof obj === "object" && obj.message) {
squeezeboxServer.processMessages(obj);
}
}
/**
* Is called if a subscribed state changes
* @param {string} id
* @param {ioBroker.State | null | undefined} state
*/
onStateChange(id, state) {
if (state) {
// The state was changed
if (squeezeboxServer) squeezeboxServer.stateChange(id, state);
}
}
}
if (require.main !== module) {
// Export the constructor in compact mode
/**
* @param {Partial<utils.AdapterOptions>} [options={}]
*/
module.exports = (options) => new Squeezeboxrpc(options);
} else {
// otherwise start the instance directly
new Squeezeboxrpc();
}