-
Notifications
You must be signed in to change notification settings - Fork 3
/
show_on_demand_bg.php
306 lines (249 loc) · 9.46 KB
/
show_on_demand_bg.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
include_once "/opt/fpp/www/common.php";
$pluginName = basename(dirname(__FILE__));
$pluginPath = $settings['pluginDirectory']."/".$pluginName."/";
$logFile = $settings['logDirectory']."/".$pluginName.".log";
$messagesCsvFile = $settings['logDirectory']."/".$pluginName."-messages.csv";
$pluginConfigFile = $settings['configDirectory'] . "/plugin." .$pluginName;
$pluginSettings = parse_ini_file($pluginConfigFile);
#$pluginVersion = urldecode($pluginSettings['pluginVersion']);
$sodEnabled = $pluginSettings['show_on_demand_enabled'];
$sodEnabled = $sodEnabled == "true" ? true : false;
$api_base_path = "https://voip.ms/api/v1";
$oldest_message_age = 180;
$onDemandPlaylist = $pluginSettings['on_demand_playlist'];
$mainPlaylist = $pluginSettings['main_playlist'];
$voipmsApiUsername = $pluginSettings['voipms_api_username'];
$voipmsApiPassword = $pluginSettings['voipms_api_password'];
$startCommand = $pluginSettings['start_command'];
$messageSuccess = $pluginSettings['message_success'];
$messageNotStarted = $pluginSettings['message_not_started'];
if($sodEnabled == 1) {
echo "Starting Show On Demand Plugin\n";
logEntry("Starting Show On Demand Plugin");
try{
logEntry("On-demand playlist: " . $onDemandPlaylist);
if (strlen($onDemandPlaylist)==0){
throw new Exception('No on-demand playlist specified.');
}
logEntry("Main playlist: " . $mainPlaylist);
if (strlen($mainPlaylist)==0){
throw new Exception('No main playlist specified.');
}
logEntry("Voip.ms username: " . $voipmsApiUsername);
if (strlen($voipmsApiUsername)==0){
throw new Exception('No voip.ms username specified.');
}
logEntry("Voip.ms password: " . "<<redacted>>");
if (strlen($voipmsApiPassword)==0){
throw new Exception('No voip.ms password specified.');
}
logEntry("Start command: " . $startCommand);
if (strlen($startCommand)==0){
throw new Exception('No start command specified.');
}
logEntry("Success message: " . $messageSuccess);
logEntry("Not-started message: " . $messageNotStarted);
} catch (Exception $e) {
logEntry($e->getMessage());
die;
}
while(true) {
try{
$fppStatus = getFppStatus();
if($fppStatus->scheduler->status=="playing") {
logEntry("fpp is playing");
$messageResponse = getMessages();
logEntry("API Response Status: " . $messageResponse->status);
if ($messageResponse->status == "success"){
$startShowForContacts = processMessages($messageResponse);
$shouldStart = count($startShowForContacts) > 0;
if ($shouldStart) {
$currentlyPlaying = $fppStatus->current_playlist->playlist;
logEntry("Currently playing: " . $currentlyPlaying);
$canStart = false;
if($currentlyPlaying == $onDemandPlaylist) {
logEntry("The on-demand playlist is playing");
$canStart = true;
}
else{
logEntry("The on-demand playlist is not playing");
$canStart = false;
}
sendResponses($startShowForContacts,$canStart);
if ($canStart){
startShow();
}
}
else{
logEntry("Nothing to do");
}
}
}else {
logEntry("fpp is not playing");
}
} catch (Exception $e) {
logEntry('Exception: ' . $e->getMessage());
}
logEntry("Sleeping");
sleep(30);
}
}else {
logEntry("Show On Demand Plugin is disabled");
}
function startShow(){
global $mainPlaylist;
$url = "http://127.0.0.1/api/command/Insert Playlist Immediate/" . $mainPlaylist ."/0/0/true";
$url = str_replace(' ', '%20', $url);
logEntry("Triggering main playlist: " . $url);
$result=file_get_contents($url);
logEntry("Result: " . $result);
}
function processMessages($messageResponse){
global $startCommand, $oldest_message_age;
$shouldStart = false;
$respondTo = array();
foreach($messageResponse->sms as $item) {
try{
$id = $item->id;
$date = $item->date;
$did = $item->did;
$contact = $item->contact;
$message = trim($item->message);
logEntry("Message ID: " . $id);
$action = "ignored";
$now = new DateTime('now');
$datetime = new DateTime( $date );
$diffInSeconds = $now->getTimestamp() - $datetime->getTimestamp();
logEntry("Message Age: " . $diffInSeconds);
if ($diffInSeconds > $oldest_message_age){
$action = "too old";
}
else {
if (strcasecmp($message, $startCommand) == 0) {
$action = "start show";
if ($shouldStart){
$action = "start show (duplicate)";
}
$shouldStart = true;
//array_push($respondTo, $contact);
$respondTo[$contact] = $did;
}
else{
logEntry("Unknown message: " . $message);
}
}
saveMessageToCsv($id, $date, $did, $contact, $message, $action);
deleteMessage($id);
} catch (Exception $e) {
logEntry('Failed on processing message: ' . $e->getMessage());
}
}
logEntry("Will respond to: " . json_encode($respondTo));
return $respondTo;
}
function sendResponses($contacts, $didStart){
global $messageNotStarted,$messageSuccess;
foreach($contacts as $destination => $did) {
logEntry("Destination: " . $destination . " DID: " . $did);
$message = $messageNotStarted;
if ($didStart){
$message = $messageSuccess;
}
sendMessage($did, $destination, $message);
}
}
function getMessages(){
global $api_base_path,$voipmsApiUsername,$voipmsApiPassword;
$url = $api_base_path . "/rest.php";
$options = array(
'http' => array(
'method' => 'GET'
)
);
$getdata = http_build_query(
array(
'api_username' => $voipmsApiUsername,
'api_password' => $voipmsApiPassword,
'method'=>'getSMS',
'type'=>'1',
)
);
$context = stream_context_create( $options );
//logEntry("API Request: " . $url ."?" .$getdata);
$result = file_get_contents( $url ."?" .$getdata, false, $context );
logEntry("API response: " . $result);
return json_decode( $result );
}
function deleteMessage($id){
global $api_base_path,$voipmsApiUsername,$voipmsApiPassword;
$url = $api_base_path . "/rest.php";
$options = array(
'http' => array(
'method' => 'GET'
)
);
$getdata = http_build_query(
array(
'api_username' => $voipmsApiUsername,
'api_password' => $voipmsApiPassword,
'method'=>'deleteSMS',
'id'=>$id,
)
);
logEntry("Deleting SMS ID: " . $id);
$context = stream_context_create( $options );
//logEntry("API Request: " . $url ."?" .$getdata);
$result = file_get_contents( $url ."?" .$getdata, false, $context );
logEntry("API response: " . $result);
return json_decode( $result );
}
function sendMessage($did, $destination, $message){
global $api_base_path,$voipmsApiUsername,$voipmsApiPassword;
$url = $api_base_path . "/rest.php";
$options = array(
'http' => array(
'method' => 'GET'
)
);
$getdata = http_build_query(
array(
'api_username' => $voipmsApiUsername,
'api_password' => $voipmsApiPassword,
'method'=>'sendSMS',
'did'=>$did,
'dst'=>$destination,
'message'=>$message
)
);
logEntry("Sending SMS to: " . $destination);
$context = stream_context_create( $options );
//logEntry("API Request: " . $url ."?" .$getdata);
$result = file_get_contents( $url ."?" .$getdata, false, $context );
logEntry("API response: " . $result);
return json_decode( $result );
}
function getFppStatus() {
$result=file_get_contents("http://127.0.0.1/api/fppd/status");
return json_decode( $result );
}
function logEntry($data) {
global $logFile,$myPid;
$data = $_SERVER['PHP_SELF']." : [".$myPid."] ".$data;
$logWrite= fopen($logFile, "a") or die("Unable to open file!");
fwrite($logWrite, date('Y-m-d h:i:s A',time()).": ".$data."\n");
fclose($logWrite);
}
function saveMessageToCsv($id, $date, $did, $contact, $message, $action) {
global $messagesCsvFile;
if (!file_exists($messagesCsvFile)) {
$csvHeaderWrite= fopen($messagesCsvFile, "a") or die("Unable to open file!");
fwrite($csvHeaderWrite, "id,date,did,contact,message,action" ."\n");
fclose($csvHeaderWrite);
}
$esc = str_replace("\"","\"\"",$message);
$csvWrite= fopen($messagesCsvFile, "a") or die("Unable to open file!");
fwrite($csvWrite, $id . "," . $date . "," . $did . "," . $contact . "," . "\"" . $esc . "\"" . "," . $action . "\n");
fclose($csvWrite);
}
?>