37_NINJA.pm 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. # $Id: $
  2. #
  3. # TODO:
  4. package main;
  5. my $dl = 4;
  6. ##########################
  7. # This block is only needed when FileLog is checked outside fhem
  8. #
  9. sub Log3($$$);
  10. sub Log($$);
  11. sub RemoveInternalTimer($);
  12. use vars qw(%attr);
  13. use vars qw(%defs);
  14. use vars qw(%modules);
  15. use vars qw($readingFnAttributes);
  16. use vars qw($reread_active);
  17. ##########################
  18. use strict;
  19. use warnings;
  20. use SetExtensions;
  21. use vars qw(%ninjaDevice);
  22. use vars qw(%ninjaTypes);
  23. use vars qw(%ninjaGroups);
  24. sub NINJA_Parse($$);
  25. sub NINJA_Send($$@);
  26. sub
  27. NINJA_Initialize($)
  28. {
  29. my ($hash) = @_;
  30. $hash->{Match} = "^.+";
  31. $hash->{SetFn} = "NINJA_Set";
  32. #$hash->{GetFn} = "NINJA_Get";
  33. $hash->{DefFn} = "NINJA_Define";
  34. $hash->{UndefFn} = "NINJA_Undef";
  35. $hash->{FingerprintFn} = "NINJA_Fingerprint";
  36. $hash->{ParseFn} = "NINJA_Parse";
  37. $hash->{AttrFn} = "NINJA_Attr";
  38. $hash->{AttrList} = "IODev"
  39. ." readonly:1"
  40. ." forceOn:1"
  41. ." $readingFnAttributes";
  42. }
  43. sub
  44. NINJA_Define__($$)
  45. {
  46. my ($hash, $def) = @_;
  47. my @a = split("[ \t][ \t]*", $def);
  48. if(@a != 4 ) {
  49. my $msg = "wrong syntax: define <name> NINJA <g> <vendor>";
  50. Log3 undef, 2, $msg;
  51. return $msg;
  52. }
  53. $a[2] =~ m/^([\da-f]{6})$/i;
  54. return "$a[2] is not a valid NINJA address" if( !defined($1) );
  55. $a[3] =~ m/^([\da-f]{2})$/i;
  56. return "$a[3] is not a valid NINJA channel" if( !defined($1) );
  57. my $name = $a[0];
  58. my $addr = $a[2];
  59. my $channel = $a[3];
  60. #return "$addr is not a 1 byte hex value" if( $addr !~ /^[\da-f]{2}$/i );
  61. #return "$addr is not an allowed address" if( $addr eq "00" );
  62. return "NINJA device $addr already used for $modules{NINJA}{defptr}{$addr}->{NAME}." if( $modules{NINJA}{defptr}{$addr}
  63. && $modules{NINJA}{defptr}{$addr}->{NAME} ne $name );
  64. $hash->{addr} = $addr;
  65. $hash->{channel} = $channel;
  66. $modules{NINJA}{defptr}{$addr} = $hash;
  67. AssignIoPort($hash);
  68. if(defined($hash->{IODev}->{NAME})) {
  69. Log3 $name, 3, "$name: I/O device is " . $hash->{IODev}->{NAME};
  70. } else {
  71. Log3 $name, 1, "$name: no I/O device";
  72. }
  73. #$attr{$name}{devStateIcon} = 'on:on:toggle off:off:toggle set.*:light_question:off' if( !defined( $attr{$name}{devStateIcon} ) );
  74. #$attr{$name}{webCmd} = 'on:off:toggle:statusRequest' if( !defined( $attr{$name}{webCmd} ) );
  75. #CommandAttr( undef, "$name userReadings consumptionTotal:consumption monotonic {ReadingsVal(\$name,'consumption',0)}" ) if( !defined( $attr{$name}{userReadings} ) );
  76. #NINJA_Send($hash, $addr, "00" );
  77. return undef;
  78. }
  79. sub
  80. NINJA_Define($$)
  81. {
  82. my ($hash, $def) = @_;
  83. my @a = split("[ \t][ \t]*", $def);
  84. Log3 undef, 0, "NINJA_define: $def";
  85. if(@a != 3 ) {
  86. my $msg = "wrong syntax: define <name> NINJA <addr>";
  87. Log3 undef, 2, $msg;
  88. return $msg;
  89. }
  90. $a[2] =~ m/^(.+)$/i;
  91. return "$a[2] is not a valid NINJA address" if( !defined($1) );
  92. my $name = $a[0];
  93. my $addr = $a[2];
  94. #return "$addr is not a 1 byte hex value" if( $addr !~ /^[\da-f]{2}$/i );
  95. return "$addr is not an allowed address" if( $addr eq "0" );
  96. return "NINJA device $addr already used for $modules{NINJA}{defptr}{$addr}->{NAME}."
  97. if( $modules{NINJA}{defptr}{$addr} && $modules{NINJA}{defptr}{$addr}->{NAME} ne $name );
  98. $hash->{addr} = $addr;
  99. $modules{NINJA}{defptr}{$addr} = $hash;
  100. AssignIoPort($hash);
  101. if(defined($hash->{IODev}->{NAME})) {
  102. Log3 $name, 3, "$name: I/O device is " . $hash->{IODev}->{NAME};
  103. } else {
  104. Log3 $name, 1, "$name: no I/O device";
  105. }
  106. #$attr{$name}{devStateIcon} = 'on:on:toggle off:off:toggle *.:light_question:off' if( !defined( $attr{$name}{devStateIcon} ) );
  107. #$attr{$name}{webCmd} = 'on:off:toggle:statusRequest' if( !defined( $attr{$name}{webCmd} ) );
  108. #CommandAttr( undef, "$name userReadings consumptionTotal:consumption monotonic {ReadingsVal(\$name,'consumption',0)}" ) if( !defined( $attr{$name}{userReadings} ) );
  109. return undef;
  110. }
  111. #####################################
  112. sub
  113. NINJA_Undef($$)
  114. {
  115. my ($hash, $arg) = @_;
  116. my $name = $hash->{NAME};
  117. my $addr = $hash->{addr};
  118. delete( $modules{NINJA}{defptr}{$addr} );
  119. return undef;
  120. }
  121. #####################################
  122. sub
  123. NINJA_Set($@)
  124. {
  125. my ($hash, $name, @aa) = @_;
  126. my $cnt = @aa;
  127. return "\"set $name\" needs at least one parameter" if($cnt < 1);
  128. my $cmd = $aa[0];
  129. my $arg = $aa[1];
  130. my $arg2 = $aa[2];
  131. my $arg3 = $aa[3];
  132. my $readonly = AttrVal($name, "readonly", "0" );
  133. #my $list = "identify:noArg reset:noArg statusRequest:noArg";
  134. #$list .= " off:noArg on:noArg toggle:noArg" if( !$readonly );
  135. my $list = "";
  136. if( $cmd eq 'toggle' ) {
  137. $cmd = ReadingsVal($name,"state","on") eq "off" ? "on" :"off";
  138. }
  139. if( !$readonly && $cmd eq 'off' ) {
  140. readingsSingleUpdate($hash, "state", "set-$cmd", 1);
  141. #NINJA_Send( $hash, 0x05, 0x00 );
  142. } elsif( !$readonly && $cmd eq 'on' ) {
  143. readingsSingleUpdate($hash, "state", "set-$cmd", 1);
  144. #NINJA_Send( $hash, 0x05, 0x01 );
  145. } elsif( $cmd eq 'statusRequest' ) {
  146. readingsSingleUpdate($hash, "state", "set-$cmd", 1);
  147. #NINJA_Send( $hash, 0x04, 0x00 );
  148. } elsif( $cmd eq 'reset' ) {
  149. readingsSingleUpdate($hash, "state", "set-$cmd", 1);
  150. #NINJA_Send( $hash, 0x04, 0x01 );
  151. #} elsif( $cmd eq 'identify' ) {
  152. # NINJA_Send( $hash, 0x06, 0x00 );
  153. } elsif ($cmd eq 'offset' ) {
  154. if (defined $arg2) {
  155. readingsSingleUpdate($hash, ".offset.$arg", $arg2, 0);
  156. } else {
  157. delete $hash->{READINGS}{".offset.$arg"};
  158. }
  159. } else {
  160. #TODO: understand
  161. return SetExtensions($hash, $list, $name, @aa);
  162. }
  163. return undef;
  164. }
  165. #####################################
  166. sub
  167. NINJA_Get($@)
  168. {
  169. my ($hash, $name, $cmd, @args) = @_;
  170. return "\"get $name\" needs at least one parameter" if(@_ < 3);
  171. my $list = "";
  172. return "Unknown argument $cmd, choose one of $list";
  173. }
  174. sub
  175. NINJA_Fingerprint($$)
  176. {
  177. my ($name, $msg) = @_;
  178. return ( "", $msg );
  179. }
  180. sub
  181. NINJA_ForceOn($)
  182. {
  183. my ($hash) = @_;
  184. #NINJA_Send( $hash, 0x05, 0x01 );
  185. }
  186. sub
  187. NINJA_Parse($$)
  188. {
  189. my ($hash, $msg) = @_;
  190. my $name = $hash->{NAME};
  191. #return undef if( $msg !~ m/^[\dA-F]{12,}$/ );
  192. if (0) { ##---------------------------------------------------
  193. if( $msg =~ m/^L/ ) {
  194. my @parts = split( ' ', substr($msg, 5), 4 );
  195. $msg = "OK 24 $parts[3]";
  196. }
  197. my( @bytes, $channel,$cmd,$addr,$data,$power,$consumption );
  198. if( $msg =~ m/^OK/ ) {
  199. @bytes = split( ' ', substr($msg, 6) );
  200. $channel = sprintf( "%02X", $bytes[0] );
  201. $cmd = $bytes[1];
  202. $addr = sprintf( "%02X%02X%02X", $bytes[2], $bytes[3], $bytes[4] );
  203. $data = $bytes[5];
  204. return "" if( $cmd == 0x04 && $bytes[6] == 170 && $bytes[7] == 170 && $bytes[8] == 170 && $bytes[9] == 170 ); # ignore commands from display unit
  205. return "" if( $cmd == 0x05 && ( $bytes[6] != 170 || $bytes[7] != 170 || $bytes[8] != 170 || $bytes[9] != 170 ) ); # ignore commands not from the plug
  206. } elsif ( $msg =~ m/^TX/ ) {
  207. # ignore TX
  208. return "";
  209. } else {
  210. DoTrigger($name, "UNKNOWNCODE $msg");
  211. Log3 $name, 3, "$name: Unknown code $msg, help me!";
  212. return "";
  213. }
  214. } #------------------------------------------------
  215. my $jsonref = NinjaPiCrust_ParseJSON($msg);
  216. my %datagram = %$jsonref;
  217. #Log3 $name, $dl, "NinjaPiCrust_Parse: \%datagram is @{[%datagram]}";
  218. my $msgtype = (keys %datagram)[0];
  219. Log3 $name, $dl, "$name: got message type '$msgtype'";
  220. my %data = %{$datagram{$msgtype}[0]};
  221. $data{MSGTYPE} = $msgtype;
  222. Log3 $name, $dl, "$name: Got $msgtype $data{G} $data{V} $data{D} $data{DA} from $msg"
  223. if (defined $data{G} and defined $data{V} and defined $data{D} and defined $data{DA});
  224. $hash->{"${name}_MSGCNT"}++;
  225. $hash->{"${name}_TIME"} = TimeNow();
  226. $hash->{RAWMSG} = $msg;
  227. # from here on, adhere to PCA301 logic for now:
  228. my $raddr = $data{G};
  229. my $rhash = $modules{NINJA}{defptr}{$raddr};
  230. my $rname = $rhash?$rhash->{NAME}:$raddr;
  231. if ( !$modules{NINJA}{defptr}{$raddr} ) {
  232. Log3 $name, 3, "NINJA Unknown device $rname, please define it";
  233. return "UNDEFINED NINJA_$rname NINJA $raddr";# $channel";
  234. }
  235. #CommandAttr( undef, "$rname userReadings consumptionTotal:consumption monotonic {ReadingsVal($rname,'consumption',0)}" ) if( !defined( $attr{$rname}{userReadings} ) );
  236. my @list;
  237. push(@list, $rname);
  238. $rhash->{NINJA_lastRcv} = TimeNow();
  239. Log3 $rhash, $dl, "$rname: identified module, commencing";
  240. #if( $rhash->{channel} ne $channel ) {
  241. # Log3 $rname, 3, "NINJA $rname, channel changed from $rhash->{channel} to $channel";
  242. #
  243. # $rhash->{channel} = $channel;
  244. # $rhash->{DEF} = "$rhash->{addr} $rhash->{channel}";
  245. # CommandSave(undef,undef) if( AttrVal( "autocreate", "autosave", 1 ) );
  246. #}
  247. my $readonly = AttrVal($rname, "readonly", "0" );
  248. my $state = "";
  249. #if( $cmd eq 0x04 ) {
  250. # $state = $data==0x00?"off":"on";
  251. # my $power = ($bytes[6]*256 + $bytes[7]) / 10.0;
  252. # my $consumption = ($bytes[8]*256 + $bytes[9]) / 100.0;
  253. # my $state = $state; $state = $power if( $readonly );
  254. # readingsBeginUpdate($rhash);
  255. # readingsBulkUpdate($rhash, "power", $power) if( $power != ReadingsVal($rname,"power",0) );
  256. # readingsBulkUpdate($rhash, "consumption", $consumption) if( $consumption != ReadingsVal($rname,"consumption",0) );
  257. # readingsBulkUpdate($rhash, "state", $state) if( $state ne ReadingsVal($rname,"state","") );
  258. # readingsEndUpdate($rhash,1);
  259. #} elsif( $cmd eq 0x05 ) {
  260. # $state = $data==0x00?"off":"on";
  261. #
  262. # readingsSingleUpdate($rhash, "state", $state, 1)
  263. #}
  264. if( AttrVal($rname, "forceOn", 0 ) == 1
  265. && $state eq "off" ) {
  266. readingsSingleUpdate($rhash, "state", "set-forceOn", 1);
  267. InternalTimer(gettimeofday()+3, "NINJA_ForceOn", $rhash, 0);
  268. }
  269. my $key = "$data{V}:$data{D}";
  270. unless (exists $ninjaDevice{$key}) {
  271. Log3 $rname, 0, "$rname: unknown VID:DID '$key'";
  272. return;
  273. }
  274. my %ndev = %{$ninjaDevice{$key}};
  275. unless ($ndev{sens}) {
  276. Log3 $rname, 0, "$rname: Not a sensor: VID:DID '$key' ($ndev{hint})";
  277. return;
  278. }
  279. if (!exists $ninjaTypes{$ndev{type}}) {
  280. Log3 $rname, 0, "$rname: Unsupported sensor VID:DID '$key' ($ndev{hint})";
  281. readingsSingleUpdate($rhash, $ndev{hint}, $data{DA}, 1);
  282. return;
  283. } else {
  284. # figure out which reding we are actually dealing with
  285. # and create update event.
  286. my %ntype = %{$ninjaTypes{$ndev{type}}};
  287. my $ntname = $ntype{name};
  288. my $now = gettimeofday();
  289. $rhash->{".reading.timestamp.$ntname"} = $now;
  290. my $val = $data{DA};
  291. my $offset = ReadingsVal($rname,".offset.$ntname",0.0);
  292. $val = $val + $offset unless ($offset == 0);
  293. my $fmt = $ntype{format};
  294. $val = sprintf($fmt,$val) if (defined $fmt);
  295. readingsSingleUpdate($rhash, $ntname, $val, 1);
  296. # if we expect more than one reading as defined by a group,
  297. # check if we have enough information to compile the group reading
  298. #
  299. # TODO: In this case we may want to deferr the individual updates
  300. # and use readingsBulkUpdate, instead
  301. if (exists $ntype{group} and exists $ninjaGroups{$ntype{group}}) {
  302. my %ngroup = %{$ninjaGroups{$ntype{group}}};
  303. Log3 $rname, $dl, "$rname: reading '$ntype{name}' is of group '$ntype{group}'";
  304. my $format = $ngroup{format};
  305. my $reading = $format;
  306. my $valid = 1;
  307. while ($format =~ /\{([^\}]+)\}/g) {
  308. my $rn = $1;
  309. Log3 $rname, $dl, "$rname: found in template: $rn";
  310. if (exists $rhash->{".reading.timestamp.$rn"}) {
  311. my $rval = ReadingsVal($rname,$rn,undef);
  312. $reading =~ s/\{$rn\}/$rval/;
  313. $valid = 0 unless (($now - $rhash->{".reading.timestamp.$rn"}) < 0.5);
  314. } else {
  315. $valid = 0;
  316. }
  317. }
  318. Log3 $rname, $dl, "$rname: got '$reading' from '$format'";
  319. readingsSingleUpdate($rhash, $ngroup{name}, $reading, 1) if ($valid);
  320. }
  321. }
  322. return @list;
  323. }
  324. %ninjaTypes = (
  325. "temperature" => { name=>"temperature", group=>"TD", format=>"%.1f" },
  326. "humidity" => { name=>"humidity", group=>"TD" }
  327. );
  328. %ninjaGroups = (
  329. "TD" => { name=>"state", format=>"T: {temperature} H: {humidity}" }
  330. );
  331. #
  332. # from devids.cvs :
  333. # VID;DID;Device Type;Default Name;State;Actuator;Sensor;Silent;Has Sub Device;Time Series data
  334. # cat ~/ninja/devids.csv | tr "; \r" "\t_ " |while read a b c d e f g h i j k; do key="\"$a:$b\" " key=${key:0:10}; type="\"$c\" "; type=${type:0:25}; echo "$key => {st=>$e, act=>$f, sens=>$g, sil=>$h, sub=>$i, tds=>$j, type=>$type, hint=>\"$d\" },"; done
  335. #
  336. # key is VID:DID
  337. # type:Device Type
  338. # hint:Default Name
  339. # st:State
  340. # act:Actuator
  341. # sens:Sensor
  342. # sil:Silent
  343. # sub:Has Sub Device
  344. # tsd:Time Series data
  345. %ninjaDevice = (
  346. "0:1" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"Block_Temperature" },
  347. "0:2" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"linear_acceleration" , hint=>"Block_Accelerometer" },
  348. "0:3" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"jiggle" , hint=>"Block_Jiggle" },
  349. "0:5" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"button" , hint=>"Push_Button" },
  350. "0:6" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"light_level" , hint=>"Light_Sensor" },
  351. "0:7" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"pir" , hint=>"PIR_Motion_Sensor" },
  352. "0:8" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"humidity" , hint=>"Humidity" },
  353. "0:9" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"Temperature" },
  354. "0:11" => {st=>0, act=>1, sens=>1, sil=>0, sub=>1, tds=>0, type=>"rf433" , hint=>"RF_433Mhz" },
  355. "0:12" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"sound" , hint=>"Sound_Sensor" },
  356. "0:13" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"La_Crosse_Temp_TX3/6" },
  357. "0:14" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"hid" , hint=>"Unknown_HID_Device" },
  358. "0:20" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"La_Crosse_Temp_WS2355" },
  359. "0:21" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"humidity" , hint=>"La_Crosse_Humidity_WS2355" },
  360. "0:22" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"rainfall" , hint=>"La_Crosse_Rainfall_WS2355" },
  361. "0:23" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"direction" , hint=>"La_Crosse_Wind_Direction" },
  362. "0:24" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"speed" , hint=>"La_Crosse_Wind_Speed" },
  363. "0:30" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"humidity" , hint=>"Humidity" },
  364. "0:31" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"Temperature" },
  365. "0:101" => {st=>0, act=>1, sens=>1, sil=>1, sub=>0, tds=>0, type=>"twitter" , hint=>"Twitter" },
  366. "0:102" => {st=>0, act=>1, sens=>1, sil=>1, sub=>0, tds=>0, type=>"facebook" , hint=>"Facebook" },
  367. "0:103" => {st=>0, act=>1, sens=>1, sil=>1, sub=>0, tds=>0, type=>"sms" , hint=>"SMS" },
  368. "0:104" => {st=>0, act=>1, sens=>1, sil=>1, sub=>0, tds=>0, type=>"dropbox" , hint=>"Dropbox" },
  369. "0:105" => {st=>0, act=>1, sens=>1, sil=>1, sub=>0, tds=>0, type=>"googledrive" , hint=>"Google_Drive" },
  370. "0:106" => {st=>0, act=>1, sens=>1, sil=>1, sub=>0, tds=>0, type=>"email" , hint=>"Email" },
  371. "0:107" => {st=>0, act=>1, sens=>1, sil=>1, sub=>0, tds=>0, type=>"salesforce" , hint=>"Salesforce" },
  372. "0:108" => {st=>0, act=>1, sens=>1, sil=>1, sub=>1, tds=>0, type=>"webhook" , hint=>"Webhook" },
  373. "0:200" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"button" , hint=>"Push_Button" },
  374. "0:201" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"momentary_switch" , hint=>"Reed_Switch" },
  375. "0:202" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"Temperature" },
  376. "0:203" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"humidity" , hint=>"Humidity" },
  377. "0:204" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"pir" , hint=>"PIR_Motion_Sensor" },
  378. "0:205" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"switch_sensor" , hint=>"Switch_Sensor" },
  379. "0:206" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"switch_actuator" , hint=>"Switch_Actuator" },
  380. "0:207" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>1, type=>"switch" , hint=>"Switch" },
  381. "0:208" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"orientation" , hint=>"Orientation" },
  382. "0:209" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"jiggle" , hint=>"Jiggle" },
  383. "0:210" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"gesture" , hint=>"Gesture" },
  384. "0:211" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"keyboard" , hint=>"Keyboard" },
  385. "0:212" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"code_reader" , hint=>"Barcode_Scanner" },
  386. "0:213" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"code_reader" , hint=>"QR_code_Scanner" },
  387. "0:214" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"distance" , hint=>"Distance" },
  388. "0:215" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"sound" , hint=>"Sound" },
  389. "0:216" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"microphone" , hint=>"Microphone" },
  390. "0:217" => {st=>0, act=>0, sens=>1, sil=>0, sub=>1, tds=>0, type=>"hid" , hint=>"HID_Device" },
  391. "0:218" => {st=>0, act=>0, sens=>1, sil=>0, sub=>1, tds=>0, type=>"rfid" , hint=>"RFID_Reader" },
  392. "0:219" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"proximity" , hint=>"Proximity_Sensor" },
  393. "0:220" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"camera_still" , hint=>"Camera" },
  394. "0:221" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"camera_video" , hint=>"Video_Camera" },
  395. "0:222" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"compass" , hint=>"Compass" },
  396. "0:223" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"location" , hint=>"Location" },
  397. "0:224" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"light" , hint=>"Light" },
  398. "0:225" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"moisture" , hint=>"Moisture" },
  399. "0:226" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"ph" , hint=>"pH_Sensor" },
  400. "0:227" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"geiger" , hint=>"Geiger_Counter" },
  401. "0:228" => {st=>0, act=>1, sens=>1, sil=>0, sub=>1, tds=>0, type=>"rf" , hint=>"RF_Transceiver" },
  402. "0:229" => {st=>0, act=>1, sens=>1, sil=>0, sub=>1, tds=>0, type=>"zigbee" , hint=>"Zigbee_Transceiver" },
  403. "0:230" => {st=>0, act=>1, sens=>1, sil=>0, sub=>1, tds=>0, type=>"zwave" , hint=>"Z-wave_Transceiver" },
  404. "0:231" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"alarm" , hint=>"Alarm" },
  405. "0:232" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"speaker" , hint=>"Speaker" },
  406. "0:233" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"light_onoff" , hint=>"Light" },
  407. "0:233" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"light_switch" , hint=>"Light" },
  408. "0:234" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"light_dim" , hint=>"Light_(Dimmable)" },
  409. "0:235" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"rgbled8" , hint=>"RGB_Light_(Basic)" },
  410. "0:236" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"rgbled" , hint=>"RGB_Light" },
  411. "0:237" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"servo" , hint=>"Servo" },
  412. "0:238" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"relay" , hint=>"Relay" },
  413. "0:239" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"stepper" , hint=>"Stepper" },
  414. "0:240" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"display_text" , hint=>"Text_Display" },
  415. "0:241" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"display_image" , hint=>"Image_Display" },
  416. "0:242" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"energy" , hint=>"Energy" },
  417. "0:243" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"power" , hint=>"Power" },
  418. "0:244" => {st=>1, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"state" , hint=>"Generic_State_Device" },
  419. "0:255" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"screen_capture" , hint=>"Screen_Capture" },
  420. "0:256" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"screen_capture" , hint=>"Mac_Screen_Capture" },
  421. "0:260" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence" },
  422. "0:261" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_Wifi_AP" },
  423. "0:262" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_Wifi_Client" },
  424. "0:263" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_Bluetooth" },
  425. "0:264" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_USB" },
  426. "0:265" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_IP" },
  427. "0:266" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_UPNP" },
  428. "0:267" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_Zeroconf" },
  429. "0:268" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_MAC" },
  430. "0:269" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"presence" , hint=>"Presence_-_Xbox_Live" },
  431. "0:280" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"mediaplayer" , hint=>"Media_Player" },
  432. "0:281" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"mediaplayer" , hint=>"Media_Player_-_Xbmc" },
  433. "0:282" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"mediaplayer" , hint=>"Media_Player_-_VLC" },
  434. "0:283" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"mediaplayer" , hint=>"Media_Player_-_iTunes" },
  435. "0:284" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"mediaplayer" , hint=>"Media_Player_-_Spotify" },
  436. "0:300" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"openurl" , hint=>"Open_URL" },
  437. "0:310" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"notification" , hint=>"Notification" },
  438. "0:311" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"notification" , hint=>"Mac_Notification" },
  439. "0:320" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"lock-screen" , hint=>"Lock_Screen" },
  440. # "0:500" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"location" , hint=>"Browser_GPS" },
  441. # "0:500" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"cpu" , hint=>"CPU_Usage" },
  442. "0:501" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"cpu" , hint=>"NinjaBlock_CPU_Usage" },
  443. "0:502" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"cpu" , hint=>"Mac_CPU_Usage" },
  444. "0:503" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"cpu" , hint=>"Raspberry_Pi_CPU_Usage" },
  445. "0:510" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"CPU_Temperature" },
  446. "0:511" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"Raspberry_Pi_CPU_Temperature" },
  447. "0:512" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"NinjaBlock_CPU_Temperature" },
  448. "0:513" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"temperature" , hint=>"Mac_CPU_Temperature" },
  449. "0:520" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"ram" , hint=>"RAM_Usage" },
  450. "0:521" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"ram" , hint=>"NinjaBlock_RAM_Usage" },
  451. "0:522" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"ram" , hint=>"Mac_RAM_Usage" },
  452. "0:523" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"ram" , hint=>"Raspberry_Pi_RAM_Usage" },
  453. "0:530" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"network-activity" , hint=>"Incoming_Network_Activity" },
  454. "0:531" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"network-activity" , hint=>"NinjaBlock_Incoming_Network_Activity" },
  455. "0:532" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"network-activity" , hint=>"Mac_Incoming_Network_Activity" },
  456. "0:533" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"network-activity" , hint=>"Raspberry_Pi_Incoming_Network_Activity" },
  457. "0:540" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"network-activity" , hint=>"Outgoing_Network_Activity" },
  458. "0:541" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"network-activity" , hint=>"NinjaBlock_Outgoing_Network_Activity" },
  459. "0:542" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"network-activity" , hint=>"Mac_Outgoing_Network_Activity" },
  460. "0:543" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"network-activity" , hint=>"Raspberry_Pi_Outgoing_Network_Activity" },
  461. "0:550" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"battery" , hint=>"Battery" },
  462. "0:551" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"battery" , hint=>"Mac_Battery" },
  463. "0:600" => {st=>1, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"ias_zone" , hint=>"IAS_Zone" },
  464. "0:999" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"rgbled" , hint=>"On_Board_RGB_LED_v2" },
  465. # "0:999" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"rgbled8" , hint=>"Status_Light" },
  466. "0:1000" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"rgbled8" , hint=>"On_Board_RGB_LED" },
  467. "0:1002" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>1, type=>"relay" , hint=>"Relay_Board" },
  468. "0:1003" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"system" , hint=>"Arduino_Version" },
  469. "0:1004" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"webcam" , hint=>"Web_Cam" },
  470. "0:1005" => {st=>0, act=>1, sens=>1, sil=>1, sub=>0, tds=>0, type=>"network" , hint=>"Network" },
  471. "0:1006" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"speech" , hint=>"USB_Text_to_Speech" },
  472. "0:1007" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"rgbled" , hint=>"Nina's_Eyes" },
  473. "0:1008" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"light" , hint=>"Philips_Hue" },
  474. "0:1009" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"relay" , hint=>"Belkin_WeMo_Socket" },
  475. "0:1010" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"light" , hint=>"ZigBee_Light" },
  476. "0:1011" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"light" , hint=>"Limitless_LED_RGB" },
  477. "0:1012" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"light" , hint=>"Limitless_LED_White" },
  478. "0:1020" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"speech" , hint=>"Text-to-Speech" },
  479. "0:1021" => {st=>0, act=>1, sens=>0, sil=>0, sub=>0, tds=>0, type=>"speech" , hint=>"Mac_Text-to-Speech" },
  480. "0:2000" => {st=>0, act=>1, sens=>1, sil=>0, sub=>1, tds=>1, type=>"sandbox" , hint=>"Sandbox" },
  481. "0:3680" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"html" , hint=>"HTML" },
  482. "0:7000" => {st=>0, act=>1, sens=>0, sil=>1, sub=>0, tds=>0, type=>"matrix_display" , hint=>"LED_Board" },
  483. "0:9001" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"metric" , hint=>"Connected_Blocks" },
  484. "0:9002" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"metric" , hint=>"Redis_Response_Time" },
  485. "0:9003" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"metric" , hint=>"MySQL_Response_Time" },
  486. "0:10000" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"led" , hint=>"Browser_LED" },
  487. "2:9714" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"airconditioner" , hint=>"Air_Conditioner" },
  488. "3:1" => {st=>0, act=>1, sens=>1, sil=>0, sub=>0, tds=>0, type=>"relay" , hint=>"Power_Socket_Switch" },
  489. "3:2" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"power" , hint=>"Power_Usage" },
  490. "3:3" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"switch_sensor" , hint=>"NetVox_Switch" },
  491. "3:11" => {st=>0, act=>1, sens=>1, sil=>0, sub=>1, tds=>0, type=>"rf433" , hint=>"Camera_Control" },
  492. "4:2" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"meeting_length" , hint=>"Meeting_Length" },
  493. "4:3" => {st=>0, act=>0, sens=>1, sil=>0, sub=>0, tds=>1, type=>"room_utilisation" , hint=>"Room_Utilisation" },
  494. "4:4" => {st=>1, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"battery_alarm" , hint=>"Battery_Alarm" },
  495. "4:5" => {st=>1, act=>0, sens=>1, sil=>0, sub=>0, tds=>0, type=>"alarm" , hint=>"Zone_Alarm" }
  496. );
  497. sub
  498. NINJA_Send($$@)
  499. {
  500. my ($hash, $cmd, $data) = @_;
  501. $hash->{NINJA_lastSend} = TimeNow();
  502. my $msg = sprintf( "%i,%i,%i,%i,%i,%i,255,255,255,255s", hex($hash->{channel}),
  503. $cmd,
  504. hex(substr($hash->{addr},0,2)), hex(substr($hash->{addr},2,2)), hex(substr($hash->{addr},4,2)),
  505. $data );
  506. IOWrite( $hash, $msg );
  507. }
  508. sub
  509. NINJA_Attr(@)
  510. {
  511. my ($cmd, $name, $attrName, $attrVal) = @_;
  512. return undef;
  513. }
  514. 1;
  515. =pod
  516. =begin html
  517. <a name="NINJA"></a>
  518. <h3>NINJA</h3>
  519. <ul>
  520. <tr><td>
  521. The NINJA is a RF controlled AC mains plug with integrated power meter functionality from ELV.<br><br>
  522. It can be integrated in to FHEM via a <a href="#JeeLink">JeeLink</a> as the IODevice.<br><br>
  523. The JeeNode sketch required for this module can be found in .../contrib/arduino/36_NINJA-pcaSerial.zip.<br><br>
  524. <a name="NINJADefine"></a>
  525. <b>Define</b>
  526. <ul>
  527. <code>define &lt;name&gt; NINJA &lt;addr&gt; &lt;channel&gt;</code> <br>
  528. <br>
  529. addr is a 6 digit hex number to identify the NINJA device.
  530. channel is a 2 digit hex number to identify the NINJA device.<br><br>
  531. Note: devices are autocreated on reception of the first message.<br>
  532. </ul>
  533. <br>
  534. <a name="NINJA_Set"></a>
  535. <b>Set</b>
  536. <ul>
  537. <li>on</li>
  538. <li>off</li>
  539. <li>identify<br>
  540. Blink the status led for ~5 seconds.</li>
  541. <li>reset<br>
  542. Reset consumption counters</li>
  543. <li>statusRequest<br>
  544. Request device status update.</li>
  545. <li><a href="#setExtensions"> set extensions</a> are supported.</li>
  546. </ul><br>
  547. <a name="NINJA_Get"></a>
  548. <b>Get</b>
  549. <ul>
  550. </ul><br>
  551. <a name="NINJA_Readings"></a>
  552. <b>Readings</b>
  553. <ul>
  554. <li>power</li>
  555. <li>consumption</li>
  556. <li>consumptionTotal<br>
  557. will be created as a default user reading to have a continous consumption value that is not influenced
  558. by the regualar reset or overflow of the normal consumption reading</li>
  559. </ul><br>
  560. <a name="NINJA_Attr"></a>
  561. <b>Attributes</b>
  562. <ul>
  563. <li>readonly<br>
  564. if set to a value != 0 all switching commands (on, off, toggle, ...) will be disabled.</li>
  565. <li>forceOn<br>
  566. try to switch on the device whenever an off status is received.</li>
  567. </ul><br>
  568. </ul>
  569. =end html
  570. =cut