auto_schedule_show.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?PHP
  2. /*
  3. The purpose of this script is to take a file from cc_files table, and insert it into
  4. the schedule table. DB columns at the time of writing are
  5. starts | ends | file_id | clip_length | fade_in | fade_out | cue_in | cue_out | media_item_played | instance_id
  6. an example of data in this row is:
  7. "9" | "2012-02-29 17:10:00" | "2012-02-29 17:15:05.037166" | 1 | "00:05:05.037166" | "00:00:00" | "00:00:00" | "00:00:00" | "00:05:05.037166" | FALSE | 5
  8. */
  9. function query($conn, $query){
  10. $result = pg_query($conn, $query);
  11. if (!$result) {
  12. echo "Error executing query $query.\n";
  13. exit(1);
  14. }
  15. return $result;
  16. }
  17. function getFileFromCcFiles($conn){
  18. $query = "SELECT * from cc_files LIMIT 2";
  19. $result = query($conn, $query);
  20. $files = array();
  21. while ($row = pg_fetch_array($result)) {
  22. $files[] = $row;
  23. }
  24. if (count($files) == 0){
  25. echo "Library is empty. Could not choose random file.";
  26. exit(1);
  27. }
  28. return $files;
  29. }
  30. function insertIntoCcShow($conn){
  31. /* Step 1:
  32. * Create a show
  33. * */
  34. $query = "INSERT INTO cc_show (name, url, genre, description, color, background_color) VALUES ('test', '', '', '', '', '')";
  35. echo $query.PHP_EOL;
  36. $result = query($conn, $query);
  37. $query = "SELECT currval('cc_show_id_seq');";
  38. $result = pg_query($conn, $query);
  39. if (!$result) {
  40. echo "Error executing query $query.\n";
  41. exit(1);
  42. }
  43. while ($row = pg_fetch_array($result)) {
  44. $show_id = $row["currval"];
  45. }
  46. return $show_id;
  47. }
  48. function insertIntoCcShowInstances($conn, $show_id, $starts, $ends, $files){
  49. /* Step 2:
  50. * Create a show instance.
  51. * Column values:
  52. * starts | ends | show_id | record | rebroadcast | instance_id | file_id | time_filled | last_scheduled | modified_instance
  53. * */
  54. $nowDateTime = new DateTime("now", new DateTimeZone("UTC"));
  55. $now = $nowDateTime->format("Y-m-d H:i:s");
  56. $columns = "(starts, ends, show_id, record, rebroadcast, instance_id, file_id, time_filled, last_scheduled, modified_instance)";
  57. $values = "('$starts', '$ends', $show_id, 0, 0, NULL, NULL, TIMESTAMP '$ends' - TIMESTAMP '$starts', '$now', 'f')";
  58. $query = "INSERT INTO cc_show_instances $columns values $values ";
  59. echo $query.PHP_EOL;
  60. $result = query($conn, $query);
  61. $query = "SELECT currval('cc_show_instances_id_seq');";
  62. $result = pg_query($conn, $query);
  63. if (!$result) {
  64. echo "Error executing query $query.\n";
  65. exit(1);
  66. }
  67. while ($row = pg_fetch_array($result)) {
  68. $show_instance_id = $row["currval"];
  69. }
  70. return $show_instance_id;
  71. }
  72. /*
  73. * id | starts | ends | file_id | clip_length| fade_in | fade_out | cue_in | cue_out | media_item_played | instance_id
  74. * 1 | 2012-02-29 23:25:00 | 2012-02-29 23:30:05.037166 | 1 | 00:05:05.037166 | 00:00:00 | 00:00:00 | 00:00:00 | 00:05:05.037166 | f | 5
  75. */
  76. function insertIntoCcSchedule($conn, $files, $show_instance_id, $p_starts, $p_ends){
  77. $columns = "(starts, ends, file_id, clip_length, fade_in, fade_out, cue_in, cue_out, media_item_played, instance_id)";
  78. $starts = $p_starts;
  79. foreach($files as $file){
  80. $endsDateTime = new DateTime($starts, new DateTimeZone("UTC"));
  81. $lengthDateInterval = getDateInterval($file["length"]);
  82. $endsDateTime->add($lengthDateInterval);
  83. $ends = $endsDateTime->format("Y-m-d H:i:s");
  84. $values = "('$starts', '$ends', $file[id], '$file[length]', '00:00:00', '00:00:00', '00:00:00', '$file[length]', 'f', $show_instance_id)";
  85. $query = "INSERT INTO cc_schedule $columns VALUES $values";
  86. echo $query.PHP_EOL;
  87. $starts = $ends;
  88. $result = query($conn, $query);
  89. }
  90. }
  91. function getDateInterval($interval){
  92. list($length,) = explode(".", $interval);
  93. list($hour, $min, $sec) = explode(":", $length);
  94. return new DateInterval("PT{$hour}H{$min}M{$sec}S");
  95. }
  96. function getEndTime($startDateTime, $p_files){
  97. foreach ($p_files as $file){
  98. $startDateTime->add(getDateInterval($file['length']));
  99. }
  100. return $startDateTime;
  101. }
  102. function rabbitMqNotify(){
  103. $ini_file = parse_ini_file("/etc/airtime/airtime.conf", true);
  104. $url = "http://localhost/api/rabbitmq-do-push/format/json/api_key/".$ini_file["general"]["api_key"];
  105. echo "Contacting $url".PHP_EOL;
  106. $ch = curl_init($url);
  107. curl_exec($ch);
  108. curl_close($ch);
  109. }
  110. $conn = pg_connect("host=localhost port=5432 dbname=airtime user=airtime password=airtime");
  111. if (!$conn) {
  112. echo "Couldn't connect to Airtime DB.\n";
  113. exit(1);
  114. }
  115. if (count($argv) > 1){
  116. if ($argv[1] == "--clean"){
  117. $tables = array("cc_schedule", "cc_show_instances", "cc_show");
  118. foreach($tables as $table){
  119. $query = "DELETE FROM $table";
  120. echo $query.PHP_EOL;
  121. query($conn, $query);
  122. }
  123. rabbitMqNotify();
  124. exit(0);
  125. } else {
  126. $str = <<<EOD
  127. This script schedules a file to play 30 seconds in the future. It
  128. modifies the database tables cc_schedule, cc_show_instances and cc_show.
  129. You can clean up these tables using the --clean option.
  130. EOD;
  131. echo $str.PHP_EOL;
  132. exit(0);
  133. }
  134. }
  135. $startDateTime = new DateTime("now + 30sec", new DateTimeZone("UTC"));
  136. //$endDateTime = new DateTime("now + 1min 30sec", new DateTimeZone("UTC"));
  137. $starts = $startDateTime->format("Y-m-d H:i:s");
  138. //$ends = $endDateTime->format("Y-m-d H:i:s");
  139. $files = getFileFromCcFiles($conn);
  140. $show_id = insertIntoCcShow($conn);
  141. $endDateTime = getEndTime(clone $startDateTime, $files);
  142. $ends = $endDateTime->format("Y-m-d H:i:s");
  143. $show_instance_id = insertIntoCcShowInstances($conn, $show_id, $starts, $ends, $files);
  144. insertIntoCcSchedule($conn, $files, $show_instance_id, $starts, $ends);
  145. rabbitMqNotify();
  146. echo PHP_EOL."Show scheduled for $starts (UTC)".PHP_EOL;