flipje

video-control-cgi-in-progres

Aug 30th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.99 KB | None | 0 0
  1.  
  2. #!/bin/bash
  3. PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  4. SCRIPT_PATH="${0}"
  5. #==================================================
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Library General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
  19. #
  20. # http://www.gnu.org/licenses/gpl.txt
  21. #
  22. #==================================================
  23. URL="http://${HTTP_HOST}/cgi-bin/player"
  24. MYSQL='mysql --defaults-file=my.cnf tools -Be'
  25.  
  26. echo "Content-type: text/html"
  27.  
  28. # exit function
  29. function die()
  30. {
  31.   echo -e "<div style=\"padding:4px; border:1px solid red; color:red;\">Error in ${SCRIPT_PATH}:\n${1}</div>"
  32.   exit 1
  33. }
  34.  
  35. # not there yet function
  36. function fNotYet()
  37. {
  38.   NODE="${1}"
  39.   COMM="${2}"
  40.   echo -e "<div style=\"padding:4px; border:1px solid orange; color:orange;\">Warning: Command \"${COMM}\" is not yet implemented in ${DOCUMENT_URI}\n</div>"
  41.   return 0
  42. }
  43.  
  44. # check for.......
  45. function fCheck()
  46. {
  47.   # checken binaries
  48.   [ -x /usr/bin/mysql ] || die "This script depends on mysql-common!"
  49.   # check op .my.cnf
  50.   [ -f "./my.cnf" ] || die "my.cnf Not found!"
  51.   # checken op connectiviteit
  52.   ( mysql --defaults-file="./my.cnf" --connect-timeout=2 -Be "SHOW DATABASES" |grep -Eq '(information_schema|mysql)' ) || die "Failed to connect to database!"
  53.   # checken op staat
  54.   [ "$( ${MYSQL} "DESC nodes" | wc -l )" -eq 9 ] || die "Database state unknown!"
  55.  
  56.   return 0
  57. }
  58.  
  59.  
  60. # Create command
  61. function fStart()
  62. {
  63.   # array met input args
  64.   local ARG=( $( echo "${QUERY_STRING}" | sed -e 's/&/\ /g' ) )
  65.   # set node var
  66.   NODE="$( echo "${ARG[0]}" | sed -e 's/node=//g' )"
  67.   # set comm var
  68.   COMM="$( echo "${ARG[1]}" | sed -e 's/comm=//g' )"
  69.   # output if
  70.   if [ ! -z "${NODE}" ] && [ ! -z "${COMM}" ] ; then
  71.     # case off commands
  72.     case "${COMM}"
  73.     in
  74.     start)
  75.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  76.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  77.       return 0
  78.       ;;
  79.     stop)
  80.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  81.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  82.       return 0
  83.       ;;
  84.     next)
  85.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  86.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  87.       return 0
  88.       ;;
  89.     prev)
  90.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  91.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  92.       return 0
  93.       ;;
  94.     slower)
  95.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  96.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  97.       return 0
  98.       ;;
  99.     faster)
  100.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  101.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  102.       return 0
  103.       ;;
  104.     blackout)
  105.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  106.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  107.       return 0
  108.       ;;
  109.     effect)
  110.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  111.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  112.       return 0
  113.       ;;
  114.     connect)
  115.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  116.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  117.       return 0
  118.       ;;
  119.     reboot)
  120.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  121.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  122.       return 0
  123.       ;;
  124.     admin)
  125.       LINE1="$( fNotYet "${NODE}" "${COMM}" )"
  126.       LINE2="$( echo -e "<div style=\"padding:4px; border:1px solid #00FF00; color:#00FF00;\">Command \"${COMM}\" was send to node: ${NODE}\n</div>\n" )"
  127.       return 0
  128.       ;;
  129.     *)
  130.       LINE1="$( echo -e "<div style=\"padding:4px; border:1px solid red; color:red;\">An error appeared! Command \"${COMM}\" was not send to node: ${NODE}\n</div>" )"
  131.       return 1
  132.       ;;
  133.     esac
  134.   else
  135.      LINE1="$( echo -e "<div style=\"padding:4px; border:1px solid red; color:red;\">An error appeared! Command \"${COMM}\" was not send to node: ${NODE}\n</div>" )"
  136.      return 1
  137.   fi
  138.  
  139.     return 0
  140.  
  141. }
  142.  
  143. # function fGetStatus ( to do, now just for demo )
  144. function fGetStatus()
  145. {
  146.   NODE="${1}"
  147.   STATE="$( ${MYSQL} "SELECT state FROM nodes WHERE host = '${NODE}'" | sed 1d )"
  148.   if [ "${STATE}" = online ]; then
  149.     echo -e "<td bgcolor=#00FF00><center><b>${NODE}</b><br><br>Node is Online</center></td>"
  150.   elif [ "${STATE}" = offline ]; then
  151.     echo -e "<td bgcolor=#FF0000><center><b>${NODE}</b><br><br>Node is Offline</center></td>"
  152.   else
  153.     echo -e "<td bgcolor=\"orange\"><center><b>${NODE}</b><br><br>Node is Unknown</center></td>"
  154.   fi
  155.  
  156.   return 0
  157. }
  158.  
  159. # function fGetPreview ( to do, now just for demo )
  160. function fGetPreview()
  161. {
  162.   echo -e "<td bgcolor=#333><font color=\"#FFF\"/><center>Preview<br><br><b>Not available<b></center></td>"
  163.   return 0
  164. }
  165.  
  166. # create index
  167. function fIndex()
  168. {
  169.   echo -e "
  170.   <HTML>
  171.    <HEAD>
  172.       <TITLE>CGI Script Controller Matrix</TITLE>
  173.    </HEAD>
  174.  
  175.   <BODY>
  176.    <body bgcolor=\"#000000\"
  177.  
  178.    <table border=\"5\" color=\"black\" width=\"100%\" height=\"100%\" cellpadding=\"10\" cellspacing=\"10\">
  179.    </table>
  180.    <table border=\"5\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
  181.      <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
  182.        <tr>
  183.          <td colspan=\"1\" bgcolor=\"#000\">  
  184.            <font color=\"#CFCFCF\"/>
  185.            <center><b>CGI Panel</b><br>
  186.            Scripting Based Matrix Controller<br>
  187.            For interactive video installations<br><center>
  188.          </td>
  189.          <td colspan=\"8\" bgcolor=\"#000\">
  190.            <font color=\"#CFCFCF\"/>
  191.            <center><h1>CGI Script Controller Panel</h1></center>
  192.            <center><i>Control Raspberry Pi nodes by pressing the corresponding button!</i></center>
  193.          </td>
  194.        </tr>"
  195. }
  196.  
  197. # create status window
  198. function fStatusWindow()
  199. {
  200.   # check for query string
  201.   if [ "X${QUERY_STRING}" != "X" ]; then
  202.      fCheck
  203.      fStart
  204.      echo -e "
  205.     <table border=\"5\" color=\"black\" width=\"100%\" height=\"100%\" cellpadding=\"10\" cellspacing=\"10\">
  206.     </table>
  207.      <table border=\"5\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
  208.      <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
  209.        <tr>
  210.          <td colspan=\"1\" bgcolor=\"#000\">  
  211.            <font color=\"#CFCFCF\"/>
  212.            ${LINE1}
  213.            ${LINE2}
  214.          </td>
  215.        </tr>
  216.      <table>"
  217.   else
  218.      echo -e "
  219.     <table border=\"5\" color=\"black\" width=\"100%\" height=\"100%\" cellpadding=\"10\" cellspacing=\"10\">
  220.     </table>
  221.      <table border=\"5\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
  222.      <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
  223.        <tr>
  224.          <td colspan=\"1\" bgcolor=\"#000\">  
  225.            <font color=\"#CFCFCF\"/>
  226.         ${LINE1}
  227.            ${LINE2}
  228.          </td>
  229.        </tr>
  230.      <table>"
  231.   fi
  232.  
  233.   return 0
  234. }
  235.  
  236.  
  237. # create buttons
  238. function fCreateButtons()
  239. {
  240.   for NODE in $( ${MYSQL} "SELECT host FROM nodes" | sed 1d )
  241.   do
  242.     STATUS="$( fGetStatus "${NODE}" )"
  243.     PREVIEW="$( fGetPreview "${NODE}" )"
  244.     echo -e "
  245.      <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
  246.            <td bgcolor=#555 colspan=\"2\"><h3><font color=\"#FFF\">
  247.             ${PREVIEW}
  248.             ${STATUS}
  249.            </td>
  250.            <td bgcolor=#777>
  251.              <font color=\"#FFF\"/>
  252.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=start\" >
  253.                <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"start playlist\"></form>
  254.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=stop\" >
  255.              <br>
  256.                <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"stop playlist\"></form>
  257.            </td>
  258.  
  259.            <td bgcolor=#777>
  260.              <font color=\"#FFF\"/>
  261.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=next\">
  262.                <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\">>\"></form>
  263.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=prev\">
  264.              <br>
  265.               <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"<<\"></form>
  266.            </td>
  267.  
  268.            <td bgcolor=#777>
  269.              <font color=\"#FFF\"/>
  270.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=slower\">
  271.                <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"Play slower\"></form>
  272.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=faster\">
  273.              <br>
  274.               <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"Play faster\"></form>
  275.            </td>
  276.  
  277.            <td bgcolor=#777>
  278.              <font color=\"#FFF\"/>
  279.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=blackout\">
  280.                <center><input style=\"height: 12px; width: 80px\" type=\"submit\" value=\"Blackout\"></form>
  281.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=effect\">
  282.               <center><input style=\"height: 12px; width: 80px\" type=\"submit\" value=\"Effect\"></form>
  283.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=connect\">
  284.                <center><input style=\"height: 12px; width: 80px\" type=\"submit\" value=\"Connect\"></form>
  285.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=reboot\">
  286.               <center><input style=\"height: 12px; width: 80px\" type=\"submit\" value=\"Reboot\"></form>
  287.            </td>
  288.  
  289.            <td bgcolor=#777>
  290.              <font color=\"#000\"/>
  291.              <form method=\"post\" action=\"${URL}/control.cgi?node=${NODE}&comm=admin\">
  292.               <center><input style=\"height: 25px; width: 120px\" type=\"submit\" value=\"Admin\"></form><br>
  293.            </td>
  294.    </table>"
  295.   done
  296.  
  297.   return 0
  298.  
  299. }
  300.  
  301. #  More layout and EOF
  302. function fEnd()
  303. {
  304.   echo -e "
  305.    <table border=\"2\" color=\"black\" width=\"100%\" height=\"10%\" cellpadding=\"10\" cellspacing=\"10\">
  306.        <tr>
  307.          <td colspan=\"1\">
  308.            <font color=\"#FFF\"/>
  309.            <b>By Flip Hess</b><br>
  310.            <br>
  311.            Augustus 2012<br>
  312.            [email protected]<br>
  313.            CGI Bash Script Controller Panel Version 1.1<br>
  314.            <br>
  315.            Made for controlling video and multimedia, but adjustable to any need.<br>
  316.            <br>
  317.          </td>
  318.          <td colspan=\"1\">
  319.            <font color=\"#FFF\"/>
  320.            <i>This program is free software</i><br>
  321.            <i>You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation</i><br>
  322.            <br>
  323.            <i>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of</i><br>
  324.            <i>MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details.</i><br>
  325.          </td>
  326.        </tr>
  327.      </table>
  328.    </table>
  329.    </BODY>
  330.    </HTML>"
  331.  
  332.  
  333.   return 0
  334.  
  335. }
  336.  
  337. # Do tha magic
  338.    fIndex
  339.    fStatusWindow
  340.    fCreateButtons
  341.    fEnd
Advertisement
Add Comment
Please, Sign In to add comment