31_PLAYBULB.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. ###############################################################################
  2. #
  3. # Developed with Kate
  4. #
  5. # (c) 2016-2017 Copyright: Marko Oldenburg (leongaultier at gmail dot com)
  6. # All rights reserved
  7. #
  8. # Special thanks goes to comitters:
  9. # - Christoph (pc1246) commandref writer
  10. #
  11. #
  12. # This script is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2 of the License, or
  15. # any later version.
  16. #
  17. # The GNU General Public License can be found at
  18. # http://www.gnu.org/copyleft/gpl.html.
  19. # A copy is found in the textfile GPL.txt and important notices to the license
  20. # from the author is found in LICENSE.txt distributed with these scripts.
  21. #
  22. # This script is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. #
  28. # $Id: 31_PLAYBULB.pm 15107 2017-09-20 19:12:40Z CoolTux $
  29. #
  30. ###############################################################################
  31. package main;
  32. use strict;
  33. use warnings;
  34. use POSIX;
  35. use JSON;
  36. use Blocking;
  37. use SetExtensions;
  38. my $version = "1.2.8";
  39. my %playbulbModels = (
  40. BTL300_v5 => {'aColor' => '0x16' ,'aEffect' => '0x14' ,'aBattery' => '0x1f' ,'aDevicename' => '0x3'}, # Candle Firmware 5
  41. BTL300_v6 => {'aColor' => '0x19' ,'aEffect' => '0x17' ,'aBattery' => '0x22' ,'aDevicename' => '0x3'}, # Candle Firmware 6
  42. BTL305_v14 => {'aColor' => '0x29' ,'aEffect' => '0x27' ,'aBattery' => '0x34' ,'aDevicename' => '0x3'}, # Candle S Firmware 1.4
  43. BTL201_v2 => {'aColor' => '0x1b' ,'aEffect' => '0x19' ,'aBattery' => 'none' ,'aDevicename' => 'none'}, # Smart
  44. BTL201M_V16 => {'aColor' => '0x25' ,'aEffect' => '0x23' ,'aBattery' => 'none' ,'aDevicename' => '0x7'}, # Smart (1/2017)
  45. BTL505_v1 => {'aColor' => '0x23' ,'aEffect' => '0x21' ,'aBattery' => 'none' ,'aDevicename' => '0x29'}, # Stripe
  46. BTL400M_v18 => {'aColor' => '0x23' ,'aEffect' => '0x21' ,'aBattery' => '0x2e' ,'aDevicename' => '0x7'}, # Garden
  47. BTL400M_v37 => {'aColor' => '0x1b' ,'aEffect' => '0x19' ,'aBattery' => '0x24' ,'aDevicename' => '0x7'}, # Garden neue Version
  48. BTL100C_v10 => {'aColor' => '0x1b' ,'aEffect' => '0x19' ,'aBattery' => 'none' ,'aDevicename' => 'none'}, # Color LED
  49. );
  50. my %effects = (
  51. 'Flash' => '00',
  52. 'Pulse' => '01',
  53. 'RainbowJump' => '02',
  54. 'RainbowFade' => '03',
  55. 'Candle' => '04',
  56. 'none' => 'FF',
  57. );
  58. my %effectsHex = (
  59. '00' => 'Flash',
  60. '01' => 'Pulse',
  61. '02' => 'RainbowJump',
  62. '03' => 'RainbowFade',
  63. '04' => 'Candle',
  64. 'ff' => 'none',
  65. );
  66. sub PLAYBULB_Initialize($);
  67. sub PLAYBULB_Define($$);
  68. sub PLAYBULB_Undef($$);
  69. sub PLAYBULB_Attr(@);
  70. sub PLAYBULB_firstRun($);
  71. sub PLAYBULB_Set($$@);
  72. sub PLAYBULB_Run($$$);
  73. sub PLAYBULB_BlockingRun($);
  74. sub PLAYBULB_gattCharWrite($$$$$$$$$$);
  75. sub PLAYBULB_gattCharRead($$$$);
  76. sub PLAYBULB_readBattery($$$);
  77. sub PLAYBULB_stateOnOff($$);
  78. sub PLAYBULB_readDevicename($$$);
  79. sub PLAYBULB_writeDevicename($$$$);
  80. sub PLAYBULB_forRun_encodeJSON($$$$$$$$$$$$$);
  81. sub PLAYBULB_forDone_encodeJSON($$$$$$$);
  82. sub PLAYBULB_BlockingDone($);
  83. sub PLAYBULB_BlockingAborted($);
  84. sub PLAYBULB_Initialize($) {
  85. my ($hash) = @_;
  86. $hash->{SetFn} = "PLAYBULB_Set";
  87. $hash->{DefFn} = "PLAYBULB_Define";
  88. $hash->{UndefFn} = "PLAYBULB_Undef";
  89. $hash->{AttrFn} = "PLAYBULB_Attr";
  90. $hash->{AttrList} = "model:BTL300_v5,BTL300_v6,BTL201_v2,BTL201M_V16,BTL505_v1,BTL400M_v18,BTL400M_v37,BTL100C_v10,BTL305_v14 ".
  91. "sshHost ".
  92. $readingFnAttributes;
  93. foreach my $d(sort keys %{$modules{PLAYBULB}{defptr}}) {
  94. my $hash = $modules{PLAYBULB}{defptr}{$d};
  95. $hash->{VERSION} = $version;
  96. }
  97. }
  98. sub PLAYBULB_Define($$) {
  99. my ( $hash, $def ) = @_;
  100. my @a = split( "[ \t][ \t]*", $def );
  101. return "too few parameters: define <name> PLAYBULB <BTMAC>" if( @a != 3 );
  102. my $name = $a[0];
  103. my $mac = $a[2];
  104. $hash->{BTMAC} = $mac;
  105. $hash->{VERSION} = $version;
  106. readingsSingleUpdate ($hash,"state","Unknown", 0);
  107. $attr{$name}{room} = "PLAYBULB" if( !defined($attr{$name}{room}) );
  108. $attr{$name}{devStateIcon} = "unreachable:light_question" if( !defined($attr{$name}{devStateIcon}) );
  109. $attr{$name}{webCmd} = "rgb:rgb FF0000:rgb 00FF00:rgb 0000FF:rgb FFFFFF:rgb F7FF00:rgb 00FFFF:rgb F700FF:effect" if( !defined($attr{$name}{webCmd}) );
  110. $hash->{helper}{effect} = ReadingsVal($name,"effect","Candle");
  111. $hash->{helper}{onoff} = ReadingsVal($name,"onoff",0);
  112. $hash->{helper}{rgb} = ReadingsVal($name,"rgb","ff0000");
  113. $hash->{helper}{sat} = ReadingsVal($name,"sat",0);
  114. $hash->{helper}{speed} = ReadingsVal($name,"speed",120);
  115. $hash->{helper}{color} = ReadingsVal($name,"color","on");
  116. if( $init_done ) {
  117. PLAYBULB_firstRun($hash);
  118. } else {
  119. InternalTimer( gettimeofday()+30, "PLAYBULB_firstRun", $hash, 0 ) ;
  120. }
  121. $modules{PLAYBULB}{defptr}{$hash->{BTMAC}} = $hash;
  122. return undef;
  123. }
  124. sub PLAYBULB_Undef($$) {
  125. my ( $hash, $arg ) = @_;
  126. my $name = $hash->{NAME};
  127. Log3 $name, 3, "PLAYBULB ($name) - undefined";
  128. delete($modules{PLAYBULB}{defptr}{$hash->{BTMAC}});
  129. return undef;
  130. }
  131. sub PLAYBULB_Attr(@) {
  132. my ( $cmd, $name, $attrName, $attrVal ) = @_;
  133. my $hash = $defs{$name};
  134. my $orig = $attrVal;
  135. if( $attrName eq "model" ) {
  136. if( $cmd eq "set" ) {
  137. PLAYBULB_Run($hash,"statusRequest",undef) if( $init_done );
  138. }
  139. }
  140. }
  141. sub PLAYBULB_firstRun($) {
  142. my ($hash) = @_;
  143. my $name = $hash->{NAME};
  144. RemoveInternalTimer($hash);
  145. PLAYBULB_Run($hash,"statusRequest",undef);
  146. ### Für kurze Zeit da neue Readings
  147. CommandDeleteReading(undef,"$name battery") if( defined(ReadingsVal($name,'battery',undef)) );
  148. }
  149. sub PLAYBULB_Set($$@) {
  150. my ($hash, $name, @aa) = @_;
  151. my ($cmd, $arg) = @aa;
  152. my $action;
  153. if( $cmd eq 'on' ) {
  154. $action = "onoff";
  155. $arg = 1;
  156. } elsif( $cmd eq 'off' ) {
  157. $action = "onoff";
  158. $arg = 0;
  159. } elsif( $cmd eq 'effect' ) {
  160. $action = $cmd;
  161. } elsif( $cmd eq 'rgb' ) {
  162. $action = $cmd;
  163. } elsif( $cmd eq 'sat' ) {
  164. $action = $cmd;
  165. } elsif( $cmd eq 'speed' ) {
  166. $action = $cmd;
  167. } elsif( $cmd eq 'color' ) {
  168. $action = $cmd;
  169. } elsif( $cmd eq 'deviceName' ) {
  170. my $wordlenght = length($arg);
  171. return "to many character for Devicename" if($wordlenght > 20 );
  172. $action = $cmd;
  173. } elsif( $cmd eq 'statusRequest' ) {
  174. $action = $cmd;
  175. $arg = undef;
  176. } else {
  177. my $list = "on:noArg off:noArg rgb:colorpicker,RGB sat:slider,0,5,255 effect:Flash,Pulse,RainbowJump,RainbowFade,Candle,none speed:slider,170,50,20 color:on,off statusRequest:noArg ";
  178. $list .= "deviceName " if( defined($attr{$name}{model}) and ($attr{$name}{model} ne "BTL400M_v18"
  179. or $attr{$name}{model} ne "BTL100C_v10"
  180. or $attr{$name}{model} ne "BTL400M_v37") );
  181. return SetExtensions($hash, $list, $name, $cmd, $arg);
  182. }
  183. PLAYBULB_Run($hash,$action,$arg);
  184. return undef;
  185. }
  186. sub PLAYBULB_Run($$$) {
  187. my ( $hash, $cmd, $arg ) = @_;
  188. my $name = $hash->{NAME};
  189. unless( defined($attr{$name}{model}) ) {
  190. readingsSingleUpdate($hash,'state','set attribut model',1);
  191. return;
  192. }
  193. my $mac = $hash->{BTMAC};
  194. my $dname;
  195. $hash->{helper}{$cmd} = $arg if( $cmd ne "deviceName" );
  196. $hash->{helper}{setDeviceName} = 1 if( $cmd eq "deviceName" );
  197. my $rgb = $hash->{helper}{rgb};
  198. my $sat = sprintf("%02x", $hash->{helper}{sat});
  199. my $effect = $hash->{helper}{effect};
  200. my $speed = sprintf("%02x", $hash->{helper}{speed});
  201. my $stateOnoff = $hash->{helper}{onoff};
  202. my $stateEffect = ReadingsVal($name,"effect","Candle");
  203. my $ac = $playbulbModels{$attr{$name}{model}}{aColor};
  204. my $ae = $playbulbModels{$attr{$name}{model}}{aEffect};
  205. my $ab = $playbulbModels{$attr{$name}{model}}{aBattery};
  206. my $adname = $playbulbModels{$attr{$name}{model}}{aDevicename};
  207. $dname = $arg if( $cmd eq "deviceName" );
  208. if( $cmd eq "color" and $arg eq "off") {
  209. $rgb = "000000";
  210. $sat = "FF";
  211. }
  212. BlockingKill($hash->{helper}{RUNNING_PID}) if(defined($hash->{helper}{RUNNING_PID}));
  213. my $response_encode = PLAYBULB_forRun_encodeJSON($cmd,$mac,$stateOnoff,$sat,$rgb,$effect,$speed,$stateEffect,$ac,$ae,$ab,$adname,$dname);
  214. $hash->{helper}{RUNNING_PID} = BlockingCall("PLAYBULB_BlockingRun", $name."|".$response_encode, "PLAYBULB_BlockingDone", 10, "PLAYBULB_BlockingAborted", $hash) unless(exists($hash->{helper}{RUNNING_PID}));
  215. Log3 $name, 4, "(Sub PLAYBULB - $name) - Call BlockingRun";
  216. }
  217. sub PLAYBULB_BlockingRun($) {
  218. my ($string) = @_;
  219. my ($name,$data) = split("\\|", $string);
  220. my $data_json = decode_json($data);
  221. my $mac = $data_json->{mac};
  222. my $stateOnoff = $data_json->{stateOnoff};
  223. my $sat = $data_json->{sat};
  224. my $rgb = $data_json->{rgb};
  225. my $effect = $data_json->{effect};
  226. my $speed = $data_json->{speed};
  227. my $stateEffect = $data_json->{stateEffect};
  228. my $ac = $data_json->{ac};
  229. my $ae = $data_json->{ae};
  230. my $ab = $data_json->{ab};
  231. my $cmd = $data_json->{cmd};
  232. my $adname = $data_json->{adname};
  233. my $dname = $data_json->{dname};
  234. my $blevel = 1000;
  235. my $cc;
  236. my $ec;
  237. my $response_encode;
  238. Log3 $name, 4, "(Sub PLAYBULB_Run - $name) - Running nonBlocking";
  239. ##### Abruf des aktuellen Status
  240. #### das c vor den bekannten Variablen steht für current
  241. my ($ccc,$cec,$csat,$crgb,$ceffect,$cspeed) = PLAYBULB_gattCharRead($name,$mac,$ac,$ae);
  242. if( defined($ccc) and defined($cec) ) {
  243. ### Regeln für die aktuellen Values
  244. if( $cmd eq "statusRequest" ) {
  245. ###### Batteriestatus einlesen
  246. $blevel = PLAYBULB_readBattery($name,$mac,$ab) if( $ab ne "none" );
  247. ###### Status ob An oder Aus
  248. $stateOnoff = PLAYBULB_stateOnOff($ccc,$cec);
  249. ###### Devicename ermitteln #######
  250. my $dname = PLAYBULB_readDevicename($name,$mac,$adname) if( $adname ne "none" );
  251. Log3 $name, 4, "(Sub PLAYBULB_Run StatusRequest - $name) - Rückgabe an Auswertungsprogramm beginnt";
  252. $response_encode = PLAYBULB_forDone_encodeJSON($blevel,$stateOnoff,$csat,$crgb,$ceffect,$cspeed,$dname);
  253. return "$name|$response_encode";
  254. }
  255. $stateEffect = "none" if( $ceffect eq "ff" );
  256. ##### Schreiben der neuen Char values
  257. PLAYBULB_gattCharWrite($name,$sat,$rgb,$effect,$speed,$stateEffect,$stateOnoff,$mac,$ac,$ae) if( !defined($dname) );
  258. PLAYBULB_writeDevicename($name,$mac,$adname,$dname) if( defined($dname) );
  259. ##### Statusabruf nach dem schreiben der neuen Char Values
  260. ($cc,$ec,$sat,$rgb,$effect,$speed) = PLAYBULB_gattCharRead($name,$mac,$ac,$ae) if( !defined($dname) );
  261. $dname = PLAYBULB_readDevicename($name,$mac,$adname) if( defined($dname) and $adname ne "none" );
  262. $stateOnoff = PLAYBULB_stateOnOff($cc,$ec) if( !defined($dname) );
  263. ###### Batteriestatus einlesen
  264. $blevel = PLAYBULB_readBattery($name,$mac,$ab) if( $ab ne "none" and !defined($dname) );
  265. Log3 $name, 4, "(Sub PLAYBULB_Run - $name) - Rückgabe an Auswertungsprogramm beginnt";
  266. $response_encode = PLAYBULB_forDone_encodeJSON($blevel,$stateOnoff,$sat,$rgb,$effect,$speed,undef) if( !defined($dname) );
  267. $response_encode = PLAYBULB_forDone_encodeJSON(undef,undef,undef,undef,undef,undef,$dname) if( defined($dname) );
  268. return "$name|$response_encode";
  269. }
  270. Log3 $name, 4, "(Sub PLAYBULB_Run - $name) - Rückgabe an Auswertungsprogramm beginnt";
  271. return "$name|err"
  272. unless( defined($cc) and defined($ec) );
  273. }
  274. sub PLAYBULB_gattCharWrite($$$$$$$$$$) {
  275. my ($name,$sat,$rgb,$effect,$speed,$stateEffect,$stateOnoff,$mac,$ac,$ae) = @_;
  276. my $sshHost = AttrVal($name,"sshHost","none");
  277. my $loop = 0;
  278. if( $sshHost ne 'none') {
  279. while ( (qx(ssh $sshHost 'ps ax | grep -v grep | grep "gatttool -b $mac"') and $loop = 0) or (qx(ssh $sshHost 'ps ax | grep -v grep | grep "gatttool -b $mac"') and $loop < 5) ) {
  280. #printf "\n(Sub PLAYBULB_Run) - gatttool noch aktiv, wait 0.5s for new check\n";
  281. sleep 0.5;
  282. $loop++;
  283. }
  284. } else {
  285. while ( (qx(ps ax | grep -v grep | grep "gatttool -b $mac") and $loop = 0) or (qx(ps ax | grep -v grep | grep "gatttool -b $mac") and $loop < 5) ) {
  286. #printf "\n(Sub PLAYBULB_Run) - gatttool noch aktiv, wait 0.5s for new check\n";
  287. sleep 0.5;
  288. $loop++;
  289. }
  290. }
  291. $speed = "01" if( $effect eq "Candle" );
  292. if( $sshHost ne 'none') {
  293. if( $stateOnoff == 0 ) {
  294. qx(ssh $sshHost 'gatttool -b $mac --char-write -a $ac -n 00000000');
  295. } else {
  296. qx(ssh $sshHost 'gatttool -b $mac --char-write -a $ac -n ${sat}${rgb}') if( $stateEffect eq "none" and $effect eq "none" );
  297. qx(ssh $sshHost 'gatttool -b $mac --char-write -a $ae -n ${sat}${rgb}${effects{$effect}}00${speed}00') if( $stateEffect ne "none" or $effect ne "none" );
  298. }
  299. } else {
  300. if( $stateOnoff == 0 ) {
  301. qx(gatttool -b $mac --char-write -a $ac -n 00000000);
  302. } else {
  303. qx(gatttool -b $mac --char-write -a $ac -n ${sat}${rgb}) if( $stateEffect eq "none" and $effect eq "none" );
  304. qx(gatttool -b $mac --char-write -a $ae -n ${sat}${rgb}${effects{$effect}}00${speed}00) if( $stateEffect ne "none" or $effect ne "none" );
  305. }
  306. }
  307. }
  308. sub PLAYBULB_gattCharRead($$$$) {
  309. my ($name,$mac,$ac,$ae) = @_;
  310. my $sshHost = AttrVal($name,"sshHost","none");
  311. my $loop = 0;
  312. if( $sshHost ne 'none') {
  313. while ( (qx(ssh $sshHost 'ps ax | grep -v grep | grep "gatttool -b $mac"') and $loop = 0) or (qx(ssh $sshHost 'ps ax | grep -v grep | grep "gatttool -b $mac"') and $loop < 5) ) {
  314. #printf "\n(Sub PLAYBULB_Run) - gatttool noch aktiv, wait 0.5s for new check\n";
  315. sleep 0.5;
  316. $loop++;
  317. }
  318. } else {
  319. while ( (qx(ps ax | grep -v grep | grep "gatttool -b $mac") and $loop = 0) or (qx(ps ax | grep -v grep | grep "gatttool -b $mac") and $loop < 5) ) {
  320. #printf "\n(Sub PLAYBULB_Run) - gatttool noch aktiv, wait 0.5s for new check\n";
  321. sleep 0.5;
  322. $loop++;
  323. }
  324. }
  325. my @cc;
  326. my @ec;
  327. if( $sshHost ne 'none') {
  328. @cc = split(": ",qx(ssh $sshHost 'gatttool -b $mac --char-read -a $ac'));
  329. @ec = split(": ",qx(ssh $sshHost 'gatttool -b $mac --char-read -a $ae'));
  330. } else {
  331. @cc = split(": ",qx(gatttool -b $mac --char-read -a $ac));
  332. @ec = split(": ",qx(gatttool -b $mac --char-read -a $ae));
  333. }
  334. return (undef,undef,undef,undef,undef,undef)
  335. unless( defined($cc[1]) and defined($ec[1]) );
  336. my $cc = join("",split(" ",$cc[1]));
  337. my $ec = substr(join("",split(" ",$ec[1])),0,8);
  338. my $sat = hex("0x".substr(join("",split(" ",$ec[1])),0,2));
  339. my $rgb = substr(join("",split(" ",$ec[1])),2,6);
  340. my $effect = $effectsHex{substr(join("",split(" ",$ec[1])),8,2)};
  341. my $speed = hex("0x".substr(join("",split(" ",$ec[1])),12,2));
  342. if( $effect eq "none" ) {
  343. $sat = hex("0x".substr(join("",split(" ",$cc[1])),0,2));
  344. $rgb = substr(join("",split(" ",$cc[1])),2,6);
  345. }
  346. return ($cc,$ec,$sat,$rgb,$effect,$speed);
  347. }
  348. sub PLAYBULB_readBattery($$$) {
  349. my ($name,$mac,$ab) = @_;
  350. my $sshHost = AttrVal($name,"sshHost","none");
  351. my $blevel;
  352. my @blevel;
  353. if( $sshHost ne 'none') {
  354. chomp(@blevel = split(": ",qx(ssh $sshHost 'gatttool -b $mac --char-read -a $ab')));
  355. } else {
  356. chomp(@blevel = split(": ",qx(gatttool -b $mac --char-read -a $ab)));
  357. }
  358. ### Bei den Garden Bulbs gibt es noch den Status wird geladen oder wird nicht geladen
  359. ### Beispielausgabe "Characteristic value/descriptor: 51 01" in diesem Beispiel steht 01 für wird geladen
  360. if( AttrVal($name,'model','none') eq 'BTL400M_v18' or AttrVal($name,'model','none') eq 'BTL400M_v37' ) {
  361. $blevel = substr(join("",split(" ",$blevel[1])),0,4);
  362. } else {
  363. $blevel = substr(join("",split(" ",$blevel[1])),0,2);
  364. }
  365. return ($blevel);
  366. }
  367. sub PLAYBULB_stateOnOff($$) {
  368. my ($cc,$ec) = @_;
  369. my $state;
  370. if( $cc eq "00000000" and $ec eq "00000000" ) {
  371. $state = "0";
  372. } else {
  373. $state = "1";
  374. }
  375. return $state;
  376. }
  377. sub PLAYBULB_readDevicename($$$) {
  378. my ($name,$mac,$adname) = @_;
  379. my $sshHost = AttrVal($name,"sshHost","none");
  380. my @dname;
  381. if( $sshHost ne 'none') {
  382. chomp(@dname = split(": ",qx(ssh $sshHost 'gatttool -b $mac --char-read -a $adname')));
  383. } else {
  384. chomp(@dname = split(": ",qx(gatttool -b $mac --char-read -a $adname)));
  385. }
  386. my $dname = join("",split(" ",$dname[1]));
  387. return pack('H*', $dname);
  388. }
  389. sub PLAYBULB_writeDevicename($$$$) {
  390. my ($name,$mac,$adname,$dname) = @_;
  391. my $sshHost = AttrVal($name,"sshHost","none");
  392. my $hexDname = unpack("H*", $dname);
  393. if( $sshHost ne 'none') {
  394. qx(ssh $sshHost 'gatttool -b $mac --char-write-req -a $adname -n $hexDname');
  395. } else {
  396. qx(gatttool -b $mac --char-write-req -a $adname -n $hexDname);
  397. }
  398. }
  399. sub PLAYBULB_forRun_encodeJSON($$$$$$$$$$$$$) {
  400. my ($cmd,$mac,$stateOnoff,$sat,$rgb,$effect,$speed,$stateEffect,$ac,$ae,$ab,$adname,$dname) = @_;
  401. my %data = (
  402. 'cmd' => $cmd,
  403. 'mac' => $mac,
  404. 'stateOnoff' => $stateOnoff,
  405. 'sat' => $sat,
  406. 'rgb' => $rgb,
  407. 'effect' => $effect,
  408. 'speed' => $speed,
  409. 'stateEffect' => $stateEffect,
  410. 'ac' => $ac,
  411. 'ae' => $ae,
  412. 'ab' => $ab,
  413. 'adname' => $adname,
  414. 'dname' => $dname
  415. );
  416. return encode_json \%data;
  417. }
  418. sub PLAYBULB_forDone_encodeJSON($$$$$$$) {
  419. my ($blevel,$stateOnoff,$sat,$rgb,$effect,$speed,$dname) = @_;
  420. my %response = (
  421. 'blevel' => $blevel,
  422. 'stateOnoff' => $stateOnoff,
  423. 'sat' => $sat,
  424. 'rgb' => $rgb,
  425. 'effect' => $effect,
  426. 'speed' => $speed,
  427. 'dname' => $dname
  428. );
  429. return encode_json \%response;
  430. }
  431. sub PLAYBULB_BlockingDone($) {
  432. my ($string) = @_;
  433. my ($name,$response) = split("\\|",$string);
  434. my $hash = $defs{$name};
  435. my $state;
  436. my $color;
  437. my $powerLevel;
  438. my $powerCharge;
  439. delete($hash->{helper}{RUNNING_PID});
  440. Log3 $name, 3, "(Sub PLAYBULB_Done - $name) - Der Helper ist diabled. Daher wird hier abgebrochen" if($hash->{helper}{DISABLED});
  441. return if($hash->{helper}{DISABLED});
  442. if( $response eq "err" ) {
  443. readingsSingleUpdate($hash,"state","unreachable", 1);
  444. return undef;
  445. }
  446. my $response_json = decode_json($response);
  447. if( !defined($hash->{helper}{setDeviceName}) ) {
  448. if( $response_json->{stateOnoff} == 1 ) { $state = "on" } else { $state = "off" } ;
  449. if( ($response_json->{sat} eq "255" and $response_json->{rgb} eq "000000") or
  450. ($response_json->{sat} eq "0" and $response_json->{rgb} eq "ffffff") ) {
  451. $color = "off"; } else { $color = "on"; }
  452. }
  453. if( AttrVal($name,'model','none') eq 'BTL400M_v18' or AttrVal($name,'model','none') eq 'BTL400M_v37' ) {
  454. $powerLevel = hex(substr($response_json->{blevel},0,2));
  455. $powerCharge = hex(substr($response_json->{blevel},3,3));
  456. } else {
  457. $powerLevel = hex($response_json->{blevel});
  458. }
  459. readingsBeginUpdate($hash);
  460. readingsBulkUpdate($hash, "color", "$color") if( !defined($hash->{helper}{setDeviceName}) );
  461. readingsBulkUpdate($hash, "powerLevel", $powerLevel) if( $powerLevel != 1000 and !defined($hash->{helper}{setDeviceName}) );
  462. readingsBulkUpdate($hash, "powerCharge", $powerCharge) if( $powerLevel != 1000 and !defined($hash->{helper}{setDeviceName}) and defined($powerCharge) );
  463. readingsBulkUpdate($hash, "deviceName", $response_json->{dname});
  464. readingsBulkUpdate($hash, "onoff", $response_json->{stateOnoff});
  465. readingsBulkUpdate($hash, "sat", $response_json->{sat}) if( $response_json->{stateOnoff} != 0 and $color ne "off" );
  466. readingsBulkUpdate($hash, "rgb", $response_json->{rgb}) if( $response_json->{stateOnoff} != 0 and $color ne "off" and !defined($hash->{helper}{setDeviceName}) );
  467. readingsBulkUpdate($hash, "effect", $response_json->{effect});
  468. readingsBulkUpdate($hash, "speed", $response_json->{speed});
  469. readingsBulkUpdate($hash, "state", $state);
  470. readingsEndUpdate($hash,1);
  471. $hash->{helper}{onoff} = $response_json->{stateOnoff};
  472. $hash->{helper}{sat} = $response_json->{sat} if( $response_json->{stateOnoff} != 0 and $color ne "off" );
  473. $hash->{helper}{rgb} = $response_json->{rgb} if( $response_json->{stateOnoff} != 0 and $color ne "off" and !defined($hash->{helper}{setDeviceName}) );
  474. $hash->{helper}{effect} = $response_json->{effect};
  475. $hash->{helper}{speed} = $response_json->{speed};
  476. delete $hash->{helper}{setDeviceName} if( defined($hash->{helper}{setDeviceName}) );
  477. Log3 $name, 4, "(Sub PLAYBULB_Done - $name) - Abschluss!";
  478. }
  479. sub PLAYBULB_BlockingAborted($) {
  480. my ($hash) = @_;
  481. my $name = $hash->{NAME};
  482. delete($hash->{helper}{RUNNING_PID});
  483. readingsSingleUpdate($hash,"state","unreachable", 1);
  484. Log3 $name, 4, "($name) - The BlockingCall Process terminated unexpectedly. Timedout";
  485. }
  486. 1;
  487. =pod
  488. =item device
  489. =item summary Modul to control MiPow Playbulb products
  490. =item summary_DE Modul zum steuern der MiPow Playbulb Produkte
  491. =begin html
  492. <a name="PLAYBULB"></a>
  493. <h3>MiPow Playbulb</h3>
  494. <p><span style="text-decoration: underline;"><strong>PLAYBULB -Smart Light from MIPOW.COM</strong></span></p>
  495. <p>This module integrates different smart lights from MIPOW into FHEM and displays several settings</p>
  496. <p>&nbsp;</p>
  497. <p><a name="PLAYBULBdefine"></a><strong>Define</strong><br /><code>define &lt;name&gt; PLAYBULB &lt;MAC-ADDRESS&gt;</code></p>
  498. <p>Example: <code>define PlaybulbCandle PLAYBULB 0B:0B:0C:D0:E0:F0</code></p>
  499. <p>&nbsp;</p>
  500. <p>With this command a new PLAYBULB device in a room called PLAYBULB is created. The parameter &lt;MAC-ADDRESS&gt; defines the bluetooth MAC address of your mipow smart light.</p>
  501. <p>&nbsp;</p>
  502. <p style="padding-left: 90px;"><strong>pre-requesites</strong>: a BT LE 4.0 key and a working bluez installation or equivalent is necessary. (find a good guide at <a href="http://www.elinux.org/RPi_Bluetooth_LE)">http://www.elinux.org/RPi_Bluetooth_LE)</a></p>
  503. <p style="padding-left: 90px;">It seems like the devices accept only one active connection.</p>
  504. <p><br /><a name="PLAYBULBreadings"></a><strong>Readings</strong></p>
  505. <ul>
  506. <ul>
  507. <ul>
  508. <li>powerLevel - percentage of batterylevel</li>
  509. <li>powerCharge - state of charging (Playbulb Garden only)</li>
  510. <li>color - indicates if colormode is on or off</li>
  511. <li>devicename - given name of the device</li>
  512. <li>effect - indicates which effect is selected (Flash; Pulse; RainbowJump; RainbowFade; Candle; none)</li>
  513. <li>onoff - indicates if the device is turned on (1) or off (0)</li>
  514. <li>rgb - shows the selected color in hex by rgb (example: FF0000 is full red)</li>
  515. <li>sat - shows the selected saturation from 0 to 255 (seems to be inverted; 0 is full saturation)</li>
  516. <li>speed - shows the selected effect speed (possible: 20; 70; 120; 170)</li>
  517. <li>state - current state of PLAYBULB device</li>
  518. </ul>
  519. </ul>
  520. </ul>
  521. <p><a name="PLAYBULBset"></a><strong>Set</strong></p>
  522. <ul>
  523. <ul>
  524. <ul>
  525. <li>on - turns device on</li>
  526. <li>off - turns device off</li>
  527. <li>rgb - colorpicker,RGB; gives the possibility to set any hue, saturation, brightness</li>
  528. <li>sat - saturation slider from 0 to 255 steps 5</li>
  529. <li>effect - Flash,Pulse,RainbowJump,RainbowFade,Candle,none; activates the selected effect</li>
  530. <li>speed - slider: values are 170, 120, 70, 20</li>
  531. <li>color - on,of; switches the device to rgb or white</li>
  532. <li>statusRequest - is necessary to request state of the device</li>
  533. <li>deviceName - changes the name of the bluetoothdevice e.g. "PlaybulbCandle"</li>
  534. </ul>
  535. </ul>
  536. </ul>
  537. <p><br /><strong>Get na</strong></p>
  538. <p>&nbsp;</p>
  539. <p><strong>Attributes&nbsp; </strong></p>
  540. <ul>
  541. <ul>
  542. <ul>
  543. <li>model<br />BTL300_v5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Candle Firmware 5<br />BTL300_v6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Candle Firmware 6<br />BTL201_v2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Smart<br />BTL201M_V16&nbsp; # Smart (1/2017)<br />BTL505_v1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Stripe<br />BTL400M_v18&nbsp; # Garden<br />BTL100C_v10&nbsp; # Color LED</li>
  544. <li>sshHost - IP or FQDN for SSH remote control</li>
  545. </ul>
  546. </ul>
  547. </ul>
  548. <p><br /><a name="PLAYBULBstate"></a><strong>state</strong></p>
  549. <ul>
  550. <ul>
  551. <ul>
  552. <li>set attribut model&nbsp;- is shown after initial define</li>
  553. <li>on - device is on</li>
  554. <li>off - device is off</li>
  555. <li>unreachable - connection to device lost</li>
  556. </ul>
  557. </ul>
  558. </ul>
  559. <p><br /><br /><br /><span style="text-decoration: underline;"><strong>Further examples and readings:</strong></span><br /><a href="https://forum.fhem.de/index.php/topic,60829.msg522226.html#msg522226">Forum thread (german only)</a><br /><br /></p>
  560. =end html
  561. =begin html_DE
  562. <a name="PLAYBULB"></a>
  563. <h3>MiPow Playbulb</h3>
  564. <p><span style="text-decoration: underline;"><strong>PLAYBULB -Smart Light von MIPOW.COM</strong></span></p>
  565. <p>Dieses Modul integriert diverse Smart Leuchten von MIPOW in FHEM und zeigt ihre Einstellungen an.</p>
  566. <p>&nbsp;</p>
  567. <p><a name="PLAYBULBdefine"></a><strong>Define</strong><br /><code>define &lt;name&gt; PLAYBULB &lt;MAC-ADDRESS&gt;</code></p>
  568. <p>Beispiel: <code>define PlaybulbCandle PLAYBULB 0B:0B:0C:D0:E0:F0</code></p>
  569. <p>&nbsp;</p>
  570. <p>Mit diesem Kommando wird ein neues PLAYBULB Device im Raum PLAYBULB angelegt. Der Parameter &lt;MAC-ADDRESS&gt; definiert die Bluetooth MAC Address der Mipow Smart Leuchte.</p>
  571. <p>&nbsp;</p>
  572. <p style="padding-left: 90px;"><strong>Vorbedingungen</strong>: Es wird ein Bluetooth LE 4.0 Stick, sowie eine funktionierende bluez Installation oder aehnlich vorausgesetzt. (Eine gute Anleitung findedsich hier: <a href="http://www.elinux.org/RPi_Bluetooth_LE)">http://www.elinux.org/RPi_Bluetooth_LE)</a></p>
  573. <p style="padding-left: 90px;">Derzeit sieht es so aus, als ob die Devices nur eine aktive Verbindung akzeptieren.</p>
  574. <p><br /><a name="PLAYBULBreadings"></a><strong>Readings</strong></p>
  575. <ul>
  576. <ul>
  577. <ul>
  578. <li>battery - Batterielevel in Prozent</li>
  579. <li>color - Zeigt an ob der Farbmodus an oder aus ist</li>
  580. <li>devicename - Geraetename</li>
  581. <li>effect - Zeigt an, welcher effect ausgewaehlt ist: (Flash; Pulse; RainbowJump; RainbowFade; Candle; none)</li>
  582. <li>onoff - Zeigt an, ob das Geraet an (1) oder aus (0) ist</li>
  583. <li>rgb - Zeigt die gewaehlte Farbe in HEX vom Typ rgb (Beispiel: FF0000 ist satt rot)</li>
  584. <li>sat - Zeigt die gewaehlte Saettigung von 0 bis 255 (scheint invertiert zu sein; 0 ist volle Saettigung)</li>
  585. <li>speed - Zeigt die gewaehlte Effektgeschwindigkeit (moeglich sind: 20; 70; 120; 170)</li>
  586. <li>state - Aktueller Zustand des Devices</li>
  587. </ul>
  588. </ul>
  589. </ul>
  590. <p><a name="PLAYBULBset"></a><strong>Set</strong></p>
  591. <ul>
  592. <ul>
  593. <ul>
  594. <li>on - Schaltet das Geraet ein</li>
  595. <li>off - Schaltet das Geraet aus</li>
  596. <li>rgb - Farbwaehler,RGB; ermoeglicht jede Farbe, Saettigung und Helligkeit einzustellen</li>
  597. <li>sat - Schieber zur Einstellung der Saettigungvon 0 bis 255, Schrittweite 5</li>
  598. <li>effect - Flash,Pulse,RainbowJump,RainbowFade,Candle,none; aktiviert den gewaehlten Effekt</li>
  599. <li>speed - Schieberegler: Werte sind 170, 120, 70, 20</li>
  600. <li>color - on,of; Schaltet das Geraet auf RGB oder weiss</li>
  601. <li>statusRequest - Ist notwendig, um den Zustand des Geraetes abzufragen</li>
  602. <li>deviceName - Aendert den Namen des Bluetoothdevice z.B. "PlaybulbCandle"</li>
  603. </ul>
  604. </ul>
  605. </ul>
  606. <p><br /><strong>Get na</strong></p>
  607. <p>&nbsp;</p>
  608. <p><strong>Attributes&nbsp; </strong></p>
  609. <ul>
  610. <ul>
  611. <ul>
  612. <li>model<br />BTL300_v5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Candle Firmware 5<br />BTL300_v6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Candle Firmware 6<br />BTL201_v2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Smart<br />BTL201M_V16&nbsp; # Smart (1/2017)<br />BTL505_v1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Stripe<br />BTL400M_v18&nbsp; # Garden<br />BTL100C_v10&nbsp; # Color LED</li>
  613. <li>sshHost - IP oder FQDN für SSH remote Kontrolle</li>
  614. </ul>
  615. </ul>
  616. </ul>
  617. <p><br /><a name="PLAYBULBstate"></a><strong>state</strong></p>
  618. <ul>
  619. <ul>
  620. <ul>
  621. <li>set attribut model&nbsp;- wird nach der Erstdefinition angezeigt</li>
  622. <li>on - Device ist an</li>
  623. <li>off - Device ist aus</li>
  624. <li>unreachable - Verbindung zum device verloren</li>
  625. </ul>
  626. </ul>
  627. </ul>
  628. <p><br /><br /><br /><span style="text-decoration: underline;"><strong>Weitere Beispiel und readings:</strong></span><br /><a href="https://forum.fhem.de/index.php/topic,60829.msg522226.html#msg522226">Forum thread</a></p>
  629. <p>&nbsp;</p>
  630. =end html_DE
  631. =cut