aboutsummaryrefslogtreecommitdiff
path: root/lib/App/MediaControl.rakumod
blob: a13c5384d7ce1cd676ec42ad5b988b848e71dfdb (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
use v6.d;
use DB::SQLite;
use ScanDir;
use Vlc::Client;
use Lirc::Client;
use Lirc::Commands;
use App::MediaControl::DB;
use App::MediaControl::Web;
 
class App::MediaControl {
    has $.config is required;
    has Vlc::Client $!vlc;
    has Lirc::Client $!lirc-client;
    has Lirc::Commands $!lirc;
    has App::MediaControl::DB $!db;
    has App::MediaControl::Web $!web;
 
    submethod TWEAK {
        $!vlc .= new(
            password => $!config<vlc><password>,
            base-uri => $!config<vlc><base-uri>,
            mrl-root => $!config<media><root>,
        );
 
        $!lirc-client .= new();
        $!lirc .= new(client=>$!lirc-client);
 
        $!db .= new(
            pool => DB::SQLite.new(
                filename => $!config<db><filename>,
                busy-timeout => 20_000,
            ),
        );
 
        $!web .= new(
            port => $!config<server><port>,
    host => $!config<server><host>,
            :$!vlc:$!lirc:$!db,
        );
    }
 
    method !start-scan() {
        my $root = $.config<media><root>;
        my $extensions = any($.config<media><extensions>.Slip);
 
        $!db.clear-seen();
        start react {
            whenever scan-dir($root-> $item {
                when $item ~~ $root {}
                when $item ~~ ScanDir::End { $!db.remove-unseen(); say "scan done" }
 
                next unless $item.d || $item.extension ~~ $extensions;
 
                my $path = $item.parent.relative($root);
                $path = '' if $path eq '.';
                my $name = $item.basename;
 
                if !$item.e {
                    $!db.remove-entry(:$path,:$name);
                }
                else {
                    my $is-dir = $item.d;
                    $!db.add-entry(:$path,:$name,:$is-dir);
                }
            }
        }
    }
 
    method start() {
        $!db.ensure-schema();
        self!start-scan();
        $!web.start();
    }
 
    method stop() {
        $!web.stop();
    }
}