@@ -23,9 +23,17 @@ function getState() {
23
23
if ( state && state . position ) {
24
24
STATUS . playbackInfo . playbackPosition = state . position ;
25
25
STATUS . state = state ;
26
+
27
+ spotify . isRepeating ( function ( err , repeating ) {
28
+ STATUS . state . isRepeating = repeating ;
29
+
30
+ spotify . isShuffling ( function ( err , shuffling ) {
31
+ STATUS . state . isShuffling = shuffling ;
32
+
33
+ updateClients ( ) ;
34
+ } ) ;
35
+ } ) ;
26
36
}
27
-
28
- updateClients ( ) ;
29
37
return state ;
30
38
} ) ;
31
39
}
@@ -67,7 +75,7 @@ function rampVolume(volume) {
67
75
} ) ;
68
76
}
69
77
70
- function setPlayerPosition ( seconds ) {
78
+ function movePlayerPosition ( seconds ) {
71
79
let positionScript = `tell application "Spotify"
72
80
set currentPosition to get player position
73
81
set desiredPosition to (currentPosition + ${ seconds } )
@@ -81,6 +89,19 @@ function setPlayerPosition(seconds) {
81
89
} ) ;
82
90
}
83
91
92
+ function setPlayerPosition ( seconds ) {
93
+ let positionScript = `tell application "Spotify"
94
+ set desiredPosition to ${ seconds }
95
+ set player position to desiredPosition
96
+ end tell` ;
97
+
98
+ STATUS . playbackInfo . playerState = `Setting Player Position ${ seconds } seconds` ;
99
+
100
+ return osascript ( positionScript ) . then ( function ( response ) {
101
+ return response ;
102
+ } ) ;
103
+ }
104
+
84
105
module . exports = {
85
106
start : function ( port ) {
86
107
//starts the REST API
@@ -179,7 +200,22 @@ module.exports = {
179
200
}
180
201
} ) ;
181
202
182
- server . get ( '/playerPosition/:seconds' , function ( req , res ) {
203
+ server . get ( '/movePlayerPosition/:seconds' , function ( req , res ) {
204
+ if ( config . get ( 'allowControl' ) ) {
205
+ try {
206
+ movePlayerPosition ( req . params . seconds ) ;
207
+ res . send ( { status : 'player-position-changed' } ) ;
208
+ }
209
+ catch ( error ) {
210
+ res . send ( { error : error } ) ;
211
+ }
212
+ }
213
+ else {
214
+ res . send ( { status : 'not-allowed' } ) ;
215
+ }
216
+ } ) ;
217
+
218
+ server . get ( '/setPlayerPosition/:seconds' , function ( req , res ) {
183
219
if ( config . get ( 'allowControl' ) ) {
184
220
try {
185
221
setPlayerPosition ( req . params . seconds ) ;
@@ -466,10 +502,26 @@ module.exports = {
466
502
}
467
503
} ) ;
468
504
469
- socket . on ( 'playerPosition' , function ( seconds ) {
505
+ socket . on ( 'movePlayerPosition' , function ( seconds ) {
506
+ if ( config . get ( 'allowControl' ) ) {
507
+ try {
508
+ movePlayerPosition ( seconds ) ;
509
+ getState ( ) ;
510
+ }
511
+ catch ( error ) {
512
+ socket . emit ( 'error' , error ) ;
513
+ }
514
+ }
515
+ else {
516
+ socket . emit ( 'control_status' , false ) ;
517
+ }
518
+ } ) ;
519
+
520
+ socket . on ( 'setPlayerPosition' , function ( seconds ) {
470
521
if ( config . get ( 'allowControl' ) ) {
471
522
try {
472
523
setPlayerPosition ( seconds ) ;
524
+ getState ( ) ;
473
525
}
474
526
catch ( error ) {
475
527
socket . emit ( 'error' , error ) ;
@@ -540,6 +592,7 @@ module.exports = {
540
592
if ( config . get ( 'allowControl' ) ) {
541
593
try {
542
594
spotify . volumeUp ( ) ;
595
+ getState ( ) ;
543
596
}
544
597
catch ( error ) {
545
598
socket . emit ( 'error' , error ) ;
@@ -554,6 +607,7 @@ module.exports = {
554
607
if ( config . get ( 'allowControl' ) ) {
555
608
try {
556
609
spotify . volumeDown ( ) ;
610
+ getState ( ) ;
557
611
}
558
612
catch ( error ) {
559
613
socket . emit ( 'error' , error ) ;
@@ -568,6 +622,7 @@ module.exports = {
568
622
if ( config . get ( 'allowControl' ) ) {
569
623
try {
570
624
spotify . setVolume ( volume ) ;
625
+ getState ( ) ;
571
626
}
572
627
catch ( error ) {
573
628
socket . emit ( 'error' , error ) ;
@@ -582,6 +637,7 @@ module.exports = {
582
637
if ( config . get ( 'allowControl' ) ) {
583
638
try {
584
639
rampVolume ( volume ) ;
640
+ getState ( ) ;
585
641
}
586
642
catch ( error ) {
587
643
socket . emit ( 'error' , error ) ;
@@ -596,6 +652,7 @@ module.exports = {
596
652
if ( config . get ( 'allowControl' ) ) {
597
653
try {
598
654
spotify . muteVolume ( ) ;
655
+ getState ( ) ;
599
656
}
600
657
catch ( error ) {
601
658
socket . emit ( 'error' , error ) ;
@@ -610,6 +667,7 @@ module.exports = {
610
667
if ( config . get ( 'allowControl' ) ) {
611
668
try {
612
669
spotify . unmuteVolume ( ) ;
670
+ getState ( ) ;
613
671
}
614
672
catch ( error ) {
615
673
socket . emit ( 'error' , error ) ;
@@ -624,6 +682,7 @@ module.exports = {
624
682
if ( config . get ( 'allowControl' ) ) {
625
683
try {
626
684
spotify . setRepeating ( true ) ;
685
+ getState ( ) ;
627
686
}
628
687
catch ( error ) {
629
688
socket . emit ( 'error' , error ) ;
@@ -638,6 +697,7 @@ module.exports = {
638
697
if ( config . get ( 'allowControl' ) ) {
639
698
try {
640
699
spotify . setRepeating ( false ) ;
700
+ getState ( ) ;
641
701
}
642
702
catch ( error ) {
643
703
socket . emit ( 'error' , error ) ;
@@ -652,6 +712,7 @@ module.exports = {
652
712
if ( config . get ( 'allowControl' ) ) {
653
713
try {
654
714
spotify . toggleRepeating ( ) ;
715
+ getState ( ) ;
655
716
}
656
717
catch ( error ) {
657
718
socket . emit ( 'error' , error ) ;
@@ -666,6 +727,7 @@ module.exports = {
666
727
if ( config . get ( 'allowControl' ) ) {
667
728
try {
668
729
spotify . setShuffling ( true ) ;
730
+ getState ( ) ;
669
731
}
670
732
catch ( error ) {
671
733
socket . emit ( 'error' , error ) ;
@@ -680,6 +742,7 @@ module.exports = {
680
742
if ( config . get ( 'allowControl' ) ) {
681
743
try {
682
744
spotify . setShuffling ( false ) ;
745
+ getState ( ) ;
683
746
}
684
747
catch ( error ) {
685
748
socket . emit ( 'error' , error ) ;
@@ -694,6 +757,7 @@ module.exports = {
694
757
if ( config . get ( 'allowControl' ) ) {
695
758
try {
696
759
spotify . toggleShuffling ( ) ;
760
+ getState ( ) ;
697
761
}
698
762
catch ( error ) {
699
763
socket . emit ( 'error' , error ) ;
@@ -706,7 +770,7 @@ module.exports = {
706
770
} ) ;
707
771
708
772
httpServer . listen ( port ) ;
709
- console . log ( 'REST API server started on: ' + port ) ;
773
+ console . log ( 'REST/Socket.io API server started on: ' + port ) ;
710
774
} ,
711
775
712
776
sendUpdates : function ( ) {
0 commit comments