2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 18:59:33 +00:00
fhem-mirror/fhem/contrib/getstate/99_getstate.pm
rudolfkoenig d2d24b3857 New version from Martin
git-svn-id: https://svn.fhem.de/fhem/trunk@325 2b470e98-0d58-463d-a4d8-8e2adae1ed80
2009-01-12 09:21:53 +00:00

133 lines
2.8 KiB
Perl

################################################################
#
# $Id: 99_getstate.pm,v 1.2 2009-01-12 09:21:53 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;