Parse MRW and create JSON file
Previously, it was to parse MRW and create a YAML file.
Now, we need to add a method to parse MRW and create a JSON file.
Tested:
create YAML file:
./gen_led_groups.pl -i witherspoon.xml -o led.yaml
create JSON file:
./gen_led_groups.pl -i witherspoon.xml -o led.json
Signed-off-by: George Liu <liuxiwei@inspur.com>
Change-Id: I508d06d50163942a87a917895138c8d5b4f24b81
diff --git a/gen_led_groups.pl b/gen_led_groups.pl
index d294563..2cb82da 100755
--- a/gen_led_groups.pl
+++ b/gen_led_groups.pl
@@ -226,8 +226,19 @@
}
printDebug("\n======================================================================\n");
-# Generate the yaml file
-generateYamlFile();
+my $index = rindex($outputFile, ".");
+my $suffix = substr($outputFile, $index + 1);
+if (lc($suffix) eq "json")
+{
+ # Generate the JSON file
+ generateJSONFile();
+}
+else
+{
+ # Generate the yaml file
+ generateYamlFile();
+}
+
#------------------------------------END OF MAIN-----------------------
# Gven a '/' separated string, returns the leaf.
@@ -294,6 +305,90 @@
close $fh;
}
+sub generateJSONFile
+{
+ package LEDGroups;
+ use JSON;
+ my $JSON = JSON->new->utf8->pretty(1);
+ $JSON->convert_blessed(1);
+
+ sub led
+ {
+ my $class = shift;
+ my $self = {
+ group => shift,
+ members => shift,
+ };
+ bless $self, $class;
+ return $self;
+ }
+
+ sub member
+ {
+ my $class = shift;
+ my $self = {
+ name => shift,
+ Action => shift,
+ DutyOn => shift,
+ Period => shift,
+ Priority => shift,
+ };
+ bless $self, $class;
+ return $self;
+ }
+
+ sub TO_JSON {
+ return { %{ shift() } };
+ }
+
+ my $fileName = $outputFile;
+ open(my $fh, '>', $fileName) or die "Could not open file '$fileName' $!";
+
+ my @leds = ();
+ foreach my $group (sort keys %hashGroup)
+ {
+ my @members = ();
+ foreach my $led (sort keys %{ $hashGroup{$group} })
+ {
+ my $action;
+ my $dutyOn;
+ my $period;
+ my $priority;
+
+ if (exists $hashGroup{$group}{$led}{Action})
+ {
+ $action = $hashGroup{$group}{$led}{Action};
+ $action = substr($action, 1, length($action) - 2);
+ }
+
+ if (exists $hashGroup{$group}{$led}{DutyOn})
+ {
+ $dutyOn = $hashGroup{$group}{$led}{DutyOn};
+ }
+
+ if (exists $hashGroup{$group}{$led}{Period})
+ {
+ $period = $hashGroup{$group}{$led}{Period};
+ }
+
+ if (exists $hashGroup{$group}{$led}{Priority})
+ {
+ $priority = $hashGroup{$group}{$led}{Priority};
+ $priority = substr($priority, 1, length($priority) - 2);
+ }
+
+ my $m = member LEDGroups($led, $action, $dutyOn, $period, $priority);
+ push @members, $m;
+ }
+ my $l = led LEDGroups($group, \@members);
+ push @leds, $l;
+ }
+ my %ledJson = ('leds' => \@leds);
+ my $json = $JSON->canonical(1)->encode(\%ledJson);
+ print $fh $json;
+ close $fh;
+}
+
# Helper function to put debug statements.
sub printDebug
{