diff options
author | Keine_Ahnung <dirk-vdr@gmx.de> | 2013-02-05 20:09:41 +0100 |
---|---|---|
committer | Keine_Ahnung <dirk-vdr@gmx.de> | 2013-02-05 20:09:41 +0100 |
commit | b2316e705cf742ac9e811d3c3e200a8f21547bab (patch) | |
tree | de56ee3bee9508cb112119da36a3c439274419ab /script/uactivity/activity/#20-switch_tv_light | |
parent | 2be7e65c7b15775993eeb3198b8c2404b5b489dd (diff) | |
download | vdr-plugin-uactivity-b2316e705cf742ac9e811d3c3e200a8f21547bab.tar.gz vdr-plugin-uactivity-b2316e705cf742ac9e811d3c3e200a8f21547bab.tar.bz2 |
Added switch_tv_light example, fixed filenames and attributes
Diffstat (limited to 'script/uactivity/activity/#20-switch_tv_light')
-rwxr-xr-x | script/uactivity/activity/#20-switch_tv_light | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/script/uactivity/activity/#20-switch_tv_light b/script/uactivity/activity/#20-switch_tv_light new file mode 100755 index 0000000..baae21f --- /dev/null +++ b/script/uactivity/activity/#20-switch_tv_light @@ -0,0 +1,116 @@ +#!/bin/sh + +set -u +set -e + +# Your Settings +# +LATITUDE="52.000000" +LONGITUDE="11.000000" +# http://search.cpan.org/~rkhill/DateTime-Event-Sunrise-0.0501/lib/DateTime/Event/Sunrise.pm#METHODS +ALTITUDE="-0.833" +SWITCH_ON_CMD="touch /tmp/tv_light" +SWITCH_OFF_CMD="rm -f /tmp/tv_light" +# +# End of user config +# apt-get install libdatetime-event-sunrise-perl + + + +JOB_ID_FILE="${4}/activity/jobid.$(basename ${0})" + + + +daylight(){ + export LATITUDE + export LONGITUDE + export ALTITUDE + +sunset="$(perl <<'EOF' 2> /dev/null + use DateTime; + use DateTime::Event::Sunrise; + use DateTime::Format::Strptime; + + my $latitude = $ENV{'LATITUDE'}; + my $longitude = $ENV{'LONGITUDE'}; + my $altitude = $ENV{'ALTITUDE'}; + + my $sunrise = DateTime::Event::Sunrise ->sunrise (longitude => $longitude, latitude => $latitude, altitude => $altitude, iteration => '1'); + my $sunset = DateTime::Event::Sunrise ->sunset (longitude => $longitude, latitude => $latitude, altitude => $altitude, iteration => '1'); + + my $strp = DateTime::Format::Strptime->new(pattern => '%H:%M %Y-%m-%d'); + print $strp->format_datetime($sunset->next(DateTime->now(time_zone=>'local'))); + + my $day_set = DateTime::SpanSet->from_sets(start_set => $sunrise, end_set => $sunset); + $day_set->contains(DateTime->now()) ? exit 1 : exit 0; +EOF +)" + + err=$? + if [ ${err} = 0 ]; then + echo "" + return 0 + elif [ ${err} = 1 ]; then + echo "${sunset}" + return 1 + else + echo "" + return 2 + fi +} + + + +delete_job(){ + if [ -f "${JOB_ID_FILE}" ]; then + JOB_ID="$(cat ${JOB_ID_FILE})" + atrm ${JOB_ID} 2> /dev/null + rm -f "${JOB_ID_FILE}" + fi +} + + + +switch_tv_light(){ + delete_job + if [ "${1}" = "on" ]; then + sunset="$(daylight)" + light=$? + if [ "${light}" = "1" ]; then + # daylight now, shedule "light on" for later + JOB_ID_TMP="$(echo "rm -f "${JOB_ID_FILE}"; ${SWITCH_ON_CMD}" | at ${sunset} 2>&1)" + JOB_ID="$(echo "${JOB_ID_TMP}" | tr -d '\n' | sed 's/^.*job \([0-9]*\).*$/\1/')" + echo "${JOB_ID}" > ${JOB_ID_FILE} + elif [ "${light}" = "0" ]; then + eval ${SWITCH_ON_CMD} + else + # Some error + eval ${SWITCH_OFF_CMD} + fi + else + eval ${SWITCH_OFF_CMD} + fi +} + + + +case "${1}" in + startup) + delete_job + ;; + started) + [ "${2}" = "true" ] && switch_tv_light on + ;; + shutdown) + switch_tv_light off + ;; + running) + if [ "${2}" = "true" ]; then + switch_tv_light on + else + switch_tv_light off + fi + ;; +esac + +exit 0 |