function radioRelay_action_radioRelay()
{
    global $_, $conf, $myUser;
    //Mise à jour des droits
    $myUser->loadRight();
    switch ($_['action']) {
        case 'radioRelay_delete_radioRelay':
            if ($myUser->can('radio relais', 'd')) {
                $radioRelayManager = new RadioRelay();
                $radioRelayManager->delete(array('id' => $_['id']));
                header('location:setting.php?section=radioRelay');
            } else {
                header('location:setting.php?section=radioRelay&error=Vous n\'avez pas le droit de faire ça!');
            }
            break;
        case 'radioRelay_plugin_setting':
            $conf->put('plugin_radioRelay_emitter_pin', $_['emiterPin']);
            $conf->put('plugin_radioRelay_emitter_code', $_['emiterCode']);
            header('location: setting.php?section=preference&block=radioRelay');
            break;
        case 'radioRelay_add_radioRelay':
            //Vérifie si on veut modifier ou ajouter un relai
            $right_toverify = isset($_['id']) ? 'u' : 'c';
            if ($myUser->can('radio relais', $right_toverify)) {
                $radioRelay = new RadioRelay();
                //Si modification on charge la ligne au lieu de la créer
                if ($right_toverify == "u") {
                    $radioRelay = $radioRelay->load(array("id" => $_['id']));
                }
                $radioRelay->setName($_['nameRadioRelay']);
                $radioRelay->setDescription($_['descriptionRadioRelay']);
                $radioRelay->setRadioCode($_['radioCodeRadioRelay']);
                $radioRelay->setRoom($_['roomRadioRelay']);
                $radioRelay->setPulse($_['pulseRadioRelay']);
                $radioRelay->save();
                header('location:setting.php?section=radioRelay');
            } else {
                header('location:setting.php?section=radioRelay&error=Vous n\'avez pas le droit de faire ça!');
            }
            break;
        case 'radioRelay_change_state':
            global $_, $myUser;
            if ($myUser->can('radio relais', 'u')) {
                $radioRelay = new RadioRelay();
                $radioRelay = $radioRelay->getById($_['engine']);
                Event::emit('relay_change_state', array('relay' => $radioRelay, 'state' => $_['state']));
                if ($radioRelay->getPulse() == 0) {
                    $cmd = dirname(__FILE__) . '/radioEmission ' . $conf->get('plugin_radioRelay_emitter_pin') . ' ' . $conf->get('plugin_radioRelay_emitter_code') . ' ' . $radioRelay->getRadioCode() . ' ' . $_['state'];
                } else {
                    $cmd = dirname(__FILE__) . '/radioEmission ' . $conf->get('plugin_radioRelay_emitter_pin') . ' ' . $conf->get('plugin_radioRelay_emitter_code') . ' ' . $radioRelay->getRadioCode() . ' pulse ' . $radioRelay->getPulse();
                }
                //TODO change bdd state
                Functions::log('Launch system command : ' . $cmd);
                system($cmd, $out);
                if (!isset($_['webservice'])) {
                    header('location:index.php?module=room&id=' . $radioRelay->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;
    }
}
示例#2
0
function radiorelay_plugin_action()
{
    global $_, $conf, $myUser;
    //Action de réponse à la commande vocale "Yana, commande de test"
    switch ($_['action']) {
        case 'radioRelay_save_radioRelay':
            Action::write(function ($_, &$response) {
                $radioRelayManager = new RadioRelay();
                if (empty($_['nameRadioRelay'])) {
                    throw new Exception("Le nom est obligatoire");
                }
                if (!is_numeric($_['radioCodeRadioRelay'])) {
                    throw new Exception("Le code radio est obligatoire et doit être numerique");
                }
                $radioRelay = !empty($_['id']) ? $radioRelayManager->getById($_['id']) : new RadioRelay();
                $radioRelay->name = $_['nameRadioRelay'];
                $radioRelay->description = $_['descriptionRadioRelay'];
                $radioRelay->room = $_['roomRadioRelay'];
                $radioRelay->pulse = $_['pulseRadioRelay'];
                $radioRelay->onCommand = $_['onRadioRelay'];
                $radioRelay->offCommand = $_['offRadioRelay'];
                $radioRelay->icon = $_['iconRadioRelay'];
                $radioRelay->radiocode = $_['radioCodeRadioRelay'];
                $radioRelay->save();
                $response['message'] = 'Relais enregistré avec succès';
            }, array('plugin_radiorelay' => 'c'));
            break;
        case 'radioRelay_delete_radioRelay':
            Action::write(function ($_, $response) {
                $radioRelayManager = new RadioRelay();
                $radioRelayManager->delete(array('id' => $_['id']));
            }, array('plugin_radiorelay' => 'd'));
            break;
        case 'radioRelay_plugin_setting':
            Action::write(function ($_, &$response) {
                global $conf;
                $conf->put('plugin_radioRelay_emitter_pin', $_['emiterPin']);
                $conf->put('plugin_radioRelay_emitter_code', $_['emiterCode']);
                $response['message'] = 'Configuration enregistrée';
            }, array('plugin_radiorelay' => 'c'));
            break;
        case 'radioRelay_manual_change_state':
            Action::write(function ($_, &$response) {
                radiorelay_plugin_change_state($_['engine'], $_['state']);
            }, array('plugin_radiorelay' => 'c'));
            break;
        case 'radioRelay_vocal_change_state':
            global $_, $myUser;
            try {
                $response['responses'][0]['type'] = 'talk';
                if (!$myUser->can('plugin_radiorelay', 'u')) {
                    throw new Exception('Je ne vous connais pas, ou alors vous n\'avez pas le droit, je refuse de faire ça!');
                }
                radiorelay_plugin_change_state($_['engine'], $_['state']);
                $response['responses'][0]['sentence'] = Personality::response('ORDER_CONFIRMATION');
            } catch (Exception $e) {
                $response['responses'][0]['sentence'] = Personality::response('WORRY_EMOTION') . '! ' . $e->getMessage();
            }
            $json = json_encode($response);
            echo $json == '[]' ? '{}' : $json;
            break;
        case 'radioRelay_plugin_setting':
            Action::write(function ($_, &$response) {
                global $conf;
                $conf->put('plugin_radioRelay_emitter_pin', $_['emiterPin']);
                $conf->put('plugin_radioRelay_emitter_code', $_['emiterCode']);
                $response['message'] = 'Configuration modifiée avec succès';
            }, array('plugin_radiorelay' => 'u'));
            break;
        case 'radioRelay_load_widget':
            require_once dirname(__FILE__) . '/../dashboard/Widget.class.php';
            Action::write(function ($_, &$response) {
                $widget = new Widget();
                $widget = $widget->getById($_['id']);
                $data = $widget->data();
                $content = '';
                if (empty($data['relay'])) {
                    $content = 'Choisissez un relais en cliquant sur l \'icone <i class="fa fa-wrench"></i> de la barre du widget';
                } else {
                    if (fileperms(Plugin::path() . 'radioEmission') != '36333') {
                        $content .= '<div style="margin:0px;" class="flatBloc pink-color">Attention, les droits vers le fichier <br/> radioEmission sont mal réglés.<br/> Référez vous à <span style="cursor:pointer;text-decoration:underline;" onclick="window.location.href=\'https://github.com/ldleman/yana-server#installation\';">la doc</span> pour les régler</div>';
                    }
                    $relay = new RadioRelay();
                    $relay = $relay->getById($data['relay']);
                    $response['title'] = $relay->name;
                    $content .= '
						<!-- CSS -->
						<style>
							
							.radiorelay_relay_pane {
							    background: none repeat scroll 0 0 #50597b;
							    list-style-type: none;
							    margin: 0;
							    cursor:default;
							    width: 100%;
							}
							.radiorelay_relay_pane li {
							    background: none repeat scroll 0 0 #50597b;
							    display: inline-block;
							    margin: 0 1px 0 0;
							    padding: 10px;
							    cursor:default;
							    vertical-align: top;
							}
							.radiorelay_relay_pane li h2 {
							    color: #ffffff;
							    font-size: 16px;
							    margin: 0 0 5px;
							    padding: 0;
							    cursor:default;
							}
							.radiorelay_relay_pane li h1 {
							    color: #B6BED9;
							    font-size: 14px;
							    margin: 0 0 10px;
							    padding: 0;
							    cursor:default;
							}

							.radiorelay_relay_pane li.radiorelay-case{
								background-color:  #373f59;
								width: 55px;
								cursor:pointer;
							}
							.radiorelay-case i{
								color:#8b95b8;
								font-size:50px;
								transition: all 0.2s ease-in-out;
							}
							.radiorelay-case.active i{
								color:#ffffff;
								text-shadow: 0 0 10px #ffffff;
							}

							.radiorelay-case.active i.fa-lightbulb-o{
								color:#FFED00;
								text-shadow: 0 0 10px #ffdc00;
							}
							.radiorelay-case.active i.fa-power-off{
								color:#BDFF00;
								text-shadow: 0 0 10px #4fff00;
							}

							.radiorelay-case.active i.fa-flash{
								color:#FFFFFF;
								text-shadow: 0 0 10px #00FFD9;
							}

							.radiorelay-case.active i.fa-gears{
								color:#FFFFFF;
								text-shadow: 0 0 10px #FF00E4;
							}

						</style>
						
						<!-- CSS -->
						<ul class="radiorelay_relay_pane">
								<li class="radiorelay-case ' . ($relay->state ? 'active' : '') . '" onclick="plugin_radiorelay_change(this,' . $relay->id . ');" style="text-align:center;">
									<i title="On/Off" class="' . $relay->icon . '"></i>
								</li>
								<li>
									<h2>' . $relay->description . '</h2>
									<h1>CODE ' . $relay->radiocode . ($relay->pulse != 0 ? ' - Pulse ' . $relay->pulse . 'µs' : '') . '</h1>
								</li>
							</ul>

						<!-- JS -->
						<script type="text/javascript">
							function plugin_radiorelay_change(element,id){
								var state = $(element).hasClass(\'active\') ? 0 : 1 ;

								$.action(
									{
										action : \'radioRelay_manual_change_state\', 
										engine: id,
										state: state
									},
									function(response){
										$(element).toggleClass("active");
									}
								);

							}
						</script>
						';
                }
                $response['content'] = $content;
            });
            break;
        case 'radioRelay_edit_widget':
            require_once dirname(__FILE__) . '/../dashboard/Widget.class.php';
            $widget = new Widget();
            $widget = $widget->getById($_['id']);
            $data = $widget->data();
            $relayManager = new RadioRelay();
            $relays = $relayManager->populate();
            $content = '<h3>Relais ciblé</h3>';
            if (count($relays) == 0) {
                $content = 'Aucun relais existant dans yana, <a href="setting.php?section=radioRelay">Créer un relais ?</a>';
            } else {
                $content .= '<select id="relay">';
                $content .= '<option value="">-</option>';
                foreach ($relays as $relay) {
                    $content .= '<option value="' . $relay->id . '">' . $relay->name . '</option>';
                }
                $content .= '</select>';
            }
            echo $content;
            break;
        case 'radioRelay_save_widget':
            require_once dirname(__FILE__) . '/../dashboard/Widget.class.php';
            $widget = new Widget();
            $widget = $widget->getById($_['id']);
            $data = $widget->data();
            $data['relay'] = $_['relay'];
            $widget->data($data);
            $widget->save();
            echo $content;
            break;
    }
}