diff --git a/CHANGED b/CHANGED index 36693ed47..8b263a493 100644 --- a/CHANGED +++ b/CHANGED @@ -468,3 +468,4 @@ - =DATE= (4.6) - bugfix: fht actuator message clarification by Klaus + - feature: getstate command from Martin (25.12) diff --git a/contrib/getstate/99_getstate.pm b/contrib/getstate/99_getstate.pm new file mode 100644 index 000000000..09c9ce4d9 --- /dev/null +++ b/contrib/getstate/99_getstate.pm @@ -0,0 +1,132 @@ +################################################################ +# +# $Id: 99_getstate.pm,v 1.1 2008-12-28 14:43:45 rudolfkoenig Exp $ +# +# Copyright notice +# +# (c) 2008 Copyright: Martin Fischer (m_fischer at gmx dot de) +# All rights reserved +# +# This script free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# The GNU General Public License can be found at +# http://www.gnu.org/copyleft/gpl.html. +# A copy is found in the textfile GPL.txt and important notices to the license +# from the author is found in LICENSE.txt distributed with these scripts. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +################################################################ + +package main; +use strict; +use warnings; +use POSIX; + +sub CommandGetState($); +sub stringToNumber($); +sub stripNumber($); +sub isNumber; +sub isInteger; +sub isFloat; + +##################################### +sub +GetState_Initialize($$) +{ + my %lhash = ( Fn=>"CommandGetState", + Hlp=>",list short status info" ); + $cmds{getstate} = \%lhash; +} + + +##################################### +sub +CommandGetState($) +{ + + my ($cl, $param) = @_; + + return "Usage: getstate " if(!$param); + + my $str; + my $sdev = $param; + + if(!defined($defs{$sdev})) { + $str = "Please define $sdev first"; + } else { + + my $r = $defs{$sdev}{READINGS}; + my $val; + my $v; + + if($r) { + foreach my $c (sort keys %{$r}) { + undef($v); + $val = $r->{$c}{VAL}; + $val =~ s/\s+$//g; + $val = stringToNumber($val); + $val = stripNumber($val); + $v = $val if (isNumber($val) && !$v); + $v = $val if (isInteger($val) && !$v); + $v = $val if (isFloat($val) && !$v); + $str .= sprintf("%s:%s ",$c,$v) if(defined($v)); + } + + } + + } + + return $str; + +} + +##################################### +sub stringToNumber($) +{ + my $s = shift; + + $s = "0" if($s =~ m/^(off|no \(yes\/no\))$/); + $s = "1" if($s =~ m/^(on|yes \(yes\/no\))$/); + + return $s; +} + +##################################### +sub stripNumber($) +{ + my $s = shift; + my @strip = (" (Celsius)", " (l/m2)", " (counter)", " (%)", " (km/h)" , "%"); + + foreach my $pattern (@strip) { + $s =~ s/\Q$pattern\E//gi; + } + + return $s; +} + +##################################### +sub isNumber +{ + $_[0] =~ /^\d+$/ +} + +##################################### +sub isInteger +{ + $_[0] =~ /^[+-]?\d+$/ +} + +##################################### +sub isFloat +{ + $_[0] =~ /^[+-]?\d+\.?\d*$/ +} + +1; diff --git a/contrib/getstate/README.getstate b/contrib/getstate/README.getstate new file mode 100644 index 000000000..c961b2351 --- /dev/null +++ b/contrib/getstate/README.getstate @@ -0,0 +1,32 @@ +getstate.pm - Copyright (c)2008 Martin Fischer + +Description: + The module getstate.pm extends FHEM to support a short status output + of a device. It is useful for monitoring the device in e.g. Cacti. + +Installation: + Copy the script 99_getstate.pm to FHEM modules directory, e.g. + 'cp 99_getstate.pm /usr/local/lib/FHEM' + and restart FHEM. + +Usage: + getstate + + Output a short string of the "READINGS" for . + + Example for a FS20-Device: + FHZ> getstate EG.sz.SD.Tv + state:0 + + Example for a FHT-Device: + FHZ> getstate EG.wz.HZ + actuator:0 day-temp:21.5 desired-temp:21.5 lowtemp-offset:4.0 [...] + + Example for a KS300/555-Device: + FHZ> getstate GH.ga.WE.01 + humidity:93 israining:0 rain:207.8 rain_raw:815 temperature:5.1 [...] + + Example for a HMS-Device: + FHZ> getstate NN.xx.RM.01 + smoke_detect:0 + diff --git a/contrib/getstate/fhem-getstate b/contrib/getstate/fhem-getstate new file mode 100755 index 000000000..9e196bdb0 --- /dev/null +++ b/contrib/getstate/fhem-getstate @@ -0,0 +1,82 @@ +#!/bin/bash +# +# fhem-getstate script for reading / writing DigiTemp values +# Copyright (C) 2008 Martin Fischer . All rights reserved. +# +# License GPLv3+: GNU GPL version 3 or later +# +# This is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# any later version. +# +# This source is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + + +NCAT=`which netcat` + +HOST="localhost" +PORT="7072" + +# Functions +function version { + echo "fhem-getstate, Version 1.0 +Copyright (C) 2008 Martin Fischer +License GPLv3+: GNU GPL version 3 or later +This is free software: you are free to change and redistribute it. +There is NO WARRANTY, to the extent permitted by law. + +Written by Martin Fischer" + exit $1 +} + +function longhelp { + echo "\ +Usage: fhem-getstate [OPTION] DEVICE + +Connect to a FHEM-Server running on 'localhost 7072' and print the status for +the given DEVICE as a space seperated list for use in e.g. Cacti. + +Mandatory arguments: + -d DEVICE print the status for DEVICE as defined in FHEM + +Optional: + -s SERVER Resolvable Hostname or IP address of FHEM (default: localhost) + -p PORT Listening Port of FHEM (default: 7072) + -q quiet mode + -h show this help + -v show version + +Reports bugs to . +" + exit $1 +} + +function usage { + echo >&2 "Usage: fhem-getstate [-s ] [-p ] -d [-h] [-v]" && exit $1; +} + +# check for arguments +if (( $# <= 0 )); then + usage 1; +fi + +# get options +while getopts "s:p:d:hv" option; do + case $option in + d) DEV=$OPTARG;; + h) longhelp 0;; + p) PORT=$OPTARG;; + s) HOST="$OPTARG";; + v) version 0;; + ?) usage 1;; + esac +done + +(echo "getstate ${DEV}" | $NCAT -w1 ${HOST} ${PORT}) + +exit 0; +