@@ -34,16 +34,16 @@ final class ShowPulseWorker extends ShowPulseBase
34
34
private $ fppStatus ;
35
35
private $ failureCount ;
36
36
private $ lastSequence ;
37
- private $ lastUpdated ;
38
- private $ statusResponse ;
37
+ private $ lastStatusCheckTime ;
38
+ private $ lastRequestCheckTime ;
39
39
40
40
public function __construct ()
41
41
{
42
42
$ this ->failureCount = 0 ;
43
43
$ this ->fppStatus = null ;
44
44
$ this ->lastSequence = null ;
45
- $ this ->statusResponse = null ;
46
- $ this ->lastUpdated = time () - $ this ->fifteenMinutesAgo ();
45
+ $ this ->lastStatusCheckTime = time () - $ this -> fifteenMinutesAgo () ;
46
+ $ this ->lastRequestCheckTime = time () - $ this ->fifteenMinutesAgo ();
47
47
}
48
48
49
49
public function getFailureCount ()
@@ -56,11 +56,11 @@ public function fifteenMinutesAgo()
56
56
return time () - 900 ;
57
57
}
58
58
59
- public function logFailure ( $ exceptionMessage )
59
+ public function logError ( $ message )
60
60
{
61
61
if ($ this ->isBelowMaxFailureThreshold ()) {
62
- $ message = $ exceptionMessage . " (Attempt " . $ this -> failureCount . " ) " ;
63
- $ this ->logError ( $ message );
62
+ $ currentDateTime = date ( ' Y-m-d h:i:s A ' ) ;
63
+ error_log ( " $ currentDateTime : $ message (Attempt $ this ->failureCount ) " );
64
64
}
65
65
}
66
66
@@ -69,37 +69,21 @@ public function getFppStatus()
69
69
$ url = $ this ->fppUrl ("fppd/status " );
70
70
$ this ->fppStatus = $ this ->httpRequest ($ url );
71
71
72
- if (is_null ($ this ->fppStatus )) {
72
+ if ($ this -> isNullOrEmpty ($ this ->fppStatus )) {
73
73
throw new Exception ("Unable to get latest status from FPP. " );
74
74
}
75
75
}
76
76
77
- public function getMediaMetaData ($ filename = null )
78
- {
79
- if (is_null ($ filename ) || empty ($ filename )) {
80
- return null ;
81
- }
82
-
83
- $ url = $ this ->fppUrl ("media/ $ filename/meta " );
84
- return $ this ->httpRequest ($ url );
85
- }
86
-
87
77
public function isTestingOrOfflinePlaylist ()
88
78
{
89
- if (is_null ($ this ->fppStatus )) {
79
+ if ($ this -> isNullOrEmpty ($ this ->fppStatus )) {
90
80
return false ;
91
81
}
92
82
93
83
$ playlistName = strtolower ($ this ->fppStatus ->current_playlist ->playlist );
94
84
return strpos ($ playlistName , 'test ' ) >= 0 || strpos ($ playlistName , 'offline ' ) >= 0 ;
95
85
}
96
86
97
- public function exponentialSleepTime ()
98
- {
99
- $ defaultDelay = 2 ;
100
- return pow (2 , $ this ->failureCount ) * $ defaultDelay ;
101
- }
102
-
103
87
public function resetFailureCount ()
104
88
{
105
89
$ this ->failureCount = 0 ;
@@ -135,77 +119,98 @@ public function sleepLongValue()
135
119
public function postStatus ()
136
120
{
137
121
if (
138
- ($ this ->lastSequence === $ this ->fppStatus ->current_sequence && $ this ->lastUpdated < $ this ->fifteenMinutesAgo ()) ||
122
+ ($ this ->lastSequence === $ this ->fppStatus ->current_sequence && $ this ->lastStatusCheckTime < $ this ->fifteenMinutesAgo ()) ||
139
123
$ this ->isTestingOrOfflinePlaylist ()
140
124
) {
141
- $ this ->statusResponse = null ;
142
125
return ;
143
126
}
144
127
145
128
$ warningCount = count ($ this ->fppStatus ->warnings );
146
129
$ statusDto = new StatusDto ($ warningCount , $ this ->fppStatus ->current_sequence , $ this ->fppStatus ->status_name );
147
130
148
- if (!empty ($ this ->fppStatus ->current_song )) {
149
- $ metaData = $ this ->getMediaMetaData ($ this ->fppStatus ->current_song );
131
+ if ($ this ->isNotNullOrEmpty ($ this ->fppStatus ->current_song )) {
132
+ $ url = $ this ->fppUrl ("media/ " . $ this ->fppStatus ->current_song . "/meta " );
133
+ $ metaData = $ this ->httpRequest ($ url );
150
134
151
- if (! is_null ($ metaData ) && ! is_null ($ metaData ->format ->tags )) {
135
+ if ($ this -> isNotNullOrEmpty ($ metaData ) && $ this -> isNotNullOrEmpty ($ metaData ->format ->tags )) {
152
136
$ statusDto ->assignMedia ($ metaData ->format ->tags ->title , $ metaData ->format ->tags ->artist );
153
137
}
154
138
}
155
139
156
140
$ url = $ this ->websiteUrl ("statuses/add " );
157
- $ this -> statusResponse = $ this ->httpRequest ($ url , "POST " , $ statusDto , $ this ->getWebsiteAuthorizationHeaders ());
141
+ $ response = $ this ->httpRequest ($ url , "POST " , $ statusDto , $ this ->getWebsiteAuthorizationHeaders ());
158
142
159
- if ($ this ->statusResponse ->failed ) {
160
- $ this ->logError ("Unable to update show status. " . $ this ->statusResponse ->messsage );
161
- return ;
143
+ if ($ response ->failed ) {
144
+ throw new Exception ("Unable to update show status. " . $ response ->message );
162
145
}
163
146
164
147
$ this ->lastSequence = $ this ->fppStatus ->current_sequence ;
165
- $ this ->lastUpdated = time ();
148
+ $ this ->lastStatusCheckTime = time ();
166
149
}
167
150
168
- public function insertNextRequest ()
151
+ /**
152
+ * @var ShowPulseResponseDto @responseDto
153
+ *
154
+ */
155
+ public function getAndInsertNextRequest ()
169
156
{
170
- if (is_null ($ this ->statusResponse ) || is_null ($ this ->statusResponse ->data )) {
157
+ if (
158
+ ($ this ->lastSequence === $ this ->fppStatus ->current_sequence && $ this ->lastRequestCheckTime < $ this ->fifteenMinutesAgo ()) ||
159
+ $ this ->isTestingOrOfflinePlaylist ()
160
+ ) {
171
161
return ;
172
162
}
173
163
174
- switch ($ this ->statusResponse ->data ->sequence ) {
164
+ $ url = $ this ->websiteUrl ("jukebox_requests/next " );
165
+ $ responseDto = $ this ->httpRequest ($ url , "GET " , null , $ this ->getWebsiteAuthorizationHeaders ());
166
+
167
+ if (is_null ($ responseDto ) || $ responseDto ->failed ) {
168
+ $ this ->logError ($ responseDto ->message );
169
+ return ;
170
+ }
171
+
172
+ if (is_null ($ responseDto ->data )) {
173
+ $ this ->lastRequestCheckTime = time ();
174
+ return ;
175
+ }
176
+
177
+ switch ($ responseDto ->data ) {
175
178
case "SP_STOP_IMMEDIATELY " :
176
- $ this ->stopPlaylistImmediately ( );
179
+ $ this ->stopPlaylist ( false );
177
180
break ;
178
181
179
182
case "SP_RESTART_IMMEDIATELY " :
180
- $ this ->stopPlaylistImmediately ( );
183
+ $ this ->stopPlaylist ( false );
181
184
$ this ->systemRestart ();
182
185
break ;
183
186
184
187
case "SP_SHUTDOWN_IMMEDIATELY " :
185
- $ this ->stopPlaylistImmediately ( );
188
+ $ this ->stopPlaylist ( false );
186
189
$ this ->systemShutdown ();
187
190
break ;
188
191
189
192
case "SP_STOP_GRACEFULLY " :
190
- $ this ->stopPlaylistGracefully (false );
193
+ $ this ->stopPlaylist (false );
191
194
break ;
192
195
193
196
case "SP_RESTART_GRACEFULLY " :
194
- $ this ->stopPlaylistGracefully (true );
197
+ $ this ->stopPlaylist (true );
195
198
$ this ->systemRestart ();
196
199
break ;
197
200
198
201
case "SP_SHUTDOWN_GRACEFULLY " :
199
- $ this ->stopPlaylistGracefully (true );
202
+ $ this ->stopPlaylist (true );
200
203
$ this ->systemShutdown ();
201
204
break ;
202
205
203
206
default :
204
207
$ this ->executeFppCommand (
205
208
"Insert Playlist After Current " ,
206
- array ($ this -> statusResponse ->data ->sequence , "-1 " , "-1 " , "false " )
209
+ array ($ responseDto ->data ->sequence , "-1 " , "-1 " , "false " )
207
210
);
208
211
}
212
+
213
+ $ this ->lastRequestCheckTime = time ();
209
214
}
210
215
211
216
private function systemRestart ()
@@ -220,21 +225,15 @@ private function systemShutdown()
220
225
$ this ->httpRequest ($ url );
221
226
}
222
227
223
- private function stopPlaylistImmediately ( )
228
+ private function stopPlaylist ( $ isGracefulStop )
224
229
{
225
- $ url = $ this ->fppUrl ("playlists/stop " );
230
+ $ url = $ isGracefulStop ? $ this -> fppUrl ( " playlists/stopgracefully " ) : $ this ->fppUrl ("playlists/stop " );
226
231
$ this ->httpRequest ($ url );
227
- }
228
232
229
- private function stopPlaylistGracefully ($ waitForIdleState )
230
- {
231
- $ url = $ this ->fppUrl ("playlists/stopgracefully " );
232
- $ this ->httpRequest ($ url );
233
-
234
- while ($ waitForIdleState ) {
233
+ while ($ isGracefulStop ) {
235
234
$ this ->getFppStatus ();
236
235
237
- if ($ this ->fppStatus ->status_name === " idle " ) {
236
+ if ($ this ->fppStatus ->status_name === $ this -> idleStatusValue () ) {
238
237
break ;
239
238
}
240
239
@@ -248,6 +247,27 @@ public function calculateSleepTime()
248
247
return $ this ->sleepShortValue ();
249
248
}
250
249
251
- return $ this ->fppStatus ->status_name === "idle " ? $ this ->sleepLongValue () : $ this ->sleepShortValue ();
250
+ return $ this ->fppStatus ->status_name === $ this ->idleStatusValue () ? $ this ->sleepLongValue () : $ this ->sleepShortValue ();
251
+ }
252
+
253
+ public function execute ()
254
+ {
255
+ try {
256
+ $ this ->getWebsiteApiKey ();
257
+ $ this ->getFppStatus ();
258
+ $ this ->postStatus ();
259
+ $ this ->getAndInsertNextRequest ();
260
+ $ sleepTime = $ this ->calculateSleepTime ();
261
+ sleep ($ sleepTime );
262
+ $ this ->resetFailureCount ();
263
+ } catch (Exception $ e ) {
264
+ $ this ->logError ($ e ->getMessage ());
265
+
266
+ $ defaultDelay = 2 ;
267
+ $ sleepTime = $ this ->getFailureCount () * $ defaultDelay ;
268
+
269
+ $ this ->increaseFailureCount ();
270
+ sleep ($ sleepTime );
271
+ }
252
272
}
253
273
}
0 commit comments