For completeness, and for a bit of content on the typically lower volume Sunday...
Set the time, called only once, upon initial loading of the webapp:
$api->set_light_times;
Sets the light timers... once on initialization of the app as a whole, and then again every time the light-off is triggered:
sub set_light_times {
my ($self) = @_;
my $on_at = $self->_config_light('on_at');
my $time = time;
$time += 60 until localtime($time) =~ /$on_at:/;
my $hrs = $self->_config_light('on_hours');
my $on_time = $time;
my $off_time = $on_time + $hrs * 3600;
my $now = time;
# check if the until() loop set things beyond today's
# on period
if ($now > ($on_time - 86400) && $now < ($off_time - 86400)){
$on_time -= 24 * 3600;
$off_time -= 24 * 3600;
}
$self->db()->update(
'light', 'value', $on_time, 'id', 'on_time'
);
$self->db()->update(
'light', 'value', $off_time, 'id', 'off_time'
);
}
...and the action logic itself:
sub action_light {
my ($self) = @_;
my $log = $log->child('action_light');
my $on_hours = $self->_config_light('on_hours');
my $aux = $self->_config_control('light_aux');
my $pin = $self->aux_pin($aux);
my $on_time = $self->_config_light('on_time');
my $off_time = $self->_config_light('off_time');
my $now = time;
if (($on_hours == 24) || ($now > $on_time && $now < $off_time)){
if (! $self->aux_state($aux)){
$self->aux_state($aux, ON);
pin_mode($pin, OUTPUT);
write_pin($pin, HIGH);
}
}
elsif ($self->aux_state($aux)){
$self->aux_state($aux, OFF);
pin_mode($pin, OUTPUT);
write_pin($pin, LOW);
$self->set_light_times;
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.