32_TechemHKV.pm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. ###############################################################################
  2. # $Id: 32_TechemHKV.pm 10660 2016-01-29 20:43:54Z herrmannj $
  3. #
  4. # this module is part of fhem under the same license
  5. # copyright 2015, joerg herrmann
  6. #
  7. # history
  8. # initial checkin
  9. #
  10. ###############################################################################
  11. package main;
  12. use strict;
  13. use warnings;
  14. use Time::HiRes qw(time);
  15. my %typeText = (
  16. '80' => 'Funkheizkostenverteiler data IIl'
  17. );
  18. sub
  19. TechemHKV_Initialize(@) {
  20. my ($hash) = @_;
  21. # require "Broker.pm";
  22. # TECHEM HKV
  23. # 61, 64 without T1 and T2
  24. $hash->{Match} = "^b..446850[\\d]{8}(61|64|69)80....A0.*";
  25. $hash->{DefFn} = "TechemHKV_Define";
  26. $hash->{UndefFn} = "TechemHKV_Undef";
  27. $hash->{SetFn} = "TechemHKV_Set";
  28. $hash->{GetFn} = "TechemHKV_Get";
  29. $hash->{NotifyFn} = "TechemHKV_Notify";
  30. $hash->{ParseFn} = "TechemHKV_Parse";
  31. $hash->{AttrList} = "".$readingFnAttributes;
  32. return undef;
  33. }
  34. sub
  35. TechemHKV_Define(@) {
  36. my ($hash, $def) = @_;
  37. my ($name, $t, $id);
  38. ($name, $t, $id, $def) = split(/ /, $def,4);
  39. return "ID must have 4 or 8 digits" if ($id !~ /^\d{4}(?:\d{4})?$/);
  40. my $lid = (length($id) == 8)?$id:undef;
  41. $id = (length($id) == 8)?substr($id,-4):$id;
  42. $modules{TechemHKV}{defptr}{$id} = $hash;
  43. $hash->{FRIENDLY} = $def if (defined($def));
  44. $hash->{LID} = $id if (length($id) == 8);
  45. # create crc table if required
  46. $data{WMBUS}{crc_table_13757} = TechemHKV_createCrcTable() unless (exists($data{WMBUS}{crc_table_13757}));
  47. # subscribe broadcast channels
  48. # TechemHKV_subscribe($hash, 'foo');
  49. TechemHKV_Run($hash) if $init_done;
  50. return undef;
  51. }
  52. sub
  53. TechemHKV_Undef(@) {
  54. my ($hash) = @_;
  55. return undef;
  56. }
  57. sub
  58. TechemHKV_Set(@) {
  59. my ($hash, $name, $cmd, @args) = @_;
  60. my $cnt = @args;
  61. return undef;
  62. }
  63. sub
  64. TechemHKV_Get(@) {
  65. my ($hash) = @_;
  66. return undef;
  67. }
  68. sub
  69. TechemHKV_Notify (@) {
  70. my ($hash, $ntfyDev) = @_;
  71. return unless (($ntfyDev->{TYPE} eq 'CUL') || ($ntfyDev->{TYPE} eq 'Global'));
  72. foreach my $event (@{$ntfyDev->{CHANGED}}) {
  73. my @e = split(' ', $event);
  74. next unless defined($e[0]);
  75. TechemHKV_Run($hash) if ($e[0] eq 'INITIALIZED');
  76. # patch CUL.pm
  77. TechemHKV_IOPatch($hash, $e[1]) if (($e[0] eq 'ATTR') && ($e[2] eq 'rfmode') && ($e[3] eq 'WMBus_T'));
  78. # disable receiver
  79. if (($e[0] eq 'ATTR') && ($e[2] eq 'rfmode') && ($e[3] ne 'WMBus_T')) {
  80. readingsBeginUpdate($hash);
  81. readingsBulkUpdate($hash, "state", "standby (IO missing)", 1);
  82. readingsBulkUpdate($hash, "temp1", "--.--") if exists($hash->{READINGS}->{'temp1'}); # exlude versions without t1,t2
  83. readingsBulkUpdate($hash, "temp2", "--.--") if exists($hash->{READINGS}->{'temp2'});
  84. readingsEndUpdate($hash, 1);
  85. }
  86. }
  87. return undef;
  88. }
  89. sub
  90. TechemHKV_Receive(@) {
  91. my ($hash, $msg) = @_;
  92. $hash->{longID} = $msg->{long} unless defined($hash->{longID});
  93. # TODO log collision if any ...
  94. my @t = localtime(time);
  95. my ($ats, $ts);
  96. $hash->{VERSION} = $msg->{version};
  97. $hash->{METER} = $typeText{$msg->{type}};
  98. delete $hash->{CHANGETIME}; # clean up, workaround for fhem prior http://forum.fhem.de/index.php/topic,47474.msg391964.html#msg391964
  99. if (($msg->{version} || '') eq '69') {
  100. readingsBeginUpdate($hash);
  101. readingsBulkUpdate($hash, "temp1", $msg->{temp1});
  102. readingsBulkUpdate($hash, "temp2", $msg->{temp2});
  103. readingsEndUpdate($hash, 1);
  104. }
  105. # day period changed
  106. $ats = ReadingsTimestamp($hash->{NAME},"current_period", "0");
  107. $ts = sprintf ("%02d-%02d-%02d 00:00:00", $msg->{actual}->{year}, $msg->{actual}->{month}, $msg->{actual}->{day});
  108. if ($ats ne $ts) {
  109. my $i;
  110. readingsBeginUpdate($hash);
  111. $hash->{".updateTimestamp"} = $ts;
  112. $i = $#{ $hash->{CHANGED} };
  113. readingsBulkUpdate($hash, "current_period", $msg->{actualVal});
  114. $hash->{CHANGETIME}->[$#{ $hash->{CHANGED} }] = $ts if ($#{ $hash->{CHANGED} } != $i ); # only add ts if there is a event to
  115. readingsEndUpdate($hash, 1);
  116. }
  117. # billing period changed
  118. $ats = ReadingsTimestamp($hash->{NAME},"previous_period", "0");
  119. $ts = sprintf ("20%02d-%02d-%02d 00:00:00", $msg->{last}->{year}, $msg->{last}->{month}, $msg->{last}->{day});
  120. if ($ats ne $ts) {
  121. my $i;
  122. readingsBeginUpdate($hash);
  123. $hash->{".updateTimestamp"} = $ts;
  124. $i = $#{ $hash->{CHANGED} };
  125. readingsBulkUpdate($hash, "previous_period", $msg->{lastVal});
  126. $hash->{CHANGETIME}->[$#{ $hash->{CHANGED} }] = $ts if ($#{ $hash->{CHANGED} } != $i ); # only add ts if there is a event to
  127. readingsEndUpdate($hash, 1);
  128. }
  129. return undef;
  130. }
  131. sub
  132. TechemHKV_Run(@) {
  133. my ($hash) = @_;
  134. # find a CUL
  135. foreach my $d (keys %defs) {
  136. # live patch CUL.pm
  137. TechemHKV_IOPatch($hash, $d) if ($defs{$d}{TYPE} eq "CUL");
  138. }
  139. return undef;
  140. }
  141. # live patch CUL.pm, aka THE HACK
  142. sub
  143. TechemHKV_IOPatch(@) {
  144. my ($hash, $iodev) = @_;
  145. return undef unless (AttrVal($iodev, "rfmode", '') eq "WMBus_T");
  146. # see if already patched
  147. readingsSingleUpdate($hash, "state", "listening", 1);
  148. return undef if ($defs{$iodev}{Clients} =~ /TechemHKV/ );
  149. $defs{$iodev}{Clients} = ":TechemHKV".$defs{$iodev}{Clients};
  150. $defs{$iodev}{'.clientArray'} = undef;
  151. return undef;
  152. }
  153. sub
  154. TechemHKV_Parse(@) {
  155. my ($iohash, $msg) = @_;
  156. my ($message, $rssi);
  157. ($msg, $rssi) = split (/::/, $msg);
  158. $msg = TechemHKV_SanityCheck($msg);
  159. return ('') unless $msg;
  160. my @m = ($msg =~ m/../g);
  161. # parse
  162. ($message->{long}, $message->{short}) = TechemHKV_ParseID(@m);
  163. $message->{type} = TechemHKV_ParseSubType(@m);
  164. $message->{version} = TechemHKV_ParseSubVersion(@m);
  165. $message->{lastVal} = TechemHKV_ParseLastPeriod(@m);
  166. $message->{actualVal} = TechemHKV_ParseActualPeriod(@m);
  167. $message->{temp1} = TechemHKV_ParseT1(@m);
  168. $message->{temp2} = TechemHKV_ParseT2(@m);
  169. ($message->{actual}->{year}, $message->{actual}->{month}, $message->{actual}->{day}) = TechemHKV_ParseActualDate(@m);
  170. ($message->{last}->{year}, $message->{last}->{month}, $message->{last}->{day}) = TechemHKV_ParseLastDate(@m);
  171. # dispatch
  172. if (exists($modules{TechemHKV}{defptr}{$message->{short}})) {
  173. my $deviceHash = $modules{TechemHKV}{defptr}{$message->{short}};
  174. TechemHKV_Receive($deviceHash, $message);
  175. return ($deviceHash->{NAME});
  176. }
  177. # broadcast
  178. return ('');
  179. }
  180. sub
  181. TechemHKV_SanityCheck(@) {
  182. my ($msg) = @_;
  183. my $rssi;
  184. my $t;
  185. my $dbg = 4;
  186. #($msg, $rssi) = split (/::/, $msg);
  187. my @m = ((substr $msg,1) =~ m/../g);
  188. # at least 3 chars
  189. if (length($msg) < 3) {
  190. Log3 ("TechemHKV", $dbg, "msg incomplete $msg");
  191. return undef;
  192. }
  193. # msg length without crc blocks
  194. my $l = hex(substr $msg, 1, 2) + 1;
  195. # full crc payload blocks
  196. my $fb = int(($l - 10) / 16);
  197. # remaining bytes ?
  198. my $rb = ($l - 10) % 16;
  199. # required len
  200. my $rl = $l + 2 + ($fb * 2) + (($rb)?2:0);
  201. if (($rl * 2) > (length($msg) -1)) {
  202. Log3 ("TechemHKV", $dbg, "msg incomplete $msg");
  203. return undef;
  204. }
  205. # CRC first 10 byte, then chunks of 16 byte then remaining
  206. if ((substr $msg, 21, 4) ne TechemHKV_crc16_13757(substr $msg, 1, 20)) {
  207. Log3 ("TechemHKV", $dbg, "crc error $msg");
  208. return undef;
  209. } else {
  210. $t = substr $msg, 3, 18;
  211. }
  212. for (my $i = 0; $i<$fb; $i++) {
  213. if ((substr $msg, 57 + ($i * 36), 4) ne TechemHKV_crc16_13757(substr $msg, 25 + ($i * 36), 32)) {
  214. Log3 ("TechemHKV", $dbg, "crc error $msg");
  215. return undef;
  216. } else {
  217. $t .= substr $msg, 25 + ($i * 36), 32;
  218. }
  219. }
  220. if ($rb) {
  221. if ((substr $msg, 25 + ($fb * 36) + ($rb * 2), 4) ne TechemHKV_crc16_13757(substr $msg, 25 + ($fb * 36), $rb * 2)) {
  222. Log3 ("TechemHKV", $dbg, "crc error $msg");
  223. return undef;
  224. } else {
  225. $t .= substr $msg, 25 + ($fb * 36), ($rb * 2);
  226. }
  227. }
  228. return $t;
  229. }
  230. sub
  231. TechemHKV_ParseID(@) {
  232. my @m = @_;
  233. return ("$m[6]$m[5]$m[4]$m[3]", "$m[4]$m[3]");
  234. }
  235. sub
  236. TechemHKV_ParseSubType(@) {
  237. my @m = @_;
  238. return "$m[8]";
  239. }
  240. sub
  241. TechemHKV_ParseSubVersion(@) {
  242. my @m = @_;
  243. return "$m[7]";
  244. }
  245. sub
  246. TechemHKV_ParseLastPeriod(@) {
  247. my @m = @_;
  248. return hex("$m[14]$m[13]");
  249. }
  250. sub
  251. TechemHKV_ParseActualPeriod(@) {
  252. my @m = @_;
  253. return hex("$m[18]$m[17]");
  254. }
  255. sub
  256. TechemHKV_ParseT1(@) {
  257. my @m = @_;
  258. return sprintf "%.2f", (hex("$m[20]$m[19]") / 100);
  259. }
  260. sub
  261. TechemHKV_ParseT2(@) {
  262. my @m = @_;
  263. return sprintf "%.2f", (hex("$m[22]$m[21]") / 100);
  264. }
  265. sub
  266. TechemHKV_ParseActualDate(@) {
  267. my @m = @_;
  268. my @t = localtime(time);
  269. my $b = hex("$m[16]$m[15]");
  270. my $d = ($b >> 4) & 0x1F;
  271. my $m = ($b >> 9) & 0x0F;
  272. my $y = $t[5] + 1900;
  273. return ($y, $m, $d);
  274. }
  275. sub
  276. TechemHKV_ParseLastDate(@) {
  277. my @m = @_;
  278. my $b = hex("$m[12]$m[11]");
  279. my $d = ($b >> 0) & 0x1F;
  280. my $m = ($b >> 5) & 0x0F;
  281. my $y = ($b >> 9) & 0x3F;
  282. return ($y, $m, $d);
  283. }
  284. sub
  285. TechemHKV_createCrcTable(@) {
  286. my $poly = 0x3D65;
  287. my $c;
  288. my @table;
  289. for (my $i=0; $i<256; $i++) {
  290. $c = ($i << 8);
  291. for (my $j=0; $j<8; $j++) {
  292. if (($c & 0x8000) != 0) {
  293. $c = 0xFFFF & (($c << 1) ^ $poly);
  294. } else {
  295. $c <<= 1;
  296. }
  297. }
  298. $table[$i] = $c;
  299. }
  300. return \@table;
  301. }
  302. sub
  303. TechemHKV_crc16_13757(@) {
  304. my ($msg) = @_;
  305. my @table = @{$data{WMBUS}{crc_table_13757}};
  306. my @in = split '', pack 'H*', $msg;
  307. my $crc = 0x0000;
  308. for (my $i=0; $i<int(@in); $i++) {
  309. $crc = 0xffff & ( ($crc << 8) ^ $table[(($crc >> 8) ^ ord($in[$i]))] );
  310. }
  311. return sprintf ("%04lX", $crc ^ 0xFFFF);
  312. }
  313. # message bus ahead
  314. # sub
  315. #TechemHKV_subscribe(@) {
  316. # my ($hash, $topic) = @_;
  317. # broker::subscribe ($topic, $hash->{NAME}, \&TechemHKV_rcvBCST);
  318. # return undef;
  319. #}
  320. #sub
  321. #TechemHKV_sendBCST(@) {
  322. # my ($hash, $topic, $msg) = @_;
  323. # broker::publish ($topic, $hash->{NAME}, $msg);
  324. # return undef;
  325. #}
  326. #sub
  327. #TechemHKV_rcvBCST(@) {
  328. # my ($name, $topic, $sender, $msg) = @_;
  329. # my $hash = $defs{$name};
  330. # return undef;
  331. #}
  332. 1;
  333. =pod
  334. =begin html
  335. <a name="TechemHKV"></a>
  336. <h3>TechemHKV</h3>
  337. <ul>
  338. This module reads the transmission of techem data meter for heating device.
  339. <br><br>
  340. It will display
  341. <ul>
  342. <li>meter data for current billing period</li>
  343. <li>meter data for previous billing period including date of request</li>
  344. <li>both temperature sensors (if supported by data meter)</li>
  345. </ul>
  346. <br>
  347. It will require a CUL in WMBUS_T mode, although the CUL may temporary set into that mode.
  348. The module keeps track of the CUL rfmode.
  349. <br>
  350. <br>
  351. <a name="TechemHKV_Define"></a>
  352. <b>Define</b>
  353. <br>
  354. <code>define &lt;name&gt; TechemHKV &lt;4|8 digit ID&gt; [&lt;speaking name&gt;]</code>
  355. <ul>
  356. <li>ID: 4 digit ID displayed at techem or 8 digit as printed on bill</li>
  357. <li>speaking name: (optional) human readable identification</li>
  358. </ul>
  359. <br>
  360. <a name="TechemHKV_Readings"></a>
  361. <b>Readings</b>
  362. <ul>
  363. <li>current_period: meter data for current billing period
  364. <br><i>unit-less data, cumulated since start of the current billing period. The reading will be updated once a day, after receiving the first update. Reading time will reflect the time of data (not the time where they were received)</i></br>
  365. </li>
  366. <li>previous_period: meter data for last billing period
  367. <br><i>unit-less data, sum of the last billing period. The reading will be updated only if a new billing period starts. Reading time will reflect the last day of previous billing period (not the time where they were received)</i></br>
  368. </li>
  369. <li>temp1: ambient temperature</li>
  370. <li>temp2: heater surface temperature</li>
  371. <br>
  372. </ul>
  373. <a name="TechemHKV_Internals"></a>
  374. <b>Internals</b>
  375. <ul>
  376. <li>friendly: human readable identification of meter as specified by define</li>
  377. <li>longID: 8 digit id of meter</li>
  378. <br>
  379. </ul>
  380. </ul>
  381. =end html
  382. =begin html_DE
  383. <a name="TechemHKV"></a>
  384. <h3>TechemHKV</h3>
  385. <ul>
  386. Das modul empfängt Daten eines Techem Heizkostenverteilers.
  387. <br><br>
  388. Empfangen werden
  389. <ul>
  390. <li>Wert des aktuellen Abrechnungszeitraumes</li>
  391. <li>Wert des vorhergehenden Abrechnungszeitraumes einschließlich des Ablesedatums</li>
  392. <li>Beide Temperatur Sensoren (sofern der Heizkostenverteiler sie sendet)</li>
  393. </ul>
  394. <br>
  395. Zum Empfang wird ein CUL im WMBUS_T mode benötigt. Dabei ist es ausreichend ihn vorrübergehend in diesen Modus zu schalten.
  396. Das Modul überwacht den rfmode aller verfügbaren CUL
  397. <br>
  398. <br>
  399. <a name="TechemHKV_Define"></a>
  400. <b>Define</b>
  401. <br>
  402. <code>define &lt;name&gt; TechemHKV &lt;4|8 digit ID&gt; [&lt;speaking name&gt;]</code>
  403. <ul>
  404. <li>ID: 4 Ziffern wie auf dem Heizkostenverteiler angezeigt oder 8 Ziffern aus der Abrechnung</li>
  405. <li>speaking name: (optional) Bezeichnung</li>
  406. </ul>
  407. <br>
  408. <a name="TechemHKV_Readings"></a>
  409. <b>Readings</b>
  410. <ul>
  411. <li>current_period: Wert des aktuellen Abrechnungszeitraumes
  412. <br><i>Der kumulierte (einheitenlose) Verbrauch seid dem Start des aktuellen Abrechnungszeitraumes. Das reading wird einmal am Tag aktualisiert. Die Zeit kennzeichnet den Stand der Daten. (und nicht den Empfangszeitpunkt der Daten)</i></br>
  413. </li>
  414. <li>previous_period: Summe des letzten Abrechnungszeitraum
  415. <br><i>Die (einheitenlose) Summe der Verbauchs im gesamten letzten Abrechnungszeitraum. Das reading wird jeweils zu Beginn eines neuen Abrechnungszeitraumes aktualisiert. Die Zeit kennzeichnet das Ablesedatum also das Ende des vorherigen Abrechnugszeitraumes. (und nicht den Empfangszeitpunkt der Daten)</i></br>
  416. </li>
  417. <li>temp1: Umgebungstemperatur</li>
  418. <li>temp2: Oberflächentemperatur des Heizkörpers</li>
  419. <br>
  420. </ul>
  421. <a name="TechemHKV_Internals"></a>
  422. <b>Internals</b>
  423. <ul>
  424. <li>friendly: die beim define übergebene, zusätzliche Bezeichnung</li>
  425. <li>longID: 8 Ziffern ID des Heizkostenverteilers</li>
  426. <br>
  427. </ul>
  428. </ul>
  429. =end html_DE
  430. =cut