package X10::Config; #------------------------------------------------------------------------------# # Libraries. # #------------------------------------------------------------------------------# use 5.006; use version; our $VERSION = qv('0.01'); use lib qw'lib/ ../lib'; use strict; use warnings; use Data::Dumper; #------------------------------------------------------------------------------# # Attributes. # #------------------------------------------------------------------------------# use IO::Handle; use X10::Zone; use X10::Item; my $configfile = "/home/mora/Projects/X10/NEWX10/X10/Config"; my $type = ""; my $name = ""; my $action = ""; #2345678901234567890123456789012345678901234567890 format outputFormatZone = @<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @<<<< $type, $name, $action . format outputFormatGroup = @<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @<<<< $type, $name, $action . format outputFormatArea = @<<<<<<<< @<<<<<<<<<<<<<<<<<<<< $type, $name . format outputFormatItem = @<<<<<<<< @<<<<<<<<<<<<<<<<<<<< @<<<< $type, $name, $action . sub new { my ($self) = {}; $self->{HOUSE} = X10::Zone->new(); bless($self); return $self } sub loadConfig { my $configfile = shift; my $house = shift; my $prevStruct = ''; my $struct = $house; open(INFILE, "<$configfile"); while() { chomp(); s/^\s+//g; s/\s+/ /; ($type,$name,$action) = split / /, $_; /ZONE|GROUP|AREA/ && do { $prevStruct = $struct; $struct = X10::Zone->new( type => $type, parent => $prevStruct, name => $name, hu => $action, ); }; /BATTERY/ && do { $struct->{battery} = $action; }; /SECURITY/ && do { $struct->{security} = $action; }; /STATE/ && do { $struct->{state} = $action; }; /LIGHT|FAN|CAMERA|CHRISTMAS/ && do { my $item = X10::Item->new( parent => $struct, type => $type, name => $name, state => 'OFF', hu => $action, dimlevel => 0, ); }; } close(INFILE); return $house; } sub dumpList { my $self = shift; my $space = shift; my $fh = shift; foreach my $type (values %{$self->list}) { printf $fh "%s%s %s %s\n", $space,$type->type,$type->name,$type->hu; my $tspace = "$space "; printf $fh "%sSECURITY %s %s\n", $tspace,$type->name,$type->security; printf $fh "%sSTATE %s %s\n", $tspace,$type->name,$type->state; printf $fh "%sBATTERY %s %s\n", $tspace,$type->name,$type->battery; if ($type->type =~ /ZONE|GROUP/) { &dumpList($type,$tspace,$fh); } elsif ( $type->type =~ /AREA/ ) { foreach my $item (values %{$type->list}) { printf $fh "%s%s %s %s\n", $tspace,$item->type,$item->name,$item->hu; } } } } sub dumpConfig { my $configfile = shift; my $house = shift; open(my $fh, ">$configfile"); my $space = ""; &dumpList($house,$space,$fh); close($fh); } sub printConfig { my $configfile = shift; my $house = shift; open(OUTFILE, ">$configfile"); select(OUTFILE); foreach my $struct (values %{$house->list}) { $type = $struct->type; $name = $struct->name; $action = $struct->hu; if ( $struct->type =~ /BATTERY|SECURITY|STATE|MOTION|LIGHT|FAN|CAMERA|CHRISTMAS/ ) { if ( $struct->type =~ /BATTERY/ ) { $action = $struct->battery; } elsif ( $struct->type =~ /SECURITY/ ) { $action = $struct->security; } elsif ( $struct->type =~ /STATE/ ) { $action = $struct->state; } elsif ( $struct->type =~ /MOTION|LIGHT|FAN|CAMERA|CHRISTMAS/ ) { $action = $struct->hu; } write(OUTFILE); } if ( $struct->type =~ /ZONE/ ) { select((select(OUTFILE), $~ = "outputFormatZone")[0]); write(OUTFILE); } if ( $struct->type =~ /GROUP/ ) { select((select(OUTFILE), $~ = "outputFormatGroup")[0]); write(OUTFILE); } if ( $struct->type =~ /AREA/ ) { select((select(OUTFILE), $~ = "outputFormatArea")[0]); write(OUTFILE); select((select(OUTFILE), $~ = "outputFormatItem")[0]); } } # close($fh); } sub defaultConfig { my ($self) = shift; my $master = X10::Zone->new(); $master->type('ZONE'); $master->parent(undef); $master->name('Master'); $master->security('OFF'); $master->state('OFF'); $master->battery('20180410'); $master->hu('G1'); my $outdoors = X10::Zone->new(); $outdoors->type('ZONE'); $outdoors->parent($master); $outdoors->name('Outdoor'); $outdoors->security('OFF'); $outdoors->state('OFF'); $outdoors->battery('20180410'); $outdoors->hu('G2'); my $indoors = X10::Zone->new(); $indoors->type('ZONE'); $indoors->parent($master); $indoors->name('Indoor'); $indoors->security('OFF'); $indoors->state('OFF'); $indoors->battery('20180410'); $indoors->hu('G3'); my $garage = X10::Zone->new(); $garage->type('ZONE'); $garage->parent($master); $garage->name('Garage'); $garage->security('OFF'); $garage->state('OFF'); $garage->battery('20180410'); $garage->hu('G4'); $master->add_to_list($outdoors->name, $outdoors); $master->add_to_list($indoors->name, $indoors); $master->add_to_list($garage->name, $garage); my $frontdoor = X10::Zone->new(); $frontdoor->type('GROUP'); $frontdoor->parent($outdoors); $frontdoor->name('Frontdoor'); $frontdoor->security('OFF'); $frontdoor->state('OFF'); $frontdoor->battery('20180410'); $frontdoor->hu('G5'); my $deck = X10::Zone->new(); $deck->type('GROUP'); $deck->parent($outdoors); $deck->name('Deck'); $deck->security('OFF'); $deck->state('OFF'); $deck->battery('20180410'); $deck->hu('G6'); my $basement = X10::Zone->new(); $basement->type('GROUP'); $basement->parent($outdoors); $basement->name('Basement'); $basement->security('OFF'); $basement->state('OFF'); $basement->battery('20180410'); $basement->hu('G7'); $outdoors->add_to_list($frontdoor->name, $frontdoor); $outdoors->add_to_list($deck->name, $deck); $outdoors->add_to_list($basement->name, $basement); my $attic = X10::Zone->new(); $attic->type('GROUP'); $attic->parent($indoors); $attic->name('Attic'); $attic->security('OFF'); $attic->state('OFF'); $attic->battery('20180410'); $attic->hu('G8'); my $firstfloor = X10::Zone->new(); $firstfloor->type('GROUP'); $firstfloor->parent($indoors); $firstfloor->name('FirstFloor'); $firstfloor->security('OFF'); $firstfloor->state('OFF'); $firstfloor->battery('20180410'); $firstfloor->hu('G9'); my $secondfloor = X10::Zone->new(); $secondfloor->type('GROUP'); $secondfloor->parent($indoors); $secondfloor->name('SecondFloor'); $secondfloor->security('OFF'); $secondfloor->state('OFF'); $secondfloor->battery('20180410'); $secondfloor->hu('G10'); my $Basement = X10::Zone->new(); $Basement->type('GROUP'); $Basement->parent($indoors); $Basement->name('Basement'); $Basement->security('OFF'); $Basement->state('OFF'); $Basement->battery('20180410'); $Basement->hu('G11'); $indoors->add_to_list($attic->name, $attic); $indoors->add_to_list($firstfloor->name, $firstfloor); $indoors->add_to_list($secondfloor->name, $secondfloor); $indoors->add_to_list($Basement->name, $Basement); my $Attic = X10::Zone->new(); $Attic->type('GROUP'); $Attic->parent($attic); $Attic->name('Attic'); $Attic->security('OFF'); $Attic->state('OFF'); $Attic->battery('20180410'); $Attic->hu('G12'); $Attic->add_to_list($Attic->name, $Attic); my $entryway = X10::Zone->new(); $entryway->type('AREA'); $entryway->parent($firstfloor); $entryway->name('EntryWay'); $entryway->security('OFF'); $entryway->state('OFF'); $entryway->battery('20180410'); $entryway->hu('G13'); my $entrywaylight = X10::Item->new(); $entrywaylight->parent($entryway); $entrywaylight->type('LIGHT'); $entrywaylight->name('Entryway Light'); $entrywaylight->state('OFF'); $entrywaylight->hu('N1'); $entrywaylight->dimlevel(0); $entryway->add_to_list($entrywaylight->name, $entrywaylight); my $livingroom = X10::Zone->new(); $livingroom->type('AREA'); $livingroom->parent($firstfloor); $livingroom->name('Livingroom'); $livingroom->security('OFF'); $livingroom->state('OFF'); $livingroom->battery('20180410'); $livingroom->hu('G14'); $firstfloor->add_to_list($entryway->name, $entryway); $firstfloor->add_to_list($livingroom->name, $livingroom); print Dumper $master; return $master; }