2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

Martins getstate added

git-svn-id: https://svn.fhem.de/fhem/trunk@307 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2008-12-28 14:43:45 +00:00
parent 40f8b5262b
commit 4205850e95
4 changed files with 247 additions and 0 deletions

View File

@ -468,3 +468,4 @@
- =DATE= (4.6)
- bugfix: fht actuator message clarification by Klaus
- feature: getstate command from Martin (25.12)

View File

@ -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=>"<devspec>,list short status info" );
$cmds{getstate} = \%lhash;
}
#####################################
sub
CommandGetState($)
{
my ($cl, $param) = @_;
return "Usage: getstate <devspec>" 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;

View File

@ -0,0 +1,32 @@
getstate.pm - Copyright (c)2008 Martin Fischer <m_fischer@gmx.de>
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 <devspec>
Output a short string of the "READINGS" for <devspec>.
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

View File

@ -0,0 +1,82 @@
#!/bin/bash
#
# fhem-getstate script for reading / writing DigiTemp values
# Copyright (C) 2008 Martin Fischer <m_fischer@gmx.de>. All rights reserved.
#
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
#
# 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 <m_fischer@gmx.de>
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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 <m_fischer@gmx.de>.
"
exit $1
}
function usage {
echo >&2 "Usage: fhem-getstate [-s <server>] [-p <port>] -d <devspec> [-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;