| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use FindBin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use lib "$FindBin::Bin/../lib"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Getopt::Long qw(GetOptions :config no_ignore_case); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Protocol::IR::Converter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Protocol::IR::Format::Tasmota; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | my $USAGE = <<'USAGE'; | |||||||||||||||||||||||||||
| 14 | ir-tasmota2wig - convert a Tasmota IR capture dump to a HAIR wig JSON file | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Reads a Tasmota console log (or a file of IRrecv lines) and converts every | ||||||||||||||||||||||||||||
| 17 | captured signal into HAIR's portable hair-wig/3 JSON format | ||||||||||||||||||||||||||||
| 18 | (https://github.com/DAB-LABS/HAIR): one wig file, one remote, raw Pronto | ||||||||||||||||||||||||||||
| 19 | hex as the signal payload. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Both Tasmota capture styles are recognized, on any mix of lines: | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | protocol-structured IRrecv: Protocol = NEC, Bits = 32, Data = 0x10EF00FF | ||||||||||||||||||||||||||||
| 24 | raw timing data IRrecv: RawData = +9185-4490+650-500+655dE-... | ||||||||||||||||||||||||||||
| 25 | IRrecv: RawData = 9185,4490,650,500,... | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | "IRsend <freq>,<rawdata>" command lines are accepted too. Timestamps and | ||||||||||||||||||||||||||||
| 28 | other log noise are ignored. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Captured signals have no button names, so each wig signal's alias (the key | ||||||||||||||||||||||||||||
| 31 | HAIR shows) is set to the signal's Data hex value -- e.g. 0x10EF00FF -- and | ||||||||||||||||||||||||||||
| 32 | is easy to rename after importing the wig in HAIR. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Usage: | ||||||||||||||||||||||||||||
| 35 | ir-tasmota2wig [options] <dump-file> | ||||||||||||||||||||||||||||
| 36 | cat dump.log | ir-tasmota2wig [options] | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Options: | ||||||||||||||||||||||||||||
| 39 | --name NAME Remote name for the wig (default: Untitled) | ||||||||||||||||||||||||||||
| 40 | --brand BRAND Manufacturer/brand | ||||||||||||||||||||||||||||
| 41 | --model MODEL Model | ||||||||||||||||||||||||||||
| 42 | --kind KIND HAIR device kind slug, e.g. tv, soundbar, ac | ||||||||||||||||||||||||||||
| 43 | --notes TEXT Free-form notes | ||||||||||||||||||||||||||||
| 44 | --out FILE Write the wig to FILE instead of stdout | ||||||||||||||||||||||||||||
| 45 | --overwrite Allow --out to overwrite an existing file | ||||||||||||||||||||||||||||
| 46 | --quiet Suppress the "skipped signals" warning | ||||||||||||||||||||||||||||
| 47 | -h, --help Show this help | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Examples: | ||||||||||||||||||||||||||||
| 50 | ir-tasmota2wig tasmota.log --name "Acme TV" --kind tv | ||||||||||||||||||||||||||||
| 51 | cat tasmota.log | ir-tasmota2wig --out remote.wig.json | ||||||||||||||||||||||||||||
| 52 | USAGE | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | my %opt; | |||||||||||||||||||||||||||
| 55 | GetOptions( | ||||||||||||||||||||||||||||
| 56 | 'name=s' => \$opt{name}, | ||||||||||||||||||||||||||||
| 57 | 'brand=s' => \$opt{brand}, | ||||||||||||||||||||||||||||
| 58 | 'model=s' => \$opt{model}, | ||||||||||||||||||||||||||||
| 59 | 'kind=s' => \$opt{kind}, | ||||||||||||||||||||||||||||
| 60 | 'notes=s' => \$opt{notes}, | ||||||||||||||||||||||||||||
| 61 | 'out=s' => \$opt{out}, | ||||||||||||||||||||||||||||
| 62 | 'overwrite!' => \$opt{overwrite}, | ||||||||||||||||||||||||||||
| 63 | 'quiet!' => \$opt{quiet}, | ||||||||||||||||||||||||||||
| 64 | 'help|h' => \$opt{help}, | ||||||||||||||||||||||||||||
| 65 | 2 | ▶ | ) or usage_error(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | if ($opt{help}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 1 | print $USAGE; | |||||||||||||||||||||||||||
| 69 | 1 | exit 0; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 1 | ▶ | usage_error("too many arguments") if @ARGV > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | ▶ | my $text = read_input(@ARGV ? $ARGV[0] : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | my $converter = Protocol::IR::Converter->new(); | |||||||||||||||||||||||||||
| 76 | 1 | my $codes = Protocol::IR::Format::Tasmota->decode_dump($text, $converter); | |||||||||||||||||||||||||||
| 77 | 1 | ▶ | die "No decodable IR signals in the input\n" unless @$codes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | my $total = count_signal_lines($text); | |||||||||||||||||||||||||||
| 80 | 1 | my $skipped = $total - @$codes; | |||||||||||||||||||||||||||
| 81 | 1 | ▶ | if ($skipped > 0 && !$opt{quiet}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$skipped > 0 and not $opt{"quiet"}
MC/DC0%0/2
$skipped > 0 and not $opt{"quiet"}
$skipped > 0
not $opt{"quiet"}
Truth table33%1/3
A and not(B)
A= $skipped > 0B= not $opt{"quiet"}
| |||||||||||||||||||||||||||||
| 82 | 0 | warn "Skipped $skipped of $total IR signal lines " | |||||||||||||||||||||||||||
| 83 | . "(unrecognized raw data or unsupported protocol)\n"; | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | ▶ | my %meta = (name => $opt{name} // 'Untitled', origin => 'converted:tasmota'); | ||||||||||||||||||||||||||
|
Condition50%1/2
$opt{"name"} // "Untitled"
MC/DC0%0/1
$opt{"name"} // "Untitled"
$opt{"name"}
Truth table50%1/2
A
A= $opt{"name"}
| |||||||||||||||||||||||||||||
| 87 | 1 | for my $k (qw(brand model kind notes)) { | |||||||||||||||||||||||||||
| 88 | 4 | ▶ | $meta{$k} = $opt{$k} if defined $opt{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 1 | my $wig = $converter->export_codes('wig', $codes, %meta); | |||||||||||||||||||||||||||
| 91 | 1 | write_output($wig, $opt{out}, $opt{overwrite}); | |||||||||||||||||||||||||||
| 92 | 1 | exit 0; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # ---------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | sub read_input { | ||||||||||||||||||||||||||
|
Subroutine
read_input: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||
| 97 | 1 | my ($file) = @_; | |||||||||||||||||||||||||||
| 98 | 1 | ▶ | if (defined $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | open my $fh, '<:raw', $file or die "Cannot open '$file': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 1 | local $/; | |||||||||||||||||||||||||||
| 101 | 1 | my $text = <$fh>; | |||||||||||||||||||||||||||
| 102 | 1 | close $fh; | |||||||||||||||||||||||||||
| 103 | 1 | return $text; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | local $/; | |||||||||||||||||||||||||||
| 106 | 0 | return scalar <STDIN>; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Number of IRrecv/IRsend signal lines, to warn about skipped ones. | ||||||||||||||||||||||||||||
| 110 | 1 | ▶ | sub count_signal_lines { | ||||||||||||||||||||||||||
|
Subroutine
count_signal_lines: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 111 | 1 | my ($text) = @_; | |||||||||||||||||||||||||||
| 112 | 1 | my $n = 0; | |||||||||||||||||||||||||||
| 113 | 1 | for my $line (split /\r?\n/, $text) { | |||||||||||||||||||||||||||
| 114 | 2 | ▶ | $n++ if $line =~ /IRrecv|IRsend/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 1 | return $n; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | sub write_output { | ||||||||||||||||||||||||||
|
Subroutine
write_output: called CC=5 SCAR=26.7
| |||||||||||||||||||||||||||||
| 120 | 1 | my ($text, $out, $overwrite) = @_; | |||||||||||||||||||||||||||
| 121 | 1 | ▶ | if (defined $out) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | die "Refusing to overwrite '$out' (use --overwrite)\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $out and not $overwrite
MC/DC0%0/2
-e $out and not $overwrite
-e $out
not $overwrite
Truth table0%0/3
A and not(B)
A= -e $outB= not $overwrite
| |||||||||||||||||||||||||||||
| 123 | if -e $out && !$overwrite; | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | open my $fh, '>:utf8', $out or die "Cannot write '$out': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | print {$fh} $text; | |||||||||||||||||||||||||||
| 126 | 0 | close $fh; | |||||||||||||||||||||||||||
| 127 | } else { | ||||||||||||||||||||||||||||
| 128 | 1 | binmode STDOUT, ':utf8'; | |||||||||||||||||||||||||||
| 129 | 1 | print $text; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub usage_error { | ||||||||||||||||||||||||||
|
Subroutine
usage_error: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 134 | 0 | my ($msg) = @_; | |||||||||||||||||||||||||||
| 135 | 0 | ▶ | print STDERR "$msg\n\n" if $msg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | print STDERR $USAGE; | |||||||||||||||||||||||||||
| 137 | 0 | exit 2; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||