diff --git a/lib/AoGSmartHome_Items.pm b/lib/AoGSmartHome_Items.pm index d25f9343f..ee78ade96 100644 --- a/lib/AoGSmartHome_Items.pm +++ b/lib/AoGSmartHome_Items.pm @@ -214,6 +214,52 @@ package AoGSmartHome_Items; use Data::Dumper; use Storable; +#--------------Logging and debugging functions---------------------------------------- + +sub log { + my ($self, $str, $prefix) = @_; + + if( !defined( $prefix ) ) { + $prefix = '[AoG]: '; + } + $str = $self->break_long_str( $str, $prefix, 300 ); + + &main::print_log( $str ); +} + +sub debug { + my( $self, $level, $str ) = @_; + if( $main::Debug{aog} >= $level ) { + $level = 'D' if $level == 0; + $self->log( $str, "[AoG] D$level: " ); + } +} + +sub error { + my ($self, $str, $level ) = @_; + $self->log( $self, $str, "[AoG] ERROR: " ); +} + +sub dump { + my( $self, $obj, $maxdepth ) = @_; + $obj = $obj || $self; + $maxdepth = $maxdepth || 2; + my $dumper = Data::Dumper->new( [$obj] ); + $dumper->Maxdepth( $maxdepth ); + return $dumper->Dump(); +} + +#---------------------------------------------------------------------------------------------- + +sub get_mh_object { + my ($self, $realname) = @_; + my $mh_object = ::get_object_by_name($realname); + if( !defined $mh_object ) { + $self->error( "Invalid device $realname; ignoring AoG item." ); + } + return $mh_object; +} + sub set_state { my ( $self, $item, $state ) = @_; @@ -224,29 +270,24 @@ sub set_state { # Map state if there is a mapping defined $state = $item->{ lc($state) } if $item->{ lc($state) }; - print STDERR "[AoGSmartHome] Debug: set_state(name='$name' realname='$realname' sub='$sub' state='$state')\n" - if $main::Debug{'aog'} > 2; + $self->debug( 2, "set_state(name='$name' realname='$realname' sub='$sub' state='$state')" ); if ( $sub =~ /^voice[_-]cmd:\s*(.+)\s*$/ ) { my $voice_cmd = $1; $voice_cmd =~ s/[#!]/$state/; - print STDERR "[AoGSmartHome] Debug: running voice command \'$voice_cmd\'\n" - if $main::Debug{'aog'}; + $self->debug( 1, "running voice command \'$voice_cmd\'" ); + &main::run_voice_cmd("$voice_cmd"); return; } elsif ( ref $sub eq 'CODE' ) { - my $mh_object = ::get_object_by_name($realname); - if( !defined $mh_object ) { - &main::print_log("[AoGSmartHome] Invalid device $realname; ignoring AoG item."); - return undef; - } + my $mh_object = $self->get_mh_object($realname); + return undef if !defined $mh_object; - print STDERR "[AoGSmartHome] Debug: running sub $sub(set, $state)\n" - if $main::Debug{'aog'}; + $self->debug( 1, "running sub $sub(set, $state)" ); &{$sub}($mh_object, $state, 'AoGSmartHome'); return; @@ -256,18 +297,14 @@ sub set_state { # Treat as a MisterHouse object, using $sub as the 'set' function. # - my $mh_object = ::get_object_by_name($realname); - if( !defined $mh_object ) { - &main::print_log("[AoGSmartHome] Invalid device $realname; ignoring AoG item."); - return undef; - } + my $mh_object = $self->get_mh_object($realname); + return undef if !defined $mh_object; if ( $mh_object->can('is_dimmable') && $mh_object->is_dimmable && $state =~ /\d+/ ) { $state = $state . '%'; } - print STDERR "[AoGSmartHome] Debug: setting object $realname to state '$state'\n" - if $main::Debug{'aog'}; + $self->debug( 1, "setting object $realname to state '$state'" ); $mh_object->$sub( $state, 'AoGSmartHome' ); @@ -283,23 +320,22 @@ sub get_state { my $sub = $item->{'sub'}; my $statesub = $item->{'statesub'}; - print STDERR "[AoGSmartHome] Debug: get_state(name='$name' realname='$realname' statesub='$statesub')\n" - if $main::Debug{'aog'} > 2; + $self->debug( 2, "get state(name='$name' realname='$realname' statesub='$statesub')" ); if ( $sub =~ /^voice[_-]cmd:\s*(.+)\s*$/ ) { my $voice_cmd = $1; # FIXME -- "get" on voice command? Hhhmmm + $self->error( "get_state called on a voice command $realname" ); return qq["on":true,"bri":254]; } elsif ( ref $statesub eq 'CODE' ) { - my $mh_object = ::get_object_by_name($realname); - &main::print_log("[AoGSmartHome] Invalid device $realname; ignoring AoG item.") if ( !defined $mh_object ); - return undef if !defined $mh_object; + my $mh_object = $self->get_mh_object($realname); + return undef if !defined $mh_object; - my $debug = "[AoGSmartHome] Debug: get_state() running sub: $statesub('$realname') - "; + my $debug = "get_state() running sub: $statesub('$realname') - "; my $state = &{$statesub}($mh_object); - print STDERR "$debug returning - $state\n" if $main::Debug{'aog'}; + $self->debug( 1, "$debug returning - $state" ); return $state; } else { @@ -307,23 +343,22 @@ sub get_state { # Treat as a MisterHouse object, using $statesub as the 'state' function. # - my $mh_object = ::get_object_by_name($realname); - &main::print_log("[AoGSmartHome] Invalid device $realname; ignoring AoG item.") if ( !defined $mh_object ); - return undef if !defined $mh_object; + my $mh_object = $self->get_mh_object($realname); + return undef if !defined $mh_object; my $cstate = $mh_object->$statesub(); $cstate =~ s/\%//; my $type = $mh_object->get_type(); - my $debug = "[AoGSmartHome] Debug: get state() -- actual object state: $cstate, object type: $type, "; + my $debug = "get state($realname) -- actual object state: $cstate, object type: $type, "; if ( $type =~ /X10/i ) { $cstate = 'on' if $cstate =~ /\d+/ || $cstate =~ /dim/ || $cstate =~ /bright/; $debug .= "determined state: $cstate, "; } - $debug .= "$debug returning $cstate\n"; + $debug .= "$debug returning $cstate"; - print STDERR $debug if $main::Debug{'aog'} > 2; + $self->debug( 1, $debug ); return $cstate; } @@ -337,18 +372,17 @@ sub get_state_list { my $sub = $mode->{'sub'}; my $statelistsub = $mode->{'statelistsub'}; - print STDERR "[AoGSmartHome] Debug: get_state_list(name='$name' realname='$realname' sub='$sub')\n" - if $main::Debug{'aog'} > 2; + $self->debug( 2, "get_state_list(name='$name' realname='$realname' sub='$sub')" ); if ( ref $statelistsub eq 'CODE' ) { - my $mh_object = ::get_object_by_name($realname); + my $mh_object = $self->get_mh_object($realname); if( !defined $mh_object ) { $mh_object = $realname; } - my $debug = "[AoGSmartHome] Debug: get_state_list() running sub: $statelistsub('$realname') - "; + my $debug = "get_state_list() running sub: $statelistsub('$realname') - "; my @statelist = &{$statelistsub}($mh_object); - print STDERR "$debug returning - @statelist\n" if $main::Debug{'aog'}; + $self->debug( 1, "$debug returning - @statelist" ); return (@statelist); } else { @@ -356,12 +390,12 @@ sub get_state_list { # Treat as a MisterHouse object, using $statelistsub as the 'statelist' function. # - my $mh_object = ::get_object_by_name($realname); - &main::print_log("[AoGSmartHome] Invalid device $realname; ignoring AoG item.") if ( !defined $mh_object ); - return undef if !defined $mh_object; + my $mh_object = $self->get_mh_object($realname); + return undef if !defined $mh_object; + my $debug = "get_state_list() running sub: $statelistsub('$realname') - "; my @statelist = $mh_object->$statelistsub(); - print STDERR "$debug returning - @statelist\n" if $main::Debug{'aog'}; + $self->debug( 1, "$debug returning - @statelist" ); return (@statelist); } } @@ -377,11 +411,7 @@ sub new { my $restoredhash = retrieve($file); $self->{idmap} = $restoredhash->{idmap}; - if ( $main::Debug{'aog'} ) { - print STDERR "[AoGSmartHome] Debug: dumping persistent IDMAP:\n"; - print STDERR Dumper $self->{idmap}; - print STDERR "[AoGSmartHome] Debug: done.\n"; - } + $self->debug( 1, "dumping persistent IDMAP:\n" . $self->dump( $self->{idmap} ) ); } return $self; @@ -407,8 +437,8 @@ sub add { if( !$realname ) { - &main::print_log("[AoGSmartHome] Realname must be specified for persistent idmap; ignoring AoG item."); - return; + $self->error( "Realname must be specified for persistent idmap; ignoring AoG item." ); + return; } if ( !$name ) { @@ -430,15 +460,16 @@ sub add { $type = $value; # Check that the type is a supported one - if ($type ne 'light' && $type ne 'scene' && $type ne 'switch' - && $type ne 'outlet'&& $type ne 'thermostat' && $type ne 'multi_switch') { - &main::print_log("[AoGSmartHome] Invalid device type '$type'; ignoring AoG item."); + if( $type ne 'light' && $type ne 'scene' && $type ne 'switch' + && $type ne 'outlet'&& $type ne 'thermostat' && $type ne 'select' + ) { + $self->error( "Invalid device type '$type'; ignoring AoG item." ); return; } } elsif ($parm eq 'room') { $room = $value; } else { - &main::print_log("[AoGSmartHome] Invalid device property '$parm'; ignoring AoG item."); + $self->error( "Invalid device property '$parm'; ignoring AoG item." ); return; } } @@ -447,7 +478,7 @@ sub add { my $uuid = $self->uuid($realname); - if( $type eq 'multi_switch' ) { + if( $type eq 'select' ) { $self->add_mode_trait( $realname, $realname, 'Mode', $sub, undef, $statesub, 0 ); } $self->{'uuids'}->{$uuid}->{'realname'} = $realname; @@ -460,7 +491,7 @@ sub add { # If no device type is provided we default to 'light' $self->{'uuids'}->{$uuid}->{'type'} = $type; $self->{'uuids'}->{$uuid}->{'room'} = $room if $room; - print "[AoGSmartHome] Added AOG device:\n " . Dumper( $self->{'uuids'}->{$uuid} ) if $main::Debug{'aog'} > 2; + $self->debug( 2, "Added AOG device:\n " . $self->dump( $self->{'uuids'}->{$uuid} ) ); } =item C @@ -482,10 +513,10 @@ sub add_mode_trait { my ($type, $room); # AoG Smart Home Provider device properties - print "[AoGSmartHome] Adding AOG mode trait '$modename' as ($name) to $itemname:\n " . Dumper( $mode ) if $main::Debug{'aog'} > 2; + $self->debug( 2, "Adding AOG mode trait '$modename' as ($name) to $itemname:\n " . $self->dump( $mode ) ); my $uuid = $self->{'idmap'}->{objects}->{$itemname}; if( !$itemname || !$uuid ) { - &main::print_log("[AoGSmartHome] Add_mode_trait -- Itemname for existing AoG item must be specified; ignoring AoG mode."); + $self->error( "Add_mode_trait -- Itemname for existing AoG item must be specified; ignoring AoG mode." ); return; } @@ -514,7 +545,7 @@ sub add_mode_trait { $self->{'uuids'}->{$uuid}->{modes} = {} if !defined $self->{'uuids'}->{$uuid}->{modes}; $self->{'uuids'}->{$uuid}->{modes}->{$keyname} = $mode; - print "[AoGSmartHome] Added AOG mode trait '$modename' to $itemname:\n " . Dumper( $mode ) if $main::Debug{'aog'} > 2; + $self->debug( 2, "Added AOG mode trait '$modename' to $itemname:\n " . $self->dump( $mode ) ); } =item C @@ -545,6 +576,7 @@ EOF foreach my $uuid ( keys %{ $self->{'uuids'} } ) { my $type = $self->{'uuids'}->{$uuid}->{'type'}; + $self->debug( 1, "Object added: $self->{'uuids'}->{$uuid}->{'realname'}" ); if ( $type eq 'light' ) { $response .= <{'uuids'}->{$uuid}->{'realname'} ."\n"); - my $mh_object = ::get_object_by_name($self->{'uuids'}->{$uuid}->{'realname'}); - &main::print_log("[AoGSmartHome] Invalid device ".$self->{'uuids'}->{$uuid}->{'realname'}."; ignoring AoG item.") if ( !defined $mh_object ); + # the device has the "Brightness" trait. Done by calling + # is_dimmable method on the object. + + my $mh_object = $self->get_mh_object($self->{'uuids'}->{$uuid}->{'realname'}); return undef if !defined $mh_object; if( $mh_object->can('is_dimmable') && $mh_object->is_dimmable ) { @@ -634,12 +664,11 @@ EOF }, EOF } elsif ( $type eq 'thermostat') { - my $mh_object = ::get_object_by_name($self->{'uuids'}->{$uuid}->{'realname'}); - &main::print_log("[AoGSmartHome] Invalid device ".$self->{'uuids'}->{$uuid}->{'realname'}."; ignoring AoG item.") if ( !defined $mh_object ); + my $mh_object = $self->get_mh_object($self->{'uuids'}->{$uuid}->{'realname'}); return undef if !defined $mh_object; if (!$mh_object->isa('Insteon::Thermostat') ) { - &main::print_log("[AoGSmartHome] '$self->{'uuids'}->{$uuid}->{'realname'} is an unsupported thermostat; ignoring AoG item."); + $self->error( "'$self->{'uuids'}->{$uuid}->{'realname'}' is an unsupported thermostat; ignoring AoG item."); next; } @@ -691,7 +720,7 @@ EOF } }, EOF - } elsif ( $type eq 'multi_switch' ) { + } elsif ( $type eq 'select' ) { $response .= <debug( 2, "action.devices.SYNC response:\n$response" ); return &main::json_page($response); } @@ -756,8 +781,8 @@ sub sync_modes { my @mode_list = values %{$self->{'uuids'}->{$uuid}->{modes}}; -# print "[AoGSmartHome] Syncing modes on AOG device:\n " . Dumper( $self->{'uuids'}->{$uuid} ) if $main::Debug{'aog'} > 2; -# print "mode_list: " . Dumper( @mode_list ); + $self->debug( 2, "Syncing modes on AOG device:\n " . $self->dump( $self->{'uuids'}->{$uuid} ) ); + $self->debug( 2, " mode_list: " . $self->dump( @mode_list ) ); if( $do_attributes_clause ) { $response .= <get_state_list( $mode ); if( scalar(@statelist) == 0 ) { - &main::print_log("[AoGSmartHome] Invalid mode -- no state list ".$mode->{'realname'}."; ignoring AoG item."); + $self->error( "Invalid mode -- no state list $mode->{'realname'}; ignoring AoG item." ); next; } $response .= <debug( 2, "sync_modes response: \n$response" ); return $response; } @@ -869,8 +894,7 @@ EOF next; } elsif ( $self->{'uuids'}->{$uuid}->{'type'} eq 'thermostat' ) { - my $mh_object = ::get_object_by_name($self->{'uuids'}->{$uuid}->{'realname'}); - &main::print_log("[AoGSmartHome] Invalid device ".$self->{'uuids'}->{$uuid}->{'realname'}."; ignoring AoG item.") if ( !defined $mh_object ); + my $mh_object = $self->get_mh_object($self->{'uuids'}->{$uuid}->{'realname'}); return undef if !defined $mh_object; if ($mh_object->isa('Insteon::Thermostat') ) { @@ -919,14 +943,13 @@ EOF next; } - elsif ( $self->{'uuids'}->{$uuid}->{'type'} eq 'multi_switch' ) { - my $mh_object = ::get_object_by_name($self->{'uuids'}->{$uuid}->{'realname'}); - &main::print_log("[AoGSmartHome] Invalid device ".$self->{'uuids'}->{$uuid}->{'realname'}."; ignoring AoG item.") if ( !defined $mh_object ); + elsif ( $self->{'uuids'}->{$uuid}->{'type'} eq 'select' ) { + my $mh_object = $self->get_mh_object($self->{'uuids'}->{$uuid}->{'realname'}); next if !defined $mh_object; my $devstate = get_state( $self, $self->{'uuids'}->{$uuid} ); if ( !defined $devstate ) { - &main::print_log("[AoGSmartHome] Device ".$self->{'uuids'}->{$uuid}->{'realname'}." has no state; ignoring AoG item."); + $self->error( "Device $self->{'uuids'}->{$uuid}->{'realname'} has no state; ignoring AoG item."); $response .= <{'uuids'}->{$uuid}->{'realname'}); - &main::print_log("[AoGSmartHome] Invalid device ".$self->{'uuids'}->{$uuid}->{'realname'}."; ignoring AoG item.") if ( !defined $mh_object ); + + my $mh_object = $self->get_mh_object($self->{'uuids'}->{$uuid}->{'realname'}); return undef if !defined $mh_object; + if( $mh_object->can('is_dimmable') && $mh_object->is_dimmable ) { # INSTEON devices return "on" or "off". The AoG "Brightness" trait @@ -1014,11 +1038,7 @@ EOF } EOF - if ( $main::Debug{'aog'} ) { - print STDERR "$main::Time_Date [AoGSmartHome] Debug: action.devices.QUERY response:\n"; - print STDERR $response; - print STDERR "$main::Time_Date [AoGSmartHome] Debug: end of response.\n"; - } + $self->debug( 2, "action.devices.QUERY response: \n$response" ); return &main::json_page($response); } @@ -1027,24 +1047,25 @@ sub query_modes { my ( $self, $uuid, $exclude ) = @_; my $response = ''; - print "query_modes for device $uuid, excluding '$exclude'\n"; if( !defined $self->{'uuids'}->{$uuid}->{modes} ) { + $self->debug( 2, "query_modes for device $uuid, excluding '$exclude' -- no modes defined" ); return $response; } + my @modelist = values %{$self->{'uuids'}->{$uuid}->{modes}}; -# print "[AoGSmartHome] Querying modes on AOG device:\n " . Dumper( $self->{'uuids'}->{$uuid} ) if $main::Debug{'aog'} > 2; -# print "modelist: " . Dumper( @modelist ); + $self->debug( 2, "Querying modes on AOG device:\n " . $self->dump( $self->{'uuids'}->{$uuid} ) ); + $self->debug( 2, " modelist: " . $self->dump( @modelist ) ); $response .= <{'keyname'} eq $exclude ) { - print "Skipping '$exclude' in response\n"; + $self->debug( 2, "Skipping '$exclude' in response" ); next; } my $devstate = get_state( $self, $mode ); if ( !defined $devstate ) { - &main::print_log("[AoGSmartHome] Device ".$mode->{'realname'}." has no state; ignoring AoG item."); + $self->error( "Device $mode->{'realname'} has no state; ignoring AoG item."); next; } $response .= <{'devices'} } ) { - &main::print_log( "Received execute onoff command for $device->{'id'} -- " . $command->{'execution'}->[0]->{'params'}->{'on'} ) - if ( $main::Debug{'aog'} ); + $self->debug( 1, "Received execute onoff command for $device->{'id'} -- " . $command->{'execution'}->[0]->{'params'}->{'on'} ); set_state( $self, $self->{'uuids'}->{$device->{'id'}}, $turn_on ? 'on' : 'off' ); $response .= qq["$device->{'id'}",]; } @@ -1112,7 +1132,7 @@ sub execute_SetModes { my $response = ''; foreach my $device ( @{ $command->{'devices'} } ) { - &main::print_log( "Received execute command on $device->{id}: " . Dumper($command->{'execution'}->[0]->{'params'}->{'updateModeSettings'}) ); + $self->debug( 1, "Received execute command on $device->{id}: " . $self->dump($command->{'execution'}->[0]->{'params'}->{'updateModeSettings'}) ); foreach my $modekey ( keys %{$command->{'execution'}->[0]->{'params'}->{'updateModeSettings'}} ) { my $newvalue = $command->{'execution'}->[0]->{'params'}->{'updateModeSettings'}->{$modekey}; $newvalue =~ s/_key$//; @@ -1230,16 +1250,15 @@ sub execute_ThermostatX { foreach my $device ( @{ $command->{'devices'} } ) { my $realname = $self->{'uuids'}->{$device->{'id'} }->{'realname'}; - my $mh_object = ::get_object_by_name($realname); - &main::print_log("[AoGSmartHome] Invalid device $realname; ignoring AoG item.") if ( !defined $mh_object ); - return undef if !defined $mh_object; + my $mh_object = $self->get_mh_object($realname); + return undef if !defined $mh_object; if ($mh_object->isa('Insteon::Thermostat') ) { my $mode = lc($mh_object->get_mode); $mode = 'heatcool' if ($mode =~ /auto/); if ( $execution_command =~ /TemperatureSetpoint/ ) { - &main::print_log("[AoGSmartHome] Setting temp: ".&CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpoint'}) ) if ( $main::Debug{'aog'} ); + $self->debug( 1,"Setting temp: ".&CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpoint'}) ); my $setpoint = &CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpoint'}); if ( $mode eq 'cool') { $mh_object->cool_setpoint($setpoint); @@ -1247,7 +1266,7 @@ sub execute_ThermostatX { $mh_object->heat_setpoint($setpoint); } } elsif ( $execution_command =~ /ThermostatTemperatureSetRange/ ) { - &main::print_log("[AoGSmartHome] Setting cool: ".&CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpointHigh'})." Heat: ".&CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpointLow'})) if ( $main::Debug{'aog'} ); + $self->debug( 1, "Setting cool: ".&CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpointHigh'})." Heat: ".&CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpointLow'})); $mh_object->cool_setpoint( &CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpointHigh'}) ); $mh_object->heat_setpoint( &CtoF($command->{'execution'}->[0]->{'params'}->{'thermostatTemperatureSetpointLow'}) ); } elsif ( $execution_command =~ /ThermostatSetMode/ ) { @@ -1330,10 +1349,7 @@ sub execute { "commands": [ EOF - if ($main::Debug{'aog'} > 2) { - $Data::Dumper::Maxdepth = 0; - print "Aog received execute request:\n " . Dumper( $body ); - } + $self->debug( 2, "Aog received execute request:\n " . $self->dump( $body, 0 ) );; # # First, send the commands to all the devices specified in the request. # @@ -1366,11 +1382,7 @@ EOF } EOF - if ( $main::Debug{'aog'} ) { - print STDERR "[AoGSmartHome] Debug: action.devices.EXECUTE response:\n"; - print STDERR $response; - print STDERR "[AoGSmartHome] Debug: end of response.\n"; - } + $self->debug( 2, "action.devices.EXECUTE response: \n$response" ); return &main::json_page($response); } diff --git a/lib/http_server_aog.pl b/lib/http_server_aog.pl index cfcde2cde..9d7389aa9 100644 --- a/lib/http_server_aog.pl +++ b/lib/http_server_aog.pl @@ -41,12 +41,47 @@ =head2 METHODS # in $::config_parms{'aog_oauth_tokens_file'} and read on startup. my $oauth_tokens; + +#--------------Logging and debugging functions---------------------------------------- + +sub aog_log { + my ($str, $prefix) = @_; + + if( !defined( $prefix ) ) { + $prefix = '[AoG]: '; + } + &main::print_log( $prefix . $str ); +} + +sub aog_debug { + my($level, $str ) = @_; + if( $main::Debug{aog} >= $level ) { + $level = 'D' if $level == 0; + aog_log( $str, "[AoG] D$level: " ); + } +} + +sub aog_error { + my ($str, $level ) = @_; + aog_log( $str, "[AoG] ERROR: " ); +} + +sub aog_dump { + my( $obj, $maxdepth ) = @_; + $maxdepth = $maxdepth || 2; + my $dumper = Data::Dumper->new( [$obj] ); + $dumper->Maxdepth( $maxdepth ); + return $dumper->Dump(); +} + +#---------------------------------------------------------------------------------------------- + sub http_server_aog_startup { if ( !$::config_parms{'aog_enable'}) { - &main::print_log("[AoGSmartHome] AoG is disabled."); + &aog_log("AoG is disabled."); return; } else { - &main::print_log("\n[AoGSmartHome] AoG is enabled; will look for AoG requests via HTTP."); + &aog_log("AoG is enabled; will look for AoG requests via HTTP."); } # We don't want defaults for these important parameters so we disable @@ -56,8 +91,8 @@ sub http_server_aog_startup { || !defined $::config_parms{'aog_client_id'} || !defined $::config_parms{'aog_project_id'} ) { - print STDERR "[AoGSmartHome] AoG is enabled but one or more .ini file parameters are missing; disabling AoG!\n"; - print STDERR "[AoGSmartHome] Required .ini file parameters: aog_auth_path aog_fulfillment_url aog_client_id aog_project_id\n"; + &aog_error( "AoG is enabled but one or more .ini file parameters are missing; disabling AoG!" ); + &aog_error( "Required .ini file parameters: aog_auth_path aog_fulfillment_url aog_client_id aog_project_id" ); $::config_parms{'aog_enable'} = 0; return; } @@ -69,16 +104,11 @@ sub http_server_aog_startup { $oauth_tokens = retrieve( $::config_parms{'aog_oauth_tokens_file'} ); } - if ( $main::Debug{'aog'} ) { - print STDERR <{$t} eq $Authorized ) { - print "[AoGSmartHome] Debug: found token '$t' for user '$Authorized'\n" - if $main::Debug{'aog'}; + &aog_debug( 1, "found token '$t' for user '$Authorized'" ); $token = $t; last; } @@ -242,8 +269,7 @@ sub process_http_aog { } } - print "[AoGSmartHome] Debug: token for user '$Authorized' did not exist; generated token '$token'\n" - if $main::Debug{'aog'}; + &aog_debug( 1, "token for user '$Authorized' did not exist; generated token '$token'" ); store $oauth_tokens, $::config_parms{'aog_oauth_tokens_file'}; } @@ -251,7 +277,7 @@ sub process_http_aog { return http_redirect("$HTTP_ARGV{'redirect_uri'}#access_token=$token&token_type=bearer&state=$HTTP_ARGV{'state'}"); } elsif ( $uri eq $::config_parms{'aog_fulfillment_url'} ) { - print "[AoGSmartHome] Debug: Processing fulfillment request.\n" if $main::Debug{'aog'}; + &aog_debug( 1, "Processing fulfillment request." ); if ( !$Http{Authorization} || $Http{Authorization} !~ /Bearer (\S+)/ ) { return http_error("401 Unauthorized"); @@ -260,15 +286,10 @@ sub process_http_aog { my $received_token = $1; if ( exists $oauth_tokens->{$received_token} ) { - print "[AoGSmartHome] Debug: fulfillment request has correct token '$received_token' for user '$oauth_tokens->{$received_token}'\n" - if $main::Debug{'aog'}; + &aog_debug( 1, "fulfillment request has correct token '$received_token' for user '$oauth_tokens->{$received_token}'" ); } else { - &main::print_log("[AoGSmartHome] Incorrect token '$received_token' in fulfillment request!"); - - print "[AoGSmartHome] Debug: Incorrect token '$received_token' in fulfillment request!\n" - if $main::Debug{'aog'}; - + &aog_error( "Incorrect token '$received_token' in fulfillment request!" ); return http_error("401 Unauthorized"); }