36_LaCrosseGateway.pm 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. # $Id: 36_LaCrosseGateway.pm 15483 2017-11-23 20:03:23Z HCS $
  2. package main;
  3. use strict;
  4. use warnings;
  5. use Time::HiRes qw(gettimeofday);
  6. use Time::Local;
  7. my $clients = ":PCA301:EC3000:LaCrosse:Level:EMT7110:KeyValueProtocol";
  8. my %matchList = (
  9. "1:PCA301" => "^\\S+\\s+24",
  10. "2:EC3000" => "^\\S+\\s+22",
  11. "3:LaCrosse" => "^(\\S+\\s+9 |OK\\sWS\\s)",
  12. "4:EMT7110" => "^OK\\sEMT7110\\s",
  13. "5:Level" => "^OK\\sLS\\s",
  14. "6:KeyValueProtocol" => "^OK\\sVALUES\\s",
  15. );
  16. sub LaCrosseGateway_Initialize($) {
  17. my ($hash) = @_;
  18. require "$attr{global}{modpath}/FHEM/DevIo.pm";
  19. $hash->{ReadFn} = "LaCrosseGateway_Read";
  20. $hash->{WriteFn} = "LaCrosseGateway_Write";
  21. $hash->{ReadyFn} = "LaCrosseGateway_Ready";
  22. $hash->{NotifyFn} = "LaCrosseGateway_Notify";
  23. $hash->{DefFn} = "LaCrosseGateway_Define";
  24. $hash->{FingerprintFn} = "LaCrosseGateway_Fingerprint";
  25. $hash->{UndefFn} = "LaCrosseGateway_Undef";
  26. $hash->{SetFn} = "LaCrosseGateway_Set";
  27. $hash->{AttrFn} = "LaCrosseGateway_Attr";
  28. $hash->{AttrList} = " Clients"
  29. ." MatchList"
  30. ." dummy"
  31. ." initCommands"
  32. ." timeout"
  33. ." watchdog"
  34. ." disable:0,1"
  35. ." tftFile"
  36. ." kvp:dispatch,readings,both"
  37. ." loopTimeReadings:1,0"
  38. ." ownSensors:dispatch,readings,both"
  39. ." mode:USB,WiFi,Cable"
  40. ." usbFlashCommand"
  41. ." filter"
  42. ." $readingFnAttributes";
  43. }
  44. #=======================================================================================
  45. sub LaCrosseGateway_Fingerprint($$) {
  46. }
  47. #=======================================================================================
  48. sub LaCrosseGateway_Notify($$) {
  49. my ($hash, $source_hash) = @_;
  50. my $name = $hash->{NAME};
  51. my $sourceName = $source_hash->{NAME};
  52. my $events = deviceEvents($source_hash, 1);
  53. if($sourceName eq "global" && grep(m/^INITIALIZED|REREADCFG$/, @{$events})) {
  54. LaCrosseGateway_Connect($hash)
  55. }
  56. }
  57. #=======================================================================================
  58. sub LaCrosseGateway_Define($$) {my ($hash, $def) = @_;
  59. my @a = split("[ \t][ \t]*", $def);
  60. if(@a != 3) {
  61. my $msg = "wrong syntax: define <name> LaCrosseGateway {none | devicename[\@baudrate] | devicename\@directio | hostname:port}";
  62. Log3 undef, 2, $msg;
  63. return $msg;
  64. }
  65. DevIo_CloseDev($hash);
  66. my $name = $a[0];
  67. my $dev = $a[2];
  68. $hash->{Clients} = $clients;
  69. $hash->{MatchList} = \%matchList;
  70. $hash->{TIMEOUT} = 0.5;
  71. if( !defined( $attr{$name}{usbFlashCommand} ) ) {
  72. $attr{$name}{usbFlashCommand} = "./FHEM/firmware/esptool.py -b 921600 -p [PORT] write_flash -ff 80m -fm dio -fs 4MB-c1 0x00000 [BINFILE] > [LOGFILE]"
  73. }
  74. if($dev eq "none") {
  75. Log3 $name, 1, "$name device is none, commands will be echoed only";
  76. $attr{$name}{dummy} = 1;
  77. return undef;
  78. }
  79. $dev .= "\@57600" if( $dev !~ m/\@/ && $def !~ m/:/ );
  80. $hash->{DeviceName} = $dev;
  81. LaCrosseGateway_Connect($hash) if($init_done);
  82. return undef;
  83. }
  84. #=======================================================================================
  85. sub LaCrosseGateway_Undef($$) {
  86. my ($hash, $arg) = @_;
  87. my $name = $hash->{NAME};
  88. BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID}));
  89. if($hash->{STATE} ne "disconnected") {
  90. DevIo_CloseDev($hash);
  91. }
  92. return undef;
  93. }
  94. #=======================================================================================
  95. sub LaCrosseGateway_RemoveLaCrossePair($) {
  96. my $hash = shift;
  97. delete($hash->{LaCrossePair});
  98. }
  99. #=======================================================================================
  100. sub LaCrosseGateway_LogOTA($) {
  101. my($text) = @_;
  102. BlockingInformParent("DoTrigger", ["global", " $text"], 0);
  103. }
  104. #=======================================================================================
  105. sub LaCrosseGateway_StartUpload($) {
  106. my ($string) = @_;
  107. my ($name, $argument) = split("\\|", $string);
  108. my $hash = $defs{$name};
  109. sleep(2);
  110. LaCrosseGateway_LogOTA("Started not blocking");
  111. my @deviceName = split('@', $hash->{DeviceName});
  112. my $port = $deviceName[0];
  113. my $logFile = AttrVal("global", "logdir", "./log") . "/LaCrosseGatewayFlash.log";
  114. my $hexFile;
  115. if($hash->{model} =~ m/^\[LaCrosseGateway32 V/) {
  116. $hexFile = "./FHEM/firmware/LaCrosseGateway32.bin";
  117. }
  118. else {
  119. $hexFile = "./FHEM/firmware/JeeLink_LaCrosseGateway.bin";
  120. }
  121. if(!-e $hexFile) {
  122. LaCrosseGateway_LogOTA("The file '$hexFile' does not exist");
  123. return $name;
  124. }
  125. LaCrosseGateway_LogOTA("flashing LaCrosseGateway $name");
  126. LaCrosseGateway_LogOTA("hex file: $hexFile");
  127. if(AttrVal($name, "mode", "WiFi") eq "WiFi") {
  128. eval "use LWP::UserAgent";
  129. if($@) {
  130. LaCrosseGateway_LogOTA("ERROR: Please install LWP::UserAgent");
  131. return $name;
  132. }
  133. eval "use HTTP::Request::Common";
  134. if($@) {
  135. LaCrosseGateway_LogOTA("ERROR: Please install HTTP::Request::Common");
  136. return $name;
  137. }
  138. LaCrosseGateway_LogOTA("Mode is LaCrosseGateway OTA-update");
  139. DevIo_CloseDev($hash);
  140. readingsSingleUpdate($hash, "state", "disconnected", 1);
  141. LaCrosseGateway_LogOTA("$name closed");
  142. my @spl = split(':', $hash->{DeviceName});
  143. my $targetIP = $spl[0];
  144. my $targetURL = "http://" . $targetIP . "/ota/firmware.bin";
  145. LaCrosseGateway_LogOTA("target: $targetURL");
  146. my $request = POST($targetURL, Content_Type => 'multipart/form-data', Content => [ file => [$hexFile, "firmware.bin"] ]);
  147. my $userAgent = LWP::UserAgent->new;
  148. $userAgent->timeout(120);
  149. LaCrosseGateway_LogOTA("Upload started, please wait a minute or two ...");
  150. my $response = $userAgent->request($request);
  151. if ($response->is_success) {
  152. LaCrosseGateway_LogOTA("");
  153. LaCrosseGateway_LogOTA("--- LGW reports ---------------------------------------------------------------------------");
  154. my @lines = split /\r\n|\n|\r/, $response->decoded_content;
  155. foreach (@lines) {
  156. LaCrosseGateway_LogOTA($_);
  157. }
  158. LaCrosseGateway_LogOTA("----------------------------------------------------------------------------------------------------");
  159. }
  160. else {
  161. LaCrosseGateway_LogOTA("");
  162. LaCrosseGateway_LogOTA("ERROR: " . $response->code);
  163. my @lines = split /\r\n|\n|\r/, $response->decoded_content;
  164. foreach (@lines) {
  165. LaCrosseGateway_LogOTA($_);
  166. }
  167. }
  168. }
  169. else {
  170. LaCrosseGateway_LogOTA("Mode is LaCrosseGateway USB-update");
  171. my $usbFlashCommand = AttrVal($name, "usbFlashCommand", "");
  172. if ($usbFlashCommand ne "") {
  173. my $command = $usbFlashCommand;
  174. $command =~ s/\Q[PORT]\E/$port/g;
  175. $command =~ s/\Q[BINFILE]\E/$hexFile/g;
  176. $command =~ s/\Q[LOGFILE]\E/$logFile/g;
  177. LaCrosseGateway_LogOTA("");
  178. LaCrosseGateway_LogOTA("Upload started, please wait a minute or two ...");
  179. `$command`;
  180. if (-e $logFile) {
  181. LaCrosseGateway_LogOTA("");
  182. LaCrosseGateway_LogOTA("--- esptool ---------------------------------------------------------------------------------");
  183. local $/ = undef;
  184. open FILE, $logFile;
  185. my $content = <FILE>;
  186. my @lines = split /\r\n|\n|\r/, $content;
  187. foreach (@lines) {
  188. LaCrosseGateway_LogOTA($_);
  189. }
  190. LaCrosseGateway_LogOTA("--- esptool ---------------------------------------------------------------------------------");
  191. LaCrosseGateway_LogOTA("");
  192. }
  193. else {
  194. LaCrosseGateway_LogOTA("WARNING: esptool created no log file");
  195. }
  196. }
  197. else {
  198. LaCrosseGateway_LogOTA("No usbFlashCommand found. Please define this attribute.");
  199. }
  200. }
  201. return $name;
  202. }
  203. #=======================================================================================
  204. sub LaCrosseGateway_UploadDone($) {
  205. my ($name) = @_;
  206. return unless(defined($name));
  207. my $hash = $defs{$name};
  208. delete($hash->{helper}{RUNNING_PID});
  209. delete($hash->{helper}{FLASHING});
  210. LaCrosseGateway_Connect($hash);
  211. LaCrosseGateway_LogOTA("$name opened");
  212. LaCrosseGateway_LogOTA("Finshed");
  213. }
  214. #=======================================================================================
  215. sub LaCrosseGateway_UploadError($) {
  216. my ($hash) = @_;
  217. my $name = $hash->{NAME};
  218. delete($hash->{helper}{RUNNING_PID});
  219. delete($hash->{helper}{FLASHING});
  220. LaCrosseGateway_LogOTA("Upload failed");
  221. LaCrosseGateway_Connect($hash);
  222. LaCrosseGateway_LogOTA("$name opened");
  223. }
  224. #=======================================================================================
  225. sub LaCrosseGateway_StartNextion($) {
  226. my ($string) = @_;
  227. my ($name, $argument) = split("\\|", $string);
  228. my $hash = $defs{$name};
  229. sleep(2);
  230. LaCrosseGateway_LogOTA("Started not blocking");
  231. my @deviceName = split('@', $hash->{DeviceName});
  232. my $port = $deviceName[0];
  233. my $logFile = AttrVal("global", "logdir", "./log") . "/NextionUpload.log";
  234. my $tftFile = AttrVal($name, "tftFile", "./FHEM/firmware/nextion.tft");
  235. if(!-e $tftFile) {
  236. LaCrosseGateway_LogOTA("The file '$tftFile' does not exist");
  237. return $name;
  238. }
  239. LaCrosseGateway_LogOTA("upload Nextion firmware to $name");
  240. LaCrosseGateway_LogOTA("tft file: $tftFile");
  241. eval "use LWP::UserAgent";
  242. if($@) {
  243. LaCrosseGateway_LogOTA("ERROR: Please install LWP::UserAgent");
  244. return $name;
  245. }
  246. eval "use HTTP::Request::Common";
  247. if($@) {
  248. LaCrosseGateway_LogOTA("ERROR: Please install HTTP::Request::Common");
  249. return $name;
  250. }
  251. my @spl = split(':', $hash->{DeviceName});
  252. my $targetIP = $spl[0];
  253. my $targetURL = "http://" . $targetIP . "/ota/nextion";
  254. LaCrosseGateway_LogOTA("target: $targetURL");
  255. my $request = POST($targetURL, Content_Type => 'multipart/form-data', Content => [ file => [$tftFile, "nextion.tft"] ]);
  256. my $userAgent = LWP::UserAgent->new;
  257. $userAgent->timeout(900);
  258. LaCrosseGateway_LogOTA("");
  259. LaCrosseGateway_LogOTA("Upload started, this can take 10 minutes or more ...");
  260. my $response = $userAgent->request($request);
  261. if ($response->is_success) {
  262. LaCrosseGateway_LogOTA("");
  263. LaCrosseGateway_LogOTA("--- LGW reports ---------------------------------------------------------------------------");
  264. my @lines = split /\r\n|\n|\r/, $response->decoded_content;
  265. foreach (@lines) {
  266. LaCrosseGateway_LogOTA($_);
  267. }
  268. LaCrosseGateway_LogOTA("----------------------------------------------------------------------------------------------------");
  269. }
  270. else {
  271. LaCrosseGateway_LogOTA("");
  272. LaCrosseGateway_LogOTA("ERROR: " . $response->code);
  273. my @lines = split /\r\n|\n|\r/, $response->decoded_content;
  274. foreach (@lines) {
  275. LaCrosseGateway_LogOTA($_);
  276. }
  277. }
  278. return $name;
  279. }
  280. #=======================================================================================
  281. sub LaCrosseGateway_Set($@) {
  282. my ($hash, @a) = @_;
  283. my $name = shift @a;
  284. my $cmd = shift @a;
  285. my $arg = join(" ", @a);
  286. my $list = "raw connect LaCrossePairForSec flash nextionUpload parse reboot";
  287. return $list if( $cmd eq '?' || $cmd eq '');
  288. if ($cmd eq "raw") {
  289. Log3 $name, 4, "set $name $cmd $arg";
  290. LaCrosseGateway_SimpleWrite($hash, $arg);
  291. }
  292. elsif ($cmd eq "flash") {
  293. CallFn("WEB", "ActivateInformFn", $hash, "global");
  294. DevIo_CloseDev($hash);
  295. readingsSingleUpdate($hash, "state", "disconnected", 1);
  296. $hash->{helper}{FLASHING} = 1;
  297. $hash->{helper}{RUNNING_PID} = BlockingCall("LaCrosseGateway_StartUpload", $name . "|" . $arg, "LaCrosseGateway_UploadDone", 300, "LaCrosseGateway_UploadError", $hash);
  298. return undef;
  299. }
  300. elsif ($cmd eq "nextionUpload") {
  301. CallFn("WEB", "ActivateInformFn", $hash, "global");
  302. DevIo_CloseDev($hash);
  303. readingsSingleUpdate($hash, "state", "disconnected", 1);
  304. $hash->{helper}{FLASHING} = 1;
  305. $hash->{helper}{RUNNING_PID} = BlockingCall("LaCrosseGateway_StartNextion", $name . "|" . $arg, "LaCrosseGateway_UploadDone", 1200, "LaCrosseGateway_UploadError", $hash);
  306. return undef;
  307. }
  308. elsif ($cmd eq "LaCrossePairForSec") {
  309. my @args = split(' ', $arg);
  310. return "Usage: set $name LaCrossePairForSec <seconds_active> [ignore_battery]" if(!$arg || $args[0] !~ m/^\d+$/ || ($args[1] && $args[1] ne "ignore_battery") );
  311. $hash->{LaCrossePair} = $args[1]?2:1;
  312. InternalTimer(gettimeofday()+$args[0], "LaCrosseGateway_RemoveLaCrossePair", $hash, 0);
  313. }
  314. elsif ($cmd eq "connect") {
  315. DevIo_CloseDev($hash);
  316. return LaCrosseGateway_Connect($hash);
  317. }
  318. elsif ($cmd eq "reboot") {
  319. if(AttrVal($name, "mode", "WiFi") eq "WiFi") {
  320. LaCrosseGateway_SimpleWrite($hash, "8377e\n");
  321. }
  322. else {
  323. my $po = $hash->{USBDev};
  324. $po->rts_active(0);
  325. $po->dtr_active(0);
  326. select undef, undef, undef, 0.01;
  327. $po->rts_active(1);
  328. $po->dtr_active(1);
  329. }
  330. LaCrosseGateway_Connect($hash);
  331. }
  332. elsif ($cmd eq "parse") {
  333. LaCrosseGateway_Parse($hash, $hash, $name, $arg);
  334. }
  335. else {
  336. return "Unknown argument $cmd, choose one of ".$list;
  337. }
  338. return undef;
  339. }
  340. #=======================================================================================
  341. sub LaCrosseGateway_OnInitTimer($) {
  342. my ($hash) = @_;
  343. my $name = $hash->{NAME};
  344. LaCrosseGateway_SimpleWrite($hash, "v\n");
  345. }
  346. #=======================================================================================
  347. sub LaCrosseGateway_DoInit($) {
  348. my $hash = shift;
  349. my $name = $hash->{NAME};
  350. my $enabled = AttrVal($name, "disable", "0") != "1" && !defined($hash->{helper}{FLASHING});
  351. if($enabled) {
  352. readingsSingleUpdate($hash, "state", "opened", 1);
  353. if(AttrVal($name, "mode", "") ne "USB") {
  354. InternalTimer(gettimeofday() +3, "LaCrosseGateway_OnInitTimer", $hash, 1);
  355. }
  356. }
  357. else {
  358. readingsSingleUpdate($hash, "state", "disabled", 1);
  359. }
  360. return undef;
  361. }
  362. #=======================================================================================
  363. sub LaCrosseGateway_Ready($) {
  364. my ($hash) = @_;
  365. my $name = $hash->{NAME};
  366. LaCrosseGateway_Connect($hash, 1);
  367. # This is relevant for windows/USB only
  368. my $po = $hash->{USBDev};
  369. my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags);
  370. if($po) {
  371. ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status;
  372. }
  373. return ($InBytes && $InBytes>0);
  374. }
  375. #=======================================================================================
  376. sub LaCrosseGateway_Write($$) {
  377. my ($hash, $cmd, $msg) = @_;
  378. my $name = $hash->{NAME};
  379. my $arg = $cmd;
  380. $arg .= " " . $msg if(defined($msg));
  381. LaCrosseGateway_SimpleWrite($hash, $arg);
  382. }
  383. #=======================================================================================
  384. sub LaCrosseGateway_Read($) {
  385. my ($hash) = @_;
  386. my $name = $hash->{NAME};
  387. my $buf = DevIo_SimpleRead($hash);
  388. return "" if(!defined($buf));
  389. my $data = $hash->{PARTIAL};
  390. $data .= $buf;
  391. while($data =~ m/\n/) {
  392. my $rmsg;
  393. ($rmsg,$data) = split("\n", $data, 2);
  394. $rmsg =~ s/\r//;
  395. LaCrosseGateway_Parse($hash, $hash, $name, $rmsg) if($rmsg);
  396. }
  397. $hash->{PARTIAL} = $data;
  398. }
  399. #=======================================================================================
  400. sub LaCrosseGateway_DeleteKVPReadings($) {
  401. my ($hash) = @_;
  402. delete $hash->{READINGS}{"FramesPerMinute"};
  403. delete $hash->{READINGS}{"RSSI"};
  404. delete $hash->{READINGS}{"UpTime"};
  405. }
  406. #=======================================================================================
  407. sub LaCrosseGateway_HandleKVP($$) {
  408. my ($hash, $kvp) = @_;
  409. my $name = $hash->{NAME};
  410. $kvp .= ",";
  411. readingsBeginUpdate($hash);
  412. if($kvp =~ m/UpTimeText=(.*?)(\,|\ ,)/) {
  413. readingsBulkUpdate($hash, "UpTime", $1);
  414. }
  415. if($kvp =~ m/RSSI=(.*?)(\,|\ ,)/) {
  416. readingsBulkUpdate($hash, "RSSI", $1);
  417. }
  418. if($kvp =~ m/FramesPerMinute=(.*?)(\,|\ ,)/) {
  419. readingsBulkUpdate($hash, "FramesPerMinute", $1);
  420. }
  421. if($kvp =~ m/ReceivedFrames=(.*?)(\,|\ ,)/) {
  422. readingsBulkUpdate($hash, "ReceivedFrames", $1);
  423. }
  424. if($kvp =~ m/FreeHeap=(.*?)(\,|\ ,)/) {
  425. readingsBulkUpdate($hash, "FreeHeap", $1);
  426. }
  427. if($kvp =~ m/OLED=(.*?)(\,|\ ,)/) {
  428. readingsBulkUpdate($hash, "OLED", $1);
  429. }
  430. if($kvp =~ m/CPU-Temperature=(.*?)(\,|\ ,)/) {
  431. readingsBulkUpdate($hash, "CPU-Temperature", $1);
  432. }
  433. if(AttrVal($name, "loopTimeReadings", "0") == "1") {
  434. if($kvp =~ m/LD\.Min=(.*?)(\,|\ ,)/) {
  435. readingsBulkUpdate($hash, "LD.Min", $1);
  436. }
  437. if($kvp =~ m/LD\.Avg=(.*?)(\,|\ ,)/) {
  438. readingsBulkUpdate($hash, "LD.Avg", $1);
  439. }
  440. if($kvp =~ m/LD\.Max=(.*?)(\,|\ ,)/) {
  441. readingsBulkUpdate($hash, "LD.Max", $1);
  442. }
  443. }
  444. readingsEndUpdate($hash, 1);
  445. }
  446. #=======================================================================================
  447. sub LaCrosseGateway_DeleteOwnSensorsReadings($) {
  448. my ($hash) = @_;
  449. delete $hash->{READINGS}{"temperature"};
  450. delete $hash->{READINGS}{"humidity"};
  451. delete $hash->{READINGS}{"pressure"};
  452. delete $hash->{READINGS}{"gas"};
  453. delete $hash->{READINGS}{"debug"};
  454. }
  455. #=======================================================================================
  456. sub LaCrosseGateway_HandleOwnSensors($$) {
  457. my ($hash, $data) = @_;
  458. readingsBeginUpdate($hash);
  459. my @bytes = split( ' ', substr($data, 5) );
  460. return "" if(@bytes < 14);
  461. my $temperature = undef;
  462. my $humidity = undef;
  463. my $pressure = undef;
  464. my $gas = undef;
  465. my $debug = undef;
  466. if($bytes[2] != 0xFF) {
  467. $temperature = ($bytes[2]*256 + $bytes[3] - 1000)/10;
  468. readingsBulkUpdate($hash, "temperature", $temperature);
  469. }
  470. if($bytes[4] != 0xFF) {
  471. $humidity = $bytes[4];
  472. readingsBulkUpdate($hash, "humidity", $humidity);
  473. }
  474. if(@bytes >= 16 && $bytes[14] != 0xFF) {
  475. $pressure = $bytes[14] * 256 + $bytes[15];
  476. $pressure /= 10.0 if $pressure > 5000;
  477. readingsBulkUpdate($hash, "pressure", $pressure);
  478. }
  479. if(@bytes >= 19 && $bytes[16] != 0xFF) {
  480. $gas = $bytes[16] * 65536 + $bytes[17] * 256 + $bytes[18];
  481. readingsBulkUpdate($hash, "gas", $gas);
  482. }
  483. if(@bytes >= 22 && $bytes[19] != 0xFF) {
  484. $debug = $bytes[19] * 65536 + $bytes[20] * 256 + $bytes[21];
  485. readingsBulkUpdate($hash, "debug", $debug);
  486. }
  487. readingsEndUpdate($hash, 1);
  488. delete $hash->{READINGS}{"temperature"} if $temperature == undef;
  489. delete $hash->{READINGS}{"humidity"} if $humidity == undef;
  490. delete $hash->{READINGS}{"pressure"} if $pressure == undef;
  491. delete $hash->{READINGS}{"gas"} if $gas == undef;
  492. delete $hash->{READINGS}{"debug"} if $debug == undef;
  493. }
  494. #=======================================================================================
  495. sub LaCrosseGateway_HandleAnalogData($$) {
  496. my ($hash, $data) = @_;
  497. if ($data =~ m/^LGW ANALOG /) {
  498. readingsBeginUpdate($hash);
  499. my @bytes = split( ' ', substr($data, 10) );
  500. return "" if(@bytes < 2);
  501. my $value = $bytes[0]*256 + $bytes[1];
  502. readingsBulkUpdate($hash, "analog", $value);
  503. readingsEndUpdate($hash, 1);
  504. }}
  505. #=======================================================================================
  506. sub LaCrosseGateway_Parse($$$$) {
  507. my ($hash, $iohash, $name, $msg) = @_;
  508. next if (!$msg || length($msg) < 1);
  509. return if ($msg =~ m/^\*\*\*CLEARLOG/);
  510. return if ($msg =~ m/[^\x20-\x7E]/);
  511. my $filter = AttrVal($name, "filter", undef);
  512. if(defined($filter)) {
  513. return if ($msg =~ m/$filter/);
  514. }
  515. if ($msg =~ m/^LGW/) {
  516. if ($msg =~ /ALIVE/) {
  517. $hash->{Alive} = TimeNow();
  518. }
  519. LaCrosseGateway_HandleAnalogData($hash, $msg);
  520. return;
  521. }
  522. if($msg =~ m/^\[LaCrosseITPlusReader.Gateway|\[LaCrosseGateway32 V/) {
  523. my $model = "";
  524. my $version = "";
  525. my $settings = "";
  526. if($msg =~ m/^\[LaCrosseGateway32 V/) {
  527. ($model, $version, $settings) = split(/ /, $msg, 3);
  528. $model .= " $version";
  529. }
  530. else {
  531. ($model, $settings) = split(/ /, $msg, 2);
  532. }
  533. $model = substr($model, 1);
  534. $hash->{model} = $model;
  535. $hash->{settings} = $settings;
  536. my $attrVal = AttrVal($name, "timeout", undef);
  537. if(defined($attrVal)) {
  538. my ($timeout, $interval) = split(',', $attrVal);
  539. if (!$interval) {
  540. $hash->{Alive} = TimeNow();
  541. }
  542. }
  543. if (ReadingsVal($name, "state", "") eq "opened") {
  544. if (my $initCommandsString = AttrVal($name, "initCommands", undef)) {
  545. my @initCommands = split(' ', $initCommandsString);
  546. foreach my $command (@initCommands) {
  547. LaCrosseGateway_SimpleWrite($hash, $command);
  548. }
  549. }
  550. readingsSingleUpdate($hash, "state", "initialized", 1);
  551. }
  552. return;
  553. }
  554. $hash->{"${name}_MSGCNT"}++;
  555. $hash->{"${name}_TIME"} = TimeNow();
  556. readingsSingleUpdate($hash, "state", $hash->{READINGS}{state}{VAL}, 0);
  557. $hash->{RAWMSG} = $msg;
  558. if($msg =~ m/^OK WS \d{1,3} 4 /) {
  559. my $osa = AttrVal($name, "ownSensors", "dispatch");
  560. if($osa eq "readings") {
  561. LaCrosseGateway_HandleOwnSensors($hash, $msg);
  562. return;
  563. }
  564. elsif ($osa eq "both") {
  565. LaCrosseGateway_HandleOwnSensors($hash, $msg);
  566. }
  567. else {
  568. LaCrosseGateway_DeleteOwnSensorsReadings($hash);
  569. }
  570. }
  571. if($msg =~ m/^OK VALUES LGW/) {
  572. my $osa = AttrVal($name, "kvp", "dispatch");
  573. if($osa eq "readings") {
  574. LaCrosseGateway_HandleKVP($hash, $msg);
  575. return;
  576. }
  577. elsif ($osa eq "both") {
  578. LaCrosseGateway_HandleKVP($hash, $msg);
  579. }
  580. else {
  581. LaCrosseGateway_DeleteKVPReadings($hash);
  582. }
  583. return if AttrVal($name, "dispatchKVP", "1") ne "1";
  584. }
  585. Dispatch($hash, $msg, "");
  586. }
  587. #=======================================================================================
  588. sub LaCrosseGateway_SimpleWrite(@) {
  589. my ($hash, $msg, $nocr) = @_;
  590. return if(!$hash);
  591. my $name = $hash->{NAME};
  592. Log3 $name, 5, "SW: $msg";
  593. $msg .= "\n" unless($nocr);
  594. $hash->{USBDev}->write($msg) if($hash->{USBDev});
  595. syswrite($hash->{TCPDev}, $msg) if($hash->{TCPDev});
  596. syswrite($hash->{DIODev}, $msg) if($hash->{DIODev});
  597. # Some linux installations are broken with 0.001, T01 returns no answer
  598. select(undef, undef, undef, 0.01);
  599. }
  600. #=======================================================================================
  601. sub LaCrosseGateway_Connect($;$) {
  602. my ($hash, $mode) = @_;
  603. my $name = $hash->{NAME};
  604. DevIo_CloseDev($hash);
  605. $mode = 0 if!($mode);
  606. my $enabled = AttrVal($name, "disable", "0") != "1" && !defined($hash->{helper}{FLASHING});
  607. if($enabled) {
  608. $hash->{nextOpenDelay} = 2;
  609. my $ret = DevIo_OpenDev($hash, $mode, "LaCrosseGateway_DoInit");
  610. return $ret;
  611. }
  612. return undef;
  613. }
  614. #=======================================================================================
  615. sub LaCrosseGateway_TriggerWatchdog($) {
  616. my ($hash) = @_;
  617. my $name = $hash->{NAME};
  618. my $watchDog = "";
  619. my $watchDogAttribute = AttrVal($name, "watchdog", undef);
  620. if($watchDogAttribute) {
  621. $watchDog = "=$watchDogAttribute"
  622. }
  623. my $command = "\"WATCHDOG Ping$watchDog\"";
  624. LaCrosseGateway_SimpleWrite($hash, $command);
  625. }
  626. #=======================================================================================
  627. sub LaCrosseGateway_OnConnectTimer($) {
  628. my ($hash) = @_;
  629. my $name = $hash->{NAME};
  630. RemoveInternalTimer($hash, "LaCrosseGateway_OnConnectTimer");
  631. my $attrVal = AttrVal($name, "timeout", undef);
  632. if(defined($attrVal)) {
  633. my ($timeout, $interval) = split(',', $attrVal);
  634. my $useOldMethod = $interval;
  635. $interval = $timeout if !$interval;
  636. InternalTimer(gettimeofday() + $interval, "LaCrosseGateway_OnConnectTimer", $hash, 0);
  637. if(AttrVal($name, "disable", "0") != "1" && !defined($hash->{helper}{FLASHING})) {
  638. my ($date, $time, $year, $month, $day, $hour, $min, $sec, $timestamp, $alive);
  639. if($useOldMethod) {
  640. $alive = InternalVal($name, "${name}_TIME", "2000-01-01 00:00:00");
  641. }
  642. else {
  643. LaCrosseGateway_TriggerWatchdog($hash);
  644. $timeout += 5;
  645. $alive = $hash->{Alive};
  646. $alive = "2000-01-01 00:00:00" if !$alive;
  647. }
  648. ($date, $time) = split( ' ', $alive);
  649. ($year, $month, $day) = split( '-', $date);
  650. ($hour, $min, $sec) = split( ':', $time);
  651. $month -= 01;
  652. $timestamp = timelocal($sec, $min, $hour, $day, $month, $year);
  653. if (gettimeofday() - $timestamp > $timeout) {
  654. return LaCrosseGateway_Connect($hash, 1);
  655. }
  656. }
  657. }
  658. }
  659. #=======================================================================================
  660. sub LaCrosseGateway_Attr(@) {
  661. my ($cmd,$name,$aName,$aVal) = @_;
  662. my $hash = $defs{$name};
  663. if( $aName eq "Clients" ) {
  664. $hash->{Clients} = $aVal;
  665. $hash->{Clients} = $clients if( !$hash->{Clients});
  666. }
  667. elsif ($aName eq "timeout") {
  668. return "Usage: attr $name $aName <checkInterval>" if($aVal && $aVal !~ m/^[0-9]{1,6}(,[0-9]{1,6})*/);
  669. RemoveInternalTimer($hash, "LaCrosseGateway_OnConnectTimer");
  670. if($aVal) {
  671. LaCrosseGateway_TriggerWatchdog($hash);
  672. my ($timeout, $interval) = split(',', $aVal);
  673. $interval = $timeout if !$interval;
  674. InternalTimer(gettimeofday()+$interval, "LaCrosseGateway_OnConnectTimer", $hash, 0);
  675. }
  676. }
  677. elsif ($aName eq "disable") {
  678. if($aVal eq "1") {
  679. DevIo_CloseDev($hash);
  680. readingsSingleUpdate($hash, "state", "disabled", 1);
  681. $hash->{"RAWMSG"} = "";
  682. $hash->{"model"} = "";
  683. }
  684. else {
  685. if($hash->{READINGS}{state}{VAL} eq "disabled") {
  686. readingsSingleUpdate($hash, "state", "disconnected", 1);
  687. InternalTimer(gettimeofday()+1, "LaCrosseGateway_Connect", $hash, 0);
  688. }
  689. }
  690. }
  691. elsif ($aName eq "MatchList") {
  692. my $match_list;
  693. if( $cmd eq "set" ) {
  694. $match_list = eval $aVal;
  695. if( $@ ) {
  696. Log3 $name, 2, $name .": $aVal: ". $@;
  697. }
  698. }
  699. if (ref($match_list) eq 'HASH') {
  700. $hash->{MatchList} = $match_list;
  701. }
  702. else {
  703. $hash->{MatchList} = \%matchList;
  704. Log3 $name, 2, $name .": $aVal: not a HASH" if( $aVal );
  705. }
  706. }
  707. elsif ($aName eq "ownSensors" && $aVal eq "dispatch") {
  708. LaCrosseGateway_DeleteOwnSensorsReadings($hash);
  709. }
  710. elsif ($aName eq "kvp" && $aVal eq "dispatch") {
  711. LaCrosseGateway_DeleteKVPReadings($hash);
  712. }
  713. return undef;
  714. }
  715. #=======================================================================================
  716. 1;
  717. =pod
  718. =item summary The IODevice for the LaCrosseGateway
  719. =item summary_DE Das IODevice für das LaCrosseGateway
  720. =begin html
  721. <a name="LaCrosseGateway"></a>
  722. <h3>LaCrosseGateway</h3>
  723. <ul>
  724. For more information about the LaCrosseGateway see here: <a href="http://www.fhemwiki.de/wiki/LaCrosseGateway">FHEM wiki</a>
  725. <br><br>
  726. <a name="LaCrosseGateway_Define"></a>
  727. <b>Define</b>
  728. <ul>
  729. <code>define &lt;name&gt; LaCrosseGateway &lt;device&gt;</code> <br>
  730. <br>
  731. USB-connected devices:<br><ul>
  732. &lt;device&gt; specifies the serial port to communicate with the LaCrosseGateway.
  733. The name of the serial-device depends on your distribution, under
  734. linux it is something like /dev/ttyACM0 or /dev/ttyUSB0.<br><br>
  735. </ul>
  736. Network-connected devices:<br><ul>
  737. &lt;device&gt; specifies the network device<br>
  738. Normally this is the IP-address and the port in the form ip:port<br>
  739. Example: 192.168.1.100:81<br>
  740. You must define the port number on the setup page of the LaCrosseGateway and use the same number here.<br>
  741. The default is 81
  742. <br><br>
  743. </ul>
  744. <br>
  745. </ul>
  746. <a name="LaCrosseGateway_Set"></a>
  747. <b>Set</b>
  748. <ul>
  749. <li>raw &lt;data&gt;<br>
  750. send &lt;data&gt; to the LaCrosseGateway. The possible command can be found in the wiki.
  751. </li><br>
  752. <li>connect<br>
  753. tries to (re-)connect to the LaCrosseGateway. It does not reset the LaCrosseGateway but only try to get a connection to it.
  754. </li><br>
  755. <li>reboot<br>
  756. Reboots the ESP8266. Works only if we are connected (state is opened or initialized)
  757. </li><br>
  758. <li>LaCrossePairForSec &lt;sec&gt; [ignore_battery]<br>
  759. enable autocreate of new LaCrosse sensors for &lt;sec&gt; seconds. If ignore_battery is not given only sensors
  760. sending the 'new battery' flag will be created.
  761. </li><br>
  762. <li>flash<br>
  763. The LaCrosseGateway needs the right firmware to be able to receive and deliver the sensor data to fhem.<br>
  764. This provides a way to flash it directly from FHEM.
  765. </li><br>
  766. <li>nextionUpload<br>
  767. Requires LaCrosseGateway V1.24 or newer.<br>
  768. Sends a Nextion firmware file (.tft) to the LaCrosseGateway. The LaCrosseGateway then distributes it to a connected Nextion display.<br>
  769. You can define the .tft file that shall be uploaded in the tftFile attribute. If this attribute does not exists, it will try to use FHEM/firmware/nextion.tft
  770. </li><br>
  771. </ul>
  772. <a name="LaCrosseGateway_Get"></a>
  773. <b>Get</b>
  774. <ul>
  775. ---
  776. </ul>
  777. <br>
  778. <a name="LaCrosseGateway_Attr"></a>
  779. <b>Attributes</b>
  780. <ul>
  781. <li>Clients<br>
  782. The received data gets distributed to a client (e.g. LaCrosse, EMT7110, ...) that handles the data.
  783. This attribute tells, which are the clients, that handle the data. If you add a new module to FHEM, that shall handle
  784. data distributed by the LaCrosseGateway module, you must add it to the Clients attribute.
  785. </li><br>
  786. <li>MatchList<br>
  787. Can be set to a perl expression that returns a hash that is used as the MatchList
  788. </li><br>
  789. <li>initCommands<br>
  790. Space separated list of commands to send for device initialization.
  791. </li><br>
  792. <li>timeout<br>
  793. format: &lt;timeout&gt<br>
  794. Asks the LaCrosseGateway every timeout seconds if it is still alive. If there is no response it reconnects to the LaCrosseGateway.<br>
  795. Can be combined with the watchdog attribute. If the watchdog attribute is set, the LaCrosseGateway also will check if it gets
  796. a request within watchdog seconds and if not, it will reboot.
  797. watchdog must be longer than timeout and does only work in combination with timeout.<br>
  798. Both should not be too short because the LaCrosseGateway needs enough time to boot before the next check.<br>
  799. Good values are: timeout 60 and watchdog 300<br>
  800. This mode needs LaCrosseGateway V1.24 or newer.
  801. <br><br><u>Old version (still working):</u><br>
  802. format: &lt;timeout, checkInterval&gt;<br>
  803. Checks every 'checkInterval' seconds if the last data reception is longer than 'timeout' seconds ago.<br>
  804. If this is the case, a new connect will be tried.
  805. </li><br>
  806. <li>watchdog<br>
  807. see timeout attribute.
  808. </li><br>
  809. <li>disable<br>
  810. if disabled, it does not try to connect and does not dispatch data
  811. </li><br>
  812. <li>kvp<br>
  813. defines how the incoming KVP-data of the LaCrosseGateway is handled<br>
  814. dispatch: (default) dispatch it to a KVP device<br>
  815. readings: create readings (e.g. RSSI, ...) in this device<br>
  816. both: dispatch and create readings
  817. </li><br>
  818. <li>ownSensors<br>
  819. defines how the incoming data of the internal LaCrosseGateway sensors is handled<br>
  820. dispatch: (default) dispatch it to a LaCrosse device<br>
  821. readings: create readings (e.g. temperature, humidity, ...) in this device<br>
  822. both: dispatch and create readings
  823. </li><br>
  824. <li>mode<br>
  825. USB, WiFi or Cable<br>
  826. Depending on how the LaCrosseGateway is connected, it must be handled differently (init, ...)
  827. </li><br>
  828. <li>tftFile<br>
  829. defines the .tft file that shall be used by the Nextion firmware upload (set nextionUpload)
  830. </li><br>
  831. <li>filter<br>
  832. defines a filter (regular expression) that is applied to the incoming data. If the regex matches, the data will be discarded.<br>
  833. This allows to suppress sensors, for example those of the neighbour.<br>
  834. The data of different kinds of sensors starts with (where xx is the ID):<br>
  835. LaCrosse sensor: OK 9 xx<br>
  836. EnergyCount 3000: OK 22 xx xx<br>
  837. EMT7110: OK EMT7110 xx xx<br>
  838. LevelSender: OK LS xx<br>
  839. Example: set lgw filter ^OK 22 117 196|^OK 9 49<br>
  840. will filter the LaCrosse sensor with ID "49" and the EC3000 with ID "117 196"
  841. </li><br>
  842. </ul>
  843. <br>
  844. </ul>
  845. =end html
  846. =cut