| 1 | package Protocol::IR::Format::JSON; | ||||||||||||||||||||||||||||
| 2 | 55 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 55 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 55 | our $VERSION = '1.5'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 55 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 55 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 55 | ▶ | use File::Basename qw(dirname); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 55 | ▶ | use Protocol::IR::Code; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 55 | ▶ | use Protocol::IR::Format::Pronto; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Import for a proprietary JSON IR database dump. One command-set document | ||||||||||||||||||||||||||||
| 15 | # is a "commands" list; each command is: | ||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | # { | ||||||||||||||||||||||||||||
| 18 | # "keycode": "G:JVCO1 16 Bit:(Start)(0xF0D6)():3", | ||||||||||||||||||||||||||||
| 19 | # "name": "PowerToggle", | ||||||||||||||||||||||||||||
| 20 | # "protocol": "JVCO1 16 Bit", | ||||||||||||||||||||||||||||
| 21 | # "pronto": "0000 006D ..." | ||||||||||||||||||||||||||||
| 22 | # } | ||||||||||||||||||||||||||||
| 23 | # | ||||||||||||||||||||||||||||
| 24 | # The payload is raw Pronto hex, exactly as a wig carries, so these documents | ||||||||||||||||||||||||||||
| 25 | # import to the same code list a wig would. A *device* document in the same | ||||||||||||||||||||||||||||
| 26 | # dump carries no commands of its own, only a "codeset" path to the re-usable | ||||||||||||||||||||||||||||
| 27 | # command set shared by every device with those commands; the path is | ||||||||||||||||||||||||||||
| 28 | # relative to the dump root, so it is resolved by walking up from the device | ||||||||||||||||||||||||||||
| 29 | # file's own directory. The wig importer auto-detects both shapes, so the two | ||||||||||||||||||||||||||||
| 30 | # formats interchange freely at the converter entry point. Import-only: the | ||||||||||||||||||||||||||||
| 31 | # "keycode"/"protocol" strings are the dump's own opaque naming, so this | ||||||||||||||||||||||||||||
| 32 | # format is never exported. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 20 | ▶ | sub decode { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=17 SCAR=28.5
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 20 | my ($class, $input, $registry) = @_; | |||||||||||||||||||||||||||
| 36 | 20 | ▶ | die "No JSON input provided\n" unless defined $input; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 19 | my ($text, $path) = _read_input($input); | |||||||||||||||||||||||||||
| 39 | 19 | my $data = _decode_json($text); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # The repeat count is a property of the device (timing.pressMinRepeats), | ||||||||||||||||||||||||||||
| 42 | # not of an individual command or its keycode. Capture it from the | ||||||||||||||||||||||||||||
| 43 | # document we were handed before we possibly follow a code-set pointer. | ||||||||||||||||||||||||||||
| 44 | 17 | my $repeat_count = _device_repeat_count($data); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # A device document: follow its pointer to the shared command set. | ||||||||||||||||||||||||||||
| 47 | 17 | ▶ | if (ref $data->{commands} ne 'ARRAY' && defined $data->{codeset}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $data->{"commands"} ne "ARRAY" and defined $data->{"codeset"}
MC/DC100%2/2
ref $data->{"commands"} ne "ARRAY" and defined $data->{"codeset"}
ref $data->{"commands"} ne "ARRAY"
defined $data->{"codeset"}
Truth table100%3/3
A and B
A= ref $data->{"commands"} ne "ARRAY"B= defined $data->{"codeset"}
| |||||||||||||||||||||||||||||
| 48 | 4 | my $set = _find_codeset($data->{codeset}, $path); | |||||||||||||||||||||||||||
| 49 | 4 | ▶ | die "Cannot find the code set '$data->{codeset}'\n" unless defined $set; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 3 | $data = _decode_json(_read_input($set)); | |||||||||||||||||||||||||||
| 51 | # A code set reached through a device keeps that device's timing. | ||||||||||||||||||||||||||||
| 52 | 3 | ▶ | $repeat_count //= _device_repeat_count($data); | ||||||||||||||||||||||||||
|
Condition33%1/3
$repeat_count //= _device_repeat_count($data)
MC/DC0%0/2
$repeat_count //= _device_repeat_count($data)
$repeat_count
_device_repeat_count($data)
Truth table33%1/3
A //= B
A= $repeat_countB= _device_repeat_count($data)
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | die "JSON requires a commands list\n" | ||||||||||||||||||||||||||||
| 56 | 16 | ▶ | unless ref($data->{commands}) eq 'ARRAY' && @{ $data->{commands} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $data->{"commands"} eq "ARRAY" and @{$data->{"commands"};}
MC/DC100%2/2
ref $data->{"commands"} eq "ARRAY" and @{$data->{"commands"};}
ref $data->{"commands"} eq "ARRAY"
@{$data->{"commands"};}
Truth table100%3/3
A and B
A= ref $data->{"commands"} eq "ARRAY"B= @{$data->{"commands"};}
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 13 | my @decoded_codes; | |||||||||||||||||||||||||||
| 59 | 13 | for my $cmd (@{ $data->{commands} }) { | |||||||||||||||||||||||||||
| 60 | 23 | ▶ | die "JSON command must be an object\n" unless ref $cmd eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | # A command may carry no Pronto payload (nothing was ever captured | ||||||||||||||||||||||||||||
| 62 | # for it); nothing to convert, so skip rather than fail the import. | ||||||||||||||||||||||||||||
| 63 | 23 | ▶ | next unless defined $cmd->{pronto} && $cmd->{pronto} ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cmd->{"pronto"} and $cmd->{"pronto"} ne ""
MC/DC50%1/2
defined $cmd->{"pronto"} and $cmd->{"pronto"} ne ""
defined $cmd->{"pronto"}
$cmd->{"pronto"} ne ""
Truth table66%2/3
A and B
A= defined $cmd->{"pronto"}B= $cmd->{"pronto"} ne ""
| |||||||||||||||||||||||||||||
| 64 | die "JSON command is missing its name\n" | ||||||||||||||||||||||||||||
| 65 | 21 | ▶ | unless defined $cmd->{name} && $cmd->{name} ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cmd->{"name"} and $cmd->{"name"} ne ""
MC/DC50%1/2
defined $cmd->{"name"} and $cmd->{"name"} ne ""
defined $cmd->{"name"}
$cmd->{"name"} ne ""
Truth table66%2/3
A and B
A= defined $cmd->{"name"}B= $cmd->{"name"} ne ""
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 20 | my $code = eval { $registry->import_format('Pronto', $cmd->{pronto}) }; | |||||||||||||||||||||||||||
| 68 | 20 | ▶ | die "JSON command '" . $cmd->{name} . "' cannot be decoded: $@\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$@ or not defined $code
MC/DC50%1/2
$@ or not defined $code
$@
not defined $code
Truth table66%2/3
A or not(B)
A= $@B= not defined $code
| |||||||||||||||||||||||||||||
| 69 | if $@ || !defined $code; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 19 | $code->alias($cmd->{name}); | |||||||||||||||||||||||||||
| 72 | # pressMinRepeats is the repeat count for a single press; the keycode's | ||||||||||||||||||||||||||||
| 73 | # trailing ":N" is not a repeat count and is deliberately ignored. | ||||||||||||||||||||||||||||
| 74 | 19 | ▶ | $code->send_count($repeat_count) if defined $repeat_count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 19 | push @decoded_codes, $code; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 11 | return \@decoded_codes; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # The repeat count for a single press is the device's timing.pressMinRepeats | ||||||||||||||||||||||||||||
| 82 | # (the Harmony hub reads it, defaulting to 3). It is a property of the | ||||||||||||||||||||||||||||
| 83 | # device, not of an individual command or its keycode; the keycode's trailing | ||||||||||||||||||||||||||||
| 84 | # ":N" is a constant on 99.95% of keycodes and is not a repeat count. | ||||||||||||||||||||||||||||
| 85 | 17 | ▶ | sub _device_repeat_count { | ||||||||||||||||||||||||||
|
Subroutine
_device_repeat_count: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 86 | 17 | my ($data) = @_; | |||||||||||||||||||||||||||
| 87 | 17 | ▶ | return undef unless ref $data eq 'HASH' && ref $data->{timing} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $data eq "HASH" and ref $data->{"timing"} eq "HASH"
MC/DC50%1/2
ref $data eq "HASH" and ref $data->{"timing"} eq "HASH"
ref $data eq "HASH"
ref $data->{"timing"} eq "HASH"
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= ref $data->{"timing"} eq "HASH"
| |||||||||||||||||||||||||||||
| 88 | 3 | my $timing = $data->{timing}; | |||||||||||||||||||||||||||
| 89 | 3 | ▶ | return $timing->{pressMinRepeats} if exists $timing->{pressMinRepeats}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | return 3; # Harmony hub default when the field is absent | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 22 | ▶ | sub _decode_json { | ||||||||||||||||||||||||||
|
Subroutine
_decode_json: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 94 | 22 | my ($text) = @_; | |||||||||||||||||||||||||||
| 95 | 22 | $text =~ s/^\x{FEFF}//; # Strip UTF-8 BOM if present | |||||||||||||||||||||||||||
| 96 | 22 | my $data = eval { JSON::PP->new->utf8->decode($text) }; | |||||||||||||||||||||||||||
| 97 | 22 | ▶ | die "Invalid JSON: $@\n" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 21 | ▶ | die "JSON top level must be a JSON object\n" unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 20 | return $data; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # A device document names its code set relative to the dump root, not to the | ||||||||||||||||||||||||||||
| 103 | # device file, so try each directory from the device file upwards. | ||||||||||||||||||||||||||||
| 104 | 4 | ▶ | sub _find_codeset { | ||||||||||||||||||||||||||
|
Subroutine
_find_codeset: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 105 | 4 | my ($rel, $base) = @_; | |||||||||||||||||||||||||||
| 106 | 4 | ▶ | return undef unless defined $rel; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 4 | ▶ | return $rel if -f $rel; # already relative to the current directory | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 3 | ▶ | return undef unless defined $base; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | my $dir = dirname($base); | |||||||||||||||||||||||||||
| 111 | 3 | while (1) { | |||||||||||||||||||||||||||
| 112 | 11 | my $path = File::Spec->catfile($dir, $rel); | |||||||||||||||||||||||||||
| 113 | 11 | ▶ | return $path if -f $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 9 | ▶ | last if $dir eq File::Spec->rootdir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 8 | my $up = dirname($dir); | |||||||||||||||||||||||||||
| 116 | 8 | ▶ | last if $up eq $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 8 | $dir = $up; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 1 | return undef; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Treat $input as file content when it looks like multi-line data, otherwise | ||||||||||||||||||||||||||||
| 123 | # as a path to a file (falling back to treating it as a single-line JSON | ||||||||||||||||||||||||||||
| 124 | # string). Returns the text and, when the input was read from a file, the | ||||||||||||||||||||||||||||
| 125 | # path it came from. | ||||||||||||||||||||||||||||
| 126 | 22 | ▶ | sub _read_input { | ||||||||||||||||||||||||||
|
Subroutine
_read_input: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 127 | 22 | my ($input) = @_; | |||||||||||||||||||||||||||
| 128 | 22 | ▶ | return ($input, undef) if $input =~ /[\r\n]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 19 | ▶ | if (-f $input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 10 | ▶ | open my $fh, '<', $input or die "Cannot open JSON file '$input': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 10 | local $/; | |||||||||||||||||||||||||||
| 133 | 10 | my $text = <$fh>; | |||||||||||||||||||||||||||
| 134 | 10 | close $fh; | |||||||||||||||||||||||||||
| 135 | 10 | return ($text, $input); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 9 | return ($input, undef); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 55 | 1; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 NAME | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Protocol::IR::Format::JSON - proprietary JSON IR database dump import | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 VERSION | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | version 1.5 | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | use Protocol::IR::Converter; | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | my $converter = Protocol::IR::Converter->new(); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Import a command-set document (file path or JSON string) | ||||||||||||||||||||||||||||
| 157 | my $codes = $converter->import_format('JSON', 'codeset.json'); | ||||||||||||||||||||||||||||
| 158 | for my $code (@$codes) { | ||||||||||||||||||||||||||||
| 159 | print $code->alias . " (" . $code->protocol . ")\n"; | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # Import a device document, which points at its command set | ||||||||||||||||||||||||||||
| 163 | my $device = $converter->import_format('JSON', 'devices/JVC/EM40NF5.json'); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # The wig entry point accepts the same documents interchangeably | ||||||||||||||||||||||||||||
| 166 | my $also = $converter->import_format('wig', 'codeset.json'); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | C<Protocol::IR::Format::JSON> imports the JSON documents of a proprietary IR | ||||||||||||||||||||||||||||
| 171 | database dump. A command-set document is a C<commands> list, each entry | ||||||||||||||||||||||||||||
| 172 | carrying a C<name> and a raw C<pronto> Pronto hex payload plus opaque | ||||||||||||||||||||||||||||
| 173 | C<keycode>/C<protocol> strings. A device document instead carries a | ||||||||||||||||||||||||||||
| 174 | C<codeset> path pointing at the re-usable command set that every device with | ||||||||||||||||||||||||||||
| 175 | those commands shares; the path is relative to the dump root, so it is | ||||||||||||||||||||||||||||
| 176 | resolved by walking up from the device file's own directory. Either way the | ||||||||||||||||||||||||||||
| 177 | payloads are the Pronto hex a wig carries, so the imported codes are | ||||||||||||||||||||||||||||
| 178 | identical to what C<import_format('wig', ...)> would produce, and the wig | ||||||||||||||||||||||||||||
| 179 | importer accepts these shapes automatically. Commands with no Pronto payload | ||||||||||||||||||||||||||||
| 180 | are skipped rather than failing the import. Dies with a concrete reason if | ||||||||||||||||||||||||||||
| 181 | the JSON is malformed, a command is missing its name, or a payload cannot be | ||||||||||||||||||||||||||||
| 182 | decoded. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Import-only: the C<keycode>/C<protocol> strings are the dump's own naming, so | ||||||||||||||||||||||||||||
| 185 | the format is never exported. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head1 METHODS | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 decode | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | my $codes = $class->decode($input, $registry); | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Parses a file path or JSON string and returns an arrayref of | ||||||||||||||||||||||||||||
| 194 | L<Protocol::IR::Code> objects, one per command with a Pronto payload, with | ||||||||||||||||||||||||||||
| 195 | C<alias> set from the command C<name> and C<send_count> set from the | ||||||||||||||||||||||||||||
| 196 | device's C<timing.pressMinRepeats> when a device document is imported. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Source code: L<https://github.com/bwarden/perl-protocol-ir> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Bug reports and feature requests: L<https://github.com/bwarden/perl-protocol-ir/issues> | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Brett T. Warden <bwarden@cpan.org> | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | Copyright (c) 2026 Brett T. Warden | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | This library is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 213 | under the terms of the GNU General Public License as published by the | ||||||||||||||||||||||||||||
| 214 | Free Software Foundation; either version 2 of the License, or (at your | ||||||||||||||||||||||||||||
| 215 | option) any later version. | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||||||||||