diff options
Diffstat (limited to 'lib/App/MediaControl')
-rw-r--r-- | lib/App/MediaControl/Web.rakumod | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/App/MediaControl/Web.rakumod b/lib/App/MediaControl/Web.rakumod new file mode 100644 index 0000000..b2ac36e --- /dev/null +++ b/lib/App/MediaControl/Web.rakumod @@ -0,0 +1,58 @@ +use v6.d; +use Cro::HTTP::Server; +use Cro::HTTP::Router; +use Vlc::Client; +use Lirc::Commands; + +class App::MediaControl::Web { + has Vlc::Client $.vlc is required; + has Lirc::Commands $.lirc is required; + has Int $.port = 8080; + has Cro::Service $!service handles <stop>; + + method start() { + my $vlc = route { + post -> 'play' { await self.vlc.command('pl_play') } + post -> 'pause' { await self.vlc.command('pl_pause') } + post -> 'stop' { await self.vlc.command('pl_stop') } + + get -> 'status' { + my $status = await self.vlc.status(); + content 'application/json', $status; + } + } + + my $ir = route { + post -> $thing, $arg { + await self.lirc.send($thing, $arg); + } + } + + my $application = route { + resources-from %?RESOURCES; + + get -> { resource 'index.html' } + get -> 'ir.png' { resource 'ir.png' } + get -> 'ir.webmanifest' { resource 'ir.webmanifest' } + + include :$vlc, :$ir; + + around -> &handler { + handler(); + CATCH { + default { + note "BOOM $_"; + response.status = 500; + content 'application/json', %( error => "$_" ); + } + } + } + }; + + $!service = Cro::HTTP::Server.new( + :port(self.port), :$application, + ); + + return $!service.start(); + } +} |