App-RPi-EnvUI

 view release on metacpan or  search on metacpan

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


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

    if (defined $time) {
        $self->db->update('aux', 'on_time', $time, 'id', $aux_id);
    }

    my $on_time = $self->aux($aux_id)->{on_time};
    my $on_length = time() - $on_time;
    return $on_time == 0 ? 0 : $on_length;
}
sub env {
    my ($self, $temp, $hum) = @_;

    if (@_ != 1 && @_ != 3){
        confess "env() requires either zero params, or two\n";
    }

    if (defined $temp){
        if ($temp !~ /^\d+$/){
            confess "env() temp param must be an integer\n";
        }
        if ($hum !~ /^\d+$/){
            confess "env() humidity param must be an integer\n";
        }
    }

    if (defined $temp){
        $self->db->insert_env($temp, $hum);
    }

    my $event_error = 0;

    if (! $self->testing){
        if ($self->{events}{env_to_db}->status == -1){
            $event_error = 1;
            print "event failure, restarting!\n";
            $self->{events}{env_to_db}->restart;
        }
    }

    my $ret = $self->db->env;

    return {temp => -1, humidity => -1, error => $event_error} if ! defined $ret;

    $ret->{error} = $event_error;

    return $ret;
}
sub graph_data {
    my ($self) = @_;

    my $graph_data = $self->db->graph_data;

    my $check = 1;
    my $count = 0;
    my %data;

    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);



( run in 0.528 second using v1.01-cache-2.11-cpan-39bf76dae61 )