diff options
author | dakkar <dakkar@thenautilus.net> | 2021-11-28 15:21:34 +0000 |
---|---|---|
committer | dakkar <dakkar@thenautilus.net> | 2021-11-28 15:21:34 +0000 |
commit | 10f2744658eeb4ccbf0342d707c08ecfb9623b91 (patch) | |
tree | c4933340cc82e6536bccf5622bc6873855a20d23 /lib/Vlc | |
parent | very rought start (diff) | |
download | media-control-10f2744658eeb4ccbf0342d707c08ecfb9623b91.tar.gz media-control-10f2744658eeb4ccbf0342d707c08ecfb9623b91.tar.bz2 media-control-10f2744658eeb4ccbf0342d707c08ecfb9623b91.zip |
move stuff around, add lirc client
Diffstat (limited to 'lib/Vlc')
-rw-r--r-- | lib/Vlc/App.rakumod | 33 | ||||
-rw-r--r-- | lib/Vlc/Client.rakumod | 46 |
2 files changed, 79 insertions, 0 deletions
diff --git a/lib/Vlc/App.rakumod b/lib/Vlc/App.rakumod new file mode 100644 index 0000000..e4401e6 --- /dev/null +++ b/lib/Vlc/App.rakumod @@ -0,0 +1,33 @@ +use v6.d; +use Cro::HTTP::Server; +use Cro::HTTP::Router; +use Vlc::Client; + +class Vlc::App { + has Vlc::Client $.vlc is required; + has Int $.port = 8080; + has Cro::Service $!service handles <stop>; + + method start() { + my $application = route { + resources-from %?RESOURCES; + + get -> { resource 'vlc.html' } + + 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; + } + }; + + $!service = Cro::HTTP::Server.new( + :port(self.port), :$application, + ); + + return $!service.start(); + } +} diff --git a/lib/Vlc/Client.rakumod b/lib/Vlc/Client.rakumod new file mode 100644 index 0000000..3620d47 --- /dev/null +++ b/lib/Vlc/Client.rakumod @@ -0,0 +1,46 @@ +use v6.d; +use Cro::HTTP::Client; +use Cro::Uri::HTTP; +use Cro::BodyParser::VlcXML; +use XML; + +multi sub maybe-bool('true') { True } +multi sub maybe-bool('false') { False } +multi sub maybe-bool($x) { $x } + +sub xml-to-hash(XML::Element $elem) { + if $elem.elements() -> @children { + return %( @children.map: { .name => xml-to-hash($_) } ) + } + + return maybe-bool($elem.contents().join('').trim) +} + +class Vlc::Client { + has Cro::HTTP::Client $!vlc; + has Str $.password is required; + has Str $.base-uri = 'http://127.0.0.1:8080/requests/'; + + method !call-vlc(Str $path, *%args) { + $!vlc ||= Cro::HTTP::Client.new( + auth => { :username(), :password(self.password) }, + add-body-parsers => [ Cro::BodyParser::VlcXML ], + ); + + state Cro::Uri::HTTP $base-uri .= parse(self.base-uri); + + return $!vlc.get( + $base-uri.add($path).add-query(|%args), + ); + } + + method command(Str $command, *%args) { + return self!call-vlc('status.xml', :$command, |%args); + } + + method status() { + my $res = await self!call-vlc('status.xml'); + my XML::Document $status = await $res.body; + return Promise.kept({:status(xml-to-hash($status.root))}) + } +} |