forked from LightsOnHudson/FPP-Plugin-Matrix-Message
/
functions.inc.php
executable file
·523 lines (332 loc) · 11.8 KB
/
functions.inc.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?php
function printHourFormats($ELEMENT_NAME,$ELEMENT_SELECTED)
{
global $DEBUG;
$T_FORMATS = array("12","24");
echo "<select name=\"".$ELEMENT_NAME."\">";
//print_r($PLUGINS_READ);
for($i=0;$i<=count($T_FORMATS)-1;$i++) {
if($T_FORMATS[$i] == $ELEMENT_SELECTED) {
echo "<option selected value=\"" . $ELEMENT_SELECTED . "\">" . $ELEMENT_SELECTED . " HR</option>";
} else {
echo "<option value=\"" . $T_FORMATS[$i] . "\">" . $T_FORMATS[$i] . " HR</option>";
}
}
echo "</select>";
}
function printTimeFormats($ELEMENT_NAME,$ELEMENT_SELECTED)
{
global $DEBUG;
$T_FORMATS = array("h:i" => "HH:MM","h:i:s" => "HH:MM:SS");
echo "<select name=\"".$ELEMENT_NAME."\">";
//print_r($PLUGINS_READ);
foreach($T_FORMATS as $key => $value)
{
if($key == $ELEMENT_SELECTED) {
echo "<option selected value=\"" . $key . "\">" . $value . "</option>";
} else {
echo "<option value=\"" . $key . "\">" . $value . "</option>";
}
}
echo "</select>";
}
function createMatrixEventFile() {
global $eventDirectory,$pluginDirectory,$pluginName,$scriptDirectory;
//echo "next event file name available: ".$nextEventFilename."\n";
$EVENT_KEY = "RUN-MATRIX";
//check to see that the file doesnt already exist - do a grep and return contents
$EVENT_CHECK = checkEventFilesForKey($EVENT_KEY);
if(!$EVENT_CHECK)
{
$nextEventFilename = getNextEventFilename();
$MAJOR=substr($nextEventFilename,0,2);
$MINOR=substr($nextEventFilename,3,2);
$eventData ="";
$eventData = "majorID=".(int)$MAJOR."\n";
$eventData .= "minorID=".(int)$MINOR."\n";
$eventData .= "name='".$EVENT_KEY."'\n";
$eventData .= "effect=''\n";
$eventData .= "startChannel=\n";
$eventData .= "script='".$EVENT_KEY.".sh'\n";
// echo "eventData: ".$eventData."<br/>\n";
file_put_contents($eventDirectory."/".$nextEventFilename, $eventData);
$scriptCMD = $pluginDirectory."/".$pluginName."/"."matrix.php";
createScriptFile($EVENT_KEY.".sh",$scriptCMD);
}
//echo "$key => $val\n";
}
function outputMessages($queueMessages) {
global $pluginDirectory,$MESSAGE_TIMEOUT, $fpp_matrixtools_Plugin, $fpp_matrixtools_Plugin_Script,$Matrix,$MATRIX_FONT,$MATRIX_FONT_SIZE,$MATRIX_PIXELS_PER_SECOND,$COLOR, $INCLUDE_TIME, $TIME_FORMAT, $HOUR_FORMAT,$SEPARATOR;
//print_r($queueMessages);
if(count($queueMessages) <=0) {
// echo "No messages to output \n";
return;
}
If (strtoupper(trim($COLOR)) == "RANDOM") {
//print_r ("Start counter");
$counter=rand(0,5);
//print_r ("Start switch");
switch ($counter) {
case "0":
$mycolor = "indigo";
//echo $mycolor;
break;
case "1":
$mycolor = "red";
//echo $mycolor;
break;
case "2":
$mycolor = "green";
//echo $mycolor;
break;
case "3":
$mycolor = "blue";
//echo $mycolor;
break;
case "4":
$mycolor = "yellow";
//echo $mycolor;
break;
case "5":
$mycolor = "purple";
//echo $mycolor;
break;
}
logEntry("Selecting RANDOM COLOR: ".$mycolor);
//print_r ("End switch");
$COLOR=($mycolor);
//print_r ($COLOR);
}
enableMatrixToolOutput();
for($i=0;$i<=count($queueMessages)-1;$i++) {
$messageText = "";
if($INCLUDE_TIME == 1 || $INCLUDE_TIME == "on") {
switch ($HOUR_FORMAT) {
case "12":
$messageTime = date($TIME_FORMAT);
break;
case "24":
$messageTime = date($TIME_FORMAT);
break;
}
logEntry( "Message time: ".$messageTime);
$messageText = "Time: ".$messageTime." ".$SEPARATOR." ";
}
//for($i=0;$i<=count($queueMessages)-1;$i++) {
$messageParts = explode("|",$queueMessages[$i]);
//echo "0: ".$messageParts[0]."\n";
//echo "1: ".$messageParts[1]."\n";
//echo "2: " .$messageParts[2]."\n";
//echo "3: ".$messageParts[3]."\n";
$messageText .= urldecode($messageParts[1]);
//echo "Sending message: ".$messageText." to matrix FIFO\n";
$cmd = $pluginDirectory."/".$fpp_matrixtools_Plugin."/".$fpp_matrixtools_Plugin_Script." --blockname \"".$Matrix."\" --color '".$COLOR."' --font ".$MATRIX_FONT." --fontsize ".$MATRIX_FONT_SIZE." --pixelspersecond ".$MATRIX_PIXELS_PER_SECOND. " --message \"".urldecode($messageText)."\"";
//echo "p10 output cmd: ".$cmd."\n";
logEntry("Matrix output cmd: ".$cmd);
exec($cmd,$outputResults);
//print_r($outputResults);
// $cmd = "/bin/echo \"".$messageText. "\" > ".$matrixFIFO;
// exec($cmd,$output);
//echo "sleeping ".$MESSAGE_TIMEOUT. " sending clear line then";
// echo "sleeping: ".$MESSAGE_TIMEOUT." before clear \n";
// sleep($MESSAGE_TIMEOUT);
sleep(1);
clearMatrix();
//$clearLineCmd = "/bin/echo \"\" > ".$matrixFIFO;
//exec($clearLineCmd,$clearOutput);
}
}
function printPluginsInstalled()
{
global $PLUGINS,$pluginDirectory;
include_once 'excluded_plugins.inc.php';
//get all plugins
$PLUGINS_INSTALLED = directoryToArray($pluginDirectory);//, $recursive)($pluginDirectory);
//print_r($PLUGINS_INSTALLED);
$PLUGINS_READ = explode(",",$PLUGINS);
//print_r($PLUGINS_READ);
echo "<select multiple=\"multiple\" name=\"PLUGINS[]\">";
for($i=0;$i<=count($PLUGINS_INSTALLED)-1;$i++) {
$PLUGIN_INSTALLED_TEMP = basename($PLUGINS_INSTALLED[$i]);
if(in_array($PLUGIN_INSTALLED_TEMP,$EXCLUDE_PLUGIN_ARRAY)) {
continue;
}
if(in_array($PLUGIN_INSTALLED_TEMP,$PLUGINS_READ)) {
echo "<option selected value=\"" . $PLUGIN_INSTALLED_TEMP . "\">" . $PLUGIN_INSTALLED_TEMP . "</option>";
} else {
echo "<option value=\"" . $PLUGIN_INSTALLED_TEMP . "\">" . $PLUGIN_INSTALLED_TEMP . "</option>";
}
}
echo "</select>";
}
function printPixelsPerSecond($ELEMENT, $PIXELS_PER_SECOND)
{
global $PLUGINS,$pluginDirectory;
//updated to 40, Nov 9 2015
$MAX_PIXELS_PER_SECOND = 40;
echo "<select name=\"".$ELEMENT."\">";
for($i=0;$i<=$MAX_PIXELS_PER_SECOND-1;$i++) {
if($i == $PIXELS_PER_SECOND) {
echo "<option selected value=\"" . $i. "\">" . $i. "</option>";
} else {
echo "<option value=\"" . $i. "\">" . $i. "</option>";
}
}
echo "</select>";
}
function printFontSizes($ELEMENT, $FONT_SIZE)
{
global $PLUGINS,$pluginDirectory;
$MAX_FONT_SIZE = 64;
echo "<select name=\"".$ELEMENT."\">";
for($i=0;$i<=$MAX_FONT_SIZE-1;$i++) {
if($i == $FONT_SIZE) {
echo "<option selected value=\"" . $i. "\">" . $i. "</option>";
} else {
echo "<option value=\"" . $i. "\">" . $i. "</option>";
}
}
echo "</select>";
}
function printFontsInstalled($ELEMENT, $FONT)
{
global $DEBUG,$PLUGINS,$pluginDirectory;
$fontsDirectory = "/usr/share/fonts/truetype/";
//$FONTS_LIST_CMD = "/usr/bin/fc-list";
//$FONT_LIST = system($FONTS_LIST_CMD);
//if($DEBUG)
// print_r($FONT_LIST);
$FONTS_INSTALLED = directoryToArray($fontsDirectory, true);//, $recursive)($pluginDirectory);
//print_r($PLUGINS_READ);
echo "<select name=\"".$ELEMENT."\">";
for($i=0;$i<=count($FONTS_INSTALLED)-1;$i++) {
$FONTINFO = pathinfo($FONTS_INSTALLED[$i]);
$FONTS_INSTALLED_TEMP = basename($FONTS_INSTALLED[$i],'.'.$FONTINFO['extension']);
if($FONTS_INSTALLED_TEMP == $FONT) {
echo "<option selected value=\"" . $FONTS_INSTALLED_TEMP . "\">" . $FONTS_INSTALLED_TEMP . "</option>";
} else {
echo "<option value=\"" . $FONTS_INSTALLED_TEMP . "\">" . $FONTS_INSTALLED_TEMP . "</option>";
}
}
echo "</select>";
}
//is fppd running?????
function isFPPDRunning() {
$FPPDStatus=null;
logEntry("Checking to see if fpp is running...");
exec("if ps cax | grep -i fppd; then echo \"True\"; else echo \"False\"; fi",$output);
if($output[1] == "True" || $output[1] == 1 || $output[1] == "1") {
$FPPDStatus = "RUNNING";
}
//print_r($output);
return $FPPDStatus;
//interate over the results and see if avahi is running?
}
//get current running playlist
function getRunningPlaylist() {
global $sequenceDirectory;
$playlistName = null;
$i=0;
//can we sleep here????
//sleep(10);
//FPPD is running and we shoud expect something back from it with the -s status query
// #,#,#,Playlist name
// #,1,# = running
$currentFPP = file_get_contents("/tmp/FPP.playlist");
logEntry("Reading /tmp/FPP.playlist : ".$currentFPP);
if($currentFPP == "false") {
logEntry("We got a FALSE status from fpp -s status file.. we should not really get this, the daemon is locked??");
}
$fppParts="";
$fppParts = explode(",",$currentFPP);
// logEntry("FPP Parts 1 = ".$fppParts[1]);
//check to see the second variable is 1 - meaning playing
if($fppParts[1] == 1 || $fppParts[1] == "1") {
//we are playing
$playlistParts = pathinfo($fppParts[3]);
$playlistName = $playlistParts['basename'];
logEntry("We are playing a playlist...: ".$playlistName);
} else {
logEntry("FPPD Daemon is starting up or no active playlist.. please try again");
}
//now we should have had something
return $playlistName;
}
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 processCallback($argv) {
global $DEBUG,$pluginName;
if($DEBUG)
print_r($argv);
//argv0 = program
//argv2 should equal our registration // need to process all the rgistrations we may have, array??
//argv3 should be --data
//argv4 should be json data
$registrationType = $argv[2];
$data = $argv[4];
logEntry("PROCESSING CALLBACK: ".$registrationType);
$clearMessage=FALSE;
switch ($registrationType)
{
case "media":
if($argv[3] == "--data")
{
$data=trim($data);
logEntry("DATA: ".$data);
$obj = json_decode($data);
$type = $obj->{'type'};
logEntry("Type: ".$type);
switch ($type) {
case "sequence":
logEntry("media sequence name received: ");
processSequenceName($obj->{'Sequence'},"STATUS");
break;
case "media":
logEntry("We do not support type media at this time");
//$songTitle = $obj->{'title'};
//$songArtist = $obj->{'artist'};
//sendMessage($songTitle, $songArtist);
//exit(0);
break;
case "both":
logEntry("We do not support type media/both at this time");
// logEntry("MEDIA ENTRY: EXTRACTING TITLE AND ARTIST");
// $songTitle = $obj->{'title'};
// $songArtist = $obj->{'artist'};
// if($songArtist != "") {
// sendMessage($songTitle, $songArtist);
//exit(0);
break;
default:
logEntry("We do not understand: type: ".$obj->{'type'}. " at this time");
exit(0);
break;
}
}
break;
exit(0);
case "playlist":
logEntry("playlist type received");
if($argv[3] == "--data")
{
$data=trim($data);
logEntry("DATA: ".$data);
$obj = json_decode($data);
$sequenceName = $obj->{'sequence0'}->{'Sequence'};
$sequenceAction = $obj->{'Action'};
processSequenceName($sequenceName,$sequenceAction);
//logEntry("We do not understand: type: ".$obj->{'type'}. " at this time");
// logEntry("We do not understand: type: ".$obj->{'type'}. " at this time");
}
break;
exit(0);
default:
exit(0);
}
}
?>