98_autocreate.pm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. ##############################################
  2. # $Id: 98_autocreate.pm 15377 2017-11-01 16:59:23Z 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->{currentlogfile}: $!";
  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", 1));
  330. return $ret;
  331. }
  332. # TODO: fix it if the device is renamed.
  333. sub
  334. CommandCreateLog($$)
  335. {
  336. my ($cl, $n) = @_;
  337. my $ac;
  338. foreach my $d (keys %defs) {
  339. next if($defs{$d}{TYPE} ne "autocreate");
  340. $ac = $d;
  341. last;
  342. }
  343. return "Please define an autocreate device with attributes first " .
  344. "(it may be disabled)" if(!$ac);
  345. return "No device named $n found" if(!$defs{$n});
  346. my $acd = $defs{$ac};
  347. my $disabled = AttrVal($ac, "disable", undef);
  348. delete $attr{$ac}{disable} if($disabled);
  349. $acd->{CHANGED}[0] = "UNDEFINED $n $defs{$n}{TYPE} none";
  350. autocreate_Notify($acd, $acd);
  351. delete $acd->{CHANGED};
  352. $attr{$ac}{disable} = 1 if($disabled);
  353. }
  354. my %ac_links=();
  355. # Optimized for linux /dev/serial/by-path/... links
  356. sub
  357. resolveSymLink($)
  358. {
  359. my ($name) = @_;
  360. return $ac_links{$name} if($ac_links{$name});
  361. return $name if($^O =~ m/Win/ || !-l $name);
  362. my $link = readlink($name);
  363. return $name if(!$link);
  364. my @p = split("/", $name);
  365. pop(@p);
  366. foreach my $l (split("/", $link)) {
  367. next if($l eq ".");
  368. if($l ne "..") {
  369. push(@p, $l); next;
  370. }
  371. pop(@p);
  372. push(@p,"") if(@p == 0); # root directory
  373. }
  374. $link = resolveSymLink(join("/", @p));
  375. $ac_links{$name} = $link;
  376. return $link;
  377. }
  378. ##########################
  379. # Table for automatically creating IO devices
  380. # PARAM in define will be replaced with the $1 from matchList
  381. my @usbtable = (
  382. { NAME => "CUL",
  383. matchList => ['cu.usbmodem.*(.)$', 'ttyACM.*(.)$'],
  384. DeviceName=> "DEVICE\@9600",
  385. flush => "\n",
  386. request => "V\n",
  387. response => "^V .* CU.*",
  388. define => "CUL_PARAM CUL DEVICE\@9600 1PARAM34", },
  389. { NAME => "CUL", # TuxRadio/RPi: CSM, SCC
  390. matchList => ["ttySP(.+)", "ttyAMA(.+)", "ttyS(.)" ],
  391. DeviceName=> "DEVICE\@38400",
  392. flush => "\n",
  393. request => "V\n",
  394. response => "^V .* CSM.*",
  395. define => "CUL_PARAM CUL DEVICE\@38400 1PARAM34", },
  396. { NAME => "TCM_ESP3",
  397. matchList => ["cu.usbserial(.*)", "cu.usbmodem(.*)",
  398. "ttyUSB(.*)", "ttyACM(.*)", "ttyAMA(.*)"],
  399. DeviceName=> "DEVICE\@57600",
  400. request => pack("H*", "5500010005700838"), # get idbase
  401. response => "^\x55\x00\x05\x01",
  402. define => "TCM_ESP3_PARAM TCM ESP3 DEVICE\@57600", },
  403. { NAME => "TCM_ESP2",
  404. matchList => ["ttyUSB(.*)"],
  405. DeviceName=> "DEVICE\@9600",
  406. request => pack("H*", "A55AAB5800000000000000000003"), # get idbase
  407. response => "^\xA5\x5A............",
  408. define => "TCM_ESP2_PARAM TCM ESP2 DEVICE\@9600", },
  409. { NAME => "FHZ",
  410. matchList => ["cu.usbserial(.*)", "ttyUSB(.*)"],
  411. DeviceName=> "DEVICE\@9600",
  412. request => pack("H*", "8105044fc90185"), # get fhtbuf
  413. response => "^\x81........",
  414. define => "FHZ_PARAM FHZ DEVICE", },
  415. { NAME => "TRX",
  416. matchList => ["cu.usbserial(.*)", "ttyUSB(.*)"],
  417. DeviceName=> "DEVICE\@38400",
  418. init => pack("H*", "0D00000000000000000000000000"), # Reset
  419. request => pack("H*", "0D00000102000000000000000000"), # GetStatus
  420. response => "^\x0d\x01\x00...........",
  421. define => "TRX_PARAM TRX DEVICE\@38400", },
  422. { NAME => "ZWDongle",
  423. matchList => ["cu.PL2303-0000(.*)", "cu.usbmodem(.*)",
  424. "ttyUSB(.*)", "ttyACM(.*)", "ttyAMA(.*)" ],
  425. DeviceName=> "DEVICE\@115200",
  426. request => pack("H*", "01030020dc06"), # GetStatus +ACK
  427. response => "^\x06.*",
  428. define => "ZWDongle_PARAM ZWDongle DEVICE\@115200", },
  429. { NAME => "FRM",
  430. matchList => ["cu.usbserial(.*)", "cu.usbmodem(.*)",
  431. "ttyUSB(.*)", "ttyACM(.*)", "ttyAMA(.*)"],
  432. DeviceName=> "DEVICE\@57600",
  433. init => pack("H*", "F9"), # Reset
  434. timeout => 5.0, # StandardFirmata blink takes time
  435. request => pack("H*", "F079F7"), # Query firmware version and filename START_SYSEX (0xF0), queryFirmware (0x79), END_SYSEX (0xF7)
  436. response => "^\xF0\x79(.*)\xF7", # Response Sysex xF0 x78 (2 Byte version) (n Byte filename) Endsysex xF7
  437. define => "FRM_PARAM FRM DEVICE\@57600", },
  438. );
  439. sub
  440. CommandUsb($$)
  441. {
  442. my ($cl, $n) = @_;
  443. return "Usage: usb [scan|create]" if("$n" !~ m/^(scan|create)$/);
  444. my $scan = 1 if($n eq "scan");
  445. my $ret = "";
  446. my $msg;
  447. my $dir = "/dev";
  448. if($^O =~ m/Win/) {
  449. return "This command is not yet supported on windows";
  450. }
  451. require "$attr{global}{modpath}/FHEM/DevIo.pm";
  452. Log3 undef, 1, "usb $n starting";
  453. ################
  454. # First try to flash unflashed CULs
  455. if($^O eq "linux") {
  456. # One device at a time to avoid endless loop
  457. my $lsusb = `lsusb`;
  458. if($lsusb) {
  459. my $culType;
  460. $culType = "CUL_V4" if($lsusb =~ m/VID=03eb.PID=2ff0/s); # FritzBox
  461. $culType = "CUL_V3" if($lsusb =~ m/VID=03eb.PID=2ff4/s); # FritzBox
  462. $culType = "CUL_V2" if($lsusb =~ m/VID=03eb.PID=2ffa/s); # FritzBox
  463. $culType = "CUL_V4" if($lsusb =~ m/03eb:2ff0/);
  464. $culType = "CUL_V3" if($lsusb =~ m/03eb:2ff4/);
  465. $culType = "CUL_V2" if($lsusb =~ m/03eb:2ffa/);
  466. if($culType) {
  467. $msg = "$culType: flash it with: CULflash none $culType";
  468. Log3 undef, 2, $msg; $ret .= $msg . "\n";
  469. if(!$scan) {
  470. AnalyzeCommand(undef, "culflash none $culType"); # Enable autoload
  471. sleep(4); # Leave time for linux to load th drivers
  472. }
  473. }
  474. }
  475. }
  476. ################
  477. # Now the /dev scan
  478. foreach my $dev (sort split("\n", `ls $dir`)) {
  479. foreach my $thash (@usbtable) {
  480. foreach my $ml (@{$thash->{matchList}}) {
  481. if($dev =~ m/$ml/) {
  482. my $PARAM = $1;
  483. $PARAM =~ s/[^A-Za-z0-9]//g;
  484. my $name = $thash->{NAME};
  485. $msg = "### $dev: checking if it is a $name";
  486. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  487. # Check if it already used
  488. foreach my $d (keys %defs) {
  489. if($defs{$d}{DeviceName}) {
  490. my $dn = $defs{$d}{DeviceName};
  491. $dn =~ s/@.*//;
  492. if(resolveSymLink($dn) eq resolveSymLink("/dev/$dev")) {
  493. $msg = "$dev is already used by the fhem device $d";
  494. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  495. goto NEXTDEVICE;
  496. }
  497. }
  498. }
  499. # Open the device
  500. my $dname = $thash->{DeviceName};
  501. $dname =~ s,DEVICE,$dir/$dev,g;
  502. my $hash = { NAME=>$name, DeviceName=>$dname, DevioText=>"Probing" };
  503. DevIo_OpenDev($hash, 0, 0);
  504. if(!defined($hash->{USBDev})) {
  505. DevIo_CloseDev($hash); # remove the ReadyFn loop
  506. $msg = "cannot open the device";
  507. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  508. goto NEXTDEVICE;
  509. }
  510. # Send reset (optional)
  511. if(defined($thash->{init})) {
  512. DevIo_SimpleWrite($hash, $thash->{init}, 0);
  513. DevIo_TimeoutRead($hash, $thash->{timeout} ? $thash->{timeout}:0.5);
  514. }
  515. # Clear the USB buffer
  516. DevIo_SimpleWrite($hash, $thash->{flush}, 0) if($thash->{flush});
  517. DevIo_TimeoutRead($hash, 0.1);
  518. DevIo_SimpleWrite($hash, $thash->{request}, 0);
  519. my $answer = DevIo_TimeoutRead($hash, 0.1);
  520. DevIo_CloseDev($hash);
  521. if($answer !~ m/$thash->{response}/) {
  522. $msg = "got wrong answer for a $name";
  523. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  524. next;
  525. }
  526. my $define = $thash->{define};
  527. $define =~ s/PARAM/$PARAM/g;
  528. $define =~ s,DEVICE,$dir/$dev,g;
  529. $msg = "create as a fhem device with: define $define";
  530. Log3 undef, 4, $msg; $ret .= $msg . "\n";
  531. if(!$scan) {
  532. Log3 undef, 1, "define $define";
  533. my $lret = CommandDefine($cl, $define);
  534. CommandSave(undef, undef)
  535. if(!$lret && AttrVal("global","autosave",1));
  536. }
  537. goto NEXTDEVICE;
  538. }
  539. }
  540. }
  541. NEXTDEVICE:
  542. }
  543. Log3 undef, 1, "usb $n end";
  544. return ($scan ? $ret : undef);
  545. }
  546. ###################################
  547. sub
  548. autocreate_Attr(@)
  549. {
  550. my @a = @_;
  551. my $do = 0;
  552. if($a[0] eq "set" && $a[2] eq "disable") {
  553. $do = (!defined($a[3]) || $a[3]) ? 1 : 2;
  554. }
  555. $do = 2 if($a[0] eq "del" && (!$a[2] || $a[2] eq "disable"));
  556. return if(!$do);
  557. $defs{$a[1]}{STATE} = ($do == 1 ? "disabled" : "active");
  558. return undef;
  559. }
  560. 1;
  561. =pod
  562. =item helper
  563. =item summary automatically create not yet defined FHEM devices
  564. =item summary_DE Erzeugt FHEM-Ger&auml;te automatisch
  565. =begin html
  566. <a name="autocreate"></a>
  567. <h3>autocreate</h3>
  568. <ul>
  569. Automatically create not yet defined FHEM devices upon reception of a message
  570. generated by this device. Note: devices which are polled (like the EMEM/EMWZ
  571. accessed through the EM1010PC) will NOT be automatically created.
  572. <br>
  573. <a name="autocreatedefine"></a>
  574. <b>Define</b>
  575. <ul>
  576. <code>define &lt;name&gt; autocreate</code><br>
  577. <br>
  578. <ul>
  579. By defining an instance, the global attribute <a href=
  580. "#autoload_undefined_devices">autoload_undefined_devices</a>
  581. is set, so that modules for unknnown devices are automatically loaded.
  582. The autocreate module intercepts the UNDEFINED event generated by each
  583. module, creates a device and optionally also FileLog and SVG
  584. entries.<br>
  585. <b>Note 1:</b> devices will be created with a unique name, which contains
  586. the type and a unique id for this type. When <a href="#rename">renaming
  587. </a> the device, the automatically created filelog and SVG devices
  588. will also be renamed.<br>
  589. <b>Note 2:</b> you can disable the automatic creation by setting the
  590. <a href="#disable">disable</a> attribute, in this case only the rename
  591. hook is active, and you can use the <a href="#createlog">createlog</a>
  592. command to add FileLog and SVG to an already defined device.
  593. <b>Note 3:</b> It makes no sense to create more than one instance of this
  594. module.
  595. </ul>
  596. <br>
  597. Example:<PRE>
  598. define autocreate autocreate
  599. attr autocreate autosave
  600. attr autocreate device_room %TYPE
  601. attr autocreate filelog test2/log/%NAME-%Y.log
  602. attr autocreate weblink
  603. attr autocreate weblink_room Plots
  604. </PRE>
  605. </ul>
  606. <a name="autocreateset"></a>
  607. <b>Set</b> <ul>N/A</ul><br>
  608. <a name="autocreateget"></a>
  609. <b>Get</b> <ul>N/A</ul><br>
  610. <a name="autocreateattr"></a>
  611. <b>Attributes</b>
  612. <ul>
  613. <a name="autosave"></a>
  614. <li>autosave<br>
  615. After creating a device, automatically save the config file with the
  616. command <a href="#save">save</a> command. Default is 1 (i.e. on), set
  617. it to 0 to switch it off.<br>
  618. <b>Note</b>: this attribute is deprecated, use the global autosave
  619. attribute instead.
  620. </li><br>
  621. <a name="device_room"></a>
  622. <li>device_room<br>
  623. "Put" the newly created device in this room. The name can contain the
  624. wildcards %TYPE and %NAME, see the example above.</li><br>
  625. <a name="filelogattr"></a>
  626. <li>filelog<br>
  627. Create a filelog associated with the device. The filename can contain
  628. the wildcards %TYPE and %NAME, see the example above. The filelog will
  629. be "put" in the same room as the device.</li><br>
  630. <a name="weblinkattr"></a>
  631. <li>weblink<br>
  632. Create an SVG associated with the device/filelog.</li><br>
  633. <a name="weblink_room"></a>
  634. <li>weblink_room<br>
  635. "Put" the newly created SVG in this room. The name can contain the
  636. wildcards %TYPE and %NAME, see the example above.</li><br>
  637. <li><a href="#disable">disable</a></li>
  638. <br>
  639. <a name="ignoreTypes"></a>
  640. <li>ignoreTypes<br>
  641. This is a regexp, to ignore certain devices, e.g. the neighbours FHT.
  642. You can specify more than one, with usual regexp syntax, e.g.<br>
  643. attr autocreate ignoreTypes CUL_HOERMANN.*|FHT_1234|CUL_WS_7<br>
  644. The word "Types" is somehow misleading, as it actually checks the
  645. generated device name.
  646. </li><br>
  647. <a name="autocreateThreshold"></a>
  648. <li>autocreateThreshold<br>
  649. A list of &lt;type&gt;:&lt;count&gt;:&lt;interval&gt; triplets. A new
  650. device is only created if there have been at least <code>count</code>
  651. events of TYPE <code>type</code> in the last <code>interval</code>
  652. seconds.<br> <code>attr autocreateThreshold
  653. LaCrosse:2:30,EMT7110:2:60</code>
  654. </li>
  655. </ul>
  656. <br>
  657. <a name="createlog"></a>
  658. <b>createlog</b>
  659. <ul>
  660. Use this command to manually add a FileLog and an SVG to an existing
  661. device.
  662. This command is part of the autocreate module.
  663. </ul>
  664. <br>
  665. <a name="usb"></a>
  666. <b>usb</b>
  667. <ul>
  668. Usage:
  669. <ul><code>
  670. usb scan<br>
  671. usb create<br>
  672. </code></ul>
  673. This command will scan the /dev directory for attached USB devices, and
  674. will try to identify them. With the argument scan you'll get back a list
  675. of FHEM commands to execute, with the argument create there will be no
  676. feedback, and the devices will be created instead.<br><br>
  677. Note that switching a CUL to HomeMatic mode is still has to be done
  678. manually.<br><br>
  679. On Linux it will also check with the lsusb command, if unflashed CULs are
  680. attached. If this is the case, it will call CULflash with the appropriate
  681. parameters (or display the CULflash command if scan is specified). The
  682. usb command will only flash one device per call.<br><br>
  683. This command is part of the autocreate module.
  684. </ul>
  685. </ul>
  686. <br>
  687. =end html
  688. =begin html_DE
  689. <a name="autocreate"></a>
  690. <h3>autocreate</h3>
  691. <ul>
  692. Erzeugt f&uuml;r noch nicht definierte FHEM-Ger&auml;te automatisch die
  693. geignete Definition (define). Diese Definition wird aus einer Nachricht
  694. gewonnen, die von diesen neuen Ger&auml;ten empfangen wurde. Hinweis:
  695. Ger&auml;te, die mit Polling arbeiten (wie z.B. der Zugriff auf EMEM/EMWZ
  696. &uuml;ber EM1010PC) werden NICHT automatisch erzeugt.
  697. <br><br>
  698. <a name="autocreatedefine"></a>
  699. <b>Define</b>
  700. <ul>
  701. <code>define &lt;name&gt; autocreate</code><br>
  702. <br>
  703. <ul>
  704. Durch die Definition dieser Instanz wird das globale Attribut <a
  705. href="#autoload_undefined_devices">autoload_undefined_devices</a>
  706. gesetzt, sodass die Module f&uuml;r unbekannte Ger&auml;te automatisch
  707. nachgeladen werden. Das autocreate-Modul interpretiert das
  708. UNDEFINED-event, welches von jedem Modul gestartet wird, erzeugt ein
  709. Ger&auml;t (device) und bei Bedarf ein FileLog sowie
  710. SVG-Eintr&auml;ge.<br>
  711. <b>Hinweis 1:</b> Ger&auml;te werden mit einem eindeutigen Namen erzeugt,
  712. der den Typ und eine individuelle ID f&uuml;r diesen Typ enth&auml;lt.
  713. Wird ein Ger&auml;t umbenannt (<a href="#rename">rename</a>), wird
  714. gleichzeitig das automatisch erzeugte FileLog und die SVG Ger&auml;te
  715. unbenannt.<br>
  716. <b>Hinweis 2:</b> Durch das Setzen des <a
  717. href="#disable">disable</a>-Attributes kann die automatische Erzeugung
  718. ausgeschaltet werden. In diesem Fall ist ausschlie&szlig;lich die oben
  719. erl&auml;uterte Umbenennung aktiv. Der <a
  720. href="#createlog">createlog</a>-Befehl kann zum Hinzuf&uuml;gen von
  721. FileLog und SVG eines bereits definierten Ger&auml;tes benutzt werden.
  722. <br>
  723. <b>Hinweis 3:</b>Es macht keinen Sinn, die Instanz dieses Moduls mehrmals
  724. zu erzeugen.
  725. </ul>
  726. <br>
  727. Beispiel:<PRE>
  728. define autocreate autocreate
  729. attr autocreate autosave
  730. attr autocreate device_room %TYPE
  731. attr autocreate filelog test2/log/%NAME-%Y.log
  732. attr autocreate weblink
  733. attr autocreate weblink_room Plots
  734. </PRE>
  735. </ul>
  736. <a name="autocreateset"></a>
  737. <b>Set</b> <ul>N/A</ul><br>
  738. <a name="autocreateget"></a>
  739. <b>Get</b> <ul>N/A</ul><br>
  740. <a name="autocreateattr"></a>
  741. <b>Attribute</b>
  742. <ul>
  743. <a name="autosave"></a>
  744. <li>autosave<br>
  745. Nach der Erzeugung eines neuen Ger&auml;tes wird automatisch die
  746. Konfigurationsdatei mit dem Befehl <a href="#save">save</a>
  747. gespeichert. Der Standardwert ist 1 (d.h. aktiviert), eine 0 schaltet
  748. die automatische Speicherung aus.<br>
  749. <b>Achtung:</b> Dieses Attribut ist unerw&uuml;nscht, bitte stattdessen
  750. das global autosave Attribut verwenden.
  751. </li><br>
  752. <a name="device_room"></a>
  753. <li>device_room<br>
  754. "Schiebt" das neu erstellte Ger&auml;t in diesen Raum. Der Name kann
  755. die Wildcards %NAME und %TYPE enthalten, siehe oben stehendes
  756. Beispiel.</li><br>
  757. <a name="filelogattr"></a>
  758. <li>filelog<br>
  759. Erstellt ein Filelog welches zu einem Ger&auml;t geh&ouml;rt. Der
  760. Dateiname darf die Wildcards %NAME und %TYPE enthalten, siehe oben
  761. stehendes Beispiel. Das Filelog wird in den gleichen Raum "geschoben"
  762. wie das zugeh&ouml;rige Ger&auml;t.</li><br>
  763. <a name="weblinkattr"></a>
  764. <li>weblink<br>
  765. Erzeugt ein SVG, welches mit dem Ger&auml;t/Filelog verkn&uuml;pft
  766. ist.</li><br>
  767. <a name="weblink_room"></a>
  768. <li>weblink_room<br>
  769. "Schiebt" das neu erstellte SVG in den bezeichneten Raum. Der Name kann
  770. die Wildcards %NAME und %TYPE enthalten, siehe oben stehendes
  771. Beispiel.</li><br>
  772. <li><a href="#disable">disable</a></li>
  773. <br>
  774. <a name="ignoreTypes"></a>
  775. <li>ignoreTypes<br>
  776. Dies ist ein Regexp, um bestimmte Ger&auml;te zu ignorieren, z.b. der
  777. Funk-Heizungsthermostat (FHT) des Nachbarn. In dem Ausdruck k&ouml;nnen
  778. mehr als ein Ger&auml;t &uuml;ber die normale Regexp-Syntax angegeben
  779. werden. Beispiel:<br>
  780. attr autocreate ignoreTypes CUL_HOERMANN.*|FHT_1234|CUL_WS_7<br>
  781. Das Wort "Types" ist etwas irref&uuml;hrend, da der Ger&auml;tename
  782. gepr&uuml;ft wird, und nicht der Typ.
  783. </li><br>
  784. <a name="autocreateThreshold"></a>
  785. <li>autocreateThreshold<br>
  786. Eine Liste of &lt;type&gt;:&lt;count&gt;:&lt;interval&gt; tripeln. Ein
  787. neues Device wird nur dann erzeugt wenn es mindestens <code>count</code>
  788. Events f&uuml;r den TYPE <code>type</code> in den letzten
  789. <code>interval</code> Sekunden gegeben hat.<br>
  790. Beispiel:<br>
  791. <code>attr autocreateThreshold LaCrosse:2:30,EMT7110:2:60</code>
  792. </li>
  793. </ul>
  794. <br>
  795. <a name="createlog"></a>
  796. <b>createlog</b>
  797. <ul>
  798. Dieser Befehl wird f&uuml;r ein manuelles Hinzuf&uuml;gen eines Logfile
  799. oder eines SVG zu einem vorhandenen Ger&auml;t verwendet.
  800. <br><br>
  801. Dieser Befehl ist Bestandteilteil des autocreate-Modules.
  802. </ul>
  803. <br>
  804. <a name="usb"></a>
  805. <b>usb</b>
  806. <ul>
  807. Verwendung:
  808. <ul><code>
  809. usb scan<br>
  810. usb create<br>
  811. </code></ul>
  812. Dieser Befehl durchsucht das /dev-Verzeichnis nach angeschlossenen
  813. USB-Ger&auml;ten und versucht gleichzeitig sie zu identifizieren. Mit dem
  814. Argument scan wird eine Liste von ausf&uuml;hrbaren FHEM-Befehlen
  815. zur&uuml;ckgegeben. Das Argument create gibt keine Liste o.&auml;.
  816. zur&uuml;ck, die Ger&auml;te werden stattdessen erzeugt.<br><br>
  817. Es ist zu beachten, dass ein CUL immer noch manuell in den
  818. HomeMatic-Modus umgeschaltet werden muss. <br><br>
  819. Unter Linux wird gleichzeitig mit dem lsusb-befehl &uuml;berpr&uuml;ft,
  820. ob nichtgeflashte CULs angeschlossen sind. Ist dies der Fall, ruft Linux
  821. CULflash mit den geeigneten Parametern auf (oder zeigt den
  822. CULflash-Befehl an, falls scan aufgef&uuml;hrt wurde).
  823. Pro usb Befehl wird nur ein Ger&auml;t geflasht.<br><br>
  824. Dieser Befehl ist Bestandteilteil des autocreate-Modules.
  825. </ul>
  826. </ul> <!-- End of autocreate -->
  827. <br>
  828. =end html_DE
  829. =cut