Exemplo n.º 1
0
function kodiCmd_action_kodiCmd()
{
    global $_, $conf, $myUser;
    switch ($_['action']) {
        case 'kodiCmd_delete_kodiCmd':
            if ($myUser->can('kodi', 'd')) {
                $kodiManager = new KodiCmd();
                $kodiManager->delete(array('id' => $_['id']));
            }
            header('location:setting.php?section=kodiCmd');
            break;
        case 'kodiCmd_plugin_setting':
            $conf->put('plugin_kodiCmd_api_url_kodi', $_['api_url_kodi']);
            header('location: setting.php?section=preference&block=kodiCmd');
            break;
        case 'kodiCmd_add_kodiCmd':
            if ($myUser->can('kodi', 'c')) {
                $kodiCmd = new KodiCmd();
                $kodiCmd->setName($_['name']);
                $kodiCmd->setDescription($_['description']);
                $kodiCmd->setJson($_['kodiJsonCode']);
                $kodiCmd->setConfidence($_['confidence']);
                $kodiCmd->setRoom($_['room']);
                $kodiCmd->save();
            }
            header('location:setting.php?section=kodiCmd');
            break;
        case 'kodiCmd_edit_kodiCmd':
            if ($myUser->can('kodi', 'c')) {
                $kodiCmd = new KodiCmd();
                $kodiCmd = $kodiCmd->getById($_['id']);
                $kodiCmd->setName($_['name']);
                $kodiCmd->setDescription($_['description']);
                $kodiCmd->setJson($_['kodiJsonCode']);
                $kodiCmd->setConfidence($_['confidence']);
                $kodiCmd->setRoom($_['room']);
                $kodiCmd->save();
            }
            header('location:setting.php?section=kodiCmd');
            break;
        case 'kodiCmd_change_state':
            global $_, $myUser;
            if ($myUser->can('kodi', 'u')) {
                $kodiCmd = new KodiCmd();
                $kodiCmd = $kodiCmd->getById($_['engine']);
                $url = $conf->get('plugin_kodiCmd_api_url_kodi');
                $timeOut = $conf->get('plugin_kodiCmd_api_timeout_kodi');
                $recoStatus = $conf->get('plugin_kodiCmd_api_recognition_status');
                $reqJSON = str_replace(chr(34), '%22', '{"jsonrpc":"2.0",' . html_entity_decode($kodiCmd->getJson()) . '}');
                if ($recoStatus != '') {
                    $conf->put('plugin_kodiCmd_api_recognition_status', date('H:i:s'));
                }
                if ($recoStatus != '' || !isset($_['webservice'])) {
                    $affirmation = kodiCmd_send_json_request($url, $reqJSON, $timeOut);
                }
                if (!isset($_['webservice'])) {
                    if ($affirmation != "OK") {
                        echo "<script>\n                                                        alert('" . $affirmation . "');\n                                                        window.location.href = \"index.php?module=room&id=" . $kodiCmd->getRoom() . "\";\n                                                  </script>";
                    } else {
                        header('location:index.php?module=room&id=' . $kodiCmd->getRoom());
                    }
                } else {
                    if ($affirmation != "OK" && $recoStatus != '') {
                        $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;
        case 'kodiCmd_recognition_status':
            $url = $conf->get('plugin_kodiCmd_api_url_kodi');
            $timeOut = $conf->get('plugin_kodiCmd_api_timeout_kodi');
            switch ($_['state']) {
                case '1':
                    $conf->put('plugin_kodiCmd_api_recognition_status', date('H:i:s'));
                    $json = '"method":"GUI.ShowNotification","params":{"title":"Reconnaissance Vocale","message":"Activée"},"id":"1"';
                    $json = urlencode(htmlspecialchars_decode($json));
                    $reqJSON = str_replace(chr(34), '%22', '{"jsonrpc":"2.0",' . html_entity_decode($json) . '}');
                    $retJSON = kodiCmd_send_json_request($url, $reqJSON, $timeOut);
                    break;
                case '0':
                    $conf->put('plugin_kodiCmd_api_recognition_status', '');
                    $json = '"method":"GUI.ShowNotification","params":{"title":"Reconnaissance Vocale","message":"Désactivée"},"id":"1"';
                    $json = urlencode(htmlspecialchars_decode($json));
                    $reqJSON = str_replace(chr(34), '%22', '{"jsonrpc":"2.0",' . html_entity_decode($json) . '}');
                    $retJSON = kodiCmd_send_json_request($url, $reqJSON, $timeOut);
                    break;
            }
            break;
    }
}