From 7c6887eaaf812b63bab6c5e134f80a2ef36aeb31 Mon Sep 17 00:00:00 2001 From: fishsoupisgood Date: Tue, 12 Jan 2021 16:58:31 +0000 Subject: works --- prometheus/.gitignore | 3 + prometheus/Makefile | 11 +++ .../html/weatherstation/updateweatherstation.php | 1 + .../html/weatherstation/updateweatherstation.pl | 83 ++++++++++++++++++++++ prometheus/usr/local/bin/make_habitable | 11 +++ prometheus/usr/local/bin/midnight_tidy | 15 ++++ prometheus/usr/local/bin/set_radiator_target | 47 ++++++++++++ prometheus/usr/local/bin/vent | 17 +++++ 8 files changed, 188 insertions(+) create mode 100644 prometheus/.gitignore create mode 100644 prometheus/Makefile create mode 120000 prometheus/home/httpd/html/weatherstation/updateweatherstation.php create mode 100755 prometheus/home/httpd/html/weatherstation/updateweatherstation.pl create mode 100755 prometheus/usr/local/bin/make_habitable create mode 100755 prometheus/usr/local/bin/midnight_tidy create mode 100755 prometheus/usr/local/bin/set_radiator_target create mode 100755 prometheus/usr/local/bin/vent (limited to 'prometheus') diff --git a/prometheus/.gitignore b/prometheus/.gitignore new file mode 100644 index 0000000..2adf2fd --- /dev/null +++ b/prometheus/.gitignore @@ -0,0 +1,3 @@ +stamp +*.bak +*.swp diff --git a/prometheus/Makefile b/prometheus/Makefile new file mode 100644 index 0000000..4c6b24f --- /dev/null +++ b/prometheus/Makefile @@ -0,0 +1,11 @@ +STUFF=$(shell find usr home \! -type d -print) + +stamp: ${STUFF} + tar cf - ${STUFF} | ssh prometheus "cd / && tar xvpf -" + touch $@ + +tidy: + perltidy -b ./home/httpd/html/weatherstation/updateweatherstation.pl + +clean: + /bin/rm -f ./home/httpd/html/weatherstation/updateweatherstation.pl.bak diff --git a/prometheus/home/httpd/html/weatherstation/updateweatherstation.php b/prometheus/home/httpd/html/weatherstation/updateweatherstation.php new file mode 120000 index 0000000..b629fc9 --- /dev/null +++ b/prometheus/home/httpd/html/weatherstation/updateweatherstation.php @@ -0,0 +1 @@ +updateweatherstation.pl \ No newline at end of file diff --git a/prometheus/home/httpd/html/weatherstation/updateweatherstation.pl b/prometheus/home/httpd/html/weatherstation/updateweatherstation.pl new file mode 100755 index 0000000..87b8893 --- /dev/null +++ b/prometheus/home/httpd/html/weatherstation/updateweatherstation.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl + +#use CGI qw(:standard); +#use Sys::Syslog qw(:standard :macros); +use Net::MQTT::Simple; +use IO::Socket; + +#openlog("weather", "", "local0"); + +my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = + gmtime(time); + +my @mons = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); +my @days = qw(Sun Mon Tue Wed Thr Fri Sat); + +my $dt = sprintf( + "%s, %02d %s %04d %02d:%02d:%02d GMT", + $days[$wday], $mday, $mons[$mon], 1900 + $year, + $hour, $min, $sec +); + +print "Content-Type: text/plain\r\n"; +print "Date: ", $dt, "\n"; +print "\n"; +print "success\n"; + +my $mqtt = Net::MQTT::Simple->new("10.32.139.1"); + +for my $t ( split( /&/, $ENV{'QUERY_STRING'} ) ) { + $t =~ s/%20/ /g; + + next unless $t =~ /^([^=]*)=([^=]*)$/; + + my ( $wot, $value ) = ( $1, $2 ); + + if ( $wot =~ /f$/ ) { + $wot =~ s/f$/c/; + $value = sprintf( "%.1f", 5.0 * ( ( $value - 32.0 ) / 9.0 ) ); + } + elsif ( $wot eq "baromin" ) { + $wot = "barommb"; + $value = sprintf( "%.1f", $value * 33.8639 ); + } + elsif ( $wot =~ /in$/ ) { + $wot =~ s/in$/mm/; + $value = sprintf( "%.1f", $value * 25.4 ); + } + + if ( $wot eq "baromin" ) { + $wot = "barommb"; + $value = sprintf( "%.1f", $value * 33.8639 ); + } + + if ( $wot eq "tempc" ) { + $mqtt->retain( "tele/weather/" . $wot, $value ); + } + else { + $mqtt->publish( "tele/weather/" . $wot, $value ); + } + + #syslog(LOG_WARNING,"fish: $wot => $value"); +} + +$mqtt->tick(0); +$mqtt->disconnect; + +alarm(5); + +my $sock = IO::Socket::INET->new( + PeerAddr => "88.96.137.6", + PeerPort => 30956, + Proto => 'tcp', + Timeout => 5 +); + +exit 0 unless defined $sock; + +$sock->print( "GET /weatherstation/updateweatherstation.php?" + . $ENV{'QUERY_STRING'} + . " HTTP/1.0\r\n" ); +$sock->print("\r\n"); +$sock->shutdown( $socket, 1 ); + diff --git a/prometheus/usr/local/bin/make_habitable b/prometheus/usr/local/bin/make_habitable new file mode 100755 index 0000000..730d632 --- /dev/null +++ b/prometheus/usr/local/bin/make_habitable @@ -0,0 +1,11 @@ +#!/bin/sh +set -x + +for i in 2fl_main_radiator 2fl_stair_radiator bathroom_radiator; do + /usr/local/bin/set_radiator_target $i 20 +done + + +for i in hall_radiator kitchen_radiator laundry_radiator kstudy_radiator bedroom_radiator ; do + set_radiator_target $i 18 +done diff --git a/prometheus/usr/local/bin/midnight_tidy b/prometheus/usr/local/bin/midnight_tidy new file mode 100755 index 0000000..2e6686e --- /dev/null +++ b/prometheus/usr/local/bin/midnight_tidy @@ -0,0 +1,15 @@ +#!/bin/sh +set -x + +/usr/local/bin/vent close + +for i in 2fl_main_radiator 2fl_stair_radiator bathroom_radiator bedroom_radiator hall_radiator kitchen_radiator kstudy_radiator laundry_radiator spare_bedroom_radiator; do + /usr/local/bin/set_radiator_target $i 10 +done + +for i in kitchen_left/POWER kitchen_right/POWER bathroom/POWER1 bathroom/POWER2 front_door/POWER 2ndfl_3gang_switch/POWER1 2ndfl_3gang_switch/POWER2 2ndfl_3gang_switch/POWER3; do + mosquitto_pub -h 127.0.0.1 -t cmnd/$i -m 0 + sleep 1 +done + + diff --git a/prometheus/usr/local/bin/set_radiator_target b/prometheus/usr/local/bin/set_radiator_target new file mode 100755 index 0000000..c52d22a --- /dev/null +++ b/prometheus/usr/local/bin/set_radiator_target @@ -0,0 +1,47 @@ +#!/bin/sh + +R=$1 +T1=$2 + + +case "$R" in + bathroom_radiator) + T2=$( expr $T1 + 2 ) + ;; + *) + T2=$( expr $T1 + 1 ) + ;; +esac + +H=127.0.0.1 + +for tries in 1 2 3 4 5 6 7 8 9 10 11 12; do + + V1=$( mosquitto_sub -h 10.32.139.1 -t stat/$R/var1 --retained-only -W 1 | sed -e 's/\..*//' ) + V2=$( mosquitto_sub -h 10.32.139.1 -t stat/$R/var2 --retained-only -W 1 | sed -e 's/\..*//' ) + + if [ $T1 -ne $V1 ]; then + mosquitto_pub -h $H -t cmnd/$R/var1 -m $T1 + sleep 1 + fi + + if [ $T2 -ne $V2 ]; then + mosquitto_pub -h $H -t cmnd/$R/var2 -m $T2 + sleep 1 + fi + + + if [ \( $T1 -eq $V1 \) -a \( $T2 -eq $V2 \) ]; then + echo Success + exit 0 + fi + + sleep 3 + +done + +echo Failed + + + + diff --git a/prometheus/usr/local/bin/vent b/prometheus/usr/local/bin/vent new file mode 100755 index 0000000..28e5ad4 --- /dev/null +++ b/prometheus/usr/local/bin/vent @@ -0,0 +1,17 @@ +#!/bin/sh + +case "$1" in + open) + mosquitto_pub -h 10.32.135.1 -t cmnd/vent/POWER2 -m 1 + sleep 20 + mosquitto_pub -h 10.32.135.1 -t cmnd/vent/POWER2 -m 0 + ;; + close) + mosquitto_pub -h 10.32.135.1 -t cmnd/vent/POWER1 -m 1 + sleep 20 + mosquitto_pub -h 10.32.135.1 -t cmnd/vent/POWER1 -m 0 + ;; + *) + echo unknown command + ;; +esac -- cgit v1.2.3