Exemplo n.º 1
0
function update($arr)
{
    if (isset($arr['action'])) {
        $bdd = getBDD();
        $music = new Music();
        switch ($arr['action']) {
            case 'on':
                $music->on();
                break;
            case 'off':
                $music->off();
                break;
            case 'start':
                $music->start();
                break;
            case 'pause':
                $music->pause();
                break;
            case 'stop':
                $music->stop();
                break;
            case 'next':
                $music->next();
                break;
            case 'previous':
                $music->previous();
                break;
            case 'repeat':
                $music->repeat();
                break;
            case 'shuffle':
                $music->shuffle();
                break;
            case 'play':
                if (isset($arr['link'])) {
                    $link = $arr['link'];
                    $music->play($link);
                    echo 200;
                } else {
                    if (isset($arr['id'])) {
                        $id = $arr['id'];
                        $req = $bdd->prepare('SELECT file FROM at_music WHERE id = :id');
                        $req->execute(array('id' => $id));
                        $data = $req->fetch();
                        $music->start();
                        $path = str_replace(" ", "\\ ", $data['file']);
                        $music->play($path);
                        echo 200;
                    } else {
                        echo 404;
                    }
                }
                break;
            case 'vol':
                if (isset($arr['level'])) {
                    $vol = $arr['level'];
                    $vol *= 10;
                    $music->volume($vol);
                    echo 200;
                } else {
                    echo 404;
                }
                break;
            case 'state':
                if ($music->isPlay()) {
                    $result = array('state' => 1, 'vol' => $music->getVol());
                } else {
                    $result = array('state' => 0, 'vol' => $music->getVol());
                }
                echo json_encode($result);
                break;
            case 'playlistadd':
                if (isset($arr['playlist'], $arr['song'])) {
                    $playlist = $arr['playlist'];
                    $song = $arr['song'];
                    $req = $bdd->prepare('INSERT INTO at_songsplaylists VALUES("", :playlist, :song)');
                    $req->execute(array('playlist' => $playlist, 'song' => $song));
                    $req->closeCursor();
                    echo 200;
                } else {
                    echo 404;
                }
                break;
            case 'playlistremove':
                if (isset($arr['playlist'], $arr['song'])) {
                    $playlist = $arr['playlist'];
                    $song = $arr['song'];
                    $req = $bdd->prepare('DELETE FROM at_songsplaylists WHERE playlist = :playlist AND song = :song');
                    $req->execute(array('playlist' => $playlist, 'song' => $song));
                    $req->closeCursor();
                    echo 200;
                } else {
                    echo 404;
                }
                break;
            case 'playlistplay':
                if (isset($arr['playlist'])) {
                    $playlist = $arr['playlist'];
                    $music->clearPlaylist();
                    $req = $bdd->prepare('SELECT at_music.file FROM at_music JOIN at_songsplaylists ON at_music.id = at_songsplaylists.song WHERE at_songsplaylists.playlist = :playlist');
                    $req->execute(array('playlist' => $playlist));
                    while ($data = $req->fetch()) {
                        $music->add($data['file']);
                    }
                    $req->closeCursor();
                    $music->start();
                    echo 200;
                } else {
                    echo 404;
                }
                break;
            case 'playlistsongs':
                if (isset($arr['playlist'])) {
                    $playlist = $arr['playlist'];
                    $result = array();
                    $req = $bdd->prepare('SELECT at_music.file, at_music.length, at_music.id FROM at_music JOIN at_songsplaylists ON at_music.id = at_songsplaylists.song WHERE at_songsplaylists.playlist = :playlist');
                    $req->execute(array('playlist' => $playlist));
                    while ($data = $req->fetch()) {
                        $title = basename($data['file']);
                        $result[] = array('id' => $data['id'], 'title' => $title, 'length' => $data['length']);
                    }
                    $req->closeCursor();
                    echo json_encode($result);
                } else {
                    echo 404;
                }
                break;
        }
        http_response_code(202);
    } else {
        if (isset($arr['welcome'])) {
            setWelcomeMusic($arr['api'], $arr['welcome']);
        } else {
            http_response_code(404);
        }
    }
}