88_ALL4000T.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ################################################################
  2. #
  3. # Copyright notice
  4. #
  5. # (c) 2010 Sacha Gloor (sacha@imp.ch)
  6. #
  7. # This script is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # The GNU General Public License can be found at
  13. # http://www.gnu.org/copyleft/gpl.html.
  14. # A copy is found in the textfile GPL.txt and important notices to the license
  15. # from the author is found in LICENSE.txt distributed with these scripts.
  16. #
  17. # This script is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. # GNU General Public License for more details.
  21. #
  22. # This copyright notice MUST APPEAR in all copies of the script!
  23. #
  24. ################################################################
  25. # $Id: 88_ALL4000T.pm 2076 2012-11-04 13:49:43Z rudolfkoenig $
  26. package main;
  27. use strict;
  28. use warnings;
  29. use XML::Simple;
  30. use Data::Dumper;
  31. use LWP::UserAgent;
  32. use HTTP::Request;
  33. sub Log($$);
  34. #####################################
  35. sub
  36. trim($)
  37. {
  38. my $string = shift;
  39. $string =~ s/^\s+//;
  40. $string =~ s/\s+$//;
  41. return $string;
  42. }
  43. sub
  44. ALL4000T_Initialize($)
  45. {
  46. my ($hash) = @_;
  47. # Consumer
  48. $hash->{DefFn} = "ALL4000T_Define";
  49. $hash->{AttrList}= "model:ALL4000T delay loglevel:0,1,2,3,4,5,6";
  50. }
  51. #####################################
  52. sub
  53. ALL4000T_Define($$)
  54. {
  55. my ($hash, $def) = @_;
  56. my $name=$hash->{NAME};
  57. my @a = split("[ \t][ \t]*", $def);
  58. Log 5, "ALL4000T Define: $a[0] $a[1] $a[2] $a[3] $a[4]";
  59. return "Define the host as a parameter i.e. ALL4000T" if(@a < 4);
  60. my $host = $a[2];
  61. my $host_port = $a[3];
  62. my $delay=$a[4];
  63. $attr{$name}{delay}=$delay if $delay;
  64. Log 1, "ALL4000T device is none, commands will be echoed only" if($host eq "none");
  65. $hash->{Host} = $host;
  66. $hash->{Host_Port} = $host_port;
  67. $hash->{STATE} = "Initialized";
  68. Log 4,"$name: Delay $delay";
  69. InternalTimer(gettimeofday()+$delay, "ALL4000T_GetStatus", $hash, 0);
  70. return undef;
  71. }
  72. #####################################
  73. sub
  74. ALL4000T_GetStatus($)
  75. {
  76. my ($hash) = @_;
  77. my $buf;
  78. if(!defined($hash->{Host_Port})) { return(""); }
  79. Log 5, "ALL4000T_GetStatus";
  80. my $name = $hash->{NAME};
  81. my $host = $hash->{Host};
  82. my $host_port = $hash->{Host_Port};
  83. my $text='';
  84. my $err_log='';
  85. my $delay=$attr{$name}{delay}||300;
  86. InternalTimer(gettimeofday()+$delay, "ALL4000T_GetStatus", $hash, 0);
  87. my $xml = new XML::Simple;
  88. my $URL="http://".$host."/xml";
  89. my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 3);
  90. my $header = HTTP::Request->new(GET => $URL);
  91. my $request = HTTP::Request->new('GET', $URL, $header);
  92. my $response = $agent->request($request);
  93. $err_log.= "Can't get $URL -- ".$response->status_line
  94. unless $response->is_success;
  95. if($err_log ne "")
  96. {
  97. Log GetLogLevel($name,2), "ALL4000T ".$err_log;
  98. return("");
  99. }
  100. my $body = $response->content;
  101. my $data = $xml->XMLin($body);
  102. my $current=trim($data->{BODY}->{FORM}->{TEXTAREA}->{xml}->{data}->{$host_port});
  103. $text="Temperature: ".$current;
  104. my $sensor="temperature";
  105. Log 4,"$name: $text";
  106. if (!$hash->{local}){
  107. $hash->{CHANGED}[0] = $text;
  108. $hash->{READINGS}{$sensor}{TIME} = TimeNow();
  109. $hash->{READINGS}{$sensor}{VAL} = $current." (Celsius)";;
  110. DoTrigger($name, undef) if($init_done);
  111. }
  112. $hash->{STATE} = "T: ".$current;
  113. return($text);
  114. }
  115. 1;
  116. =pod
  117. =begin html
  118. <a name="ALL4000T"></a>
  119. <h3>ALL4000T</h3>
  120. <ul>
  121. Note: this module requires the following perl modules: XML::Simple LWP::UserAgent
  122. HTTP::Request.
  123. <br><br>
  124. <a name="ALL4000Tdefine"></a>
  125. <b>Define</b>
  126. <ul>
  127. <code>define &lt;name&gt; ALL4000T &lt;ip-address&gt; &lt;port&gt; &lt;delay&gt;</code>
  128. <br><br>
  129. Defines a temperature sensor connected on an Allnet 4000 device via its ip address and port. Use the delay argument to define the delay between polls.<br><br>
  130. Examples:
  131. <ul>
  132. <code>define AUSSEN.POOL.TEMP.vorlauf ALL4000T 192.168.68.20 t2 120</code><br>
  133. </ul>
  134. </ul>
  135. <br>
  136. </ul>
  137. =end html
  138. =cut