2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-04-12 08:41:41 +00:00

Sunrise changes

git-svn-id: https://svn.fhem.de/fhem/trunk@377 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2009-05-30 10:59:15 +00:00
parent 466d8cec52
commit c2b24391c4
7 changed files with 75 additions and 47 deletions

View File

@ -498,3 +498,4 @@
- bugfix: FHT "summer" fix (avoiding a lot of syncnow)
- feature: FHEMWEB modules added
- feature: holiday module + doc + example + holiday2we attribute
- bugfix: sunrise stuff fixed, doc missing

View File

@ -119,7 +119,10 @@ at_Exec($)
if($count) {
$def =~ s/{\d+}/{$count}/ if($def =~ m/^\+?\*{\d+}/); # Replace the count
Log GetLogLevel($name,5), "redefine at command $name as $def";
$data{AT_RECOMPUTE} = 1; # Tell sunrise compute the next day
CommandDefine(undef, "$name at $def"); # Recompute the next TRIGGERTIME
delete($data{AT_RECOMPUTE});
$attr{$name} = $oldattr;
}
$at_tdiff = undef;

View File

@ -12,7 +12,7 @@ use strict;
use warnings;
use Math::Trig;
sub sr($$$$);
sub sr($$$$$$);
sub sunrise_rel(@);
sub sunset_rel(@);
sub sunrise_abs(@);
@ -38,56 +38,56 @@ SUNRISE_EL_Initialize($)
my ($hash) = @_;
}
##########################
# Compute:
# Compute the _next_ event
# rise: 1: event is sunrise (else sunset)
# isrel: 1: _relative_ times until the next event (else absolute for today)
# isrel: 1: relative times
# seconds: second offset to event
# daycheck: if set, then return 1 if the sun is visible, 0 else
sub
sr($$$$)
sr($$$$$$)
{
my ($rise, $seconds, $isrel, $daycheck) = @_;
my ($rise, $seconds, $isrel, $daycheck, $min, $max) = @_;
my $needrise = ($rise || $daycheck) ? 1 : 0;
my $needset = (!$rise || $daycheck) ? 1 : 0;
$seconds = 0 if(!$seconds);
my $nt = time;
my @lt = localtime($nt);
my $gmtoff = _calctz($nt,@lt); # in hour
my ($rt,$st) = _sr($needrise,$needset, $lt[5]+1900,$lt[4]+1,$lt[3], $gmtoff);
my $nh = $lt[2] + $lt[1]/60 + $lt[0]/3600;
my ($rt,$st) = _sr($needrise,$needset, $lt[5]+1900,$lt[4]+1,$lt[3], $gmtoff);
my $sst = ($rise ? $rt : $st) + ($seconds/3600);
my $nh = $lt[2] + $lt[1]/60 + $lt[0]/3600; # Current hour since midnight
if($daycheck) {
return 0 if($nh < $rt || $nh > $st);
return 1;
}
$seconds = 0 if(!$seconds);
my $sst = ($rise ? $rt : $st);
if(!$isrel) {
$sst += ($seconds/3600);
return h2hms_fmt($sst);
}
$sst += ($seconds/3600);
my $diff = 0;
if(int(($nh-$sst)*3600) >= 0) {
$nt += 86400; # Tommorow
if($data{AT_RECOMPUTE} || # compute it for tommorow
int(($nh-$sst)*3600) >= 0) { # if called a subsec earlier
$nt += 86400;
$diff = 24;
@lt = localtime($nt);
$gmtoff = _calctz($nt,@lt); # in hour
($rt,$st) = _sr($needrise,$needset, $lt[5]+1900,$lt[4]+1,$lt[3], $gmtoff);
$sst = ($rise ? $rt : $st);
$sst += ($seconds/3600);
$sst = ($rise ? $rt : $st) + ($seconds/3600);
}
$diff = $diff + $sst - $nh;
return h2hms_fmt($diff);
$sst = hms2h($min) if(defined($min) && (hms2h($min) > $sst));
$sst = hms2h($max) if(defined($max) && (hms2h($max) < $sst));
$sst += $diff if($isrel);
$sst -= $nh if($isrel == 1);
return h2hms_fmt($sst);
}
sub
_sr($$$$$$)
{
@ -304,6 +304,15 @@ _calctz($@)
}
sub
hms2h($)
{
my $in = shift;
my @a = split(":", $in);
return 0 if(int(@a) < 2 || $in !~ m/^[\d:]*$/);
return $a[0]+$a[1]/60 + ($a[2] ? $a[2]/3600 : 0);
}
sub
h2hms($)
{
@ -324,11 +333,13 @@ h2hms_fmt($)
}
sub sunrise_rel(@) { return sr(1, shift, 1, 0) }
sub sunset_rel(@) { return sr(0, shift, 1, 0) }
sub sunrise_abs(@) { return sr(1, shift, 0, 0) }
sub sunset_abs(@) { return sr(0, shift, 0, 0) }
sub isday() { return sr(1, 0, 0, 1) }
sub sunrise_rel(@) { return sr(1, shift, 1, 0, shift, shift) }
sub sunset_rel(@) { return sr(0, shift, 1, 0, shift, shift) }
sub sunrise_abs(@) { return sr(1, shift, 0, 0, shift, shift) }
sub sunset_abs(@) { return sr(0, shift, 0, 0, shift, shift) }
sub sunrise(@) { return sr(1, shift, 2, 0, shift, shift) }
sub sunset(@) { return sr(0, shift, 2, 0, shift, shift) }
sub isday() { return sr(1, 0, 0, 1, undef, undef) }
sub sunrise_coord($$$) { ($long, $lat, $tz) = @_; return undef; }
1;

View File

@ -396,3 +396,7 @@
- pgm3: bugfix, format table for userdef
- pgm3: feature X10_support, taillogorder optional with date
- pgm3: HMS100CO added, fhem.html relating pgm3 updated
- Sat May 30 2009 (Rudi)
- 99_SUNRISE_EL: sunrise/sunset called in "at" computes correctly the next
event. New "sunrise()/sunset()" calls added, min max optional parameter.

View File

@ -2848,10 +2848,14 @@ A line ending with \ will be concatenated with the next one, so long lines
define a12 at +*{sunset_rel()} { fhem("set lamp on-till 23:00") if($we) }
# Switch lamp1 and lamp2 on from 7:00 till 10 minutes after sunrise
define a13 at *07:00 set lamp1,lamp2 on-till {sunrise_abs(+600)}
define a13 at *07:00 set lamp1,lamp2 on-till {sunrise(+600)}
# Switch the lamp off 2 minutes after sunrise each day
define a14 at +*{sunrise_rel(+120)} set lamp on
define a14 at +{sunrise(+120)} set lamp on
# Switch lamp1 on at sunset, not before 18:00 and not after 21:00
define a15 at *{sunset(0,"18:00","21:00")} set lamp1 on
</PRE>
Notes:<br>
@ -2864,11 +2868,9 @@ A line ending with \ will be concatenated with the next one, so long lines
</li>
<li>if the current time is greater than the time specified, then the
command will be executed tomorrow. This is why the relative forms
of the sunset/sunrise functions should be used with the relative
(+) flag</li>
command will be executed tomorrow.</li>
<li>In order to use the sunrise_rel()/sunset_rel() functions,
<li>In order to use the sunrise()/sunset() functions,
put { sunrise_coord(long, lat, "") } into your
<a href="#lastinclude">lastinclude</a> file, as in the above example.
If you are not using sunrise_coord, then the coordinates for
@ -3420,25 +3422,32 @@ A line ending with \ will be concatenated with the next one, so long lines
<h3>SUNRISE_EL</h3>
<ul>
This module is used to define the functions<pre>
sunrise, sunset,
sunrise_rel, sunset_rel
sunrise_abs, sunset_abs
isday, sunrise_coord</pre>
perl functions, to be used in <a href="#at">at</a> or <a
href="#notify">notify</a> commands.<br>
perl functions, to be used in <a href="#at">at</a> or FS20 on-till commands.<br>
First you should call the sunrise_coord function with your exact longitude
and latitude (see e.g. maps.google.com for the exact values, which should be
in the form of a floating point value). This command should be part of the
<a href="#lastinclude">lastinclude</a> file. The default value is Frankfurt
am Main, Germany.<br><br>
sunrise_rel()/sunset_rel() return the relative time to the next
sunrise/sunset. The argument (in seconds) will be added to this time. The
relative values should be used as arguments to the <a href="#at">at</a>, see
the examples there for details.<br><br>
sunrise()/sunset() returns the absolute time of the next sunrise/sunset,
adding 24 hours if the next event is tomorrow, to use it in the timespec of
an at device or for the on-till command for FS20 devices.<br>
sunrise_abs()/sunset_abs() return the absolute time to the next
sunrise/sunset, with the argument added to the time. These are used e.g. in
arguments to the "on-till" FS20 command.<br><br>
sunrise_rel()/sunset_rel() returns the relative time to the next
sunrise/sunset. <br>
sunrise_abs()/sunset_abs() return the absolute time of the corresponding
event today (no 24 hours added).<br>
All functions take up to three arguments:<br>
<ul>
<li>The first specifies an offset (in seconds), which will be added to the
event.
<li>The second and third specify min and max values (format: "HH:MM").
</ul>
<br>
isday() can be used in some notify or at commands to check if the sun is up or
down.

View File

@ -251,7 +251,7 @@ by fhem.pl?</b>
receiver or with googleearth. Compute the latitude/longitude as needed, and
enter them in your lastinclude file with the command:
<pre>{sunrise_coord("<latitude>", "<longitude>", "") }</pre>
After restart, { sunrise_abs() } will return the time of the sunrise today,
After restart, { sunrise() } will return the time of the next sunrise,
in a HH:MM:SS format.<br><br>
Note: 99_SUNRISE_EL.pm is the ExtraLight version of the original

View File

@ -34,11 +34,11 @@ define a11 at +*{sunset_rel()} { fhem("set lamp on-till 23:00") if($we) }
##################################
# Switch lamp1 and lamp2 on from 7:00 till 10 minutes after sunrise
define a12 at *07:00 set lamp1,lamp2 on-till {sunrise_abs(+600)}
define a12 at *07:00 set lamp1,lamp2 on-till {sunrise(+600)}
##################################
# Switch lamp1 on at sunrise, but not before 07:00
define a13 at +*{max(abstime2rel("07:00"),sunrise_rel())} set lamp1 on
# Switch lamp1 on at sunrise, but not before 07:00 and not after 09:00
define a13 at +*{sunrise(0,"07:00","09:00"))} set lamp1 on
##################################
# Blink 3 times if the piri sends a command