70_SCIVT.pm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. ##############################################
  2. # $Id: 70_SCIVT.pm 3738 2013-08-18 14:13:59Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use Device::SerialPort;
  7. my %sets = (
  8. "cmd" => "",
  9. "freq" => "",
  10. );
  11. #####################################
  12. sub
  13. SCIVT_Initialize($)
  14. {
  15. my ($hash) = @_;
  16. # Consumer
  17. $hash->{DefFn} = "SCIVT_Define";
  18. $hash->{GetFn} = "SCIVT_Get";
  19. $hash->{SetFn} = "SCIVT_Set";
  20. $hash->{AttrList}= "model:SCD10,SCD20,SCD30";
  21. }
  22. #####################################
  23. sub
  24. SCIVT_Define($$)
  25. {
  26. my ($hash, $def) = @_;
  27. my @a = split("[ \t][ \t]*", $def);
  28. return "Define the serial device as a parameter, use none for a fake device"
  29. if(@a != 3);
  30. $hash->{STATE} = "Initialized";
  31. my $dev = $a[2];
  32. Log3 $hash, 1, "SCIVT device is none, commands will be echoed only"
  33. if($dev eq "none");
  34. if($dev ne "none") {
  35. Log3 $hash, 3, "SCIVT opening device $dev";
  36. my $po = new Device::SerialPort ($dev);
  37. return "SCIVT Can't open $dev: $!" if(!$po);
  38. Log3 $hash, 2, "SCIVT opened device $dev";
  39. $po->close();
  40. }
  41. $hash->{DeviceName} = $dev;
  42. $hash->{Timer} = 900; # call every 15 min
  43. $hash->{Cmd} = 'F'; # get all data, min/max unchanged
  44. my $tn = TimeNow();
  45. $hash->{READINGS}{"freq"}{TIME} = $tn;
  46. $hash->{READINGS}{"freq"}{VAL} = $hash->{Timer};
  47. $hash->{READINGS}{"cmd"}{TIME} = $tn;
  48. $hash->{READINGS}{"cmd"}{VAL} = $hash->{Cmd};
  49. $hash->{CHANGED}[0] = "freq: $hash->{Timer}";
  50. $hash->{CHANGED}[1] = "cmd: $hash->{Cmd}";
  51. # InternalTimer blocks if init_done is not true
  52. my $oid = $init_done;
  53. $init_done = 1;
  54. SCIVT_GetStatus($hash);
  55. $init_done = $oid;
  56. return undef;
  57. }
  58. #####################################
  59. sub
  60. SCIVT_Set($@)
  61. {
  62. my ($hash, @a) = @_;
  63. return "\"set SCIVT\" needs at least two parameter" if(@a < 3);
  64. my $name = $hash->{NAME};
  65. Log3 $name, 4, "SCIVT Set request $a[1] $a[2], old: Timer:$hash->{Timer} Cmd: $hash->{Cmd}";
  66. return "Unknown argument $a[1], choose one of " . join(" ", sort keys %sets)
  67. if(!defined($sets{$a[1]}));
  68. $name = shift @a;
  69. my $type = shift @a;
  70. my $arg = join("", @a);
  71. my $tn = TimeNow();
  72. if($type eq "freq")
  73. {
  74. if ($arg > 0)
  75. {
  76. $hash->{Timer} = $arg * 60;
  77. $hash->{READINGS}{$type}{TIME} = $tn;
  78. $hash->{READINGS}{$type}{VAL} = $hash->{Timer};
  79. $hash->{CHANGED}[0] = "$type: $hash->{Timer}";
  80. }
  81. }
  82. if($type eq "cmd")
  83. {
  84. if ($arg eq "F")
  85. {
  86. $hash->{Cmd} = 'F'; # F : get all data
  87. }
  88. if ($arg eq "L") # L : get all data and clear min-/max values
  89. {
  90. $hash->{Cmd} = 'L';
  91. }
  92. $hash->{READINGS}{$type}{TIME} = $tn;
  93. $hash->{READINGS}{$type}{VAL} = $hash->{Cmd};
  94. $hash->{CHANGED}[0] = "$type: $hash->{Cmd}";
  95. }
  96. DoTrigger($name, undef) if($init_done);
  97. Log3 $name, 3, "SCIVT Set result Timer:$hash->{Timer} sec Cmd:$hash->{Cmd}";
  98. return "SCIVT => Timer:$hash->{Timer} Cmd:$hash->{Cmd}";
  99. }
  100. #####################################
  101. sub
  102. SCIVT_Get($@)
  103. {
  104. my ($hash, @a) = @_;
  105. return "get for an SCIVT device needs exactly one parameter" if(@a != 2);
  106. my $name = $hash->{NAME};
  107. my $v;
  108. if($a[1] eq "data")
  109. {
  110. $v = SCIVT_GetLine($hash->{DeviceName}, $hash->{Cmd});
  111. if(!defined($v))
  112. {
  113. Log3 $name, 2, "SCIVT Get $a[1] error";
  114. return "$a[0] $a[1] => Error";
  115. }
  116. $v =~ s/[\r\n]//g; # Delete the NewLine
  117. $hash->{READINGS}{$a[1]}{VAL} = $v;
  118. $hash->{READINGS}{$a[1]}{TIME} = TimeNow();
  119. }
  120. else
  121. {
  122. if($a[1] eq "param")
  123. {
  124. $v = "$hash->{DeviceName} $hash->{Timer} $hash->{Cmd}";
  125. }
  126. else
  127. {
  128. return "Unknown argument $a[1], must be data or param";
  129. }
  130. }
  131. Log3 $name, 3, "SCIVT Get $a[1] $v";
  132. return "$a[0] $a[1] => $v";
  133. }
  134. #####################################
  135. sub
  136. SCIVT_GetStatus($)
  137. {
  138. my ($hash) = @_;
  139. my $dnr = $hash->{DEVNR};
  140. my $name = $hash->{NAME};
  141. # Call us in n minutes again.
  142. InternalTimer(gettimeofday()+ $hash->{Timer}, "SCIVT_GetStatus", $hash,1);
  143. my %vals;
  144. my $result = SCIVT_GetLine($hash->{DeviceName}, $hash->{Cmd});
  145. if(!defined($result))
  146. {
  147. Log3 $name, 4, "SCIVT read error, retry $hash->{DeviceName}, $hash->{Cmd}";
  148. $result = SCIVT_GetLine($hash->{DeviceName}, $hash->{Cmd});
  149. }
  150. if(!defined($result))
  151. {
  152. Log3 $name, 2, "SCIVT read error, abort $hash->{DeviceName}, $hash->{Cmd}";
  153. $hash->{STATE} = "timeout";
  154. return $hash->{STATE};
  155. }
  156. if (length($result) < 10)
  157. {
  158. Log3 $name, 2, "SCIVT incomplete line ($result)";
  159. $hash->{STATE} = "incomplete";
  160. }
  161. else
  162. {
  163. $result =~ s/^.*R://;
  164. $result =~ s/[\r\n ]//g;
  165. Log3 $name, 3, "SCIVT $result (raw)";
  166. $result=~ s/,/./g;
  167. my @data = split(";", $result);
  168. my @names = ("Vs", "Is", "Temp", "minV", "maxV", "minI", "maxI");
  169. my $tn = TimeNow();
  170. for(my $i = 0; $i < int(@names); $i++)
  171. {
  172. $hash->{CHANGED}[$i] = "$names[$i]: $data[$i]";
  173. $hash->{READINGS}{$names[$i]}{TIME} = $tn;
  174. $hash->{READINGS}{$names[$i]}{VAL} = $data[$i];
  175. }
  176. DoTrigger($name, undef) if($init_done);
  177. $result =~ s/;/ /g;
  178. $hash->{STATE} = "$result";
  179. }
  180. return $hash->{STATE};
  181. }
  182. #####################################
  183. sub
  184. SCIVT_GetLine($$)
  185. {
  186. my $retry = 0;
  187. my ($dev,$cmd) = @_;
  188. return "R:13,66; 0,0;30;13,62;15,09;- 0,2; 2,8;\n"
  189. if($dev eq "none"); # Fake-mode
  190. my $serport = new Device::SerialPort ($dev);
  191. if(!$serport) {
  192. Log3 undef, 1, "SCIVT: Can't open $dev: $!";
  193. return undef;
  194. }
  195. $serport->reset_error();
  196. $serport->baudrate(1200);
  197. $serport->databits(8);
  198. $serport->parity('none');
  199. $serport->stopbits(1);
  200. $serport->handshake('none');
  201. my $rm = "SCIVT timeout reading the answer";
  202. my $data="";
  203. $serport->write($cmd);
  204. sleep(1);
  205. for(;;)
  206. {
  207. my ($rout, $rin) = ('', '');
  208. vec($rin, $serport->FILENO, 1) = 1;
  209. my $nfound = select($rout=$rin, undef, undef, 3.0);
  210. if($nfound < 0) {
  211. $rm = "SCIVT Select error $nfound / $!";
  212. goto DONE;
  213. }
  214. last if($nfound == 0);
  215. my $buf = $serport->input();
  216. if(!defined($buf) || length($buf) == 0) {
  217. $rm = "SCIVT EOF on $dev";
  218. goto DONE;
  219. }
  220. $data .= $buf;
  221. if($data =~ m/[\r\n]/) { # Newline received
  222. $serport->close();
  223. return $data;
  224. }
  225. }
  226. DONE:
  227. $serport->close();
  228. Log3 undef, 3, "SCIVT $rm";
  229. return undef;
  230. }
  231. 1;
  232. =pod
  233. =begin html
  234. <a name="SCIVT"></a>
  235. <h3>SCIVT</h3>
  236. <ul>
  237. <br>
  238. <a name="SCIVTdefine"></a>
  239. <b>Define</b>
  240. <ul>
  241. <code>define &lt;name&gt; SCIVT &lt;SCD-device&gt;</code>
  242. <br><br>
  243. Define a SCD series solar controler device. Details see <a
  244. href="http://english.ivt-hirschau.de/content.php?parent_id=CAT_64&doc_id=DOC_118">here</a>.
  245. You probably need a Serial to USB controller like the PL2303.
  246. <br>
  247. Defining an SCIVT device will schedule an internal task, which reads the
  248. status of the device every 5 minutes, and triggers notify/filelog commands.
  249. <br>Note: Currently this device does not support a "set" function, only
  250. a single get function which reads the device status immediately.
  251. <br><br>
  252. Example:
  253. <ul>
  254. <code>define scd SCIVT /dev/ttyUSB2</code><br>
  255. </ul>
  256. <br>
  257. </ul>
  258. <a name="SVICTset"></a>
  259. <b>Set</b> <ul>N/A</ul><br>
  260. <a name="SVICTget"></a>
  261. <b>Get</b>
  262. <ul>
  263. <code>get SCVIT data</code>
  264. <br>
  265. </ul>
  266. <br>
  267. <a name="SVICTattr"></a>
  268. <b>Attributes</b>
  269. <ul>
  270. <li><a href="#model">model</a> (SCD)</li>
  271. </ul>
  272. <br>
  273. </ul>
  274. =end html
  275. =cut