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;
lib/App/RPi/EnvUI/API.pm view on Meta::CPAN
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);
$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 {
( run in 2.019 seconds using v1.01-cache-2.11-cpan-5a3173703d6 )