App-RPi-EnvUI

 view release on metacpan or  search on metacpan

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

package App::RPi::EnvUI::API;

use strict;
use warnings;
use App::RPi::EnvUI::DB;
use App::RPi::EnvUI::Event;
use Carp qw(confess);
use Crypt::SaltedHash;
use Data::Dumper;
use DateTime;
use JSON::XS;
use Logging::Simple;
use Mock::Sub no_warnings => 1;
use RPi::Const qw(:all);

our $VERSION = '0.30';

# configure handlers

BEGIN {
    if ($ENV{SUPPRESS_WARN}){
        $SIG{__WARN__} = sub {};
    }
}

# mocked sub handles for when we're in testing mode
# readPin(), writePin() and pinMode() from wiringPi

our ($temp_sub, $hum_sub, $rp_sub, $wp_sub, $pm_sub);

# class variables

my $api;
my $master_log;
my $log;
my $sensor;
my $events;

# class variables for the light operation

our ($light_on_at, $light_on_hours);
our ($dt_now_test, $dt_light_on, $dt_light_off);
our $light_initialized = 0;

# public environment methods

sub new {

    # return the stored object if we've already run new()

    if (defined $api){
        $log->_5('returning stored API object');
        return $api if defined $api;
    }

    my $self = bless {}, shift;

    my $caller = (caller)[0];
    $self->_args(@_, caller => $caller);

    warn "API in test mode\n" if $self->testing;

    $self->_init;

    $api = $self;

    $log->_5("successfully initialized the system");

    if (! $self->testing && ! defined $events){
        $self->events;
        $log->_5('successfully created new async events')
    }
    else {
        $log->_5("async events have already been spawned");
    }

    return $self;
}
sub action_humidity {
    my ($self, $aux_id, $humidity) = @_;

    my $log = $log->child('action_humidity');
    $log->_6("aux: $aux_id, humidity: $humidity");

    my $limit = $self->_config_control('humidity_limit');
    my $min_run = $self->_config_control('humidity_aux_on_time');

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

    if (! $self->aux_override($aux_id) && $humidity != -1){
        if ($humidity < $limit && $self->aux_time($aux_id) == 0) {
            $log->_5("humidity limit reached turning $aux_id to HIGH");
            $self->aux_state($aux_id, HIGH);
            $self->aux_time($aux_id, time());
        }
        if ($humidity >= $limit && $self->aux_time($aux_id) >= $min_run) {
            $log->_5("humidity above limit setting $aux_id to LOW");

            $self->aux_state($aux_id, LOW);
            $self->aux_time($aux_id, 0);
        }
        $self->switch($aux_id);
    }
}
sub action_temp {
    my ($self, $aux_id, $temp) = @_;

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



( run in 0.696 second using v1.01-cache-2.11-cpan-5511b514fd6 )