Điều khiển RPI từ xa – Phiên bản Web

Giao diện web dễ sử dụng, không cần nhớ từ khóa. Tuy nhiên để exec các lệnh Linux với quyền root vẫn phải cần đến file Bash php-exec như trong bài trước.

Screenshot 2014-10-20 07.02.38

1. Mã nguồn PHP như sau

class rctrl
{
        static private $srv = array('camera/motion','email_off','sms/gammu','media/dlna','nas/samba');
        static private $onoff = array();
        static private $pid = array('motion.pid','email.off','gammu-smsd.pid','minidlna/minidlna.pid','samba/smbd.pid');

        static private function cur_srv(){
                for ($i=0; $i<count(self::$pid); $i++){
                        self::$onoff [$i] = file_exists('/run/' . self::$pid[$i]) ? 1 : 0;
                }
        }
        static private function rctrl_exec(){
                for ($i=0; $i<count(self::$srv); $i++){
                        $cmd = 'sudo /mnt/tool/php-exec ' . explode('/',self::$srv [$i])[0] . ' ' . self::$onoff [$i];
                        exec ($cmd);
                }
        }
        static public function drawUI(){
                if (array_key_exists('srv', $_POST)){
                        self::$onoff = $_POST['srv'];
                        self::rctrl_exec();
                }elseif (array_key_exists('cron', $_POST)){
                        $tmpname = tempnam(sys_get_temp_dir(), '~CR');
                        file_put_contents($tmpname, $_POST['cron']);
                        exec("sudo /mnt/tool/php-exec cron $tmpname");
                }
                self::cur_srv();
                $ui = '<h2>RPI - REMOTE CONTROL</h2><h3><font color="blue">Turn service on/off</font></h3>';
                $ui .= '<form action="rctrl.php" method="post"><font face="Courier New, Courier, monospace">';
                for ($i=0; $i<count(self::$srv); $i++){
                        $ui .= str_replace(' ','&nbsp;',str_pad(self::$srv [$i], 15)) . '<input type="hidden" name="srv[]" value="'.(self::$onoff [$i]$
                }
                $ui .= '</font><br /><input type="submit" value="Update" /></form>';
                $ui .= '<br /><h3><font color="blue">2. Crontab</font></h3><form method="post" action="rctrl.php"><textarea name="cron" cols="70" rows="10">'.$cron.'</textarea><br /><br /><input type="submit" value="Update" />';
                echo $ui;
        }
}
rctrl::drawUI();
?>

2. File php-exec thay đổi lại như sau

#!/bin/bash
[ $# -ne 2 ] && exit 1
service=$1
onoff=$2
case $service in
        camera)
                if [ "$onoff" = '1' ]; then
                        [ "$(pidof motion)" ] || /usr/bin/motion
                else
                        [ "$(pidof motion)" ] && /usr/bin/killall motion
                fi
                ;;
        email_off|mail_off)
                if [ "$onoff" = "1" ]; then
                        touch /run/email.off
                else
                        [ -f /run/email.off ] && rm /run/email.off
                fi
                ;;
        sms|ems)
                if [ "$onoff" = "1" ]; then
                        [ "$(pidof gammu-smsd)" ] || /etc/init.d/gammu-smsd start
                else
                        [ "$(pidof gammu-smsd)" ] && { killall gammu-smsd; rm /run/gammu-smsd.pid; }
                fi
                ;;
        nas|samba)
                if [ "$onoff" = '1' ]; then
                        [ "$(pidof smbd)" ] || service samba start
                else
                        [ "$(pidof smbd)" ] && service samba stop
                fi
                ;;
        media|minidlna)
                if [ "$onoff" = '1' ]; then
                        [ "$(pidof minidlna)" ] || service minidlna start
                else
                        [ "$(pidof minidlna)" ] && service minidlna stop
                fi
                ;;
        cron)
                if [ "$onoff" = "list" ]; then
                        crontab -l
                else #---crontab xem CR (\r, ASCII 13) như một ký tự bình thường -> command not found
                        [ -f "$onoff" ] && sed 's/\n//g' "$onoff" && crontab "$onoff"
                fi
                ;;
        *)
                exit 1
                ;;
esac
exit $?

Leave a Comment

Filed under Software

Leave a Reply