Пример #1
0
function door_action_door()
{
    global $_, $conf, $myUser;
    switch ($_['action']) {
        case 'door_delete_door':
            if ($myUser->can('porte', 'd')) {
                $doorManager = new Door();
                $doorManager->delete(array('id' => $_['id']));
            }
            header('location:setting.php?section=door');
            break;
        case 'door_add_door':
            if ($myUser->can('porte', 'c')) {
                $door = new Door();
                $door->setName($_['nameDoor']);
                $door->setDescription($_['descriptionDoor']);
                $door->setPinRelay($_['pinDoorRelay']);
                $door->setPinCaptor($_['pinDoorCaptor']);
                $door->setRoom($_['roomDoor']);
                $door->save();
            }
            header('location:setting.php?section=door');
            break;
        case 'door_get_state':
            if ($myUser->can('porte', 'r')) {
                $door = new Door();
                $door = $door->getById($_['engine']);
                $cmd = '/usr/local/bin/gpio mode ' . $door->getPinCaptor() . ' in';
                system($cmd, $out);
                $cmd = '/usr/local/bin/gpio read ' . $door->getPinCaptor();
                exec($cmd, $out);
                if (!isset($_['webservice'])) {
                    echo $out[0];
                } else {
                    $affirmation = trim($out[0]) ? 'Ouvert' : 'Fermé';
                    $response = array('responses' => array(array('type' => 'talk', 'sentence' => $affirmation)));
                    $json = json_encode($response);
                    echo $json == '[]' ? '{}' : $json;
                }
            }
            break;
        case 'door_change_state':
            global $_, $myUser;
            if ($myUser->can('porte', 'u')) {
                $door = new Door();
                $door = $door->getById($_['engine']);
                $cmd = '/usr/local/bin/gpio mode ' . $door->getPinRelay() . ' out';
                system($cmd, $out);
                $cmd = '/usr/local/bin/gpio write ' . $door->getPinRelay() . ' ' . $_['state'];
                system($cmd, $out);
                //TODO change bdd state
                if (!isset($_['webservice'])) {
                    header('location:index.php?module=room&id=' . $door->getRoom());
                } else {
                    $affirmations = array('A vos ordres!', 'Bien!', 'Oui commandant!', 'Avec plaisir!', 'J\'aime vous obéir!', 'Avec plaisir!', 'Certainement!', 'Je fais ça sans tarder!', 'Avec plaisir!', 'Oui chef!');
                    $affirmation = $affirmations[rand(0, count($affirmations) - 1)];
                    $response = array('responses' => array(array('type' => 'talk', 'sentence' => $affirmation)));
                    $json = json_encode($response);
                    echo $json == '[]' ? '{}' : $json;
                }
            } else {
                $response = array('responses' => array(array('type' => 'talk', 'sentence' => 'Je ne vous connais pas, je refuse de faire ça!')));
                echo json_encode($response);
            }
            break;
    }
}