App-RPi-EnvUI

 view release on metacpan or  search on metacpan

lib/App/RPi/EnvUI/API.pm  view on Meta::CPAN


    my $log = $log->child('action_temp');

    my $limit = $self->_config_control('temp_limit');
    my $min_run = $self->_config_control('temp_aux_on_time');

    $log->_6("limit: $limit, minimum runtime: $min_run");

    if (! $self->aux_override($aux_id) && $temp != -1){
        if ($temp > $limit && $self->aux_time($aux_id) == 0){
            $log->_5("temp limit reached turning $aux_id to HIGH");
            $self->aux_state($aux_id, HIGH);
            $self->aux_time($aux_id, time);
        }
        elsif ($temp <= $limit && $self->aux_time($aux_id) >= $min_run){
            $log->_5("temp below limit setting $aux_id to LOW");
            $self->aux_state($aux_id, LOW);
            $self->aux_time($aux_id, 0);
        }
        $self->switch($aux_id);
    }
}
sub action_light {
    my ($self) = @_;

    #FIXME: remove set_light_times() from here and docs
    # - modify/remove the light times from the db and db API (and docs)
    # - figure out better method for class vars that belong in here

    my $log = $log->child('action_light');
    my $aux      = $self->_config_control('light_aux');
    my $pin      = $self->aux_pin($aux);
    my $override = $self->aux_override($aux);

    return if $override;

    my $dt_now = defined $dt_now_test
        ? $dt_now_test->set_time_zone('local')
        : DateTime->now->set_time_zone('local');

    if (! $light_initialized){

        $log->_5("initializing light");

        $light_on_hours = $self->_config_light('on_hours');

        $light_on_at = $self->_config_light('on_at');

        $log->_6("light on: $light_on_at, light hours: $light_on_hours");

        ($dt_light_on, $dt_light_off)
          = _init_light_time($dt_now, $light_on_at, $light_on_hours);
        
        $light_initialized = 1;
    }

    if ($light_on_hours == 24 || $dt_now > $dt_light_on){
        if (! $self->aux_state($aux)){
            $log->_6("turning light on");
            $self->aux_state($aux, ON);
            pin_mode($pin, OUTPUT);
            write_pin($pin, HIGH);
        }
    }

    if (! $light_on_hours || $dt_now > $dt_light_off){
        if ($self->aux_state($aux)){
            $log->_6("turning light off");
            $self->aux_state($aux, OFF);
            pin_mode($pin, OUTPUT);
            write_pin($pin, LOW);
            $dt_light_on = _set_light_on_time($dt_now, $light_on_at);
            $dt_light_off = _set_light_off_time($dt_light_on, $light_on_hours);
        }
    }
}
sub aux {
    my ($self, $aux_id) = @_;
    my $log = $log->child('aux');
    $log->_7("getting aux information for $aux_id");
    return $self->db->aux($aux_id);
}
sub auxs {
    my $self = shift;

    my $log = $log->child('auxs');
    $log->_7("retrieving all auxs");

    return $self->db->auxs;
}
sub aux_id {
    my ($self, $aux) = @_;

    my $log = $log->child('aux_id');
    $log->_7("aux ID is $aux->{id}");

    return $aux->{id};
}
sub aux_override {
    my $self = shift;
    # sets a manual override flag if an aux is turned on manually (via button)

    my ($aux_id, $override) = @_;

    my $log = $log->child('aux_override');

    if (! defined $aux_id || $aux_id !~ /^aux/){
        confess "aux_override() requires an aux ID as its first param\n";
    }

    if (defined $override){
        $log->_5("attempting override of aux: $aux_id");
        my $toggle = $self->aux($aux_id)->{toggle};

        if ($toggle != 1){
            $log->_5(
                "toggling of aux id $aux_id is disabled in the config file"
            );
            return -1;
        }

        $log->_5("override set operation called for $aux_id");
        $self->db->update('aux', 'override', $override, 'id', $aux_id);
        $log->_5("override set to $override for aux id: $aux_id");
    }

    return $self->aux($aux_id)->{override};
}
sub aux_pin {
    my $self = shift;

lib/App/RPi/EnvUI/API.pm  view on Meta::CPAN


    my $need = 5760 - @$graph_data; # approx 4 per min, for 24 hours (4*60*24)

    for (@$graph_data) {

        # we need to pad out to get to 24 hours worth of valid data

        if ($need){
            my $last_t = $_->[2];
            my $last_h = $_->[3];
            while($need){
                push @{ $data{temp} }, [ $count, $last_t ];
                push @{ $data{humidity} }, [ $count, $last_h ];
                $need--;
                $count++;
            }
        }
        
        next if $_->[2] > 120;
        last if $count == 300;
        push @{ $data{temp} }, [ $count, $_->[2] ];
        push @{ $data{humidity} }, [ $count, $_->[3] ];

        $count++;
        $check++;
    }

    return \%data;
}
sub humidity {
    my $self = shift;
    return $self->env->{humidity};
}
sub read_sensor {
    my $self = shift;

    my $log = $log->child('read_sensor');

    if (! defined $self->sensor){
        confess "\$self->{sensor} is not defined";
    }
    my $temp = $self->sensor->temp('f');
    my $hum = $self->sensor->humidity;

    $log->_6("temp: $temp, humidity: $hum");

    return ($temp, $hum);
}
sub switch {
    my ($self, $aux_id) = @_;

    my $log = $log->child('switch');

    my $state = $self->aux_state($aux_id);
    my $pin = $self->aux_pin($aux_id);

    if ($pin != -1){
        if (read_pin($pin) != $state){
            if ($state){
                $log->_6("set $pin state to HIGH");
                pin_mode($pin, OUTPUT);
                write_pin($pin, HIGH);
            }
            else {
                $log->_6("set $pin state to LOW");
                pin_mode($pin, OUTPUT);
                write_pin($pin, LOW);
            }
        }
        else {
            $log->_6("pin $pin state already set properly");
        }
    }
}
sub temp {
    my $self = shift;
    return $self->env->{temp};
}

# public core operational methods

sub auth {
    my ($self, $user, $pw) = @_;

    if (! defined $user){
        confess "\n\nauth() requires a username sent in\n\n";
    }

    if (! defined $pw){
        confess "\n\nauth() requires a password sent in\n\n";

    }
    my $csh = Crypt::SaltedHash->new(algorithm => 'SHA1');

    my $crypted = $self->db->user($user)->{pass};

    return $csh->validate($crypted, $pw);
}
sub events {
    my $self = shift;

    my $log = $log->child('events');

    $events = App::RPi::EnvUI::Event->new($self->testing);

    $self->{events}{env_to_db} = $events->env_to_db;
    $self->{events}{env_action} = $events->env_action;

    $self->{events}{env_to_db}->start;
    $self->{events}{env_action}->start;

    $log->_5("events successfully started");
}
sub log {
    my $self = shift;
    $master_log->file($self->log_file) if $self->log_file;
    $master_log->level($self->debug_level);
    return $master_log;
}
sub passwd {
    my ($self, $pw) = @_;

    if (! defined $pw){
        confess "\n\nplain text password string required\n\n";
    }



( run in 1.461 second using v1.01-cache-2.11-cpan-f56aa216473 )