diff --git a/src/store.js b/src/store.js index 9e2006ab..3440d1ad 100644 --- a/src/store.js +++ b/src/store.js @@ -374,11 +374,15 @@ const actions = { if (!state.synclounge.lastHostTimeline || isNaN(state.synclounge.lastHostTimeline.time)) { status = 'error' } else { - let difference = Math.abs(state.chosenClient.lastTimelineObject.time - state.synclounge.lastHostTimeline.time) - if (difference > 1500 && difference < state.settings.SYNCFLEXABILITY) { - status = 'ok' + let hostAge = Math.abs(new Date().getTime() - state.synclounge.lastHostTimeline.recievedAt) + let hostTime = 0 + state.synclounge.lastHostTimeline.time + console.log('Adding hosttime', hostAge) + if (state.synclounge.lastHostTimeline.playerState === 'playing') { + hostTime = parseInt(hostTime) + parseInt(hostAge) } - if (difference > 3000) { + let difference = Math.abs(data.time - (hostTime)) + console.log('Reporting a difference of', difference) + if (difference > state.settings.SYNCFLEXABILITY) { status = 'notok' } } diff --git a/src/store/modules/plex/helpers/PlexClient.js b/src/store/modules/plex/helpers/PlexClient.js index 69f02b02..faf9ec03 100644 --- a/src/store/modules/plex/helpers/PlexClient.js +++ b/src/store/modules/plex/helpers/PlexClient.js @@ -28,6 +28,8 @@ module.exports = function PlexClient () { this.events = new EventEmitter() this.labels = [] + this.lastSyncCommand = 0 + this.userData = null // Latest objects for reference in the future @@ -243,11 +245,16 @@ module.exports = function PlexClient () { } return this.seekTo(time) } - this.sync = function (hostTimeline, SYNCFLEXABILITY, SYNCMODE) { + this.sync = function (hostTimeline, SYNCFLEXABILITY, SYNCMODE, POLLINTERVAL) { return new Promise(async (resolve, reject) => { if (this.clientIdentifier === 'PTPLAYER9PLUS10') { await this.getTimeline() } + let lastCommandTime = Math.abs(this.lastSyncCommand - new Date().getTime()) + if (this.lastSyncCommand && this.clientIdentifier !== 'PTPLAYER9PLUS10' && lastCommandTime < POLLINTERVAL) { + console.log('Too soon for another sync command', lastCommandTime) + return reject(new Error('Too soon for another sync command')) + } let lagTime = Math.abs(hostTimeline.recievedAt - new Date().getTime()) if (lagTime) { console.log('Adding lag time of', lagTime) @@ -264,6 +271,7 @@ module.exports = function PlexClient () { if (parseInt(difference) > parseInt(SYNCFLEXABILITY) || (bothPaused && difference > 10)) { // We need to seek! console.log('STORE: we need to seek as we are out by', difference) + this.lastSyncCommand = new Date().getTime() // Decide what seeking method we want to use if (SYNCMODE === 'cleanseek' || hostTimeline.playerState === 'paused') { return resolve(await this.cleanSeek(hostTimeline.time)) @@ -329,6 +337,7 @@ module.exports = function PlexClient () { // Now that we've built our params, it's time to hit the client api console.log('Sending command') await this.hitApi(command, params, this.chosenConnection) + await this.waitForMovement() console.log('PlayMedia DONE') resolve(true) }) diff --git a/src/store/modules/synclounge.js b/src/store/modules/synclounge.js index 9d0527ec..92f4f52b 100644 --- a/src/store/modules/synclounge.js +++ b/src/store/modules/synclounge.js @@ -354,7 +354,7 @@ export default { } } try { - await rootState.chosenClient.sync(data, rootState.settings.SYNCFLEXABILITY, rootState.settings.SYNCMODE) + await rootState.chosenClient.sync(data, rootState.settings.SYNCFLEXABILITY, rootState.settings.SYNCMODE, rootState.settings.CLIENTPOLLINTERVAL) } catch (e) { return resolve() }