public static function pulse($pin, $miliseconds, $state) { Gpio::write($pin, $state); usleep($miliseconds); $state = $state == 1 ? 0 : 1; Gpio::write($pin, $state); }
/** * @param string $bitStringLength * @return string */ private function readBitStringOfLength($bitStringLength) { $bitString = ''; $this->sckGpio->changeDirectionToOut(); $this->dataGpio->changeDirectionToIn(); for ($i = 0; $i < $bitStringLength; $i++) { $this->sckGpio->writeValue(1); $bitString .= $this->dataGpio->readValue(); $this->sckGpio->writeValue(0); } return $bitString; }
public static function test() { echo "Setting up Pins 17 and 22\n"; $gpio = new Gpio(); $gpio->setup(17, "out"); $gpio->setup(22, "out"); echo "Turning on Pins 17 and 22\n"; $gpio->output(17, 1); $gpio->output(22, 1); echo "Sleeping!\n"; sleep(3); echo "Turning off Pins 17 and 22\n"; $gpio->output(17, 0); $gpio->output(22, 0); echo "Unexporting all pins\n"; $gpio->unexportAll(); }
function wirerelay_plugin_change_state($engine, $state) { $wireRelay = new WireRelay(); $wireRelay = $wireRelay->getById($engine); Gpio::mode($wireRelay->pin, 'out'); if ($wireRelay->pulse == 0) { Gpio::write($wireRelay->pin, $state); } else { Gpio::pulse($wireRelay->pin, $wireRelay->pulse, 1); } //Reference device state change for other plugins $device = new Device(); $device = $device->load(array('plugin' => 'wireRelay', 'uid' => $wireRelay->id)); $device->setValue('value', $state); $device->save(); }
} else { echo '<br/>Plugin invalide, fichier principal manquant <span class="label label-error">Erreur</span>'; } } else { echo '<br/>Echec de l\'extraction <span class="label label-error">Erreur</span>'; } unlink($tempZipName); } else { echo '<br/>Echec du téléchargement <span class="label label-error">Erreur</span>'; } break; case 'CHANGE_GPIO_STATE': if ($myUser == false) { exit('Vous devez vous connecter pour cette action.'); } else { Gpio::write($_["pin"], $_["state"], true); } break; // Gestion des interfaces de seconde génération // Gestion des interfaces de seconde génération case 'SUBSCRIBE_TO_CLIENT': Action::write(function ($_, &$response) { global $myUser, $conf; if (!isset($_['ip'])) { throw new Exception("IP invalide"); } if (!isset($_['port']) || !is_numeric($_['port'])) { throw new Exception("Port invalide"); } $url = Functions::getBaseUrl('action.php') . '/action.php'; $client = new CLient($_['ip'], $_['port']);
function dash_monitoring_plugin_actions() { global $myUser, $_, $conf; switch ($_['action']) { case 'dash_monitoring_plugin_load': if ($myUser == false) { exit('Vous devez vous connecter pour cette action.'); } header('Content-type: application/json'); $response = array(); switch ($_['bloc']) { case 'ram': $response['title'] = 'RAM'; $hdds = Monitoring::ram(); $response['content'] = ' <div style="width: 100%"> <canvas id="RAM_PIE"></canvas> <br/><br/> <ul class="graphic_pane"> <li class="pane_orange"> <h1>RAM UTILISEE</h1> <h2>' . $hdds['percentage'] . '%</h2> </li><li class="pane_cyan"> <h1>RAM LIBRE</h1> <h2>' . $hdds['free'] . ' Mo</h2> </li><li class="pane_red"> <h1>RAM TOTALE</h1> <h2>' . $hdds['total'] . ' Mo</h2> </li> </ul> </div> <script> $("#RAM_PIE:visible").chart({ type : "doughnut", label : ["RAM UTILISEE","RAM LIBRE"], backgroundColor : ["' . ($hdds['percentage'] > 80 ? '#E64C65' : '#FCB150') . '","#4FC4F6"], segmentShowStroke:false, data : [' . $hdds['percentage'] . ',' . (100 - $hdds['percentage']) . '] }); </script>'; break; case 'system': $response['title'] = 'Système'; if (PHP_OS != 'WINNT') { $heat = Monitoring::heat(); $cpu = Monitoring::cpu(); } $response['content'] = '<ul> <li><strong>Distribution :</strong> ' . Monitoring::distribution() . '</li> <li><strong>Kernel :</strong> ' . Monitoring::kernel() . '</li> <li><strong>HostName :</strong> ' . Monitoring::hostname() . '</li> <li><strong>Température :</strong> <span class="label ' . $heat["label"] . '">' . $heat["degrees"] . '°C</span></li> <li><strong>Temps de marche :</strong> ' . Monitoring::uptime() . '</li> <li><strong>CPU :</strong> <span class="label label-info">' . $cpu['current_frequency'] . ' Mhz</span> (Max ' . $cpu['maximum_frequency'] . ' Mhz/ Min ' . $cpu['minimum_frequency'] . ' Mhz)</li> <li><strong>Charge :</strong> <span class="label label-info">' . $cpu['loads'] . ' </span> | ' . $cpu['loads5'] . ' 5min | ' . $cpu['loads15'] . ' 15min</li> <li><strong>Version WiringPi : </strong> ' . exec("gpio -v | grep 'gpio version' | cut -c15-") . '</li> <li><strong>Raspberry Pi Details : </strong> ' . exec("gpio -v") . '</li> </ul>'; break; case 'vocal': if ($myUser->getId() == '') { exit('{"error":"invalid or missing token"}'); } if (!$myUser->can('vocal', 'r')) { exit('{"error":"insufficient permissions for this account"}'); } list($host, $port) = explode(':', $_SERVER['HTTP_HOST']); $actionUrl = 'http://' . $host . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI']; $actionUrl = substr($actionUrl, 0, strpos($actionUrl, '?')); Plugin::callHook("vocal_command", array(&$response, $actionUrl)); $response['title'] = count($response['commands']) . ' Commandes vocales'; $response['content'] = '<ul>'; foreach ($response['commands'] as $command) { $response['content'] .= '<li title="Sensibilité : ' . $command['confidence'] . '">' . $command['command'] . '</li>'; } $response['content'] .= '</ul>'; break; case 'network': $response['title'] = 'Réseau'; $ethernet = array(); $lan = ''; $wan = ''; $http = ''; $connections = ''; if (PHP_OS != 'WINNT') { $ethernet = Monitoring::ethernet(); $lan = Monitoring::internalIp(); $wan = Monitoring::externalIp(); $http = Monitoring::webServer(); $connections = Monitoring::connections(); } $response['content'] = '<ul> <li><strong>IP LAN :</strong> <code>' . $lan . '</code></li> <li><strong>IP WAN :</strong> <code>' . $wan . '</code></li> <li><strong>Serveur HTTP :</strong> ' . $http . '</li> <li><strong>Ethernet :</strong> ' . $ethernet['up'] . ' Montant / ' . $ethernet['down'] . ' Descendant</li> <li><strong>Connexions :</strong> <span class="label label-info">' . $connections . '</span></li> </ul>'; break; case 'gpio': $response['title'] = 'GPIO'; if (PHP_OS != 'WINNT') { $version = Functions::getRaspVersion(); $response['content'] .= '<pre><table style="width:100%;text-align:center;"><tbody>'; // getListOptionalWiringPin // // Liste des GPIO $i = 0; // var_dump(Gpio::getListWiringPin($version)); foreach (Gpio::getListWiringPin($version) as $key => $GPIO) { $class = 'info'; $value = 'off'; if (Gpio::read_($key, false)) { $class = 'warning'; $value = 'on'; } if ($i % 2 == 0) { $response['content'] .= '<tr><td><strong>' . $GPIO . '</strong></td><td>-></td><td> <span onclick="change_gpio_state(' . $key . ',this);" style="width:20px;text-align:center;" class="label label-' . $class . ' pointer">' . $value . '</span> </td>'; } else { $response['content'] .= '<td> <span onclick="change_gpio_state(' . $key . ',this);" style="width:20px;text-align:center;" class="pointer label label-' . $class . '">' . $value . '</span> </td><td><-</td><td><strong>' . $GPIO . '</strong></td></tr>'; } $i++; } $listeOfOptionnalGpios = Gpio::getListOptionalWiringPin($version); if (!empty($listeOfOptionnalGpios)) { $response['content'] .= '<tr><td colspan="6">Liste des Gpio optionnels</td></tr>'; //liste des GPIO optionnels $i = 0; foreach (Gpio::getListOptionalWiringPin($version) as $GPIO) { $class = 'info'; $value = 'off'; if (Gpio::read_($key, false)) { $class = 'warning'; $value = 'on'; } if ($i % 2 == 0) { $response['content'] .= '<tr><td><strong>' . Gpio::getNameOfWiringPins($i) . '</strong></td><td>-></td><td> <span onclick="change_gpio_state(' . $i . ',this);" style="width:20px;text-align:center;" class="label label-' . $class . ' pointer">' . $value . '</span> </td>'; } else { $response['content'] .= '<td> <span onclick="change_gpio_state(' . $i . ',this);" style="width:20px;text-align:center;" class="pointer label label-' . $class . '">' . $value . '</span> </td><td><-</td><td><strong>' . Gpio::getNameOfWiringPins($i + 1) . '</strong></td></tr>'; } $i++; } } $response['content'] .= '</tbody></table></pre>'; } else { $response['content'] .= 'Information indisponible sur cet OS :' . PHP_OS; } break; case 'pins': $response['title'] = 'PINS'; if (PHP_OS != 'WINNT') { $version = Functions::getRaspVersion(); //un peu de style $response["content"] .= '<style>.label_pin{padding:5px;border: 1px solid #FFF;}.pin_gpio{background: #FCBF21}.pin_power{background: #FB0C1A}.pin_ground{background: #000}.pin_UART{background: #00AF08}.pin_SPI{background: #9B04EF}.pin_I2C{background: #0044AB}.legende_pins a{color:#FFF}.legende_pins{margin:5px; border: 1px solid #FFF;display:inline;padding: 5px}</style>'; $response["content"] .= '<pre><table style="width:100%;text-align:center;margin:5px;"><tbody>'; //on met les titres $response["content"] .= '<tr><th>WiringPi</th><th>Nom</th><th></th><th></th><th>Nom</th><th>WiringPi</th></tr>'; $i = 0; foreach (Gpio::getListPins($version) as $numero => $infos) { $wiringPi = is_null($infos[0]) ? "N/A" : $infos[0]; $nom = $infos[1]; $class = ""; //on détermine les class switch ($nom) { case preg_match("~GPIO~Uis", $nom) ? true : false: $class = "gpio"; break; case "0V": $class = "ground"; break; case "3,3V": case "5V": $class = "power"; break; case "MOSI": case "MISO": case "SCLK": case "CE0": case "CE1": $class = "SPI"; break; case "RxD": case "TxD": $class = "UART"; break; case preg_match("~SDA~Uis", $nom) ? true : false: case preg_match("~SCL~Uis", $nom) ? true : false: $class = "I2C"; break; default: $class = ""; break; } $class = !empty($class) ? 'pin_' . $class : ""; if ($i % 2 == 0) { $response['content'] .= '<tr><td>' . $wiringPi . '</td><td>' . $nom . '</td><td class="label_pin ' . $class . '" ><i class="fa fa-dot-circle-o"></i></td>'; } else { $response['content'] .= '<td class="label_pin ' . $class . '" ><i class="fa fa-dot-circle-o"></i></td><td>' . $nom . '</td><td>' . $wiringPi . '</td></tr>'; } $i++; } $temp_list = Gpio::getListOptionalPins($version); if (!empty($temp_list)) { $response['content'] .= '<tr><td colspan="6">Pins optionnels</td></tr>'; $i = 0; foreach (Gpio::getListOptionalPins($version) as $numero => $infos) { $wiringPi = is_null($infos[0]) ? "N/A" : $infos[0]; $nom = $infos[1]; $class = ""; //on détermine les class switch ($nom) { case preg_match("~GPIO~Uis", $nom) ? true : false: $class = "gpio"; break; case "0V": $class = "ground"; break; case "3,3V": case "5V": $class = "power"; break; case "MOSI": case "MISO": case "SCLK": case "CE0": case "CE1": $class = "SPI"; break; case "RxD": case "TxD": $class = "UART"; break; case preg_match("~SDA~Uis", $nom) ? true : false: case preg_match("~SCL~Uis", $nom) ? true : false: $class = "I2C"; break; default: $class = ""; break; } $class = !empty($class) ? 'pin_' . $class : ""; if ($i % 2 == 0) { $response['content'] .= '<tr><td>' . $wiringPi . '</td><td>' . $nom . '</td><td class="label_pin ' . $class . '" ><i class="fa fa-dot-circle-o"></i></td>'; } else { $response['content'] .= '<td class="label_pin ' . $class . '" ><i class="fa fa-dot-circle-o"></i></td><td>' . $nom . '</td><td>' . $wiringPi . '</td></tr>'; } $i++; } } $response['content'] .= '</tbody></table>'; $response['content'] .= '<div class="legende_pins pin_ground"><a href="http://fr.wikipedia.org/wiki/Terre_(%C3%A9lectricit%C3%A9)">ground</a></div><div class="legende_pins pin_power"><a href="http://fr.wikipedia.org/wiki/Alimentation_%C3%A9lectrique">power</a></div><div class="legende_pins pin_gpio"><a href="http://fr.wikipedia.org/wiki/General_Purpose_Input/Output">gpio</a></div><div class="legende_pins pin_SPI"><a href="http://fr.wikipedia.org/wiki/Serial_Peripheral_Interface">SPI</a></div><div class="legende_pins pin_UART"><a href="http://fr.wikipedia.org/wiki/UART">UART</a></div><div class="legende_pins pin_I2C"><a href="http://fr.wikipedia.org/wiki/I2C">I2C</a></div></tr>'; $response['content'] .= '</pre>'; } else { $response['content'] .= 'Information indisponible sur cet OS :' . PHP_OS; } break; case 'users': $users = array(); if (PHP_OS != 'WINNT') { $users = Monitoring::users(); } $response['title'] = count($users) . ' utilisateurs connectés'; $response['content'] = '<ul>'; foreach ($users as $value) { $response['content'] .= '<li>Utilisateur <strong class="badge">' . $value['user'] . '</strong> IP : <code>' . $value['ip'] . '</code>, Connexion : ' . $value['hour'] . ' </li>'; } $response['content'] .= '</ul>'; break; case 'services': $response['title'] = 'Services'; $services = array(); if (PHP_OS != 'WINNT') { $services = Monitoring::services(); $response['content'] = '<ul>'; foreach ($services as $value) { $response['content'] .= '<li ' . ($value['status'] ? 'class="service-active"' : '') . '>- ' . $value['name'] . '</li>'; } $response['content'] .= '</ul>'; } else { $response['content'] .= 'Information indisponible sur cet OS :' . PHP_OS; } break; case 'hdd': $response['title'] = 'HDD'; $hdds = array(); if (PHP_OS != 'WINNT') { $hdds = Monitoring::hdd(); $response['content'] = '<ul>'; foreach ($hdds as $value) { $response['content'] .= '<li><strong class="badge">' . $value['name'] . '</strong><br><strong> Espace :</strong> ' . $value['used'] . '/' . $value['total'] . '<strong> Format :</strong> ' . $value['format'] . ' </li>'; } $response['content'] .= '</ul>'; } else { $response['content'] .= 'Information indisponible sur cet OS :' . PHP_OS; } break; case 'disk': $response['title'] = 'Disques'; $disks = array(); if (PHP_OS != 'WINNT') { $disks = Monitoring::disks(); $response['content'] = '<ul>'; foreach ($disks as $value) { $response['content'] .= '<li><strong class="badge">' . $value['name'] . '</strong> Statut : ' . $value['size'] . ' Type : ' . $value['type'] . ' Chemin : ' . $value['mountpoint'] . ' </li>'; } $response['content'] .= '</ul>'; } else { $response['content'] .= 'Information indisponible sur cet OS :' . PHP_OS; } break; } echo json_encode($response); exit(0); break; case 'dash_monitoring_plugin_edit': echo '<label>Time Zone</label><input id="zone" type="text">'; break; case 'dash_monitoring_plugin_save': break; case 'dash_monitoring_plugin_delete': break; case 'dash_monitoring_plugin_move': break; } }
} else { echo '<br/>Plugin invalide, fichier principal manquant <span class="label label-error">Erreur</span>'; } } else { echo '<br/>Echec de l\'extraction <span class="label label-error">Erreur</span>'; } unlink($tempZipName); } else { echo '<br/>Echec du téléchargement <span class="label label-error">Erreur</span>'; } break; case 'CHANGE_GPIO_STATE': if (!$myUser) { exit('Vous devez vous connecter pour cette action.'); } $state = Gpio::changePinState($_["pin"]); echo json_encode(array("state" => $state)); break; // Gestion des interfaces de seconde génération // Gestion des interfaces de seconde génération case 'SUBSCRIBE_TO_CLIENT': Action::write(function ($_, &$response) { global $myUser, $conf; if (!isset($_['ip'])) { throw new Exception("IP invalide"); } if (!isset($_['port']) || !is_numeric($_['port'])) { throw new Exception("Port invalide"); } $url = Functions::getBaseUrl('action.php') . '/action.php'; $client = new CLient($_['ip'], $_['port']);
/** * @param Gpio $gpio * @throws Exception */ private function exportGpioIfNeeded(Gpio $gpio) { if (!file_exists($gpio->getBaseGpioPath())) { throw new Exception("GPIO port " . $gpio->getGpioPinNumber() . " is not exported"); } }
function wirerelay_plugin_change_state($engine, $state) { $wireRelay = new WireRelay(); $wireRelay = $wireRelay->getById($engine); Gpio::mode($wireRelay->pin, 'out'); if ($wireRelay->pulse == 0) { Gpio::write($wireRelay->pin, $state); } else { Gpio::pulse($wireRelay->pin, $wireRelay->pulse, 1); } }
function speechcommands_action() { global $_, $conf, $myUser; switch ($_['action']) { case 'plugin_speechcommands_save': if (!$myUser->can('speech_command', 'c')) { exit('Permissions insufisantes'); } require_once 'SpeechCommand.class.php'; $command = new SpeechCommand(); $command = !empty($_['id']) ? $command->getById($_['id']) : new SpeechCommand(); $command->command = $_['command']; $command->action = $_['type']; $command->parameter = $_['parameter']; $command->confidence = $_['confidence']; $command->state = $_['state'] == 'on' ? 1 : 0; $command->save(); header('location: setting.php?section=speechcommands'); break; case 'plugin_speechcommands_delete': if (!$myUser->can('speech_command', 'd')) { exit('Permissions insufisantes'); } require_once 'SpeechCommand.class.php'; $command = new SpeechCommand(); $command->delete(array('id' => $_['id'])); header('location: setting.php?section=speechcommands'); break; case 'speechcommands_execute': global $_; require_once 'SpeechCommand.class.php'; $command = new SpeechCommand(); $command = $command->getById($_['command']); set_error_handler(function ($errno, $errstr, $errfile, $errline, array $errcontext) { // error was suppressed with the @-operator if (0 === error_reporting()) { return false; } throw new ErrorException($errstr, 0, $errno, $errfile, $errline); }); try { switch ($command->action) { case 'talk': $response = array('responses' => array(array('type' => 'talk', 'sentence' => $command->parameter))); $json = json_encode($response); echo $json == '[]' ? '{}' : $json; break; case 'gpio': list($pin, $value) = explode(',', $command->parameter); Gpio::write($pin, $value, true); break; case 'server_command': System::commandSilent(html_entity_decode($command->parameter)); echo '{}'; break; case 'client_command': $response = array('responses' => array(array('type' => 'command', 'program' => $command->parameter))); $json = json_encode($response); echo $json == '[]' ? '{}' : $json; break; case 'sound': $response = array('responses' => array(array('type' => 'sound', 'file' => $command->parameter))); $json = json_encode($response); echo $json == '[]' ? '{}' : $json; break; case 'url': $content = file_get_contents($command->parameter); $response = array('responses' => array(array('type' => 'talk', 'sentence' => $content))); $json = json_encode($response); echo $json == '[]' ? '{}' : $json; break; default: throw new Exception('Aucun action n\'est spécifiée'); break; } } catch (Exception $e) { $response = array('responses' => array(array('type' => 'talk', 'sentence' => Personality::response('WORRY_EMOTION') . ', le problème viens de : ' . $e->getMessage()))); $json = json_encode($response); echo $json == '[]' ? '{}' : $json; } break; } }