2016-10-24 74 views
0

我試圖做一些切換開關:當其從它會發出一些指令(IN1),如果其對它將發送另一個命令(IN2撥動開關EXEC shell腳本

命令的發送通過shell腳本

什麼我使用的是這樣一個按鈕:

<?php 
if ($_GET['IN1']) { 
    echo shell_exec("sudo sh /home/cubie/phpdemo/IN1.sh"); 
} 
?> 

<a href="?IN1=true">IN1</a> 

,但我發現這一點: http://www.w3schools.com/howto/howto_css_switch.asp

我想用它。但是,我該怎麼做相同的,但與開關

IN1on

IN1off

===

一些編輯 確定現在我知道了。 如果你想顯示切換按鈕我已經設置

<link rel="stylesheet" type="text/css" href="style.css" /> 


<?php 

function get_in1_status() { 
    return shell_exec('cat /sys/class/gpio/gpio19_pg7/value'); 
    /* return (boolean) */ } 
/* return TRUE if you object is active ; else return FALSE */ 

function set_all_off() { 
    shell_exec('java -cp /home/cubie/demo/ allinone 2'); 
    } 

function set_all_on() { 
    shell_exec('java -cp /home/cubie/demo/ allinone 1'); 
    } 

$current_value = get_in1_status(); 

?> 


<label class="switch"> 
    <input type="checkbox" 
    <?php echo ($current_value == true) ? 'checked="checked"' : null; ?> 
    <?php 
    if($current_value == true) { 
    set_all_on(); 
    } 
    else if($current_value == false){ 
    set_all_off(); 
    } 
    ?> 
    > 
    <div class="slider round"></div> 
</label> 
+2

你真的不希望有使用sudo執行網絡訪問的腳本。相反,更改IN1.sh文件權限,以便PHP可以執行它。 – faraday703

+0

它對我來說並不是一個大問題,因爲它僅僅適用於本地網絡中的家庭使用,但現在的問題是如何使它與交換機一起工作,以及如果以後我確定它會與其他命令 –

回答

1

,你需要得到你的對象的當前值。 你有什麼辦法得到它嗎?

<?php 

function get_in1_status() { /* return (boolean) */ } 
/* return TRUE if you object is active ; else return FALSE */ 

function set_in1_on() { shell_exec('sudo sh /home/cubie/phpdemo/IN1.sh'); } 

$current_value = get_in1_status(); 

?> 
<label class="switch"> 
    <input type="checkbox" <?php echo ($current_value == true) ? 'checked="checked"' : null; ?>> 
    <div class="slider round"></div> 
</label> 

在get_in1_status,例如,你可以調用另一個.SH代碼將返回您的服務狀態。你可以將這個值存儲在文件或數據庫中,沒有其他任何可以讓你獲得真/假值的東西。


問: OK很大。對於目前的價值,我可以得到它的一些其他文件 /sys/class/gpio/gpio19_pg7 /值 的值將是0或1,所以我該怎麼做?如果關閉,我想要執行一個 腳本,如果它打開,我想執行另一個腳本。 - ANMAR Mashat

當然,你可以:) 如果該值是一個文件,試圖例如:

<?php function get_in1_status() { 
    return shell_exec('cat /sys/class/gpio/gpio19_pg7/value'); 
} 

在PHP中,1等於true(事實上,任何值不同的,falsenull等於真)。 如果你想如果你想有一個特定的文本有明確的布爾值可以強制$result = (boolean) 1;所以$result將等於true :)

<?php 
echo (get_in1_status() ? 'on': 'off'); // will return 'on' or 'off' value 
echo (get_in1_status() == true ? 'on': 'off'); // Same. will return 'on' or 'off' value 
echo get_in1_status(); // will return the content on the value 
echo (boolean) get_in1_status(); // will return 1 or 0 


if(get_in1_status()) { 
    // if yes 
} 
else { 
    // if no 
} 
?> 

現在,你只需要選擇。


根據您的工作:幾乎:)但是,開關等的邏輯是不正確的。你需要檢查的價值和與事件改變(例如:趕上刷新頁面,通過_GET值)

參見:

如果代碼不工作,試評上第一回獲取in1_status函數並取消註釋第二個:) 如果它仍然不能正常工作...嘗試另一個shell命令。什麼是value?一份文件 ?一個程序?使用cat/echo或直接執行該行。

您的功能:

<?php 

function get_in1_status() { 
    return shell_exec('cat /sys/class/gpio/gpio19_pg7/value'); 
    //return file_get_contents('/sys/class/gpio/gpio19_pg7/value'); // solution 2 
} 

function set_all_off() { 
    shell_exec('java -cp /home/cubie/demo/ allinone 2'); 
} 

function set_all_on() { 
    shell_exec('java -cp /home/cubie/demo/ allinone 1'); 
} 

現在,該事件。當我捕捉到'in1'_GET參數時,我運行切換邏輯。 更多的PHP代碼:

<?php 

// change the value 
if(isset($_GET['in1'])) { // if url contains 'in1' parameter 
    switch($_GET['in1']) { 
    case 'on': // ?in1=on 
     set_all_off(); 
     break; 

    case 'off': // ?in1=off 
     set_all_on(); 
     break; 

    default: // ?in1 or ?in1=anything_else 
     if(get_in1_status()) 
      set_all_off(); // if true=on -> switch off 
     else 
      set_all_on(); // if true=off -> switch ofn 
     break; 
    } 
} 

// get the current value 
$current_value = get_in1_status(); 

?> 

和HTML(這是一個例子這裏我用一個<a />元素提示:。提交表單更改值或使用Ajax例如做動態)

<a href="?in1"> 
    Default (switch): 
    <label class="switch"> 
    <input type="checkbox" <?php echo ($current_value) ? 'checked="checked"' : null; ?> /> 
    <div class="slider round"></div> 
    </label> 
</a> 

<br /> 

<a href="?ini=on">On</a> <a href="?ini=off">On</a> 

(代碼未測試)

+0

確定很好。對於當前值我可以得到它在一些其他文件 /sys/class/gpio/gpio19_pg7 /值 –

+0

的值將是0或1,所以我該如何做?如果關閉,我想執行一個腳本,如果它打開,我想執行另一個腳本。 –

+0

它可以是1/0不轉/錯? –