function wirerelay_plugin_action() { global $_, $conf, $myUser; //Action de réponse à la commande vocale "Yana, commande de test" switch ($_['action']) { case 'wireRelay_save_wireRelay': Action::write(function ($_, &$response) { $wireRelayManager = new WireRelay(); if (empty($_['nameWireRelay'])) { throw new Exception("Le nom est obligatoire"); } if (!is_numeric($_['pinWireRelay'])) { throw new Exception("Le PIN GPIO est obligatoire et doit être numerique"); } $wireRelay = !empty($_['id']) ? $wireRelayManager->getById($_['id']) : new WireRelay(); $wireRelay->name = $_['nameWireRelay']; $wireRelay->description = $_['descriptionWireRelay']; $wireRelay->pin = $_['pinWireRelay']; $wireRelay->room = $_['roomWireRelay']; $wireRelay->pulse = $_['pulseWireRelay']; $wireRelay->oncommand = $_['onWireRelay']; $wireRelay->offcommand = $_['offWireRelay']; $wireRelay->icon = $_['iconWireRelay']; $wireRelay->save(); //Reference device for other plugins $device = new Device(); $device->label = $wireRelay->name; $device->plugin = 'wireRelay'; $device->type = Device::ACTUATOR; $device->location = $wireRelay->room; $device->icon = $wireRelay->icon; $device->setValue('value', 0); $device->state = 1; $device->uid = $wireRelay->id; $device->save(); $response['message'] = 'Relais enregistré avec succès'; }, array('plugin_wirerelay' => 'c')); break; case 'wireRelay_delete_wireRelay': Action::write(function ($_, $response) { $wireRelayManager = new WireRelay(); $wireRelayManager->delete(array('id' => $_['id'])); }, array('plugin_wirerelay' => 'd')); break; case 'wireRelay_plugin_setting': Action::write(function ($_, $response) { $conf->put('plugin_wireRelay_emitter_pin', $_['emiterPin']); $conf->put('plugin_wireRelay_emitter_code', $_['emiterCode']); }, array('plugin_wirerelay' => 'c')); break; case 'wireRelay_manual_change_state': Action::write(function ($_, &$response) { wirerelay_plugin_change_state($_['engine'], $_['state']); }, array('plugin_wirerelay' => 'c')); break; case 'wireRelay_vocal_change_state': global $_, $myUser; try { $response['responses'][0]['type'] = 'talk'; if (!$myUser->can('plugin_wirerelay', 'u')) { throw new Exception('Je ne vous connais pas, ou alors vous n\'avez pas le droit, je refuse de faire ça!'); } wirerelay_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 'wireRelay_load_widget': require_once dirname(__FILE__) . '/../dashboard/Widget.class.php'; Action::write(function ($_, &$response) { $widget = new Widget(); $widget = $widget->getById($_['id']); $data = $widget->data(); 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 { $relay = new WireRelay(); $relay = $relay->getById($data['relay']); $response['title'] = $relay->name; $content = ' <!-- CSS --> <style> .relay_pane { background: none repeat scroll 0 0 #50597b; list-style-type: none; margin: 0; cursor:default; width: 100%; } .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; } .relay_pane li h2 { color: #ffffff; font-size: 16px; margin: 0 0 5px; padding: 0; cursor:default; } .relay_pane li h1 { color: #B6BED9; font-size: 14px; margin: 0 0 10px; padding: 0; cursor:default; } .relay_pane li.wirerelay-case{ background-color: #373f59; cursor:pointer; width: 55px; } .wirerelay-case i{ color:#8b95b8; font-size:50px; transition: all 0.2s ease-in-out; } .wirerelay-case.active i{ color:#ffffff; text-shadow: 0 0 10px #ffffff; } .wirerelay-case.active i.fa-lightbulb-o{ color:#FFED00; text-shadow: 0 0 10px #ffdc00; } .wirerelay-case.active i.fa-power-off{ color:#BDFF00; text-shadow: 0 0 10px #4fff00; } .wirerelay-case.active i.fa-flash{ color:#FFFFFF; text-shadow: 0 0 10px #00FFD9; } .wirerelay-case.active i.fa-gears{ color:#FFFFFF; text-shadow: 0 0 10px #FF00E4; } </style> <!-- CSS --> <ul class="relay_pane"> <li class="wirerelay-case ' . (Gpio::read($relay->pin, true) ? 'active' : '') . '" onclick="plugin_wirerelay_change(this);" style="text-align:center;"> <i title="On/Off" class="' . $relay->icon . '"></i> </li> <li> <h2>' . $relay->description . '</h2> <h1>PIN ' . $relay->pin . ($relay->pulse != 0 ? ' - Pulse ' . $relay->pulse . 'µs' : '') . '</h1> </li> </ul> <!-- JS --> <script type="text/javascript"> function plugin_wirerelay_change(element){ var state = $(element).hasClass(\'active\') ? 0 : 1 ; $.action( { action : \'wireRelay_manual_change_state\', engine: ' . $relay->id . ', state: state }, function(response){ $(element).toggleClass("active"); } ); } </script> '; } $response['content'] = $content; }); break; case 'wireRelay_edit_widget': require_once dirname(__FILE__) . '/../dashboard/Widget.class.php'; $widget = new Widget(); $widget = $widget->getById($_['id']); $data = $widget->data(); $relayManager = new WireRelay(); $relays = $relayManager->populate(); $content = '<h3>Relais ciblé</h3>'; if (count($relays) == 0) { $content = 'Aucun relais existant dans yana, <a href="setting.php?section=wireRelay">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 'wireRelay_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; } }
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; } }
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; } }