Skip to content

Commit b0aefd1

Browse files
committed
last device ID persistence
1 parent 64a48b1 commit b0aefd1

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/library/connector.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Connector extends EventEmitter {
2323
#clientSecret = null
2424
#port = null
2525
#server = null
26+
#lastDeviceId = null
2627
#setup = false
2728
#faked = false
2829
#error = false
@@ -184,12 +185,13 @@ class Connector extends EventEmitter {
184185
res.end()
185186
}
186187

187-
startSetup(clientId = null, clientSecret = null, refreshToken = null) {
188+
startSetup(clientId = null, clientSecret = null, refreshToken = null, lastDeviceId = null) {
188189
logger.info('Starting connector setup.')
189190

190191
this.#clientId = clientId
191192
this.#clientSecret = clientSecret
192193
this.#refreshToken = refreshToken
194+
this.#lastDeviceId = lastDeviceId
193195

194196
if (this.#refreshToken)
195197
this.#refreshAccessToken().then(() => this.#setSetup(true)).catch(e => {
@@ -213,16 +215,28 @@ class Connector extends EventEmitter {
213215

214216
this.#listenWithRetry()
215217

218+
this.#lastDeviceId = null
219+
216220
this.#saveGlobalSettings({
217221
clientId: null,
218222
clientSecret: null,
219223
refreshToken: null,
220-
accessToken: null
224+
accessToken: null,
225+
lastDeviceId: null
221226
})
222227

223228
logger.warn('The connector setup has been invalidated.')
224229
}
225230

231+
saveLastDeviceId(deviceId) {
232+
this.#lastDeviceId = deviceId
233+
this.#saveGlobalSettings({ lastDeviceId: deviceId })
234+
}
235+
236+
get lastDeviceId() {
237+
return this.#lastDeviceId
238+
}
239+
226240
#listenWithRetry(attempt = 0) {
227241
const port = constants.CONNECTOR_DEFAULT_PORT + attempt
228242

src/library/wrapper.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ class Wrapper extends EventEmitter {
3838
super()
3939

4040
connector.on('setupStateChanged', state => {
41-
if (state)
41+
if (state) {
42+
if (connector.lastDeviceId)
43+
this.#lastDeviceId = connector.lastDeviceId
44+
4245
this.#updatePlaybackState(true)
43-
else {
46+
} else {
4447
this.#updatePlaybackContext(null)
4548
this.#setPlaying(false)
4649
this.#setRepeatState('off')
@@ -54,8 +57,12 @@ class Wrapper extends EventEmitter {
5457
}
5558
})
5659

57-
if (connector.set)
60+
if (connector.set) {
61+
if (connector.lastDeviceId)
62+
this.#lastDeviceId = connector.lastDeviceId
63+
5864
this.#updatePlaybackState()
65+
}
5966

6067
setInterval(() => {
6168
if (!connector.set)
@@ -600,6 +607,9 @@ class Wrapper extends EventEmitter {
600607
this.#updatePlaybackStateStatus = 'skip'
601608
this.#lastDeviceId = lastDeviceId
602609
this.emit('deviceChanged', lastDeviceId)
610+
611+
if (lastDeviceId)
612+
connector.saveLastDeviceId(lastDeviceId)
603613
}
604614

605615
if (this.#lastDevices && this.#lastDevices.length === devices.length && this.#lastDevices.every((device, index) => device.id === devices[index].id))

src/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ StreamDeck.connect().then(() => {
1616

1717
if (settings.clientId && settings.clientSecret && settings.refreshToken) {
1818
logger.info('Found global settings.')
19-
connector.startSetup(settings.clientId, settings.clientSecret, settings.refreshToken)
19+
connector.startSetup(settings.clientId, settings.clientSecret, settings.refreshToken, settings.lastDeviceId)
2020
} else {
2121
logger.info('No global settings found.')
2222
connector.startSetup()

0 commit comments

Comments
 (0)