Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: Logical ways to calculate being within two times by stevieb
in thread Logical ways to calculate being within two times by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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 or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (None)
    As of 2022-01-25 23:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      In 2022, my preferred method to securely store passwords is:












      Results (69 votes). Check out past polls.

      Notices?