37_harmony.pm 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945
  1. # $Id: 37_harmony.pm 15393 2017-11-05 11:06:17Z justme1968 $
  2. package main;
  3. use strict;
  4. use warnings;
  5. use Data::Dumper;
  6. use JSON;
  7. use MIME::Base64;
  8. use IO::Socket::INET;
  9. use Encode qw(encode_utf8);
  10. #use XML::Simple qw(:strict);
  11. use HttpUtils;
  12. my $harmony_isFritzBox = undef;
  13. sub
  14. harmony_isFritzBox()
  15. {
  16. $harmony_isFritzBox = int( qx( [ -f /usr/bin/ctlmgr_ctl ] && echo 1 || echo 0 ) ) if( !defined($harmony_isFritzBox) );
  17. return $harmony_isFritzBox;
  18. }
  19. sub
  20. harmony_decode_json($)
  21. {
  22. my ($data) = @_;
  23. return eval { decode_json($data) } if( harmony_isFritzBox() );
  24. return eval { JSON->new->utf8(0)->decode($data) };
  25. }
  26. sub
  27. harmony_Initialize($)
  28. {
  29. my ($hash) = @_;
  30. $hash->{ReadFn} = "harmony_Read";
  31. $hash->{DefFn} = "harmony_Define";
  32. $hash->{NotifyFn} = "harmony_Notify";
  33. $hash->{UndefFn} = "harmony_Undefine";
  34. $hash->{SetFn} = "harmony_Set";
  35. $hash->{GetFn} = "harmony_Get";
  36. $hash->{AttrFn} = "harmony_Attr";
  37. $hash->{AttrList} = "disable:1 nossl:1 $readingFnAttributes";
  38. $hash->{FW_detailFn} = "harmony_detailFn";
  39. $hash->{FW_showStatus} = 1;
  40. }
  41. #####################################
  42. sub
  43. harmony_Define($$)
  44. {
  45. my ($hash, $def) = @_;
  46. my @a = split("[ \t][ \t]*", $def);
  47. return "Usage: define <name> harmony [username password] ip" if(@a < 3 || @a > 5);
  48. return "Usage: define <name> harmony [username password] ip" if(@a == 4 && $a[2] ne "DEVICE" );
  49. delete( $hash->{helper}{username} );
  50. delete( $hash->{helper}{password} );
  51. my $name = $a[0];
  52. if( @a == 3 ) {
  53. my $ip = $a[2];
  54. $hash->{ip} = $ip;
  55. } elsif( @a == 4 ) {
  56. my $id = $a[3];
  57. return "$name: device '$id' already defined" if( defined($modules{$hash->{TYPE}}{defptr}{$id}) );
  58. $hash->{id} = $id;
  59. $modules{$hash->{TYPE}}{defptr}{$id} = $hash;
  60. } elsif( @a == 5 ) {
  61. my $username = harmony_encrypt($a[2]);
  62. my $password = harmony_encrypt($a[3]);
  63. my $ip = $a[4];
  64. $hash->{DEF} = "$username $password $ip";
  65. $hash->{helper}{username} = $username;
  66. $hash->{helper}{password} = $password;
  67. $hash->{ip} = $ip;
  68. }
  69. $hash->{NAME} = $name;
  70. $hash->{STATE} = "Initialized";
  71. $hash->{ConnectionState} = "Initialized";
  72. #$attr{$name}{nossl} = 1 if( !$init_done && harmony_isFritzBox() );
  73. $hash->{NOTIFYDEV} = "global";
  74. if( $init_done ) {
  75. harmony_connect($hash) if( !defined($hash->{id}) );
  76. }
  77. return undef;
  78. }
  79. sub
  80. harmony_Notify($$)
  81. {
  82. my ($hash,$dev) = @_;
  83. return if($dev->{NAME} ne "global");
  84. return if(!grep(m/^INITIALIZED|REREADCFG$/, @{$dev->{CHANGED}}));
  85. harmony_connect($hash) if( !defined($hash->{id}) );
  86. return undef;
  87. }
  88. sub
  89. harmony_Undefine($$)
  90. {
  91. my ($hash, $arg) = @_;
  92. if( defined($hash->{id}) ) {
  93. delete( $modules{$hash->{TYPE}}{defptr}{$hash->{id}} );
  94. return undef;
  95. }
  96. RemoveInternalTimer($hash);
  97. harmony_disconnect($hash);
  98. return undef;
  99. }
  100. sub
  101. harmony_detailFn()
  102. {
  103. my ($FW_wname, $d, $room, $pageHash) = @_; # pageHash is set for summaryFn.
  104. my $hash = $defs{$d};
  105. return if( !defined( $hash->{discoveryinfo} ) );
  106. my $clientId = $hash->{discoveryinfo}->{setupSessionClient};
  107. $clientId =~ s/^\w*-//;
  108. my $hubIP = $hash->{discoveryinfo}->{ip};
  109. my $hubName = $hash->{discoveryinfo}->{friendlyName};
  110. $hubName =~ s/ /%20/;
  111. return "<a href=\"http://sl.dhg.myharmony.com/mobile/2/production/?locale=de-DE&clientId=$clientId&hubIP=$hubIP&hubName=$hubName&settings\" target=\"_blank\">myHarmony config</a><br>"
  112. }
  113. sub
  114. harmony_idOfActivity($$;$)
  115. {
  116. my ($hash, $label, $default) = @_;
  117. my $quoted_label = $label;
  118. $quoted_label =~ s/\./ /g;
  119. $quoted_label = quotemeta($quoted_label);
  120. foreach my $activity (@{$hash->{config}->{activity}}) {
  121. return $activity->{id} if( $activity->{label} =~ m/^$label$/ );
  122. return $activity->{id} if( $activity->{label} =~ m/^$quoted_label$/ );
  123. }
  124. return $default;
  125. }
  126. sub
  127. harmony_labelOfActivity($$;$)
  128. {
  129. my ($hash, $id, $default) = @_;
  130. foreach my $activity (@{$hash->{config}->{activity}}) {
  131. return $activity->{label} if( $activity->{id} == $id );
  132. }
  133. return $default;
  134. }
  135. sub
  136. harmony_activityOfId($$)
  137. {
  138. my ($hash, $id) = @_;
  139. foreach my $activity (@{$hash->{config}->{activity}}) {
  140. return $activity if( $activity->{id} == $id );
  141. }
  142. return undef;
  143. }
  144. sub
  145. harmony_idOfDevice($$;$)
  146. {
  147. my ($hash, $label, $default) = @_;
  148. my $quoted_label = $label;
  149. $quoted_label =~ s/\./ /g;
  150. $quoted_label = quotemeta($quoted_label);
  151. foreach my $device (@{$hash->{config}->{device}}) {
  152. return $device->{id} if( $device->{label} =~ m/^$label$/ );
  153. return $device->{id} if( $device->{label} =~ m/^$quoted_label$/ );
  154. }
  155. return $default;
  156. }
  157. sub
  158. harmony_labelOfDevice($$;$)
  159. {
  160. my ($hash, $id, $default) = @_;
  161. return undef if( $id eq '<unknown>' );
  162. return undef if( !defined($hash->{config}) );
  163. foreach my $device (@{$hash->{config}->{device}}) {
  164. return $device->{label} if( $device->{id} == $id );
  165. }
  166. return $default;
  167. }
  168. sub
  169. harmony_deviceOfId($$)
  170. {
  171. my ($hash, $id) = @_;
  172. return undef if( !defined($hash->{config}) );
  173. foreach my $device (@{$hash->{config}->{device}}) {
  174. return $device if( $device->{id} == $id );
  175. }
  176. return undef;
  177. }
  178. sub
  179. harmony_actionOfCommand($$)
  180. {
  181. my ($device, $command) = @_;
  182. return undef if( ref($device) ne "HASH" );
  183. $command = lc($command);
  184. foreach my $group (@{$device->{controlGroup}}) {
  185. foreach my $function (@{$group->{function}}) {
  186. #if( lc($function->{name}) eq $command ) {
  187. if( lc($function->{name}) =~ m/^$command$/ ) {
  188. return harmony_decode_json($function->{action});
  189. }
  190. }
  191. }
  192. return undef;
  193. }
  194. sub
  195. harmony_hubOfDevice($)
  196. {
  197. my ($id) = @_;
  198. foreach my $d (sort keys %defs) {
  199. next if( !defined($defs{$d}) );
  200. next if( $defs{$d}->{TYPE} ne "harmony" );
  201. next if( $defs{$d}->{id} );
  202. next if( !harmony_deviceOfId($defs{$d}, $id) );
  203. Log3 undef, 3, "harmony: IODev for device $id is $d" ;
  204. return $d;
  205. }
  206. }
  207. sub
  208. harmony_Set($$@)
  209. {
  210. my ($hash, $name, $cmd, @params) = @_;
  211. my ($param_a, $param_h) = parseParams(\@params);
  212. my ($param, $param2) = @{$param_a};
  213. #$cmd = lc( $cmd );
  214. my $list = "";
  215. if( defined($hash->{id}) ) {
  216. if( !$hash->{hub} ) {
  217. $hash->{hub} = harmony_hubOfDevice($hash->{id});
  218. return "no hub found for device $name ($hash->{id})" if( !$hash->{hub} );
  219. }
  220. if( $cmd ne "?" && !$param ) {
  221. $cmd = "PowerOn" if( $cmd eq "on" );
  222. $cmd = "PowerOff" if( $cmd eq "off" );
  223. my $device = harmony_deviceOfId( $defs{$hash->{hub}}, $hash->{id} );
  224. if( harmony_actionOfCommand( $device, $cmd ) ) {
  225. $param = $cmd;
  226. $cmd = "command";
  227. }
  228. }
  229. if( $cmd eq "command" ) {
  230. $param2 = $param;
  231. $param = $hash->{id};
  232. $hash = $defs{$hash->{hub}};
  233. } elsif( $cmd eq "hidDevice" || $cmd eq "text" || $cmd eq "cursor" || $cmd eq "special" || $cmd eq "hid" ) {
  234. my $id = $hash->{id};
  235. $hash = $defs{$hash->{hub}};
  236. my $device = harmony_deviceOfId( $hash, $id );
  237. return "unknown device" if( !$device );
  238. return "no keyboard associated with device $device->{label}" if( !$device->{IsKeyboardAssociated} );
  239. if( !$hash->{hidDevice} || $hash->{hidDevice} ne $id ) {
  240. $hash->{hidDevice} = $id;
  241. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.engine?sethiddevice' token=''>deviceId=$id</oa>");
  242. sleep( 3 );
  243. }
  244. return if( $cmd eq "hidDevice" );
  245. } else {
  246. $list = "command hidDevice:noArg text cursor:up,down,left,right,pageUp,pageDown,home,end special:previousTrack,nextTrack,stop,playPause,volumeUp,volumeDown,mute";
  247. #my $device = harmony_deviceOfId( $defs{$hash->{hub}}, $hash->{id} );
  248. #$list .= " on off" if( !defined($device->{isManualPower}) || $device->{isManualPower} eq "false" || !$device->{isManualPower} );
  249. return "Unknown argument $cmd, choose one of $list";
  250. }
  251. }
  252. if( $cmd ne "?" && !$param ) {
  253. if( $cmd eq 'off' ) {
  254. $cmd = "activity";
  255. $param = "-1";
  256. } elsif( my $activity = harmony_activityOfId($hash, $hash->{currentActivityID}) ) {
  257. if( harmony_actionOfCommand( $activity, $cmd ) ) {
  258. $param = $cmd;
  259. $cmd = "command";
  260. }
  261. }
  262. }
  263. if( $cmd eq 'activity' ) {
  264. $param = harmony_idOfActivity($hash, $param) if( $param && $param !~ m/^([\d-])+$/ );
  265. return "unknown activity" if( !$param );
  266. harmony_sendEngineGet($hash, "startactivity", "activityId=$param:timestamp=0");
  267. delete $hash->{channelAfterStart};
  268. $hash->{channelAfterStart} = $param2 if( $param2 );
  269. return undef;
  270. } elsif( $cmd eq "channel" ) {
  271. delete $hash->{channelAfterStart};
  272. return "no current activity" if( !defined($hash->{currentActivityID}) || $hash->{currentActivityID} == -1 );
  273. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  274. return "no device with 'channel changing role' in current activity $activity->{label}" if( !$activity->{isTuningDefault} );
  275. return "missing channel" if( !$param );
  276. harmony_sendEngineGet($hash, "changeChannel", "channel=$param:timestamp=0");
  277. return undef;
  278. } elsif( $cmd eq "command" ) {
  279. my $action;
  280. if( !$param2 ) {
  281. return "unknown activity" if( !$hash->{currentActivityID} );
  282. return "unknown command" if( !$param );
  283. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  284. return "unknown activity" if( !$activity );
  285. $action = harmony_actionOfCommand( $activity, $param );
  286. return "unknown command $param" if( !$action );
  287. } else {
  288. $param = harmony_idOfDevice($hash, $param) if( $param && $param !~ m/^([\d-])+$/ );
  289. return "unknown device" if( !$param );
  290. return "unknown command" if( !$param2 );
  291. my $device = harmony_deviceOfId( $hash, $param );
  292. return "unknown device" if( !$device );
  293. $action = harmony_actionOfCommand( $device, $param2 );
  294. return "unknown command $param2" if( !$action );
  295. }
  296. my $duration = $param_h->{duration};
  297. return "duration musst be numeric" if( defined($duration) && $duration !~ m/^([\d.-])+$/ );
  298. $duration = 0.1 if( !$duration || $duration < 0 );
  299. $duration = 5 if $duration > 5;
  300. Log3 $name, 4, "$name: sending $action->{command} for ${duration}s for ". harmony_labelOfDevice($hash, $action->{deviceId} );
  301. my $payload = "status=press:action={'command'::'$action->{command}','type'::'$action->{type}','deviceId'::'$action->{deviceId}'}:timestamp=0";
  302. harmony_sendEngineRender($hash, "holdAction", $payload);
  303. select(undef, undef, undef, ($duration));
  304. $payload = "status=release:action={'command'::'$action->{command}','type'::'$action->{type}','deviceId'::'$action->{deviceId}'}:timestamp=".$duration*1000;
  305. harmony_sendEngineRender($hash, "holdAction", $payload);
  306. return undef;
  307. } elsif( $cmd eq "getCurrentActivity" ) {
  308. harmony_sendEngineGet($hash, "getCurrentActivity");
  309. return undef;
  310. } elsif( $cmd eq "getConfig" ) {
  311. harmony_sendEngineGet($hash, "config");
  312. return undef;
  313. } elsif( $cmd eq "hidDevice" ) {
  314. my $id = $param;
  315. if( !$id && $hash->{currentActivityID} ) {
  316. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  317. return "unknown activity" if( !$activity );
  318. return "no device with 'keyboard text entry role' in current activity $activity->{label}" if( !$activity->{KeyboardTextEntryActivityRole} );
  319. $id = $activity->{KeyboardTextEntryActivityRole};
  320. } else {
  321. $id = harmony_idOfDevice($hash, $id) if( $id && $id !~ m/^([\d-])+$/ );
  322. return "unknown device $param" if( $param && !$id );
  323. my $device = harmony_deviceOfId( $hash, $id );
  324. return "unknown device" if( !$device );
  325. return "no keyboard associated with device $device->{label}" if( !$device->{IsKeyboardAssociated} );
  326. }
  327. $hash->{hidDevice} = $id;
  328. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.engine?sethiddevice' token=''>deviceId=$id</oa>");
  329. return undef;
  330. } elsif( $cmd eq "hid" || $cmd eq "text" || $cmd eq "cursor" || $cmd eq "special" ) {
  331. return "nothing to send" if( !$param );
  332. return "unknown activity" if( !$hash->{currentActivityID} );
  333. return "unknown command" if( !$param );
  334. if( !$hash->{hidDevice} ) {
  335. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  336. return "unknown activity" if( !$activity );
  337. return "no device with 'keyboard text entry role' in current activity $activity->{label}" if( !$activity->{KeyboardTextEntryActivityRole} );
  338. }
  339. if( $cmd eq "text" ) {
  340. $hash->{hid} = "" if( !$hash->{hid} );
  341. $hash->{hid} .= join(' ', @params);
  342. $param = undef;
  343. } elsif( $cmd eq "cursor" ) {
  344. $param = lc( $param ) if( $param );
  345. $param = "0700004A" if( $param eq "home" );
  346. $param = "0700004B" if( $param eq "pageup" );
  347. $param = "0700004D" if( $param eq "end" );
  348. $param = "0700004E" if( $param eq "pagedown" );
  349. $param = "0700004F" if( $param eq "right" );
  350. $param = "07000050" if( $param eq "left" );
  351. $param = "07000051" if( $param eq "down" );
  352. $param = "07000052" if( $param eq "up" );
  353. return "unknown cursor direction $param" if( $param !~ m/^07/ );
  354. } elsif( $cmd eq "special" ) {
  355. $param = lc( $param ) if( $param );
  356. $param = "01000081" if( $param eq "systempower" );
  357. $param = "01000082" if( $param eq "systemsleep" );
  358. $param = "01000083" if( $param eq "systemwake" );
  359. $param = "0C0000B5" if( $param eq "nexttrack" );
  360. $param = "0C0000B6" if( $param eq "previoustrack" );
  361. $param = "0C0000B7" if( $param eq "stop" );
  362. $param = "0C0000CD" if( $param eq "playpause" );
  363. $param = "0C0000E9" if( $param eq "volumeup" );
  364. $param = "0C0000EA" if( $param eq "volumedown" );
  365. $param = "0C0000E2" if( $param eq "mute" );
  366. return "unknown special key $param" if( $param !~ m/^0(1|C)/ );
  367. }
  368. harmony_sendHID($hash, $param);
  369. return undef;
  370. } elsif( $cmd eq "reconnect" ) {
  371. delete $hash->{helper}{UserAuthToken} if( $param && $param eq "all" );
  372. delete $hash->{identity} if( $param && $param eq "all" );
  373. harmony_connect($hash);
  374. return undef;
  375. } elsif( $cmd eq "autocreate" ) {
  376. return harmony_autocreate($hash,$param);
  377. return undef;
  378. } elsif( $cmd eq "sleeptimer" ) {
  379. my $interval = $param?$param*60:60*60;
  380. $interval = -1 if( $interval < 0 );
  381. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.engine?setsleeptimer'>interval=$interval</oa>", "setsleeptimer");
  382. return undef;
  383. } elsif( $cmd eq "sync" ) {
  384. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='setup.sync' token=''/>");
  385. return undef;
  386. } elsif( $cmd eq "update" ) {
  387. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?update' token=''>format=json</oa>");
  388. return undef;
  389. } elsif( $cmd eq "active" ) {
  390. return "can't activate disabled hub." if(AttrVal($name, "disable", undef));
  391. $hash->{ConnectionState} = "Disconnected";
  392. readingsSingleUpdate( $hash, "state", $hash->{ConnectionState}, 1 );
  393. harmony_connect($hash);
  394. return undef;
  395. } elsif( $cmd eq "inactive" ) {
  396. harmony_disconnect($hash);
  397. readingsSingleUpdate($hash, "state", "inactive", 1);
  398. return undef;
  399. } elsif( $cmd eq "xxx" ) {
  400. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?check' token=''>format=json</oa>");
  401. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?status' token=''>format=json</oa>");
  402. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?update' token=''>format=json</oa>");
  403. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.automation?notify' token=''></oa>");
  404. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.automation?getState' token=''></oa>");
  405. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.automation.state?notify' token=''></oa>");
  406. #harmony_sendIq($hash, "<query xmlns='jabber:iq:roster'/>");
  407. #harmony_sendIq($hash, "<query xmlns='http://jabber.org/protocol/disco#info'/>");
  408. #harmony_sendIq($hash, "<query xmlns='http://jabber.org/protocol/disco#items'/>");
  409. return undef;
  410. }
  411. if( $hash->{config} ) {
  412. return undef if( !defined($hash->{config}) );
  413. my $activities;
  414. if( $hash->{config}->{activity} ) {
  415. foreach my $activity (sort { ($a->{activityOrder}||0) <=> ($b->{activityOrder}||0) } @{$hash->{config}->{activity}}) {
  416. next if( $activity->{id} == -1 );
  417. $activities .= "," if( $activities );
  418. $activities .= $activity->{label};
  419. }
  420. }
  421. if( my $activity = harmony_activityOfId($hash, -1) ) {
  422. $activities .= "," if( $activities );
  423. $activities .= $activity->{label};
  424. }
  425. if( $activities ) {
  426. $activities =~ s/ /./g;
  427. $list .= " activity:$activities";
  428. }
  429. my $hidDevices;
  430. my $autocreateDevices;
  431. if( $hash->{config}->{device} ) {
  432. foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  433. if( $device->{IsKeyboardAssociated} ) {
  434. $hidDevices .= "," if( $hidDevices );
  435. $hidDevices .= harmony_labelOfDevice($hash, $device->{id} );
  436. }
  437. if( !defined($modules{$hash->{TYPE}}{defptr}{$device->{id}}) ) {
  438. $autocreateDevices .= "," if( $autocreateDevices );
  439. $autocreateDevices .= harmony_labelOfDevice($hash, $device->{id} );
  440. }
  441. }
  442. }
  443. if( $hidDevices ) {
  444. $hidDevices =~ s/ /./g;
  445. $list .= " hidDevice:,$hidDevices";
  446. }
  447. if( $autocreateDevices ) {
  448. $autocreateDevices =~ s/ /./g;
  449. $list .= " autocreate:$autocreateDevices,";
  450. }
  451. }
  452. $list .= " channel" if( defined($hash->{currentActivityID}) && $hash->{currentActivityID} != -1 );
  453. $list .= " command active:noArg inactive:noArg getConfig:noArg getCurrentActivity:noArg off:noArg reconnect:noArg sleeptimer sync:noArg text cursor:up,down,left,right,pageUp,pageDown,home,end special:previousTrack,nextTrack,stop,playPause,volumeUp,volumeDown,mute";
  454. $list .= " update:noArg" if( $hash->{hubUpdate} );
  455. return "Unknown argument $cmd, choose one of $list";
  456. }
  457. sub
  458. harmony_getLoginToken($)
  459. {
  460. my ($hash) = @_;
  461. #return if( defined($hash->{helper}{UserAuthToken}) );
  462. if( 0 ) {
  463. my $https = "https";
  464. $https = "http" if( AttrVal($hash->{NAME}, "nossl", 0) );
  465. my $json = encode_json( { clientId => '',
  466. clientTypeId => 'ControlApp' } );
  467. my($err,$data) = HttpUtils_BlockingGet({
  468. url => "$https://svcs.myharmony.com/discovery/Discovery.svc/json/GetJson2Uris",
  469. timeout => 10,
  470. #noshutdown => 1,
  471. #httpversion => "1.1",
  472. header => "Content-Type: application/json;charset=utf-8",
  473. data => $json,
  474. });
  475. harmony_dispatch( {hash=>$hash,type=>'GetJson2Uris'},$err,$data );
  476. }
  477. return if( defined($hash->{helper}{UserAuthToken}) );
  478. if( 1 || !$hash->{helper}{username} ) {
  479. $hash->{helper}{UserAuthToken} = "";
  480. return;
  481. }
  482. my $https = "https";
  483. $https = "http" if( AttrVal($hash->{NAME}, "nossl", 0) );
  484. my $json = encode_json( { email => harmony_decrypt($hash->{helper}{username}),
  485. password => harmony_decrypt($hash->{helper}{password}) } );
  486. my($err,$data) = HttpUtils_BlockingGet({
  487. url => "$https://svcs.myharmony.com/CompositeSecurityServices/Security.svc/json/GetUserAuthToken",
  488. timeout => 10,
  489. #noshutdown => 1,
  490. #httpversion => "1.1",
  491. header => "Content-Type: application/json;charset=utf-8",
  492. data => $json,
  493. });
  494. harmony_dispatch( {hash=>$hash,type=>'token'},$err,$data );
  495. }
  496. sub
  497. harmony_attr2hash($)
  498. {
  499. my ($attr) = @_;
  500. my @args = split(' ', $attr);
  501. my %params = ();
  502. while (@args) {
  503. my $arg = shift(@args);
  504. my ($name,$value) = split("=", $arg,2);
  505. while( $value && $value =~ m/^'/ && $value !~ m/'$/ ) {
  506. my $next = shift(@args);
  507. last if( !defined($next) );
  508. $value .= " ". $next;
  509. }
  510. $params{$name} = substr( $value, 1, -1 );
  511. }
  512. return \%params;
  513. }
  514. sub
  515. harmony_CDATA2hash($)
  516. {
  517. my ($cdata) = @_;
  518. my @args = split(':', $cdata);
  519. my %params = ();
  520. while (@args) {
  521. my $arg = shift(@args);
  522. my ($name,$value) = split("=", $arg,2);
  523. #fix for updates=table: 0x...
  524. if( $args[0] && $args[0] !~ m/=/ ) {
  525. my $next = shift(@args);
  526. last if( !defined($next) );
  527. $value .= ":". $next;
  528. }
  529. ##fix for http://...
  530. #if( $args[0] && $args[0] =~ m/^\/\// ) {
  531. # my $next = shift(@args);
  532. # last if( !defined($next) );
  533. # $value .= ":". $next;
  534. #}
  535. #fix for json {...<key>:<value>...}
  536. while( $value && $value =~ m/^{/ && $value !~ m/}$/ ) {
  537. my $next = shift(@args);
  538. last if( !defined($next) );
  539. $value .= ":". $next;
  540. }
  541. $params{$name} = $value if( $name );
  542. }
  543. return \%params;
  544. }
  545. use constant { CTRL => 0x01,
  546. SHIFT => 0x02,
  547. ALT => 0x04,
  548. GUI => 0x08,
  549. RIGHT_CTRL => 0x10,
  550. RIGHT_SHIFT => 0x20,
  551. RIGHT_ALT => 0x40,
  552. RIGHT_GUI => 0x80,
  553. };
  554. my %keys = ( '1' => '0702001E',
  555. '2' => '0702001F',
  556. '3' => '07020020',
  557. '4' => '07020021',
  558. '5' => '07020022',
  559. '6' => '07020023',
  560. '7' => '07020024',
  561. '8' => '07020025',
  562. '9' => '07020026',
  563. '0' => '07020027',
  564. '\\n'=> '07000028',
  565. '\\e'=> '07000029',
  566. '\\t'=> '0700002B',
  567. ' ' => '0700002C',
  568. '!' => '0702001E',
  569. '"' => '0702001F',
  570. '§' => '07020020',
  571. '$' => '07020021',
  572. '%' => '07020022',
  573. '&' => '07020023',
  574. '/' => '07020024',
  575. '(' => '07020025',
  576. ')' => '07020026',
  577. '=' => '07020027',
  578. 'ß' => '0700002D',
  579. '´' => '0700002E',
  580. 'ü' => '0700002F',
  581. '+' => '07000030',
  582. '#' => '07000031',
  583. 'ö' => '07000033',
  584. 'ä' => '07000034',
  585. '<' => '07000035',
  586. ',' => '07000036',
  587. '.' => '07000037',
  588. '-' => '07000038',
  589. '?' => '0702002D',
  590. '`' => '0702002E',
  591. 'Ü' => '0702002F',
  592. '*' => '07020030',
  593. "'" => '07020031',
  594. 'Ö' => '07020033',
  595. 'Ä' => '07020034',
  596. '>' => '07020035',
  597. ';' => '07020036',
  598. ':' => '07020037',
  599. '_' => '07020038',
  600. 'F1' => '0700003A',
  601. 'F2' => '0700003B',
  602. 'F3' => '0700003C',
  603. 'F4' => '0700003D',
  604. 'F5' => '0700003E',
  605. 'F6' => '0700003F',
  606. 'F7' => '07000040',
  607. 'F8' => '07000041',
  608. 'F9' => '07000042',
  609. 'F10' => '07000043',
  610. 'F11' => '07000044',
  611. 'F12' => '07000045',
  612. 'KP/' => '07000054',
  613. 'KP*' => '07000055',
  614. 'KP-' => '07000056',
  615. 'KP+' => '07000057',
  616. 'KP\\n' => '07000058',
  617. 'KP1' => '07000059',
  618. 'KP2' => '0700005A',
  619. 'KP3' => '0700005C',
  620. 'KP4' => '0700005C',
  621. 'KP5' => '0700005D',
  622. 'KP6' => '0700005E',
  623. 'KP7' => '0700005F',
  624. 'KP8' => '07000060',
  625. 'KP9' => '07000061',
  626. 'KP0' => '07000062',
  627. );
  628. sub
  629. harmony_char2hid($)
  630. {
  631. my ($char) = @_;
  632. my $ret;
  633. if( $char ge '1' && $char le '9' ) {
  634. $ret = sprintf( "070000%02X", 0x1E + ord($char) - ord('1') );
  635. } elsif( $char ge 'a' && $char le 'z' ) {
  636. $ret = sprintf( "070000%02X", 0x04 + ord($char) - ord('a') );
  637. } elsif( $char ge 'A' && $char le 'Z' ) {
  638. $ret = sprintf( "070200%02X", 0x04 + ord($char) - ord('A') );
  639. } elsif( defined( $keys{$char} ) ) {
  640. $ret = $keys{$char};
  641. }
  642. return $ret;
  643. }
  644. sub
  645. harmony_updateActivity($$;$)
  646. {
  647. my ($hash,$id,$modifier) = @_;
  648. $modifier = "" if( !$modifier );
  649. if( $hash->{currentActivityID} && $hash->{currentActivityID} ne $id ) {
  650. my $id = $hash->{currentActivityID};
  651. $hash->{previousActivityID} = $id;
  652. my $previous = harmony_labelOfActivity($hash,$id,$id);
  653. readingsSingleUpdate( $hash, "previousActivity", $previous, 0 );
  654. }
  655. if( !$modifier && defined($modules{$hash->{TYPE}}{defptr}) ) {
  656. if( my $activity = harmony_activityOfId($hash, $id)) {
  657. foreach my $id (keys %{$activity->{fixit}}) {
  658. if( my $hash = $modules{$hash->{TYPE}}{defptr}{$id} ) {
  659. my $state = $activity->{fixit}->{$id}->{Power};
  660. $state = "Manual" if( !$state );
  661. readingsSingleUpdate( $hash, "power", lc($state), 1 );
  662. }
  663. }
  664. }
  665. }
  666. $hash->{currentActivityID} = $id;
  667. my $activity = harmony_labelOfActivity($hash,$id,$id);
  668. readingsSingleUpdate( $hash, "currentActivity", "$modifier$activity", 1 );
  669. $activity =~ s/ /./g;
  670. if( !$modifier && $activity ne ReadingsVal($hash->{NAME},"activity", "" ) ) {
  671. readingsSingleUpdate( $hash, "activity", $activity, 1 );
  672. harmony_sendEngineGet($hash, "changeChannel", "channel=$hash->{channelAfterStart}:timestamp=0") if( $hash->{channelAfterStart} );
  673. delete $hash->{channelAfterStart};
  674. }
  675. delete $hash->{hidDevice} if( $id == -1 );
  676. }
  677. sub
  678. harmony_Read($)
  679. {
  680. my ($hash) = @_;
  681. my $name = $hash->{NAME};
  682. my $buf;
  683. my $ret = sysread($hash->{CD}, $buf, 1024*1024);
  684. if(!defined($ret) || $ret <= 0) {
  685. harmony_disconnect( $hash );
  686. InternalTimer(gettimeofday()+2, "harmony_connect", $hash, 0);
  687. return;
  688. }
  689. my $data = $hash->{helper}{PARTIAL};
  690. $data .= $buf;
  691. #FIXME: should use real xmpp/xml parser
  692. # see forum https://forum.fhem.de/index.php/topic,14163.msg575033.html#msg575033
  693. $data =~ s/<iq\/>//g;
  694. $data =~ s/<\/iq><iq/<\/iq>\n<iq/g;
  695. my @lines = split( "\n", $data );
  696. foreach my $line (@lines) {
  697. if( $line =~ m/^<(\w*)\s*([^>]*)?\/>(.*)?/ ) {
  698. Log3 $name, 5, "$name: tag: $1, attr: $2";
  699. $data = $3;
  700. if( $line eq "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>" ) {
  701. $hash->{ConnectionState} = "LoggedIn";
  702. if( $hash->{helper}{UserAuthToken} ) {
  703. harmony_getSessionToken($hash);
  704. } else {
  705. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.ping?get' token=''></oa>");
  706. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.harmony/vnd.logitech.harmony.system?systeminfo' token=''></oa>");
  707. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.deviceinfo?get' token=''></oa>");
  708. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.setup/vnd.logitech.account?getProvisionInfo' token=''></oa>");
  709. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.statedigest?get' token=''>format=json</oa>");
  710. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?check' token=''>format=json</oa>");
  711. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?status' token=''>format=json</oa>");
  712. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?update' token=''>format=json</oa>");
  713. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='proxy.resource?get' token=''>hetag= :uri=dynamite:://HomeAutomationService/Config/:encode=true</oa>");
  714. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='harmony.automation?getstate' token=''></oa>");
  715. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.pair'>name=1vm7ATw/tN6HXGpQcCs/A5MkuvI#iOS6.0.1#iPhone</oa>");
  716. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='connect.discoveryinfo?get'>format=json</oa>");
  717. #harmony_sendEngineGet($hash, "config");
  718. }
  719. RemoveInternalTimer($hash);
  720. InternalTimer(gettimeofday()+50, "harmony_ping", $hash, 0);
  721. }
  722. $line = $3;
  723. }
  724. if( $line =~ m/^<(\w*)([^>]*)>(.*)<\/\1>(.*)?/ ) {
  725. Log3 $name, 5, "$name: tag: $1, attr: $2";
  726. #Log3 $name, 5, " data: $3";
  727. $data = $4;
  728. my $tag = $1;
  729. my $attr = $2;
  730. my $content = $3;
  731. #if( $content =~ m/^<(\w*)([^>]*)>(.*)<\/\1>(.*)?/ ) {
  732. # Log3 $name, 1, "$name: tag: $1, attr: $2";
  733. # Log3 $name, 1, Dumper harmony_attr2hash($2);
  734. #}
  735. if( $content =~ m/<!\[CDATA\[(.*)\]\]>/ ) {
  736. my $cdata = $1;
  737. my $json;
  738. my $decoded;
  739. if( $cdata =~ m/^{.*}$/ ) {
  740. $json = harmony_decode_json($cdata);
  741. $decoded = $json;
  742. } else {
  743. $decoded = harmony_CDATA2hash($cdata);
  744. }
  745. my $error = $decoded->{errorCode};
  746. if( $error && $error != 200 ) {
  747. Log3 $name, 2, "$name: error ($error): $decoded->{errorString}";
  748. }
  749. if( ($tag eq "iq" && $content =~ m/statedigest\?get'/)
  750. || ($tag eq "message" && $content =~ m/type="connect.stateDigest\?notify"/) ) {
  751. Log3 $name, 4, "$name: statedigest: $cdata";
  752. if( $decoded ) {
  753. if( defined($decoded->{syncStatus}) ) {
  754. harmony_sendEngineGet($hash, "config") if( $hash->{syncStatus} && !$decoded->{syncStatus} );
  755. $hash->{syncStatus} = $decoded->{syncStatus};
  756. }
  757. if( defined($decoded->{hubUpdate}) && $decoded->{hubUpdate} eq "true" && !$hash->{hubUpdate} ) {
  758. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logtech.setup/vnd.logitech.firmware?check' token=''>format=json</oa>");
  759. }
  760. $hash->{activityStatus} = $decoded->{activityStatus} if( defined($decoded->{activityStatus}) );
  761. $hash->{hubSwVersion} = $decoded->{hubSwVersion} if( defined($decoded->{hubSwVersion}) );
  762. $hash->{hubUpdate} = ($decoded->{hubUpdate} eq 'true'?1:0) if( defined($decoded->{hubUpdate}) );
  763. my $modifier = "";
  764. $modifier = "starting " if( $hash->{activityStatus} == 1 );
  765. $modifier = "stopping " if( $hash->{activityStatus} == 3 );
  766. harmony_updateActivity($hash, $decoded->{activityId}, $modifier) if( defined($decoded->{activityId}) );
  767. if( defined($decoded->{sleepTimerId}) ) {
  768. if( $decoded->{sleepTimerId} == -1 ) {
  769. delete $hash->{sleeptimer};
  770. } else {
  771. harmony_sendEngineGet($hash, "gettimerinterval", "timerId=$decoded->{sleepTimerId}");
  772. }
  773. }
  774. }
  775. } elsif( $tag eq "message" ) {
  776. if( $content =~ m/type="harmony.engine\?startActivityFinished"/ ) {
  777. if( my $id = $decoded->{activityId} ) {
  778. if( harmony_activityOfId($hash, $id) ) {
  779. if( $id == -1 && $hash->{helper}{ignorePowerOff} ) {
  780. delete $hash->{helper}{ignorePowerOff};
  781. } else {
  782. harmony_updateActivity($hash, $id);
  783. }
  784. } else {
  785. $hash->{helper}{ignorePowerOff} = 1;
  786. }
  787. }
  788. } elsif( $content =~ m/type="vnd.logitech.harmony\/vnd.logitech.control.button\?pressType"/ ) {
  789. DoTrigger( $name, "vnd.logitech.control.button: $decoded->{type}" );
  790. } elsif( $content =~ m/type="automation.state\?notify"/ ) {
  791. DoTrigger( $name, "automation.state: $cdata" );
  792. } else {
  793. Log3 $name, 4, "$name: unknown message: $content";
  794. }
  795. } elsif( $tag eq "iq" ) {
  796. if( $content =~ m/errorcode='(\d*)'.*errorstring='(.*)'/ && $1 != 100 && $1 != 200 ) {
  797. Log3 $name, 2, "$name: error ($1): $2";
  798. } elsif( $content =~ m/vnd.logitech.pair/ ) {
  799. if( !$hash->{identity} && $decoded->{identity} ) {
  800. $hash->{identity} = $decoded->{identity};
  801. harmony_connect($hash);
  802. } else {
  803. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='connect.discoveryinfo?get'>format=json</oa>");
  804. #harmony_sendEngineGet($hash, "config");
  805. }
  806. } elsif( $content =~ m/\?startactivity/i ) {
  807. if( $cdata =~ m/done=(\d*):total=(\d*)(:deviceId=(\d*))?/ ) {
  808. my $done = $1;
  809. my $total = $2;
  810. my $id = $4;
  811. $id = "<unknown>" if( !defined($id) );
  812. my $label = harmony_labelOfDevice($hash,$id,$id);
  813. if( $done == $total ) {
  814. Log3 $name, 4, "$name: done starting/stopping device: $label";
  815. } elsif( $done == 1 ) {
  816. Log3 $name, 4, "$name: starting/stopping device: $label";
  817. } else {
  818. Log3 $name, 4, "$name: starting/stopping device ($done/$total): $label";
  819. }
  820. } else {
  821. Log3 $name, 3, "$name: unknown startactivity message: $content";
  822. }
  823. } elsif( $content =~ m/discoveryinfo\?get/ && $decoded ) {
  824. Log3 $name, 4, "$name: ". Dumper $decoded;
  825. $hash->{discoveryinfo} = $decoded;
  826. #$hash->{current_fw_version} = $decoded->{current_fw_version} if( defined($decoded->{current_fw_version}) );
  827. harmony_sendEngineGet($hash, "config");
  828. } elsif( $content =~ m/engine\?changeChannel/ && $decoded ) {
  829. } elsif( $content =~ m/engine\?gettimerinterval/ && $decoded ) {
  830. $hash->{sleeptimer} = FmtDateTime( gettimeofday() + $decoded->{interval} );
  831. } elsif( $content =~ m/firmware\?/ && $decoded ) {
  832. Log3 $name, 4, "$name: firmware: $cdata";
  833. if( $decoded->{status} && $decoded->{newVersion} ) {
  834. $hash->{newVersion} = $decoded->{newVersion};
  835. my $txt = $decoded->{newVersion};
  836. $txt .= ", isCritical: $decoded->{isCritical}";
  837. $txt .= ", bytes: $decoded->{totalBytes}";
  838. readingsSingleUpdate( $hash, "newVersion", $txt, 1 ) if( $txt ne ReadingsVal($hash->{NAME},"newVersion", "" ) );
  839. } else {
  840. delete $hash->{newVersion};
  841. }
  842. } elsif( $content =~ m/\?config/ && $decoded ) {
  843. $hash->{config} = $decoded;
  844. Log3 $name, 3, "$name: new config ";
  845. #Log3 $name, 5, "$name: ". Dumper $json;
  846. #my $station = $hash->{config}->{content}->{contentImageHost};
  847. #$station =~ s/{stationId}/4faa0c3b7232c50c26001b86/;
  848. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='proxy.resource?get' token=''>hetag= :uri=content:://1.0/user;$station:encode=true</oa>");
  849. #foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  850. # my $content = $hash->{config}->{content}->{contentDeviceHost};
  851. # $content =~ s/{deviceProfileUri}/$device->{deviceProfileUri}/;
  852. # harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='proxy.resource?get' token=''>hetag= :uri=content:://1.0/user;$content:encode=true</oa>");
  853. # last;
  854. #}
  855. #harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='proxy.resource?get' token=''>hetag= :uri=content:://1.0/user;$hash->{config}->{content}->{householdUserProfileUri}:encode=true</oa>");
  856. harmony_sendIq($hash, "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.statedigest?get' token=''>format=json</oa>");
  857. #harmony_sendEngineGet($hash, "getCurrentActivity");
  858. } elsif( $cdata =~ m/result=(.*)/ ) {
  859. my $result = $1;
  860. Log3 $name, 4, "$name: got result $1";
  861. if( $content =~ m/getCurrentActivity/ ) {
  862. harmony_updateActivity($hash, $result);
  863. } else {
  864. Log3 $name, 3, "$name: unknown result: $content";
  865. }
  866. } elsif( $content =~ m/mime='hid.report'/ ) {
  867. harmony_sendHID($hash) if( $hash->{hid} );
  868. } else {
  869. Log3 $name, 3, "$name: unknown iq: $content";
  870. Log 3, Dumper $decoded;
  871. Log 3, Dumper harmony_decode_json($decoded->{resource}) if( !$json && $decoded->{resource} && $decoded->{resource} =~ m/^{.*}$/ );
  872. }
  873. } else {
  874. Log3 $name, 3, "$name: unhandled tag: $line";
  875. }
  876. } elsif( $content =~ m/mime='hid.report'/ ) {
  877. harmony_sendHID($hash) if( $hash->{hid} );
  878. } elsif( $line =~ m/<iq id='ping-(\d+)' type='result'><\/iq>/ ) {
  879. Log3 $name, 5, "$name: got ping response $1";
  880. } elsif( $line ) {
  881. Log3 $name, 4, "$name: unknown (no cdata): $line";
  882. }
  883. } elsif( $line =~ m/^<\?xml.*id='([\w-]*).*error.*>/ ) {
  884. Log3 $name, 2, "$name: error: $1" if( $1 );
  885. Log3 $name, 4, "$name: $line";
  886. harmony_disconnect($hash);
  887. } elsif( $line =~ m/^<\?xml.*PLAIN.*>/ ) {
  888. my $identity = $hash->{identity}?$hash->{identity}:"guest";
  889. my $auth = encode_base64("\0$identity\@connect.logitech.com\0gatorade.",'');
  890. harmony_send($hash, "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>$auth</auth>");
  891. $data = "";
  892. } elsif( $line =~ m/^<.*>$/ ) {
  893. Log3 $name, 4, "$name: unknown: $line";
  894. } elsif( $line ) {
  895. #Log3 $name, 5, "$name: $line";
  896. }
  897. }
  898. $hash->{helper}{PARTIAL} = $data;
  899. #Log 3, "length: ". length($hash->{helper}{PARTIAL});
  900. }
  901. sub
  902. harmony_disconnect($)
  903. {
  904. my ($hash) = @_;
  905. my $name = $hash->{NAME};
  906. RemoveInternalTimer($hash);
  907. $hash->{ConnectionState} = "Disconnected";
  908. readingsSingleUpdate( $hash, "state", $hash->{ConnectionState}, 1 ) if( $hash->{ConnectionState} ne ReadingsVal($name, "state", "" ) );
  909. return if( !$hash->{CD} );
  910. Log3 $name, 2, "$name: disconnect";
  911. close($hash->{CD}) if($hash->{CD});
  912. delete($hash->{FD});
  913. delete($hash->{CD});
  914. delete($selectlist{$name});
  915. $hash->{LAST_DISCONNECT} = FmtDateTime( gettimeofday() );
  916. }
  917. sub
  918. harmony_connect($)
  919. {
  920. my ($hash) = @_;
  921. my $name = $hash->{NAME};
  922. return if( IsDisabled($name) );
  923. harmony_disconnect($hash);
  924. Log3 $name, 4, "$name: connect";
  925. harmony_getLoginToken($hash);
  926. if( defined($hash->{helper}{UserAuthToken}) ) {
  927. my $timeout = $hash->{TIMEOUT} ? $hash->{TIMEOUT} : 3;
  928. my $conn = IO::Socket::INET->new(PeerAddr => "$hash->{ip}:5222", Timeout => $timeout);
  929. if( $conn ) {
  930. Log3 $name, 3, "$name: connected";
  931. $hash->{ConnectionState} = "Connected";
  932. readingsSingleUpdate( $hash, "state", $hash->{ConnectionState}, 1 ) if( $hash->{ConnectionState} ne ReadingsVal($name, "state", "" ) );
  933. $hash->{LAST_CONNECT} = FmtDateTime( gettimeofday() );
  934. $hash->{FD} = $conn->fileno();
  935. $hash->{CD} = $conn; # sysread / close won't work on fileno
  936. $hash->{CONNECTS}++;
  937. $selectlist{$name} = $hash;
  938. $hash->{helper}{PARTIAL} = "";
  939. harmony_send($hash, "<stream:stream to='connect.logitech.com' xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' xml:lang='en' version='1.0'>");
  940. } else {
  941. harmony_disconnect( $hash );
  942. InternalTimer(gettimeofday()+10, "harmony_connect", $hash, 0);
  943. }
  944. }
  945. }
  946. sub
  947. harmony_getSessionToken($)
  948. {
  949. my ($hash) = @_;
  950. my $name = $hash->{NAME};
  951. my $unique_id = '1vm7ATw/tN6HXGpQcCs/A5MkuvI';
  952. my $device = 'iOS6.0.1#iPhone';
  953. harmony_sendPair($hash, "token=$hash->{helper}{UserAuthToken}:name=$unique_id#$device");
  954. }
  955. sub
  956. harmony_send($$)
  957. {
  958. my ($hash, $data) = @_;
  959. my $name = $hash->{NAME};
  960. return undef if( !$hash->{CD} );
  961. Log3 $name, 4, "$name: send: $data";
  962. syswrite $hash->{CD}, $data;
  963. }
  964. my $id = 0;
  965. sub
  966. harmony_sendIq($$;$)
  967. {
  968. my ($hash, $xml, $type) = @_;
  969. $type = 'get' if ( !$type );
  970. ++$id;
  971. my $iq = "<iq type='$type' id='$id' from='guest'>$xml</iq>";
  972. $iq = "<iq type='$type' id='$id'>$xml</iq>";
  973. harmony_send($hash,$iq);
  974. }
  975. sub
  976. harmony_sendPair($$)
  977. {
  978. my ($hash, $payload) = @_;
  979. $payload = '' if ( !$payload );
  980. my $xml = "<oa xmlns='connect.logitech.com' mime='vnd.logitech.connect/vnd.logitech.pair'>$payload</oa>";
  981. harmony_sendIq($hash,$xml);
  982. }
  983. sub
  984. harmony_sendEngineGet($$;$)
  985. {
  986. my ($hash, $endpoint, $payload) = @_;
  987. $payload = '' if ( !$payload );
  988. my $xml = "<oa xmlns='connect.logitech.com' mime='vnd.logitech.harmony/vnd.logitech.harmony.engine?$endpoint'>$payload</oa>";
  989. harmony_sendIq($hash,$xml);
  990. }
  991. sub
  992. harmony_sendHID($;$)
  993. {
  994. my ($hash, $code) = @_;
  995. if( !$code ) {
  996. return if( !$hash->{hid} );
  997. my $char = substr($hash->{hid}, 0, 1);
  998. $hash->{hid} = substr($hash->{hid}, 1);
  999. if( $char eq '\\' || ord($char) == 0xC3 ) {
  1000. $char .= substr($hash->{hid}, 0, 1);
  1001. $hash->{hid} = substr($hash->{hid}, 1);
  1002. }
  1003. $code = harmony_char2hid( $char );
  1004. }
  1005. my $xml = "<oa xmlns='connect.logitech.com' mime='hid.report' token=''>{'code':'$code'}</oa>";
  1006. harmony_sendIq($hash,$xml);
  1007. }
  1008. sub
  1009. harmony_sendEngineRender($$$)
  1010. {
  1011. my ($hash, $endpoint, $payload) = @_;
  1012. #my $xml = "<oa xmlns='connect.logitech.com' mime='vnd.logitech.harmony/vnd.logitech.harmony.engine?$endpoint' token=''>$payload</oa>";
  1013. my $xml = "<oa xmlns='connect.logitech.com' mime='vnd.logitech.harmony/vnd.logitech.harmony.engine?$endpoint'>$payload</oa>";
  1014. harmony_sendIq($hash,$xml, "render");
  1015. }
  1016. sub
  1017. harmony_ping($)
  1018. {
  1019. my( $hash ) = @_;
  1020. return if( $hash->{ConnectionState} eq "Disconnected" );
  1021. ++$id;
  1022. harmony_send($hash, "<iq type='get' id='ping-$id'><ping xmlns='urn:xmpp:ping'/></iq>");
  1023. RemoveInternalTimer($hash);
  1024. InternalTimer(gettimeofday()+50, "harmony_ping", $hash, 0);
  1025. }
  1026. sub
  1027. harmony_dispatch($$$)
  1028. {
  1029. my ($param, $err, $data) = @_;
  1030. my $hash = $param->{hash};
  1031. my $name = $hash->{NAME};
  1032. if( $err ) {
  1033. Log3 $name, 2, "$name: http request failed: $err";
  1034. } elsif( $data ) {
  1035. Log3 $name, 4, "$name: $data";
  1036. if( $data !~ m/^{.*}$/ ) {
  1037. Log3 $name, 2, "$name: invalid json detected: $data";
  1038. return undef;
  1039. }
  1040. my $json = harmony_decode_json($data);
  1041. if( $param->{type} eq 'token' ) {
  1042. harmony_parseToken($hash, $json);
  1043. } elsif( $param->{type} eq 'GetJson2Uris' ) {
  1044. Log 1, Dumper $json;
  1045. }
  1046. }
  1047. }
  1048. sub
  1049. harmony_autocreate($;$)
  1050. {
  1051. my($hash, $param) = @_;
  1052. my $name = $hash->{NAME};
  1053. return if( !defined($hash->{config}) );
  1054. my $id = $param;
  1055. $id = harmony_idOfDevice($hash, $id) if( $id && $id !~ m/^([\d-])+$/ );
  1056. return "unknown device $param" if( $param && !$id );
  1057. #foreach my $d (keys %defs) {
  1058. # next if($defs{$d}{TYPE} ne "autocreate");
  1059. # return undef if( IsDisabled($defs{$d}{NAME} ) );
  1060. #}
  1061. my $autocreated = 0;
  1062. foreach my $device (@{$hash->{config}->{device}}) {
  1063. next if( $id && $device->{id} != $id );
  1064. if( defined($modules{$hash->{TYPE}}{defptr}{$device->{id}}) ) {
  1065. Log3 $name, 4, "$name: device '$device->{id}' already defined";
  1066. next;
  1067. }
  1068. my $devname = "harmony_". $device->{id};
  1069. my $define = "$devname harmony DEVICE $device->{id}";
  1070. Log3 $name, 3, "$name: create new device '$devname' for device '$device->{id}'";
  1071. my $cmdret = CommandDefine(undef,$define);
  1072. if($cmdret) {
  1073. Log3 $name, 1, "$name: Autocreate: An error occurred while creating device for id '$device->{id}': $cmdret";
  1074. } else {
  1075. $cmdret = CommandAttr(undef,"$devname alias $device->{label}") if( defined($device->{label}) );
  1076. $cmdret = CommandAttr(undef,"$devname event-on-change-reading .*");
  1077. $cmdret = CommandAttr(undef,"$devname room $hash->{TYPE}");
  1078. $cmdret = CommandAttr(undef,"$devname stateFormat power");
  1079. #$cmdret = CommandAttr(undef,"$devname IODev $name");
  1080. $autocreated++;
  1081. }
  1082. }
  1083. CommandSave(undef,undef) if( $autocreated && AttrVal( "autocreate", "autosave", 1 ) );
  1084. return "created $autocreated devices";
  1085. }
  1086. sub
  1087. harmony_parseToken($$)
  1088. {
  1089. my($hash, $json) = @_;
  1090. my $name = $hash->{NAME};
  1091. RemoveInternalTimer($hash);
  1092. my $error = $json->{ErrorCode};
  1093. if( $error && $error != 200 ) {
  1094. Log3 $name, 2, "$name: error ($error): $json->{Message}";
  1095. $hash->{lastError} = $json->{Message};
  1096. }
  1097. my $had_token = $hash->{helper}{UserAuthToken};
  1098. $hash->{helper}{AccountId} = $json->{GetUserAuthTokenResult}->{AccountId};
  1099. $hash->{helper}{UserAuthToken} = $json->{GetUserAuthTokenResult}->{UserAuthToken};
  1100. if( $hash->{helper}{UserAuthToken} ) {
  1101. $hash->{ConnectionState} = "GotToken";
  1102. } else {
  1103. $hash->{STATE} = "Error";
  1104. $hash->{ConnectionState} = "Error";
  1105. RemoveInternalTimer($hash);
  1106. InternalTimer(gettimeofday()+60, "harmony_connect", $hash, 0);
  1107. }
  1108. }
  1109. sub
  1110. harmony_data2string($)
  1111. {
  1112. my ($data) = @_;
  1113. return "" if( !defined($data) );
  1114. return $data if( !ref($data) );
  1115. return $data if( ref($data) =~ m/JSON::..::Boolean/ );
  1116. return "[". join(',', @{$data}) ."]" if(ref($data) eq "ARRAY");
  1117. return Dumper $data;
  1118. }
  1119. sub
  1120. harmony_GetPower($$)
  1121. {
  1122. my ($hash, $activity) = @_;
  1123. my $power = "";
  1124. return $power if( !defined($activity->{fixit}) );
  1125. foreach my $id (keys %{$activity->{fixit}}) {
  1126. my $label = harmony_labelOfDevice($hash, $id);
  1127. my $state = $activity->{fixit}->{$id}->{Power};
  1128. $state = "Manual" if( !$state );
  1129. $power .= "\n\t\t\t$label: $state";
  1130. }
  1131. return $power;
  1132. }
  1133. sub
  1134. harmony_Get($$@)
  1135. {
  1136. my ($hash, $name, $cmd, @params) = @_;
  1137. my ($param) = @params;
  1138. #$cmd = lc( $cmd );
  1139. my $list = "";
  1140. if( defined($hash->{id}) ) {
  1141. if( !$hash->{hub} ) {
  1142. $hash->{hub} = harmony_hubOfDevice($hash->{id});
  1143. return "no IODev found for device $name ($hash->{id})" if( !$hash->{hub} );
  1144. }
  1145. if( $cmd eq "commands" || $cmd eq "deviceCommands" ) {
  1146. $cmd = "deviceCommands";
  1147. $param = $hash->{id};
  1148. $hash = $defs{$hash->{hub}};
  1149. } else {
  1150. $list = "commands:noArg";
  1151. return "Unknown argument $cmd, choose one of $list";
  1152. }
  1153. }
  1154. my $ret;
  1155. if( $cmd eq "activities" ) {
  1156. return "no activities found" if( !defined($hash->{config}) || !defined($hash->{config}->{activity}) );
  1157. my $ret = "";
  1158. foreach my $activity (sort { ($a->{activityOrder}||0) <=> ($b->{activityOrder}||0) } @{$hash->{config}->{activity}}) {
  1159. next if( $activity->{id} == -1 );
  1160. $ret .= "\n" if( $ret );
  1161. $ret .= sprintf( "%s\t%-24s", $activity->{id}, $activity->{label});
  1162. foreach my $param (@params) {
  1163. $ret .= "\t". harmony_data2string($activity->{$param}) if( $param && defined($activity->{$param}) );
  1164. }
  1165. if( $param && $param eq "power" ) {
  1166. my $power = harmony_GetPower($hash, $activity);
  1167. $ret .= $power if( $power );
  1168. }
  1169. }
  1170. if( my $activity = harmony_activityOfId($hash, -1) ) {
  1171. $ret .= "\n-1\t\t$activity->{label}";
  1172. if( $param && $param eq "power" ) {
  1173. my $power = harmony_GetPower($hash, $activity);
  1174. $ret .= $power if( $power );
  1175. }
  1176. }
  1177. #$ret = sprintf("%s\t\t%-24s\n", "ID", "LABEL"). $ret if( $ret );
  1178. return $ret;
  1179. } elsif( $cmd eq "devices" ) {
  1180. return "no devices found" if( !defined($hash->{config}) || !defined($hash->{config}->{device}) );
  1181. my $ret = "";
  1182. foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  1183. $ret .= "\n" if( $ret );
  1184. $ret .= sprintf( "%s\t%-20s\t%-20s\t%-15s\t%-15s", $device->{id}, $device->{label}, $device->{type}, $device->{manufacturer}, $device->{model});
  1185. foreach my $param (@params) {
  1186. $ret .= "\t". harmony_data2string($device->{$param}) if( $param && defined($device->{$param}) );
  1187. }
  1188. }
  1189. #$ret = sprintf("%s\t\t%-20s\t%-20s\t%-15s\t%-15s\n", "ID", "LABEL", "TYPE", "MANUFACTURER", "MODEL"). $ret if( $ret );
  1190. return $ret;
  1191. } elsif( $cmd eq "commands" ) {
  1192. return "no commands found" if( !defined($hash->{config}) || !defined($hash->{config}->{activity}) );
  1193. my $id = $param;
  1194. $id = harmony_idOfActivity($hash, $id) if( $id && $id !~ m/^([\d-])+$/ );
  1195. return "unknown activity $param" if( $param && !$id );
  1196. my $ret = "";
  1197. foreach my $activity (sort { ($a->{activityOrder}||0) <=> ($b->{activityOrder}||0) } @{$hash->{config}->{activity}}) {
  1198. next if( $activity->{id} == -1 );
  1199. next if( $id && $activity->{id} != $id );
  1200. $ret .= "$activity->{label}\n";
  1201. #$ret .= "$device->{label}\t$device->{manufacturer}\t$device->{model}\n";
  1202. foreach my $group (@{$activity->{controlGroup}}) {
  1203. $ret .= "\t$group->{name}\n";
  1204. foreach my $function (@{$group->{function}}) {
  1205. my $action = harmony_decode_json($function->{action});
  1206. $ret .= sprintf( "\t\t%-20s\t%s (%s)\n", $function->{name}, $function->{label}, harmony_labelOfDevice($hash, $action->{deviceId}, $action->{deviceId}) );
  1207. }
  1208. }
  1209. }
  1210. return $ret;
  1211. } elsif( $cmd eq "deviceCommands" ) {
  1212. return "no commands found" if( !defined($hash->{config}) || !defined($hash->{config}->{device}) );
  1213. my $id = $param;
  1214. $id = harmony_idOfDevice($hash, $id) if( $id && $id !~ m/^([\d-])+$/ );
  1215. return "unknown device $param" if( $param && !$id );
  1216. my $ret = "";
  1217. foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  1218. next if( $id && $device->{id} != $id );
  1219. $ret .= "$device->{label}\t$device->{manufacturer}\t$device->{model}\n";
  1220. foreach my $group (@{$device->{controlGroup}}) {
  1221. $ret .= "\t$group->{name}\n";
  1222. foreach my $function (@{$group->{function}}) {
  1223. $ret .= sprintf( "\t\t%-20s\t%s\n", $function->{name}, $function->{label} );
  1224. }
  1225. }
  1226. }
  1227. return "no commands found" if( !$ret );
  1228. return $ret;
  1229. } elsif( $cmd eq "activityDetail"
  1230. || $cmd eq "deviceDetail" ) {
  1231. return undef if( !defined($hash->{config}) );
  1232. $param = harmony_idOfActivity($hash, $param) if( $param && $param !~ m/^([\d-])+$/ && $cmd eq "activityDetail" );
  1233. $param = harmony_idOfDevice($hash, $param) if( $param && $param !~ m/^([\d-])+$/ && $cmd eq "deviceDetail" );
  1234. my $var;
  1235. $var = $hash->{config}->{activity} if( $cmd eq "activityDetail" );
  1236. $var = $hash->{config}->{device} if( $cmd eq "deviceDetail" );
  1237. if( $param ) {
  1238. foreach my $v (@{$var}) {
  1239. if( $v->{id} eq $param ) {
  1240. $var = $v;
  1241. last;
  1242. }
  1243. }
  1244. }
  1245. return Dumper $var;
  1246. } elsif( $cmd eq "configDetail" ) {
  1247. return undef if( !defined($hash->{config}) );
  1248. return Dumper $hash->{config};
  1249. } elsif( $cmd eq "currentActivity" ) {
  1250. return "unknown activity" if( !$hash->{currentActivityID} );
  1251. my $activity = harmony_activityOfId($hash, $hash->{currentActivityID});
  1252. return "unknown activity" if( !$activity );
  1253. return $activity->{label};
  1254. }
  1255. $list .= "activities:noArg devices:noArg";
  1256. if( $hash->{config} ) {
  1257. return undef if( !defined($hash->{config}) );
  1258. my $activities;
  1259. foreach my $activity (sort { ($a->{activityOrder}||0) <=> ($b->{activityOrder}||0) } @{$hash->{config}->{activity}}) {
  1260. next if( $activity->{id} == -1 );
  1261. $activities .= "," if( $activities );
  1262. $activities .= $activity->{label};
  1263. }
  1264. if( $activities ) {
  1265. $activities =~ s/ /./g;
  1266. $list .= " commands:,$activities";
  1267. }
  1268. my $devices;
  1269. foreach my $device (sort { $a->{id} <=> $b->{id} } @{$hash->{config}->{device}}) {
  1270. $devices .= "," if( $devices );
  1271. $devices .= $device->{label};
  1272. }
  1273. if( $devices ) {
  1274. $devices =~ s/ /./g;
  1275. $list .= " deviceCommands:,$devices";
  1276. }
  1277. }
  1278. if( $cmd eq 'showAccount' ) {
  1279. my $user = $hash->{helper}{username};
  1280. my $password = $hash->{helper}{password};
  1281. return 'no user set' if( !$user );
  1282. return 'no password set' if( !$password );
  1283. $user = harmony_decrypt( $user );
  1284. $password = harmony_decrypt( $password );
  1285. return "user: $user\npassword: $password";
  1286. }
  1287. $list .= " showAccount";
  1288. $list .= " currentActivity:noArg";
  1289. $list =~ s/^ //;
  1290. return "Unknown argument $cmd, choose one of $list";
  1291. }
  1292. sub
  1293. harmony_Attr($$$)
  1294. {
  1295. my ($cmd, $name, $attrName, $attrVal) = @_;
  1296. my $orig = $attrVal;
  1297. if( $attrName eq "disable" ) {
  1298. my $hash = $defs{$name};
  1299. RemoveInternalTimer($hash);
  1300. if( $cmd eq "set" && $attrVal ne "0" ) {
  1301. $attrVal = 1;
  1302. harmony_disconnect($hash);
  1303. } else {
  1304. $attr{$name}{$attrName} = 0;
  1305. harmony_connect($hash);
  1306. }
  1307. }
  1308. if( $cmd eq "set" ) {
  1309. if( !defined($orig) || $orig ne $attrVal ) {
  1310. $attr{$name}{$attrName} = $attrVal;
  1311. return $attrName ." set to ". $attrVal;
  1312. }
  1313. }
  1314. return;
  1315. }
  1316. sub
  1317. harmony_encrypt($)
  1318. {
  1319. my ($decoded) = @_;
  1320. my $key = getUniqueId();
  1321. my $encoded;
  1322. return $decoded if( $decoded =~ /^crypt:(.*)/ );
  1323. for my $char (split //, $decoded) {
  1324. my $encode = chop($key);
  1325. $encoded .= sprintf("%.2x",ord($char)^ord($encode));
  1326. $key = $encode.$key;
  1327. }
  1328. return 'crypt:'. $encoded;
  1329. }
  1330. sub
  1331. harmony_decrypt($)
  1332. {
  1333. my ($encoded) = @_;
  1334. my $key = getUniqueId();
  1335. my $decoded;
  1336. $encoded = $1 if( $encoded =~ /^crypt:(.*)/ );
  1337. for my $char (map { pack('C', hex($_)) } ($encoded =~ /(..)/g)) {
  1338. my $decode = chop($key);
  1339. $decoded .= chr(ord($char)^ord($decode));
  1340. $key = $decode.$key;
  1341. }
  1342. return $decoded;
  1343. }
  1344. 1;
  1345. =pod
  1346. =item summary module for logitech harmony hub based remots
  1347. =item summary_DE Modul für Logitech Harmony Hub basierte Fernbedienungen
  1348. =begin html
  1349. <a name="harmony"></a>
  1350. <h3>harmony</h3>
  1351. <ul>
  1352. Defines a device to integrate a Logitech Harmony Hub based remote control into fhem.<br><br>
  1353. It is possible to: start and stop activities, send ir commands to devices, send keyboard input by bluetooth and
  1354. smart keyboard usb dongles.<br><br>
  1355. You probably want to use it in conjunction with the <a href="#fakeRoku">fakeRoku</a> module.<br><br>
  1356. Notes:
  1357. <ul>
  1358. <li>JSON has to be installed on the FHEM host.</li>
  1359. <li>For hubs with firmware version 3.x.y &lt;username&gt; and &lt;password&gt; are not required as no authentication
  1360. with the logitech myharmony server is needed for the full functionality of this module.</li>
  1361. <li>For hubs with firmware version 4.x.y &lt;username&gt; and &lt;password&gt; are required for device level control.
  1362. Activit level control is (currently) still possible without authentication.</li>
  1363. <li>activity and device names can be given as id or name. names can be given as a regex and spaces in names musst be replaced by a single '.' (dot).</li>
  1364. </ul><br>
  1365. <a name="harmony_Define"></a>
  1366. <b>Define</b>
  1367. <ul>
  1368. <code>define &lt;name&gt; harmony [&lt;username&gt; &lt;password&gt;] &lt;ip&gt;</code><br>
  1369. <br>
  1370. Defines a harmony device.<br><br>
  1371. Examples:
  1372. <ul>
  1373. <code>define hub harmony 10.0.1.4</code><br>
  1374. </ul>
  1375. </ul><br>
  1376. <a name="harmony_Readings"></a>
  1377. <b>Readings</b>
  1378. <ul>
  1379. <li>currentActivity<br>
  1380. the name of the currently selected activity.</li>
  1381. <li>previousActivity<br>
  1382. the name of the previous selected activity. does not trigger an event.</li>
  1383. <li>newVersion<br>
  1384. will be set if a new firmware version is avaliable.</li>
  1385. </ul><br>
  1386. <a name="harmony_Internals"></a>
  1387. <b>Internals</b>
  1388. <ul>
  1389. <li>currentActivityID<br>
  1390. the id of the currently selected activity.</li>
  1391. <li>previousActivityID<br>
  1392. the id of the previous selected activity.</li>
  1393. <li>sleeptimer<br>
  1394. timeout for sleeptimer if any is set.</li>
  1395. </ul><br>
  1396. <a name="harmony_Set"></a>
  1397. <b>Set</b>
  1398. <ul>
  1399. <li>activity &lt;id&gt;|&ltname&gt; [&lt;channel&gt;]<br>
  1400. switch to this activit and optionally switch to &lt;channel&gt;</li>
  1401. <li>channel &lt;channel&gt;<br>
  1402. switch to &lt;channel&gt; in the current activity</li>
  1403. <li>command [&lt;id&gt;|&ltname&gt;] &lt;command&gt; [duration=&lt;duration&gt;]<br>
  1404. send the given ir command for the current activity or for the given device</li>
  1405. <li>getConfig<br>
  1406. request the configuration from the hub</li>
  1407. <li>getCurrentActivity<br>
  1408. request the current activity from the hub</li>
  1409. <li>off<br>
  1410. switch current activity off</li>
  1411. <li>reconnect [all]<br>
  1412. close connection to the hub and reconnect, if <code>all</code> is given also reconnect to the logitech server</li>
  1413. <li>sleeptimer [&lt;timeout&gt;]<br>
  1414. &lt;timeout&gt; -> timeout in minutes<br>
  1415. -1 -> timer off<br>
  1416. default -> 60 minutes</li>
  1417. <li>sync<br>
  1418. syncs the hub to the myHarmony config</li>
  1419. <li>hidDevice [&lt;id&gt;|&ltname&gt;]<br>
  1420. sets the target device for keyboard commands, if no device is given -> set the target to the
  1421. default device for the current activity.</li>
  1422. <li>text &lt;text&gt;<br>
  1423. sends &lt;text&gt; by bluetooth/smart keaboard dongle. a-z ,A-Z ,0-9, \n, \e, \t and space are currently possible</li>
  1424. <li>cursor &lt;direction&gt;<br>
  1425. moves the cursor by bluetooth/smart keaboard dongle. &lt;direction&gt; can be one of: up, down, left, right, pageUp, pageDown, home, end.</li>
  1426. <li>special &lt;key&gt;<br>
  1427. sends special key by bluetooth/smart keaboard dongle. &lt;key&gt; can be one of: previousTrack, nextTrack, stop, playPause, volumeUp, volumeDown, mute.</li>
  1428. <li>autocreate [&lt;id&gt;|&ltname&gt;]<br>
  1429. creates a fhem device for a single/all device(s) in the harmony hub. if activities are startet the state
  1430. of these devices will be updatet with the power state defined in these activites.</li>
  1431. <li>update<br>
  1432. triggers a firmware update. only available if a new firmware is available.</li>
  1433. <li>inactive<br>
  1434. inactivates the current device. note the slight difference to the
  1435. disable attribute: using set inactive the state is automatically saved
  1436. to the statefile on shutdown, there is no explicit save necesary.<br>
  1437. this command is intended to be used by scripts to temporarily
  1438. deactivate the harmony device.<br>
  1439. the concurrent setting of the disable attribute is not recommended.</li>
  1440. <li>active<br>
  1441. activates the current device (see inactive).</li>
  1442. </ul>
  1443. The command, hidDevice, text, cursor and special commmands are also available for the autocreated devices. The &lt;id&gt;|&ltname&gt; paramter hast to be omitted.<br><br>
  1444. <a name="harmony_Get"></a>
  1445. <b>Get</b>
  1446. <ul>
  1447. <li>activites [&lt;param&gt;]<br>
  1448. lists all activities<br>
  1449. parm = power -> list power state for each device in activity</li>
  1450. <li>devices [&lt;param&gt;]<br>
  1451. lists all devices</li>
  1452. <li>commands [&lt;id&gt;|&ltname&gt;]<br>
  1453. lists the commands for the specified activity or for all activities</li>
  1454. <li>deviceCommands [&lt;id&gt;|&ltname&gt;]<br>
  1455. lists the commands for the specified device or for all devices</li>
  1456. <li>activityDetail [&lt;id&gt;|&ltname&gt;]</li>
  1457. <li>deviceDetail [&lt;id&gt;|&ltname&gt;]</li>
  1458. <li>configDetail</li>
  1459. <li>currentActivity<br>
  1460. returns the current activity name</li>
  1461. <li>showAccount<br>
  1462. display obfuscated user and password in cleartext</li>
  1463. </ul>
  1464. The commands commmand is also available for the autocreated devices. The &lt;id&gt;|&ltname&gt; paramter hast to be omitted.<br><br>
  1465. <a name="harmony_Attr"></a>
  1466. <b>Attributes</b>
  1467. <ul>
  1468. <li>disable<br>
  1469. 1 -> disconnect from the hub</li>
  1470. </ul>
  1471. </ul>
  1472. =end html
  1473. =cut