Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've been experiencing some trouble with some clock timing code and its associated code that sets the times in my indoor grow room automation software, and am looking for some assistance in completely re-writing it.

I have to admit that what I thought was going to be a simple task ended up (at least to me) not being simple at all. The below attempt at the refactor uses DateTime objects to perform the math. It seems like it has to do a lot of expensive work, and I'm hoping the Monks can point out and/or help me devise something that's more efficient in terms of the work being done. Note that the light timer routine is run every 15 seconds in an async event, so it most definitely is not just a one-off process.

An example of what I'm trying to achieve:

  • light needs to turn on at a certain time (1800 hrs localtime in the example below)
  • light needs to remain on for a certain number of hours (12 in the example, simulating the flowering season)
  • after the on time is exceeded, light needs to turn off until next on time

One problem I experienced with my existing code was that I was searching for the on-time with a regex using localtime(): if (localtime(time) =~ /$on_time:/){...}. If the power went out and came back on say at 1805 hrs, the light would not trigger, nor the future date would be set. This is a significant and potentially fatal problem for the plants (it can cause the females enough stress to go hermaphrodite) if this isn't noticed within a day or two. This new code does not check the time in the same way, and it eliminates this (single) problem.

Am I way overthinking this here? Perhaps I've been staring at it for too long and am blinded to something simply obvious. Or, maybe not, and this is a good way to achieve what I want and there's just a more efficient and less costly way. Or perhaps it's perfect, and I shouldn't change anything. I have laid out the code so it doesn't wrap here on PM, and added comments in hopes to make it easier to follow as it's not just a simple code block.

Thoughts?

use warnings; use strict; use feature 'say'; use DateTime; # this is a prototype of a light timer my $on_at = '18:00'; # lights on my $on_time = 12; # hours on for my $off_time; my $on = 0; # light is on bool my $t = time; my $count = 0; # iter count my $done = 20000; # num iterations # dt objects my ($t1, $on_at_dt, $off_at_dt) = init_time($on_at, $on_time); while (1){ # on if ($t1 > $on_at_dt && ! $on){ # light is not on, and it's time $on = 1; say "\non: $t1"; } # off if ($t1 > $off_at_dt && $on){ # light's out time $on = 0; say "off: $t1"; $on_at_dt = set_on_timer( $t1, $on_at_dt, $on_at ); $off_at_dt = set_off_timer( $on_at_dt, $off_at_dt, $on_time ); } # expedite the current time for testing $t1->add(seconds => 30); $count++; last if $count == $done; } sub init_time { my ($on_at, $on_time) = @_; my $t1 = DateTime->now; $t1->set_time_zone('local'); my $on_at_dt = $t1->clone; $on_at_dt->set_hour( (split(/:/, $on_at))[0] ); $on_at_dt->set_minute( (split(/:/, $on_at))[1] ); my $off_at_dt = $on_at_dt->clone; $off_at_dt->add(hours => $on_time); return ($t1, $on_at_dt, $off_at_dt); } sub set_off_timer { my ($on_dt, $off_dt, $on_time) = @_; $off_dt = $on_dt->clone; $off_dt->add(hours => $on_time); return $off_dt; } sub set_on_timer { my ($t1, $dt, $on_at) = @_; $dt = $t1->clone; $dt->set_second(0); $dt->set_hour((split(/:/, $on_at))[0]); $dt->set_minute((split(/:/, $on_at))[1]); return $dt; }

Here are a couple of benchmarks I put together to try to get it to be as efficient as I could make it.

Thanks,

-stevieb


In reply to Efficiency of indoor grow light timer 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 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?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-23 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found