aboutsummaryrefslogtreecommitdiff
path: root/lib/App/MediaControl/Web.rakumod
blob: 77d8e05b5857c04388642daa09283e8f7375a457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use v6.d;
use Cro::HTTP::Server;
use Cro::HTTP::Router;
use Cro::WebApp::Template;
use Vlc::Client;
use Lirc::Commands;
use App::MediaControl::DB;
 
class App::MediaControl::Web {
    has Vlc::Client $.vlc is required;
    has Lirc::Commands $.lirc is required;
    has App::MediaControl::DB $.db is required;
    has Int $.port = 8080;
    has Str $.host = '*';
    has Cro::Service $!service handles <stop>;
 
    method start() {
        my $vlc = route {
            post -> 'play' { await self.vlc.command('pl_play'}
            post -> 'play'Int:D $id {
                my $file = self.db.get-entry($id);
                await self.vlc.play-file(|%(
                    $file<path name>:p # no comma! 
                ));
                self.db.mark-entry-watched($id);
            }
            post -> 'pause' { await self.vlc.command('pl_pause'}
            post -> 'stop' { await self.vlc.command('pl_stop'}
            post -> 'subs' { await self.vlc.command('key'val=>'subtitle-track'}
            post -> 'audio' { await self.vlc.command('key'val=>'audio-track'}
            post -> 'seek':$val { await self.vlc.command('seek',:$val}
            post -> 'snapshot':$val { await self.vlc.command('key',val=>'snapshot'}
 
            get -> 'status' {
                my $status = await self.vlc.status();
                my $playlist = await self.vlc.playlist();
                content 'application/json', %:$status:$playlist );
            }
        }
 
        my $ir = route {
            post -> $thing, $arg {
                await self.lirc.send($thing, $arg);
            }
        }
 
        my $media = route {
            get -> $id=Nil {
                my %reply = children => @(self.db.get-children-of($id));
                with $id {
                    %reply<parents> = self.db.get-parents-of($id);
                };
                content 'application/json'%reply;
            }
            get -> 'recent' {
                content 'application/json'@(self.db.get-recently-watched-folders());
            }
        };
 
        my $application = route {
            resources-from %?RESOURCES;
            templates-from-resources;
 
            get -> { resource 'index.html' }
            get -> 'ir.png' { resource 'ir.png' }
            get -> 'ir.webmanifest' {
                my $root = request.header('ProxyMountRoot'|| '/';
                template 'ir.webmanifest', %:$root );
            }
 
            include :$vlc:$ir:$media;
 
            around -> &handler {
                handler();
                CATCH {
                    default {
                        note "BOOM $_";
                        response.status = 500;
                        content 'application/json', %error => "$_" );
                    }
                }
            }
        };
 
        $!service = Cro::HTTP::Server.new(
            :host(self.host), :port(self.port),
            :$application,
        );
 
        return $!service.start();
    }
}