98_autocreate.pm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. ##############################################
  2. # $Id: 98_autocreate.pm 11984 2016-08-19 12:47:50Z rudolfkoenig $
  3. package main;
  4. use strict;
  5. use warnings;
  6. sub resolveSymLink($);
  7. # Problems:
  8. # - Not all CUL_EM devices return a power
  9. # - Not all CUL_WS devices return a temperature
  10. # - No plot files for BS/CUL_FHTTK/USF1000/X10/WS300
  11. # - check "UNDEFINED" parameters for BS/USF1000/X10
  12. my %flogpar = (
  13. # Oregon sensors:
  14. # * temperature
  15. "(THR128|THWR288A|THN132N|THGR132N).*"
  16. => { GPLOT => "temp4:Temp,", FILTER => "%NAME" },
  17. # * temperature, humidity
  18. "(THGR228N|THGR810|THGR918|THGR328N|RTGR328N|WTGR800_T|WT450H).*"
  19. => { GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME" },
  20. # * temperature, humidity, pressure
  21. "(BTHR918N|BTHR918|BTHR918N).*"
  22. => { GPLOT => "rain4press4:Temp/Press,temp4hum4:Temp/Hum,",
  23. FILTER => "%NAME" },
  24. # * anenometer
  25. "(WGR800|WGR918|WTGR800_A).*"
  26. => { GPLOT => "wind4windDir4:WindDir/WindSpeed,", FILTER => "%NAME" },
  27. # * Oregon sensors: Rain gauge
  28. "(PCR800|RGR918).*"
  29. => { GPLOT => "rain4:RainRate", FILTER => "%NAME" },
  30. # X10 sensors received by RFXCOM
  31. "(RFXX10SEC|TRX_DS10A).*"
  32. => { GPLOT => "fht80tf:Window,", FILTER => "%NAME" },
  33. # X10 Window sensors received by RFXTRX
  34. "TRX_DS10A.*"
  35. => { GPLOT => "fht80tf:Window,", FILTER => "%NAME" },
  36. # TX3 temperature sensors received by RFXTRX
  37. "TX3.*"
  38. => { GPLOT => "temp4hum4:Temp/Hum,", FILTER => "%NAME" },
  39. # USB-WDE1
  40. "USBWX_[0-8]"
  41. => { GPLOT => "temp4hum6:Temp/Hum,", FILTER => "%NAME" },
  42. "USBWX_ks300"
  43. => { GPLOT => "temp4hum6:Temp/Hum,temp4rain10:Temp/Rain,hum6wind8:Wind/Hum,",
  44. FILTER => "%NAME:T:.*" },
  45. # HomeMatic
  46. "CUL_HM_THSensor.*"
  47. => { GPLOT => "temp4hum6:Temp/Hum,", FILTER => "%NAME:T:.*" },
  48. "CUL_HM_KS550.*"
  49. => { GPLOT => "temp4rain10:Temp/Rain,hum6wind8:Wind/Hum,",
  50. FILTER => "%NAME:T:.*" },
  51. "CUL_HM_HM-CC-TC.*"
  52. => { GPLOT => "temp4hum6:Temp/Hum,", FILTER => "%NAME:T:.*" },
  53. );
  54. #####################################
  55. sub
  56. autocreate_Initialize($)
  57. {
  58. my ($hash) = @_;
  59. $hash->{DefFn} = "autocreate_Define";
  60. $hash->{NotifyFn} = "autocreate_Notify";
  61. $hash->{AttrFn} = "autocreate_Attr";
  62. $hash->{AttrList}= "autosave filelog device_room weblink weblink_room " .
  63. "disable ignoreTypes autocreateThreshold";
  64. my %ahash = ( Fn=>"CommandCreateLog",
  65. Hlp=>"<device>,create log/weblink for <device>",
  66. ModuleName => "autocreate" );
  67. $cmds{createlog} = \%ahash;
  68. my %bhash = ( Fn=>"CommandUsb",
  69. Hlp=>"[scan|create],display or create fhem-entries for USB devices",
  70. ModuleName => "autocreate" );
  71. $cmds{usb} = \%bhash;
  72. }
  73. #####################################
  74. sub
  75. autocreate_Define($$)
  76. {
  77. my ($hash, $def) = @_;
  78. my $name = $hash->{NAME};
  79. $hash->{STATE} = "active";
  80. $hash->{NOTIFYDEV} = "global";
  81. $attr{global}{autoload_undefined_devices} = 1; # Make sure we work correctly
  82. return undef;
  83. }
  84. sub
  85. replace_wildcards($$)
  86. {
  87. my ($hash, $str) = @_;
  88. return "" if(!$str);
  89. my $t = $hash->{TYPE}; $str =~ s/[%\$]TYPE/$t/g;
  90. my $n = $hash->{NAME}; $str =~ s/[%\$]NAME/$n/g;
  91. return $str;
  92. }
  93. #####################################
  94. sub
  95. autocreate_Notify($$)
  96. {
  97. my ($ntfy, $dev) = @_;
  98. my $me = $ntfy->{NAME};
  99. my $max = int(@{$dev->{CHANGED}});
  100. my $ret = "";
  101. my $nrcreated;
  102. for (my $i = 0; $i < $max; $i++) {
  103. my $s = $dev->{CHANGED}[$i];
  104. $s = "" if(!defined($s));
  105. my $temporary;
  106. ################
  107. # Special for EnOcean. DO NOT use it elsewhere
  108. if($s =~ m/^UNDEFINED -temporary/) {
  109. $temporary = 1;
  110. $s =~ s/ -temporary//;
  111. }
  112. if($s =~ m/^UNDEFINED ([^ ]*) ([^ ]*) (.*)$/) {
  113. my ($name, $type, $arg) = ($1, $2, $3);
  114. next if(AttrVal($me, "disable", undef));
  115. my $it = AttrVal($me, "ignoreTypes", undef);
  116. next if($it && $name =~ m/$it/i);
  117. my $at = AttrVal($me, "autocreateThreshold", undef);
  118. LoadModule($type) if( !$at );
  119. if( $at || $modules{$type}{AutoCreate} ) {
  120. my @at = split( '[, ]', $at?$at:"" );
  121. my $hash = $defs{$me};
  122. my $now = gettimeofday();
  123. #remove old events
  124. foreach my $t (keys %{$hash->{received}}) {
  125. my @v = grep { my $l = $_;
  126. $l =~ s/:.*//;
  127. ($t=~ m/^$l$/) ? $_ : undef} @at;
  128. my (undef,undef,$interval) = split( ':', $v[0]?$v[0]:"" );
  129. if( !@v ) {
  130. if( my $fp = $modules{$t}{AutoCreate} ) {
  131. foreach my $k (keys %{$fp}) {
  132. next if($name !~ m/^$k$/ || !$fp->{$k}{autocreateThreshold});
  133. (undef, $interval) = split( ':',$fp->{$k}{autocreateThreshold});
  134. last;
  135. }
  136. }
  137. }
  138. $interval = 60 if( !$interval );
  139. foreach my $a (keys %{$hash->{received}{$t}}) {
  140. foreach my $time (keys %{$hash->{received}{$t}{$a}}) {
  141. if( $time < $now - $interval ) {
  142. #Log3 $me, 5,
  143. # "autocreate: removed event for '$t $a' with timestamp $time";
  144. delete( $hash->{received}{$t}{$a}{$time} );
  145. }
  146. }
  147. delete($hash->{received}{$t}{$a}) if(!%{$hash->{received}{$t}{$a}});
  148. }
  149. delete( $hash->{received}{$t} ) if( !%{$hash->{received}{$t}} );
  150. }
  151. my @v = grep { my $l = $_;
  152. $l =~ s/:.*//;
  153. ($type=~ m/^$l$/) ? $_ : undef} @at;
  154. #if there is an entry for this type
  155. if( @v || $modules{$type}{AutoCreate} ) {
  156. my( undef, $min_count, $interval ) = split( ':', $v[0]?$v[0]:"" );
  157. my $found;
  158. if( !@v ) {
  159. if( my $fp = $modules{$type}{AutoCreate} ) {
  160. foreach my $k (keys %{$fp}) {
  161. next if($name !~ m/^$k$/ || !$fp->{$k}{autocreateThreshold});
  162. ($min_count, $interval) =
  163. split(':', $fp->{$k}{autocreateThreshold});
  164. $found = 1;
  165. last;
  166. }
  167. }
  168. }
  169. if( @v || $found ) {
  170. $min_count = 2 if( !$min_count );
  171. $interval = 60 if( !$interval );
  172. #add this event
  173. $hash->{received}{$type}{$arg}{$now} = 1;
  174. my $count = keys %{$hash->{received}{$type}{$arg}};
  175. Log3 $me, 4, "autocreate: received $count event(s) for ".
  176. "'$type $arg' during the last $interval seconds";
  177. if( $count < $min_count ) {
  178. Log3 $me, 4, "autocreate: ignoring event for ".
  179. "'$type $arg': at least $min_count needed";
  180. next;
  181. }
  182. }
  183. #forget entries for this type
  184. delete( $hash->{received}{$type}{$arg} );
  185. delete( $hash->{received}{$type} ) if( !%{$hash->{received}{$type}} );
  186. }
  187. }
  188. my ($cmd, $ret);
  189. my $hash = $defs{$name}; # Called from createlog
  190. ####################
  191. if(!$hash) {
  192. $cmd = "$name $type $arg";
  193. $cmd = "-temporary $name $type $arg" if($temporary);
  194. Log3 $me, 2, "autocreate: define $cmd";
  195. $ret = CommandDefine(undef, $cmd);
  196. if($ret) {
  197. Log3 $me, 1, "ERROR: $ret";
  198. last;
  199. }
  200. }
  201. $hash = $defs{$name};
  202. $nrcreated++;
  203. my $room = replace_wildcards($hash, AttrVal($me, "device_room", "%TYPE"));
  204. # preserve room for createlog
  205. $room = $attr{$name}{room} if($attr{$name} && $attr{$name}{room});
  206. $attr{$name}{room} = $room if($room);
  207. $nrcreated = 0 if($temporary); # do not save
  208. next if($modules{$hash->{TYPE}}{noAutocreatedFilelog} || $temporary);
  209. ####################
  210. my $fl = replace_wildcards($hash, AttrVal($me, "filelog", ""));
  211. my $flname = "FileLog_$name";
  212. delete($defs{$flname}) if($fl); # If we are re-creating it with createlog.
  213. my ($gplot, $filter, $devattr) = ("", $name, "");
  214. my $fp = $modules{$hash->{TYPE}}{AutoCreate};
  215. $fp = \%flogpar if(!$fp);
  216. foreach my $k (keys %{$fp}) {
  217. next if($name !~ m/^$k$/);
  218. $gplot = $fp->{$k}{GPLOT};
  219. $filter = replace_wildcards($hash, $fp->{$k}{FILTER});
  220. $devattr = $fp->{$k}{ATTR};
  221. last;
  222. }
  223. if($fl) {
  224. $cmd = "$flname FileLog $fl $filter";
  225. Log3 $me, 2, "autocreate: define $cmd";
  226. $ret = CommandDefine(undef, $cmd);
  227. if($ret) {
  228. Log3 $me, 1, "ERROR: $ret";
  229. last;
  230. }
  231. $attr{$flname}{room} = $room if($room);
  232. $attr{$flname}{logtype} = "${gplot}text";
  233. }
  234. if($devattr) {
  235. foreach my $attrNV (split(" ", $devattr)) {
  236. my ($an, $av) = split(":", $attrNV, 2);
  237. CommandAttr(undef, "$name $an $av");
  238. }
  239. }
  240. ####################
  241. next if(!AttrVal($me, "weblink", 1) || !$gplot || !$fl);
  242. $room = replace_wildcards($hash, AttrVal($me, "weblink_room", "Plots"));
  243. my $wnr = 1;
  244. foreach my $wdef (split(/,/, $gplot)) {
  245. next if(!$wdef);
  246. my ($gplotfile, $stuff) = split(/:/, $wdef);
  247. next if(!$gplotfile);
  248. my $wlname = "SVG_$name";
  249. $wlname .= "_$wnr" if($wnr > 1);
  250. $wnr++;
  251. delete($defs{$wlname}); # If we are re-creating it with createlog.
  252. $cmd = "$wlname SVG $flname:$gplotfile:CURRENT";
  253. Log3 $me, 2, "autocreate: define $cmd";
  254. $ret = CommandDefine(undef, $cmd);
  255. if($ret) {
  256. Log3 $me, 1, "ERROR: define $cmd: $ret";
  257. last;
  258. }
  259. $attr{$wlname}{room} = $room if($room);
  260. $attr{$wlname}{label} = '"' . $name .
  261. ' Min $data{min1}, Max $data{max1}, Last $data{currval1}"';
  262. $ret = CommandSet(undef, "$wlname copyGplotFile");
  263. if($ret) {
  264. Log3 $me, 1, "ERROR: set $wlname copyGplotFile: $ret";
  265. last;
  266. }
  267. }
  268. }
  269. ################
  270. if($s =~ m/^RENAMED ([^ ]*) ([^ ]*)$/) {
  271. my ($old, $new) = ($1, $2);
  272. if($defs{"FileLog_$old"}) {
  273. CommandRename(undef, "FileLog_$old FileLog_$new");
  274. my $hash = $defs{"FileLog_$new"};
  275. my $oldfile = $hash->{currentlogfile};
  276. $hash->{DEF} =~ s/$old/$new/g;
  277. $hash->{REGEXP} =~ s/$old/$new/g;
  278. $hash->{logfile} =~ s/$old/$new/g;
  279. $hash->{currentlogfile} =~ s/$old/$new/g;
  280. Log3 $me, 2, "autocreate: renamed FileLog_$old to FileLog_$new";
  281. $nrcreated++;
  282. notifyRegexpChanged($hash, $hash->{REGEXP});
  283. # Content
  284. if($oldfile ne $hash->{currentlogfile} &&
  285. open(IN, $oldfile) && open(OUT, ">".$hash->{currentlogfile})) {
  286. while(my $l = <IN>) {
  287. $l =~ s/$old/$new/;
  288. syswrite(OUT, $l);
  289. }
  290. close(IN); close(OUT);
  291. unlink($oldfile);
  292. my $fh = new IO::File ">>$hash->{currentlogfile}";
  293. $hash->{FH} = $fh;
  294. } else {
  295. Log 1, "$oldfile or $hash->{currentfile}: $!";
  296. close(IN);
  297. }
  298. }
  299. if($defs{"SVG_$old"}) {
  300. CommandRename(undef, "SVG_$old SVG_$new");
  301. my $hash = $defs{"SVG_$new"};
  302. my $oldfile = $hash->{GPLOTFILE};
  303. $hash->{DEF} =~ s/$old/$new/g;
  304. $hash->{GPLOTFILE} =~ s/$old/$new/g;
  305. $hash->{LOGDEVICE} =~ s/$old/$new/g;
  306. $attr{"SVG_$new"}{label} =~ s/$old/$new/g;
  307. Log3 $me, 2, "autocreate: renamed SVG_$old to SVG_$new";
  308. use vars qw($FW_gplotdir);# gplot directory
  309. if($oldfile ne $hash->{GPLOTFILE} && $FW_gplotdir) {
  310. $oldfile = $FW_gplotdir."/".$oldfile.".gplot";
  311. my $newfile = $FW_gplotdir."/".$hash->{GPLOTFILE}.".gplot";
  312. if(open(IN, $oldfile) && open(OUT, ">$newfile")) {
  313. while(my $l = <IN>) {
  314. $l =~ s/$old/$new/;
  315. syswrite(OUT, $l);
  316. }
  317. close(IN); close(OUT);
  318. unlink($oldfile);
  319. } else {
  320. Log 1, "$oldfile or $newfile: $!";
  321. close(IN);
  322. }
  323. }
  324. $nrcreated++;
  325. }
  326. }
  327. }
  328. CommandSave(undef, undef) if(!$ret && $nrcreated &&
  329. AttrVal($me,"autosave",
  330. AttrVal("global","autosave",1)));
  331. return $ret;
  332. }
  333. # TODO: fix it if the device is renamed.
  334. sub
  335. CommandCreateLog($$)
  336. {
  337. my ($cl, $n) = @_;
  338. my $ac;
  339. foreach my $d (keys %defs) {
  340. next if($defs{$d}{TYPE} ne "autocreate");
  341. $ac = $d;
  342. last;
  343. }
  344. return "Please define an autocreate device with attributes first " .
  345. "(it may be disabled)" if(!$ac);
  346. return "No device named $n found" if(!$defs{$n});
  347. my $acd = $defs{$ac};
  348. my $disabled = AttrVal($ac, "disable", undef);
  349. delete $attr{$ac}{disable} if($disabled);
  350. $acd->{CHANGED}[0] = "UNDEFINED $n $defs{$n}{TYPE} none";
  351. autocreate_Notify($acd, $acd);
  352. delete $acd->{CHANGED};
  353. $attr{$ac}{disable} = 1 if($disabled);
  354. }
  355. my %ac_links=();
  356. # Optimized for linux /dev/serial/by-path/... links
  357. sub
  358. resolveSymLink($)
  359. {
  360. my ($name) = @_;
  361. return $ac_links{$name} if($ac_links{$name});
  362. return $name if($^O =~ m/Win/ || !-l $name);
  363. my $link = readlink($name);
  364. return $name if(!$link);
  365. my @p = split("/", $name);
  366. pop(@p);
  367. foreach my $l (split("/", $link)) {
  368. next if($l eq ".");
  369. if($l ne "..") {
  370. push(@p, $l); next;
  371. }
  372. pop(@p);
  373. push(@p,"") if(@p == 0); # root directory
  374. }
  375. $link = resolveSymLink(join("/", @p));
  376. $ac_links{$name} = $link;
  377. return $link;
  378. }
  379. ##########################
  380. # Table for automatically creating IO devices
  381. # PARAM in define will be replaced with the $1 from matchList
  382. my @usbtable = (
  383. { NAME => "CUL",
  384. matchList => ['cu.usbmodem.*(.)$', 'ttyACM.*(.)$'],
  385. DeviceName=> "DEVICE\@9600",
  386. flush => "\n",
  387. request => "V\n",
  388. response => "^V .* CU.*",
  389. define => "CUL_PARAM CUL DEVICE\@9600 1PARAM34", },
  390. { NAME => "CUL", # TuxRadio/RPi: CSM
  391. matchList => ["ttySP(.*)", "ttyAMA(.*)", ],
  392. DeviceName=> "DEVICE\@38400",
  393. flush => "\n",
  394. request => "V\n",
  395. response => "^V .* CSM.*",
  396. define => "CUL_PARAM CUL DEVICE\@38400 1PARAM34", },
  397. { NAME => "TCM_ESP3",
  398. matchList => ["cu.usbserial(.*)", "cu.usbmodem(.*)",
  399. "ttyUSB(.*)", "ttyACM(.*)", "ttyAMA(.*)"],
  400. DeviceName=> "DEVICE\@57600",
  401. request => pack("H*", "5500010005700838"), # get idbase
  402. response => "^\x55\x00\x05\x01",
  403. define => "TCM_ESP3_PARAM TCM ESP3 DEVICE\@57600", },
  404. { NAME => "TCM_ESP2",
  405. matchList => ["ttyUSB(.*)"],
  406. DeviceName=> "DEVICE\@9600",
  407. request => pack("H*", "A55AAB5800000000000000000003"), # get idbase
  408. response => "^\xA5\x5A............",
  409. define => "TCM_ESP2_PARAM TCM ESP2 DEVICE\@9600", },
  410. { NAME => "FHZ",
  411. matchList => ["cu.usbserial(.*)", "ttyUSB(.*)"],
  412. DeviceName=> "DEVICE\@9600",
  413. request => pack("H*", "8105044fc90185"), # get fhtbuf
  414. response => "^\x81........",
  415. define => "FHZ_PARAM FHZ DEVICE", },
  416. { NAME => "TRX",
  417. matchList => ["cu.usbserial(.*)", "ttyUSB(.*)"],
  418. DeviceName=> "DEVICE\@38400",
  419. init => pack("H*", "0D00000000000000000000000000"), # Reset
  420. request => pack("H*", "0D00000102000000000000000000"), # GetStatus
  421. response => "^\x0d\x01\x00...........",
  422. define => "TRX_PARAM TRX DEVICE\@38400", },
  423. { NAME => "ZWDongle",
  424. matchList => ["cu.PL2303-0000(.*)", "cu.usbmodem(.*)",
  425. "ttyUSB(.*)", "ttyACM(.*)" ],
  426. DeviceName=> "DEVICE\@115200",
  427. request => pack("H*", "01030020dc06"), # GetStatus +ACK
  428. response => "^\x06.*",
  429. define => "ZWDongle_PARAM ZWDongle DEVICE\@115200", },
  430. { NAME => "FRM",
  431. matchList => ["cu.usbserial(.*)", "cu.usbmodem(.*)",
  432. "ttyUSB(.*)", "ttyACM(.*)", "ttyAMA(.*)"],
  433. DeviceName=> "DEVICE\@57600",
  434. init => pack("H*", "F9"), # Reset
  435. timeout => 5.0, # StandardFirmata blink takes time
  436. request => pack("H*", "F079F7"), # Query firmware version and filename START_SYSEX (0xF0), queryFirmware (0x79), END_SYSEX (0xF7)
  437. response => "^\xF0\x79(.*)\xF7", # Response Sysex xF0 x78 (2 Byte version) (n Byte filename) Endsysex xF7
  438. define => "FRM_PARAM FRM DEVICE\@57600", },
  439. );
  440. sub
  441. CommandUsb($$)
  442. {
  443. my ($cl, $n) = @_;
  444. return "Usage: usb [scan|create]" if("$n" !~ m/^(scan|create)$/);
  445. my $scan = 1 if($n eq "scan");
  446. my $ret = "";
  447. my $msg;
  448. my $dir = "/dev";
  449. if($^O =~ m/Win/) {
  450. return "This command is not yet supported on windows";
  451. }
  452. require "$attr{global}{modpath}/FHEM/DevIo.pm";
  453. Log3 undef, 1, "usb $n starting";
  454. ################
  455. # First try to flash unflashed CULs
  456. if($^O eq "linux") {
  457. # One device at a time to avoid endless loop
  458. my $lsusb = `lsusb`;
  459. if($lsusb) {
  460. my $culType;
  461. $culType = "CUL_V4" if($lsusb =~ m/VID=03eb.PID=2ff0/s); # FritzBox
  462. $culType = "CUL_V3" if($lsusb =~ m/VID=03eb.PID=2ff4/s); # FritzBox
  463. $culType = "CUL_V2" if($lsusb =~ m/VID=03eb.PID=2ffa/s); # FritzBox
  464. $culType = "CUL_V4" if($lsusb =~ m/03eb:2ff0/);
  465. $culType = "CUL_V3" if($lsusb =~ m/03eb:2ff4/);
  466. $culType = "CUL_V2" if($lsusb =~ m/03eb:2ffa/);
  467. if($culType) {
  468. $msg = "$culType: flash it with: CULflash none $culType";
  469. Log3 undef, 2, $msg; $ret .= $msg . "\n";
  470. if(!$scan) {
  471. AnalyzeCommand(undef, "culflash none $culType"); # Enable autoload
  472. sleep(4); # Leave time for linux to load th drivers
  473. }
  474. }
  475. }
  476. }
  477. ################
  478. # Now the /dev scan
  479. foreach my $dev (sort split("\n", `ls $dir`)) {
  480. foreach my $thash (@usbtable) {
  481. foreach my $ml (@{$thash->{matchList}}) {
  482. if($dev =~ m/$ml/) {
  483. my $PARAM = $1;
  484. $PARAM =~ s/[^A-Za-z0-9]//g;
  485. my $name = $thash->{NAME};
  486. $msg = "### $dev: checking if it is a $name";
  487. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  488. # Check if it already used
  489. foreach my $d (keys %defs) {
  490. if($defs{$d}{DeviceName}) {
  491. my $dn = $defs{$d}{DeviceName};
  492. $dn =~ s/@.*//;
  493. if(resolveSymLink($dn) eq resolveSymLink("/dev/$dev")) {
  494. $msg = "$dev is already used by the fhem device $d";
  495. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  496. goto NEXTDEVICE;
  497. }
  498. }
  499. }
  500. # Open the device
  501. my $dname = $thash->{DeviceName};
  502. $dname =~ s,DEVICE,$dir/$dev,g;
  503. my $hash = { NAME=>$name, DeviceName=>$dname, DevioText=>"Probing" };
  504. DevIo_OpenDev($hash, 0, 0);
  505. if(!defined($hash->{USBDev})) {
  506. DevIo_CloseDev($hash); # remove the ReadyFn loop
  507. $msg = "cannot open the device";
  508. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  509. goto NEXTDEVICE;
  510. }
  511. # Send reset (optional)
  512. if(defined($thash->{init})) {
  513. DevIo_SimpleWrite($hash, $thash->{init}, 0);
  514. DevIo_TimeoutRead($hash, $thash->{timeout} ? $thash->{timeout}:0.5);
  515. }
  516. # Clear the USB buffer
  517. DevIo_SimpleWrite($hash, $thash->{flush}, 0) if($thash->{flush});
  518. DevIo_TimeoutRead($hash, 0.1);
  519. DevIo_SimpleWrite($hash, $thash->{request}, 0);
  520. my $answer = DevIo_TimeoutRead($hash, 0.1);
  521. DevIo_CloseDev($hash);
  522. if($answer !~ m/$thash->{response}/) {
  523. $msg = "got wrong answer for a $name";
  524. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  525. next;
  526. }
  527. my $define = $thash->{define};
  528. $define =~ s/PARAM/$PARAM/g;
  529. $define =~ s,DEVICE,$dir/$dev,g;
  530. $msg = "create as a fhem device with: define $define";
  531. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  532. if(!$scan) {
  533. Log3 undef, 1, "define $define";
  534. my $lret = CommandDefine($cl, $define);
  535. CommandSave(undef, undef)
  536. if(!$lret && AttrVal("global","autosave",1));
  537. }
  538. goto NEXTDEVICE;
  539. }
  540. }
  541. }
  542. NEXTDEVICE:
  543. }
  544. Log3 undef, 1, "usb $n end";
  545. return ($scan ? $ret : undef);
  546. }
  547. ###################################
  548. sub
  549. autocreate_Attr(@)
  550. {
  551. my @a = @_;
  552. my $do = 0;
  553. if($a[0] eq "set" && $a[2] eq "disable") {
  554. $do = (!defined($a[3]) || $a[3]) ? 1 : 2;
  555. }
  556. $do = 2 if($a[0] eq "del" && (!$a[2] || $a[2] eq "disable"));
  557. return if(!$do);
  558. $defs{$a[1]}{STATE} = ($do == 1 ? "disabled" : "active");
  559. return undef;
  560. }
  561. 1;
  562. =pod
  563. =item helper
  564. =item summary automatically create not yet defined FHEM devices
  565. =item summary_DE Erzeugt FHEM-Ger&auml;te automatisch
  566. =begin html
  567. <a name="autocreate"></a>
  568. <h3>autocreate</h3>
  569. <ul>
  570. Automatically create not yet defined FHEM devices upon reception of a message
  571. generated by this device. Note: devices which are polled (like the EMEM/EMWZ
  572. accessed through the EM1010PC) will NOT be automatically created.
  573. <br>
  574. <a name="autocreatedefine"></a>
  575. <b>Define</b>
  576. <ul>
  577. <code>define &lt;name&gt; autocreate</code><br>
  578. <br>
  579. <ul>
  580. By defining an instance, the global attribute <a href=
  581. "#autoload_undefined_devices">autoload_undefined_devices</a>
  582. is set, so that modules for unknnown devices are automatically loaded.
  583. The autocreate module intercepts the UNDEFINED event generated by each
  584. module, creates a device and optionally also FileLog and SVG
  585. entries.<br>
  586. <b>Note 1:</b> devices will be created with a unique name, which contains
  587. the type and a unique id for this type. When <a href="#rename">renaming
  588. </a> the device, the automatically created filelog and SVG devices
  589. will also be renamed.<br>
  590. <b>Note 2:</b> you can disable the automatic creation by setting the
  591. <a href="#disable">disable</a> attribute, in this case only the rename
  592. hook is active, and you can use the <a href="#createlog">createlog</a>
  593. command to add FileLog and SVG to an already defined device.
  594. <b>Note 3:</b> It makes no sense to create more than one instance of this
  595. module.
  596. </ul>
  597. <br>
  598. Example:<PRE>
  599. define autocreate autocreate
  600. attr autocreate autosave
  601. attr autocreate device_room %TYPE
  602. attr autocreate filelog test2/log/%NAME-%Y.log
  603. attr autocreate weblink
  604. attr autocreate weblink_room Plots
  605. </PRE>
  606. </ul>
  607. <a name="autocreateset"></a>
  608. <b>Set</b> <ul>N/A</ul><br>
  609. <a name="autocreateget"></a>
  610. <b>Get</b> <ul>N/A</ul><br>
  611. <a name="autocreateattr"></a>
  612. <b>Attributes</b>
  613. <ul>
  614. <a name="autosave"></a>
  615. <li>autosave<br>
  616. After creating a device, automatically save the config file with the
  617. command <a href="#save">save</a> command. Default is 1 (i.e. on), set
  618. it to 0 to switch it off.<br>
  619. <b>Note</b>: this attribute is deprecated, use the global autosave
  620. attribute instead.
  621. </li><br>
  622. <a name="device_room"></a>
  623. <li>device_room<br>
  624. "Put" the newly created device in this room. The name can contain the
  625. wildcards %TYPE and %NAME, see the example above.</li><br>
  626. <a name="filelogattr"></a>
  627. <li>filelog<br>
  628. Create a filelog associated with the device. The filename can contain
  629. the wildcards %TYPE and %NAME, see the example above. The filelog will
  630. be "put" in the same room as the device.</li><br>
  631. <a name="weblinkattr"></a>
  632. <li>weblink<br>
  633. Create an SVG associated with the device/filelog.</li><br>
  634. <a name="weblink_room"></a>
  635. <li>weblink_room<br>
  636. "Put" the newly created SVG in this room. The name can contain the
  637. wildcards %TYPE and %NAME, see the example above.</li><br>
  638. <li><a href="#disable">disable</a></li>
  639. <br>
  640. <a name="ignoreTypes"></a>
  641. <li>ignoreTypes<br>
  642. This is a regexp, to ignore certain devices, e.g. the neighbours FHT.
  643. You can specify more than one, with usual regexp syntax, e.g.<br>
  644. attr autocreate ignoreTypes CUL_HOERMANN.*|FHT_1234|CUL_WS_7<br>
  645. The word "Types" is somehow misleading, as it actually checks the
  646. generated device name.
  647. </li><br>
  648. <a name="autocreateThreshold"></a>
  649. <li>autocreateThreshold<br>
  650. A list of &lt;type&gt;:&lt;count&gt;:&lt;interval&gt; triplets. A new
  651. device is only created if there have been at least <code>count</code>
  652. events of TYPE <code>type</code> in the last <code>interval</code>
  653. seconds.<br> <code>attr autocreateThreshold
  654. LaCrosse:2:30,EMT7110:2:60</code>
  655. </li>
  656. </ul>
  657. <br>
  658. <a name="createlog"></a>
  659. <b>createlog</b>
  660. <ul>
  661. Use this command to manually add a FileLog and an SVG to an existing
  662. device.
  663. This command is part of the autocreate module.
  664. </ul>
  665. <br>
  666. <a name="usb"></a>
  667. <b>usb</b>
  668. <ul>
  669. Usage:
  670. <ul><code>
  671. usb scan<br>
  672. usb create<br>
  673. </code></ul>
  674. This command will scan the /dev directory for attached USB devices, and
  675. will try to identify them. With the argument scan you'll get back a list
  676. of FHEM commands to execute, with the argument create there will be no
  677. feedback, and the devices will be created instead.<br><br>
  678. Note that switching a CUL to HomeMatic mode is still has to be done
  679. manually.<br><br>
  680. On Linux it will also check with the lsusb command, if unflashed CULs are
  681. attached. If this is the case, it will call CULflash with the appropriate
  682. parameters (or display the CULflash command if scan is specified). The
  683. usb command will only flash one device per call.<br><br>
  684. This command is part of the autocreate module.
  685. </ul>
  686. </ul>
  687. <br>
  688. =end html
  689. =begin html_DE
  690. <a name="autocreate"></a>
  691. <h3>autocreate</h3>
  692. <ul>
  693. Erzeugt f&uuml;r noch nicht definierte FHEM-Ger&auml;te automatisch die
  694. geignete Definition (define). Diese Definition wird aus einer Nachricht
  695. gewonnen, die von diesen neuen Ger&auml;ten empfangen wurde. Hinweis:
  696. Ger&auml;te, die mit Polling arbeiten (wie z.B. der Zugriff auf EMEM/EMWZ
  697. &uuml;ber EM1010PC) werden NICHT automatisch erzeugt.
  698. <br><br>
  699. <a name="autocreatedefine"></a>
  700. <b>Define</b>
  701. <ul>
  702. <code>define &lt;name&gt; autocreate</code><br>
  703. <br>
  704. <ul>
  705. Durch die Definition dieser Instanz wird das globale Attribut <a
  706. href="#autoload_undefined_devices">autoload_undefined_devices</a>
  707. gesetzt, sodass die Module f&uuml;r unbekannte Ger&auml;te automatisch
  708. nachgeladen werden. Das autocreate-Modul interpretiert das
  709. UNDEFINED-event, welches von jedem Modul gestartet wird, erzeugt ein
  710. Ger&auml;t (device) und bei Bedarf ein FileLog sowie
  711. SVG-Eintr&auml;ge.<br>
  712. <b>Hinweis 1:</b> Ger&auml;te werden mit einem eindeutigen Namen erzeugt,
  713. der den Typ und eine individuelle ID f&uuml;r diesen Typ enth&auml;lt.
  714. Wird ein Ger&auml;t umbenannt (<a href="#rename">rename</a>), wird
  715. gleichzeitig das automatisch erzeugte FileLog und die SVG Ger&auml;te
  716. unbenannt.<br>
  717. <b>Hinweis 2:</b> Durch das Setzen des <a
  718. href="#disable">disable</a>-Attributes kann die automatische Erzeugung
  719. ausgeschaltet werden. In diesem Fall ist ausschlie&szlig;lich die oben
  720. erl&auml;uterte Umbenennung aktiv. Der <a
  721. href="#createlog">createlog</a>-Befehl kann zum Hinzuf&uuml;gen von
  722. FileLog und SVG eines bereits definierten Ger&auml;tes benutzt werden.
  723. <br>
  724. <b>Hinweis 3:</b>Es macht keinen Sinn, die Instanz dieses Moduls mehrmals
  725. zu erzeugen.
  726. </ul>
  727. <br>
  728. Beispiel:<PRE>
  729. define autocreate autocreate
  730. attr autocreate autosave
  731. attr autocreate device_room %TYPE
  732. attr autocreate filelog test2/log/%NAME-%Y.log
  733. attr autocreate weblink
  734. attr autocreate weblink_room Plots
  735. </PRE>
  736. </ul>
  737. <a name="autocreateset"></a>
  738. <b>Set</b> <ul>N/A</ul><br>
  739. <a name="autocreateget"></a>
  740. <b>Get</b> <ul>N/A</ul><br>
  741. <a name="autocreateattr"></a>
  742. <b>Attribute</b>
  743. <ul>
  744. <a name="autosave"></a>
  745. <li>autosave<br>
  746. Nach der Erzeugung eines neuen Ger&auml;tes wird automatisch die
  747. Konfigurationsdatei mit dem Befehl <a href="#save">save</a>
  748. gespeichert. Der Standardwert ist 1 (d.h. aktiviert), eine 0 schaltet
  749. die automatische Speicherung aus.<br>
  750. <b>Achtung:</b> Dieses Attribut ist unerw&uuml;nscht, bitte stattdessen
  751. das global autosave Attribut verwenden.
  752. </li><br>
  753. <a name="device_room"></a>
  754. <li>device_room<br>
  755. "Schiebt" das neu erstellte Ger&auml;t in diesen Raum. Der Name kann
  756. die Wildcards %NAME und %TYPE enthalten, siehe oben stehendes
  757. Beispiel.</li><br>
  758. <a name="filelogattr"></a>
  759. <li>filelog<br>
  760. Erstellt ein Filelog welches zu einem Ger&auml;t geh&ouml;rt. Der
  761. Dateiname darf die Wildcards %NAME und %TYPE enthalten, siehe oben
  762. stehendes Beispiel. Das Filelog wird in den gleichen Raum "geschoben"
  763. wie das zugeh&ouml;rige Ger&auml;t.</li><br>
  764. <a name="weblinkattr"></a>
  765. <li>weblink<br>
  766. Erzeugt ein SVG, welches mit dem Ger&auml;t/Filelog verkn&uuml;pft
  767. ist.</li><br>
  768. <a name="weblink_room"></a>
  769. <li>weblink_room<br>
  770. "Schiebt" das neu erstellte SVG in den bezeichneten Raum. Der Name kann
  771. die Wildcards %NAME und %TYPE enthalten, siehe oben stehendes
  772. Beispiel.</li><br>
  773. <li><a href="#disable">disable</a></li>
  774. <br>
  775. <a name="ignoreTypes"></a>
  776. <li>ignoreTypes<br>
  777. Dies ist ein Regexp, um bestimmte Ger&auml;te zu ignorieren, z.b. der
  778. Funk-Heizungsthermostat (FHT) des Nachbarn. In dem Ausdruck k&ouml;nnen
  779. mehr als ein Ger&auml;t &uuml;ber die normale Regexp-Syntax angegeben
  780. werden. Beispiel:<br>
  781. attr autocreate ignoreTypes CUL_HOERMANN.*|FHT_1234|CUL_WS_7<br>
  782. Das Wort "Types" ist etwas irref&uuml;hrend, da der Ger&auml;tename
  783. gepr&uuml;ft wird, und nicht der Typ.
  784. </li><br>
  785. <a name="autocreateThreshold"></a>
  786. <li>autocreateThreshold<br>
  787. Eine Liste of &lt;type&gt;:&lt;count&gt;:&lt;interval&gt; tripeln. Ein
  788. neues Device wird nur dann erzeugt wenn es mindestens <code>count</code>
  789. Events f&uuml;r den TYPE <code>type</code> in den letzten
  790. <code>interval</code> Sekunden gegeben hat.<br>
  791. Beispiel:<br>
  792. <code>attr autocreateThreshold LaCrosse:2:30,EMT7110:2:60</code>
  793. </li>
  794. </ul>
  795. <br>
  796. <a name="createlog"></a>
  797. <b>createlog</b>
  798. <ul>
  799. Dieser Befehl wird f&uuml;r ein manuelles Hinzuf&uuml;gen eines Logfile
  800. oder eines SVG zu einem vorhandenen Ger&auml;t verwendet.
  801. <br><br>
  802. Dieser Befehl ist Bestandteilteil des autocreate-Modules.
  803. </ul>
  804. <br>
  805. <a name="usb"></a>
  806. <b>usb</b>
  807. <ul>
  808. Verwendung:
  809. <ul><code>
  810. usb scan<br>
  811. usb create<br>
  812. </code></ul>
  813. Dieser Befehl durchsucht das /dev-Verzeichnis nach angeschlossenen
  814. USB-Ger&auml;ten und versucht gleichzeitig sie zu identifizieren. Mit dem
  815. Argument scan wird eine Liste von ausf&uuml;hrbaren FHEM-Befehlen
  816. zur&uuml;ckgegeben. Das Argument create gibt keine Liste o.&auml;.
  817. zur&uuml;ck, die Ger&auml;te werden stattdessen erzeugt.<br><br>
  818. Es ist zu beachten, dass ein CUL immer noch manuell in den
  819. HomeMatic-Modus umgeschaltet werden muss. <br><br>
  820. Unter Linux wird gleichzeitig mit dem lsusb-befehl &uuml;berpr&uuml;ft,
  821. ob nichtgeflashte CULs angeschlossen sind. Ist dies der Fall, ruft Linux
  822. CULflash mit den geeigneten Parametern auf (oder zeigt den
  823. CULflash-Befehl an, falls scan aufgef&uuml;hrt wurde).
  824. Pro usb Befehl wird nur ein Ger&auml;t geflasht.<br><br>
  825. Dieser Befehl ist Bestandteilteil des autocreate-Modules.
  826. </ul>
  827. </ul> <!-- End of autocreate -->
  828. <br>
  829. =end html_DE
  830. =cut