12_HProtocolGateway.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. # $Id: 12_HProtocolGateway.pm 17337 2018-09-13 15:15:11Z eisler $
  2. ####################################################################################################
  3. #
  4. # 12_HProtocolGateway.pm
  5. #
  6. # Copyright: Stephan Eisler
  7. # Email: fhem.dev@hausautomatisierung.co
  8. #
  9. # This file is part of fhem.
  10. #
  11. # Fhem is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # Fhem is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  23. #
  24. ####################################################################################################
  25. package main;
  26. use strict;
  27. use warnings;
  28. use DevIo;
  29. my @tankList = undef;
  30. my %sets = (
  31. 'readValues' => 1,
  32. );
  33. sub HProtocolGateway_Initialize($) {
  34. my ($hash) = @_;
  35. $hash->{Clients} = "HProtocolTank";
  36. $hash->{DefFn} = "HProtocolGateway_Define";
  37. $hash->{InitFn} = "HProtocolGateway_Init";
  38. $hash->{GetFn} = "HProtocolGateway_Get";
  39. $hash->{SetFn} = "HProtocolGateway_Set";
  40. $hash->{AttrFn} = "HProtocolGateway_Attr";
  41. $hash->{AttrList} = "device " .
  42. "baudrate:300,600,1200,2400,4800,9600 " .
  43. "parityBit:N,E,O " .
  44. "databitsLength:5,6,7,8 " .
  45. "stopBit:0,1" .
  46. "pollIntervalMins " .
  47. "path";
  48. }
  49. sub HProtocolGateway_Define($$) {
  50. my ($hash, $def) = @_;
  51. my @a = split("[ \t][ \t]*", $def);
  52. my $name = $hash->{NAME};
  53. return "Wrong syntax: use define <name> HProtocolGateway </dev/tty???>" if(int(@a) != 3);
  54. $hash->{helper}{DeviceName} = $a[2];
  55. $hash->{Clients} = "HProtocolTank";
  56. $hash->{STATE} = "Initialized";
  57. $attr{$name}{room} = "HProtocol";
  58. HProtocolGateway_DeviceConfig($hash);
  59. HProtocolGateway_Poll($hash) if defined(AttrVal($hash->{NAME}, 'pollIntervalMins', undef)); # if pollIntervalMins defind -> start timer
  60. return undef;
  61. }
  62. sub HProtocolGateway_Get($$@) {
  63. my ($hash, $name, $opt, @args) = @_;
  64. return "\"get $name\" needs at least one argument" unless(defined($opt));
  65. if ($opt eq "update") {
  66. HProtocolGateway_GetUpdate($hash);
  67. return "Done.";
  68. } else {
  69. # IMPORTANT! This defines the list of possible commands
  70. my $list = "update:noArg";
  71. return "Unknown argument $opt, choose one of $list";
  72. }
  73. return -1;
  74. }
  75. sub HProtocolGateway_Set($@) {
  76. my ($hash, @a) = @_;
  77. my $name = $a[0];
  78. my $cmd = $a[1];
  79. if(!defined($sets{$cmd})) {
  80. return 'Unknown argument ' . $cmd . ', choose one of ' . join(' ', keys %sets) . ":noArg"
  81. }
  82. if ($cmd eq 'readValues') {
  83. RemoveInternalTimer($hash);
  84. InternalTimer(gettimeofday() + 1, 'HProtocolGateway_GetUpdate', $hash, 0);
  85. }
  86. return undef
  87. }
  88. sub HProtocolGateway_GetUpdate($) {
  89. my ($hash) = @_;
  90. my $name = $hash->{NAME};
  91. foreach (@tankList) {
  92. my $tankHash = $_;
  93. my $mode = AttrVal($tankHash->{NAME},"mode","");
  94. my $command = "\$A";
  95. if ($mode eq "Volume") {
  96. $command = "\$B";
  97. } elsif ($mode eq "Ullage") {
  98. $command = "\$C";
  99. }
  100. my $hID = AttrVal($tankHash->{NAME},"hID","");
  101. my $msg = $command . $hID . "\r\n";
  102. DevIo_SimpleWrite($hash, $msg , 2);
  103. my ($err, $data) = HProtocolGateway_ReadAnswer($hash,$tankHash);
  104. Log3 $name, 5, "err:". $err;
  105. Log3 $name, 5, "data:". $data;
  106. }
  107. my $pollInterval = AttrVal($hash->{NAME}, 'pollIntervalMins', 0); #restore pollIntervalMins Timer
  108. InternalTimer(gettimeofday() + ($pollInterval * 60), 'HProtocolGateway_Poll', $hash, 0) if ($pollInterval > 0);
  109. }
  110. sub HProtocolGateway_ReadAnswer($$) {
  111. my ($hash,$tankHash) = @_;
  112. my $name = $hash->{NAME};
  113. return ("No FD (dummy device?)", undef)
  114. if(!$hash || ($^O !~ /Win/ && !defined($hash->{FD})));
  115. for(;;) {
  116. return ("Device lost when reading answer", undef)
  117. if(!$hash->{FD});
  118. my $rin = '';
  119. vec($rin, $hash->{FD}, 1) = 1;
  120. my $nfound = select($rin, undef, undef, 3);
  121. if($nfound <= 0) {
  122. next if ($! == EAGAIN() || $! == EINTR());
  123. my $err = ($! ? $! : "Timeout");
  124. return("ProtocolGateway_ReadAnswer $err", undef);
  125. }
  126. my $buf = DevIo_SimpleRead($hash);
  127. return ("No data", undef) if(!defined($buf));
  128. my $ret = HProtocolGateway_Read($hash, $buf,$tankHash);
  129. return (undef, $ret) if(defined($ret));
  130. }
  131. }
  132. sub HProtocolGateway_Read($@) {
  133. my ($hash, $data, $tankHash) = @_;
  134. my $name = $hash->{NAME};
  135. my $buffer = $hash->{PARTIAL};
  136. Log3 $name, 5, "HProtocolGateway ($name) - received $data (buffer contains: $buffer)";
  137. $buffer .= $data;
  138. my $msg;
  139. # as long as the buffer contains CR (complete datagramm)
  140. while($buffer =~ m/\r/)
  141. {
  142. ($msg, $buffer) = split("\r", $buffer, 2);
  143. chomp $msg;
  144. HProtocolGateway_ParseMessage($hash, $msg, $tankHash);
  145. }
  146. $hash->{PARTIAL} = $buffer;
  147. return $msg if(defined($data));
  148. return undef;
  149. }
  150. sub HProtocolGateway_ParseMessage($$) {
  151. my ($hash, $data, $tankHash) = @_;
  152. my $name = $hash->{NAME};
  153. $data =~ s/^.//; # remove #
  154. my ($tankdata,$water,$temperature,$probe_offset,$version,$error,$checksum)=split(/@/,$data);
  155. my $test = "#".$tankdata.$water.$temperature.$probe_offset.$version.$error;
  156. # calculate XOR CRC
  157. my $check = 0;
  158. $check ^= $_ for unpack 'C*', $test;
  159. # convert to HEX
  160. $check = sprintf '%02X', $check;
  161. # Unitronics Vision130
  162. if ($water == 0 && $temperature == 0 && $probe_offset == 0 && $version == 0 && $error == 0 && $checksum == 0 ) {
  163. $check = 0;
  164. }
  165. return if($check ne $checksum);
  166. my ($filllevel,$volume,$ullage) = (0,0,0);
  167. my $mode = AttrVal($tankHash->{NAME},"mode","");
  168. if ($mode eq "FillLevel") {
  169. $filllevel = $tankdata;
  170. $volume = HProtocolGateway_Tank($hash,$tankHash,$filllevel);
  171. } elsif ($mode eq "Volume") {
  172. $volume = $tankdata;
  173. } elsif ($mode eq "Ullage") {
  174. $ullage = $tankdata;
  175. }
  176. my $sign = substr($temperature,0,1);
  177. $temperature =~ s/^.//;
  178. if ($sign eq "-") { $temperature = int($temperature) * -1 };
  179. $temperature = $temperature / 10;
  180. $sign = substr($probe_offset,0,1);
  181. $probe_offset =~ s/^.//;
  182. if ($sign eq "-") { $probe_offset = int($probe_offset) * -1 };
  183. my $volume_15C = $volume * (1 + 0.00084 * ( 15 - $temperature ));
  184. $volume_15C = int($volume_15C);
  185. # Update all received readings
  186. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "ullage", $ullage);
  187. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "filllevel", $filllevel);
  188. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "volume", $volume);
  189. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "volume_15C", $volume_15C);
  190. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "temperature", $temperature);
  191. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "waterlevel", $water);
  192. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "probe_offset", $probe_offset);
  193. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "version", $version);
  194. HProtocolGateway_UpdateTankDevice($hash, $tankHash->{NAME}, "error", $error);
  195. }
  196. sub HProtocolGateway_UpdateTankDevice($$$$) {
  197. my ($hash, $tankName, $reading, $value) = @_;
  198. my $message = $tankName . " " . $reading . " " . $value;
  199. my $success = Dispatch($hash, $message, undef);
  200. if (!$success) {
  201. my $name = $hash->{NAME};
  202. Log3 $name, 1, "$name: failed to update tank device";
  203. }
  204. }
  205. sub HProtocolGateway_RegisterTank($) {
  206. my ($tankHash) = @_;
  207. # Remove undefined elements of empty array
  208. @tankList = grep defined, @tankList;
  209. @tankList = (@tankList, $tankHash);
  210. }
  211. # called when definition is undefined
  212. # (config reload, shutdown or delete of definition)
  213. sub HProtocolGateway_Undef($$)
  214. {
  215. my ($hash, $name) = @_;
  216. # close the connection
  217. DevIo_CloseDev($hash);
  218. return undef;
  219. }
  220. # called repeatedly if device disappeared
  221. sub HProtocolGateway_Ready($)
  222. {
  223. my ($hash) = @_;
  224. # try to reopen the connection in case the connection is lost
  225. return DevIo_OpenDev($hash, 1, "HProtocolGateway_Init");
  226. }
  227. # will be executed upon successful connection establishment (see DevIo_OpenDev())
  228. sub HProtocolGateway_Init($)
  229. {
  230. my ($hash) = @_;
  231. # send a status request to the device
  232. # DevIo_SimpleWrite($hash, "get_status\r\n", 2);
  233. return undef;
  234. }
  235. # Called during attribute create/change
  236. sub HProtocolGateway_Attr (@) {
  237. my ($command, $name, $attr, $val) = @_;
  238. my $hash = $defs{$name};
  239. my $msg = '';
  240. if ($attr eq 'poll_interval') {
  241. if (defined($val)) {
  242. if ($val =~ m/^(0*[1-9][0-9]*)$/) {
  243. RemoveInternalTimer($hash);
  244. HProtocolGateway_Poll($hash) if ($main::init_done);
  245. } else {
  246. $msg = 'Wrong poll intervall defined. pollIntervalMins must be a number > 0';
  247. }
  248. } else {
  249. RemoveInternalTimer($hash);
  250. }
  251. } elsif ($attr eq 'path') {
  252. $attr{$name}{path} = $val;
  253. } elsif ($attr eq 'baudrate') {
  254. $attr{$name}{baudrate} = $val;
  255. HProtocolGateway_DeviceConfig($hash);
  256. } elsif ($attr eq 'databitsLength') {
  257. $attr{$name}{databitsLength} = $val;
  258. HProtocolGateway_DeviceConfig($hash);
  259. } elsif ($attr eq 'parityBit') {
  260. $attr{$name}{parityBit} = $val;
  261. HProtocolGateway_DeviceConfig($hash);
  262. } elsif ($attr eq 'stopBit') {
  263. $attr{$name}{stopBit} = $val;
  264. HProtocolGateway_DeviceConfig($hash);
  265. }
  266. }
  267. sub HProtocolGateway_DeviceConfig($) {
  268. my ($hash) = @_;
  269. my $name = $hash->{NAME};
  270. my $deviceName = $hash->{helper}{DeviceName};
  271. $hash->{DeviceName} = $deviceName."@".$attr{$name}{baudrate}.",".$attr{$name}{databitsLength}.",".$attr{$name}{parityBit}.",".$attr{$name}{stopBit};
  272. DevIo_CloseDev($hash) if(DevIo_IsOpen($hash));
  273. my $ret = DevIo_OpenDev($hash, 0, "HProtocolGateway_Init");
  274. return $ret
  275. }
  276. # Request measurements regularly
  277. sub HProtocolGateway_Poll($) {
  278. my ($hash) = @_;
  279. my $name = $hash->{NAME};
  280. HProtocolGateway_Set($hash, ($name, 'readValues'));
  281. my $pollInterval = AttrVal($hash->{NAME}, 'pollIntervalMins', 0);
  282. if ($pollInterval > 0) {
  283. InternalTimer(gettimeofday() + ($pollInterval * 60), 'HProtocolGateway_Poll', $hash, 0);
  284. }
  285. }
  286. sub HProtocolGateway_Tank($$$) {
  287. my ($hash,$tankHash,$filllevel) = @_;
  288. my $name = $hash->{NAME};
  289. my $path = AttrVal($name,"path","");
  290. my $type = AttrVal($tankHash->{NAME},"type","");
  291. my %TankChartHash;
  292. open my $fh, '<', $path.$type or die "Cannot open: $!";
  293. while (my $line = <$fh>) {
  294. $line =~ s/\s*\z//;
  295. my @array = split /,/, $line;
  296. my $key = shift @array;
  297. $TankChartHash{$key} = $array[0];
  298. }
  299. close $fh;
  300. my $messwert = $filllevel/10;
  301. my $volume = 0;
  302. foreach my $level (sort keys %TankChartHash) {
  303. if ($level ne "level" && $messwert <= $level) {
  304. $volume = $TankChartHash{$level};
  305. last;
  306. }
  307. }
  308. return $volume;
  309. }
  310. 1;
  311. =pod
  312. =item summary support for the HLS 6010 Probes
  313. =begin html
  314. <a name="HProtocolGateway"></a>
  315. <h3>HProtocolGateway</h3>
  316. <ul>
  317. The HProtocolGateway is a fhem module for the RS232 standard interface for HLS 6010 Probes connected to a Hectronic OPTILEVEL Supply.
  318. <br /><br /><br />
  319. <a name="HProtocolGateway"></a>
  320. <b>Define</b>
  321. <ul>
  322. <code>define &lt;name&gt; HProtocolGateway /dev/tty???<br />
  323. attr &lt;name&gt; pollIntervalMins 2<br />
  324. attr &lt;name&gt; path /opt/fhem/<br />
  325. attr &lt;name&gt; baudrate 1200<br />
  326. attr &lt;name&gt; databitsLength 8<br />
  327. attr &lt;name&gt; parityBit N<br />
  328. attr &lt;name&gt; stopBit 1</code>
  329. <br />
  330. <br />
  331. Defines an HProtocolGateway connected to RS232 serial standard interface.<br /><br />
  332. path is the path for tank&lt;01&gt;.csv strapping table files.<br /><br />
  333. <code>
  334. level,volume<br />
  335. 10,16<br />
  336. 520,7781<br />
  337. 1330,29105<br />
  338. 1830,43403<br />
  339. 2070,49844<br />
  340. 2220,53580<br />
  341. 2370,57009<br />
  342. 2400,57650<br />
  343. 2430,58275<br />
  344. 2370,57009<br />
  345. 2400,57650<br />
  346. 2430,58275<br />
  347. </code>
  348. <br /><br />
  349. <a name="HProtocolGateway"></a>
  350. <b>Attributes</b>
  351. <ul>
  352. <li>pollIntervalMins<br />
  353. poll Interval in Mins</li>
  354. <li>path<br />
  355. Strapping Table csv file path</li>
  356. <li>baudrate<br />
  357. Baudrate / 300, 600, 1200, 2400, 4800, 9600</li>
  358. <li>databitsLength<br />
  359. Databits Length / 5, 6, 7, 8</li>
  360. <li>parityBit<br />
  361. Parity Bit / N, E, O</li>
  362. <li>stopBit<br />
  363. Stop Bit / 0, 1</li>
  364. </ul><br />
  365. </ul><br />
  366. </ul><br />
  367. =end html
  368. =cut