99_GetState.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ################################################################
  2. #
  3. # $Id: 99_getstate.pm,v 1.3 2009-12-16 16:46:00 m_fischer Exp $
  4. #
  5. # Copyright notice
  6. #
  7. # (c) 2008 Copyright: Martin Fischer (m_fischer at gmx dot de)
  8. # All rights reserved
  9. #
  10. # This script free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # The GNU General Public License can be found at
  16. # http://www.gnu.org/copyleft/gpl.html.
  17. # A copy is found in the textfile GPL.txt and important notices to the license
  18. # from the author is found in LICENSE.txt distributed with these scripts.
  19. #
  20. # This script is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU General Public License for more details.
  24. #
  25. ################################################################
  26. package main;
  27. use strict;
  28. use warnings;
  29. use POSIX;
  30. sub CommandGetState($);
  31. sub stringToNumber($);
  32. sub stripNumber($);
  33. sub isNumber;
  34. sub isInteger;
  35. sub isFloat;
  36. #####################################
  37. sub
  38. GetState_Initialize($$)
  39. {
  40. my %lhash = ( Fn=>"CommandGetState",
  41. Hlp=>"<devspec>,list short status info" );
  42. $cmds{getstate} = \%lhash;
  43. }
  44. #####################################
  45. sub
  46. CommandGetState($)
  47. {
  48. my ($cl, $param) = @_;
  49. return "Usage: getstate <devspec>" if(!$param);
  50. my $str;
  51. my $sdev = $param;
  52. if(!defined($defs{$sdev})) {
  53. $str = "Please define $sdev first";
  54. } else {
  55. my $r = $defs{$sdev}{READINGS};
  56. my $val;
  57. my $v;
  58. if($r && $defs{$sdev}{TYPE} ne "CUL_WS") {
  59. foreach my $c (sort keys %{$r}) {
  60. undef($v);
  61. $val = $r->{$c}{VAL};
  62. $val =~ s/\s+$//g;
  63. $val = stringToNumber($val);
  64. $val = stripNumber($val);
  65. $val =~ s/\s+$//g;
  66. $v = $val if (isNumber($val) && !$v);
  67. $v = $val if (isInteger($val) && !$v);
  68. $v = $val if (isFloat($val) && !$v);
  69. $c =~ s/:/-/g;
  70. $str .= sprintf("%s:%s ",$c,$v) if(defined($v));
  71. }
  72. }
  73. if ($r && $defs{$sdev}{TYPE} eq "CUL_WS") {
  74. $v = $defs{$sdev}{READINGS}{state}{VAL};
  75. $v =~ s/:\s+/:/g;
  76. $v =~ s/\s+/ /g;
  77. $str = $v;
  78. }
  79. }
  80. return $str;
  81. }
  82. #####################################
  83. sub stringToNumber($)
  84. {
  85. my $s = shift;
  86. $s = "0" if($s =~ m/^(off|no \(yes\/no\))$/);
  87. $s = "1" if($s =~ m/^(on|yes \(yes\/no\))$/);
  88. return $s;
  89. }
  90. #####################################
  91. sub stripNumber($)
  92. {
  93. my $s = shift;
  94. my @strip = (" (Celsius)", " (l/m2)", " (counter)", " (%)", " (km/h)" , "%");
  95. foreach my $pattern (@strip) {
  96. $s =~ s/\Q$pattern\E//gi;
  97. }
  98. return $s;
  99. }
  100. #####################################
  101. sub isNumber
  102. {
  103. $_[0] =~ /^\d+$/
  104. }
  105. #####################################
  106. sub isInteger
  107. {
  108. $_[0] =~ /^[+-]?\d+$/
  109. }
  110. #####################################
  111. sub isFloat
  112. {
  113. $_[0] =~ /^[+-]?\d+\.?\d*$/
  114. }
  115. 1;