-
Notifications
You must be signed in to change notification settings - Fork 1
/
find-zero-byte.pl
executable file
·146 lines (115 loc) · 3.26 KB
/
find-zero-byte.pl
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/perl
use warnings;
use strict;
# Adjust this to $PREFIX/share/perl/5.10.1 (or whatever perl version)
use lib '/opt/mythtv/master/share/perl/5.10.1';
use MythTV;
# Fill in this hash with cardid => serial number for each HDPVR, power device
# pair. If a zero-byte file is detected from any device in here, its power
# will be turned off for 15s, then back on
my %hdpvrs = ( 32 => "GHUCYEL8" );
my $Myth = new MythTV();
# $0 cardid chanid starttime
my ($cardid, $chanid, $starttime, $cancel) = @ARGV;
die "You must provide cardid, chanid, starttime!\n"
if (!$cardid || !$chanid || !$starttime);
my $recording = undef;
my $delay = 30;
my $runningfile = "/tmp/$cardid-$chanid-$starttime.running";
my $cancelfile = "/tmp/$cardid-$chanid-$starttime.cancel";
if ((defined $cancel) && ($cancel eq "cancel"))
{
touch($cancelfile) if -f $runningfile;
exit 0;
}
touch($runningfile);
# Give the HDPVR time to start capturing
my $count = 0;
while ($count < 20) {
cleanup(0) if (-f $cancelfile);
$count++;
sleep 1;
}
while ((!$recording || !(exists $recording->{'local_path'}) ||
!(-f $recording->{'local_path'})) && $delay )
{
$recording = $Myth->new_recording($chanid, myth_to_unix_time($starttime));
if (!$recording || !(exists $recording->{'local_path'}) ||
!(-f $recording->{'local_path'}) )
{
if($recording) {
print $recording;
}
sleep 5;
cleanup(0) if (-f $cancelfile);
$delay -= 5;
if (!$delay) {
no_recording($cardid, $chanid, $starttime);
cleanup(2);
}
}
}
# If we get here, we have a recording.
if (-f $recording->{'local_path'})
{
# And it's local. Wait 10s if 0 bytes, then check again for 0 byte size.
my $delay = 1;
my $filesize = 0;
while( !$filesize && $delay >= 0 )
{
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) =
stat($recording->{'local_path'});
$filesize = $size;
if ($size != 0)
{
cleanup(0);
}
sleep 10;
cleanup(0) if (-f $cancelfile);
$delay--;
}
# #@$$@%ing zero byte file!
zero_byte($cardid, $chanid, $starttime);
cleanup(1);
}
cleanup(0);
sub touch
{
my $file = shift;
open FH, ">", $file;
print FH "\n";
close FH;
}
sub cleanup
{
my $exitcode = shift;
unlink $runningfile if -f $runningfile;
unlink $cancelfile if -f $cancelfile;
exit $exitcode;
}
sub no_recording
{
my ($cardid, $chanid, $starttime) = @_;
my $ishdpvr = exists $hdpvrs{$cardid};
print "No recording for channel $chanid at $starttime\n";
print "Recording " . ($ishdpvr ? "" : "not ") . "from HD-PVR.\n";
if ( $ishdpvr )
{
my @command = ( "hdpvr-power", $hdpvrs{$cardid}, "cycle" );
system @command;
}
}
sub zero_byte
{
my ($cardid, $chanid, $starttime) = @_;
my $ishdpvr = exists $hdpvrs{$cardid};
print "Zero byte recording for channel $chanid at $starttime\n";
print "Recording " . ($ishdpvr ? "" : "not ") . "from HD-PVR.\n";
if ( $ishdpvr )
{
my @command = ( "hdpvr-power", $hdpvrs{$cardid}, "cycle" );
system @command;
}
}
# vim:ts=4:sw=4:ai:et:si:sts=4