26_tahoma.pm 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. # $Id: 26_tahoma.pm 13224 2017-01-24 23:05:57Z mike3436 $
  2. ################################################################
  3. #
  4. # Copyright notice
  5. #
  6. # (c) 2015 mike3436 (mike3436@online.de)
  7. #
  8. # This script is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # The GNU General Public License can be found at
  14. # http://www.gnu.org/copyleft/gpl.html.
  15. # A copy is found in the textfile GPL.txt and important notices to the license
  16. # from the author is found in LICENSE.txt distributed with these scripts.
  17. #
  18. # This script is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. # This copyright notice MUST APPEAR in all copies of the script!
  24. #
  25. ################################################################
  26. # $Id: 26_tahoma.pm
  27. #
  28. # 2014-08-01 V 0100 first Version using XML Interface
  29. # 2015-08-16 V 0200 communication to server changes from xml to json
  30. # 2015-08-16 V 0201 some standard requests after login which are not neccessary disabled (so the actual requests are not equal to flow of iphone app)
  31. # 2016-02-14 V 0202 bugs forcing some startup warning messages fixed
  32. # 2016-02-20 V 0203 perl exception while parsing json string captured
  33. # 2016-02-24 V 0204 commands open,close,my,stop and setClosure added
  34. # 2016-04-24 V 0205 commands taken from setup
  35. # 2016-06-16 V 0206 updateDevices called for devices created before setup has been read
  36. # 2016-11-15 V 0207 BLOCKING=0 can be used, all calls asynchron, attribut levelInvert inverts RollerShutter position
  37. # 2016-11-29 V 0208 HttpUtils used instead of LWP::UserAgent, BLOCKING=0 set as default, umlaut can be used in Tahoma names
  38. # 2016-12-15 V 0209 perl warnings during startup and login eliminated
  39. # 2017-01-08 V 0210 tahoma_cancelExecutions: cancel command added
  40. # 2017-01-10 V 0211 tahoma_getStates: read all states based on table {setup}{devices}[n]{definition}{states}
  41. # 2017-01-24 V 0212 tahoma_getStates: read all states recovered
  42. # 2017-01-24 V 0212 start scene with launchActionGroup so cancel is working on scenes now
  43. # 2017-01-24 V 0212 Attribut interval used to disable or enable refreshAllstates
  44. # 2017-01-24 V 0212 Setup changes recognized for reading places
  45. package main;
  46. use strict;
  47. use warnings;
  48. use utf8;
  49. use Encode qw(decode_utf8);
  50. use JSON;
  51. #use Data::Dumper;
  52. use Time::HiRes qw(time);
  53. use HttpUtils;
  54. sub tahoma_parseGetSetupPlaces($$);
  55. sub tahoma_UserAgent_NonblockingGet($);
  56. sub tahoma_encode_utf8($);
  57. my $hash_;
  58. sub tahoma_Initialize($)
  59. {
  60. my ($hash) = @_;
  61. $hash->{DefFn} = "tahoma_Define";
  62. $hash->{NOTIFYDEV} = "global";
  63. $hash->{NotifyFn} = "tahoma_Notify";
  64. $hash->{UndefFn} = "tahoma_Undefine";
  65. $hash->{SetFn} = "tahoma_Set";
  66. $hash->{GetFn} = "tahoma_Get";
  67. $hash->{AttrFn} = "tahoma_Attr";
  68. $hash->{AttrList} = "IODev ".
  69. "blocking ".
  70. "debug:1 ".
  71. "disable:1 ".
  72. "interval ".
  73. "logfile ".
  74. "url ".
  75. "placeClasses ".
  76. "levelInvert ".
  77. "userAgent ";
  78. $hash->{AttrList} .= $readingFnAttributes;
  79. }
  80. #####################################
  81. sub tahoma_Define($$)
  82. {
  83. my ($hash, $def) = @_;
  84. my @a = split("[ \t][ \t]*", $def);
  85. my $ModuleVersion = "0212";
  86. my $subtype;
  87. my $name = $a[0];
  88. if( $a[2] eq "DEVICE" && @a == 4 ) {
  89. $subtype = "DEVICE";
  90. my $device = $a[3];
  91. my $fid = (split "/", $device)[-1];
  92. $hash->{device} = $device;
  93. $hash->{fid} = $fid;
  94. $hash->{INTERVAL} = 0;
  95. my $d = $modules{$hash->{TYPE}}{defptr}{"$fid"};
  96. return "device $device already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
  97. $modules{$hash->{TYPE}}{defptr}{"$fid"} = $hash;
  98. } elsif( $a[2] eq "PLACE" && @a == 4 ) {
  99. $subtype = "PLACE";
  100. my $oid = $a[@a-1];
  101. my $fid = (split "-", $oid)[0];
  102. $hash->{oid} = $oid;
  103. $hash->{fid} = $fid;
  104. $hash->{INTERVAL} = 0;
  105. my $d = $modules{$hash->{TYPE}}{defptr}{"$fid"};
  106. return "place oid $oid already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
  107. $modules{$hash->{TYPE}}{defptr}{"$fid"} = $hash;
  108. } elsif( $a[2] eq "SCENE" && @a == 4 ) {
  109. $subtype = "SCENE";
  110. my $oid = $a[@a-1];
  111. my $fid = (split "-", $oid)[0];
  112. $hash->{oid} = $oid;
  113. $hash->{fid} = $fid;
  114. $hash->{INTERVAL} = 0;
  115. my $d = $modules{$hash->{TYPE}}{defptr}{"$fid"};
  116. return "scene oid $oid already defined as $d->{NAME}" if( defined($d) && $d->{NAME} ne $name );
  117. $modules{$hash->{TYPE}}{defptr}{"$fid"} = $hash;
  118. } elsif( $a[2] eq "ACCOUNT" && @a == 5 ) {
  119. $subtype = "ACCOUNT";
  120. my $username = $a[@a-2];
  121. my $password = $a[@a-1];
  122. $hash->{Clients} = ":tahoma:";
  123. $hash->{username} = $username;
  124. $hash->{password} = $password;
  125. $hash->{BLOCKING} = 0;
  126. $hash->{INTERVAL} = 0;
  127. $hash->{VERSION} = $ModuleVersion;
  128. } else {
  129. return "Usage: define <name> tahoma device\
  130. define <name> tahoma ACCOUNT username password\
  131. define <name> tahoma DEVICE id\
  132. define <name> tahoma SCENE oid username password\
  133. define <name> tahoma PLACE oid" if(@a < 4 || @a > 5);
  134. }
  135. $hash->{NAME} = $name;
  136. $hash->{SUBTYPE} = $subtype;
  137. $hash->{STATE} = "Initialized";
  138. if( $init_done ) {
  139. tahoma_connect($hash) if( $hash->{SUBTYPE} eq "ACCOUNT" );
  140. tahoma_initDevice($hash) if( $hash->{SUBTYPE} eq "DEVICE" );
  141. tahoma_initDevice($hash) if( $hash->{SUBTYPE} eq "PLACE" );
  142. tahoma_initDevice($hash) if( $hash->{SUBTYPE} eq "SCENE" );
  143. }
  144. return undef;
  145. }
  146. sub tahoma_Notify($$)
  147. {
  148. my ($hash,$dev) = @_;
  149. return if($dev->{NAME} ne "global");
  150. return if(!grep(m/^INITIALIZED|REREADCFG$/, @{$dev->{CHANGED}}));
  151. tahoma_connect($hash) if( $hash->{SUBTYPE} eq "ACCOUNT" );
  152. tahoma_initDevice($hash) if( $hash->{SUBTYPE} eq "DEVICE" );
  153. tahoma_initDevice($hash) if( $hash->{SUBTYPE} eq "PLACE" );
  154. tahoma_initDevice($hash) if( $hash->{SUBTYPE} eq "SCENE" );
  155. }
  156. sub tahoma_Undefine($$)
  157. {
  158. my ($hash, $arg) = @_;
  159. delete( $modules{$hash->{TYPE}}{defptr}{"$hash->{device}"} ) if( $hash->{SUBTYPE} eq "DEVICE" );
  160. delete( $modules{$hash->{TYPE}}{defptr}{"$hash->{oid}"} ) if( $hash->{SUBTYPE} eq "PLACE" );
  161. delete( $modules{$hash->{TYPE}}{defptr}{"$hash->{oid}"} ) if( $hash->{SUBTYPE} eq "SCENE" );
  162. return undef;
  163. }
  164. sub tahoma_login($)
  165. {
  166. my ($hash) = @_;
  167. my $name = $hash->{NAME};
  168. Log3 $name, 3, "$name: tahoma_login";
  169. $hash->{logged_in} = undef;
  170. $hash->{startup_run} = undef;
  171. $hash->{startup_done} = undef;
  172. $hash->{url} = "https://www.tahomalink.com/enduser-mobile-web/externalAPI/json/";
  173. $hash->{url} = $attr{$name}{url} if (defined $attr{$name}{url});
  174. $hash->{userAgent} = "TaHoma/7980 CFNetwork/758.5.3 Darwin/15.6.0";
  175. $hash->{userAgent} = $attr{$name}{userAgent} if (defined $attr{$name}{userAgent});
  176. $hash->{timeout} = 10;
  177. $hash->{HTTPCookies} = undef;
  178. Log3 $name, 2, "$name: login start";
  179. tahoma_UserAgent_NonblockingGet({
  180. timeout => 10,
  181. noshutdown => 1,
  182. hash => $hash,
  183. page => 'login',
  184. data => {'userId' => $hash->{username} , 'userPassword' => $hash->{password}},
  185. callback => \&tahoma_dispatch,
  186. nonblocking => 1,
  187. });
  188. }
  189. my @startup_pages = ( 'getEndUser',
  190. 'getSetup',
  191. 'getActionGroups',
  192. #'/../../enduserAPI/setup/interactiveNotifications',
  193. #'/../../enduserAPI/setup/interactiveNotifications/history',
  194. #'getCalendarDayList',
  195. #'getCalendarRuleList',
  196. #'/../../enduserAPI/conditionGroups',
  197. #'getScheduledExecutions',
  198. #'getHistory',
  199. #'getSetupTriggers',
  200. #'getUserPreferences',
  201. #'getSetupOptions',
  202. #'getAvailableProtocolsType',
  203. #'getActiveProtocolsType',
  204. #'getSetupQuota?quotaId=smsCredit',
  205. #'getSetupDawnAndDuskTimes',
  206. '../../enduserAPI/setup/gateways',
  207. #'../../enduserAPI/setup/gateways',
  208. #'../../enduserAPI/setup/subscribe/notification/apple/com.somfy.iPhoneTaHoma',
  209. #'../../enduserAPI/setup/subscribe/notification/devices/tahoma',
  210. #'/../../enduserAPI/setup/subscribe/notification/apple/com.somfy.iPhoneTaHoma',
  211. #'../../enduserAPI/setup/subscribe/notification/devices/tahoma',
  212. 'getCurrentExecutions',
  213. 'refreshAllStates' );
  214. sub tahoma_startup($)
  215. {
  216. my ($hash) = @_;
  217. my $name = $hash->{NAME};
  218. Log3 $name, 4, "$name: tahoma_startup";
  219. return if (!$hash->{logged_in});
  220. return if ($hash->{startup_done});
  221. if (!defined($hash->{startup_run}))
  222. {
  223. $hash->{startup_run} = 0;
  224. }
  225. else
  226. {
  227. $hash->{startup_run}++;
  228. if ($hash->{startup_run} >= scalar @startup_pages)
  229. {
  230. $hash->{startup_done} = 1;
  231. return;
  232. }
  233. }
  234. my $page = $startup_pages[$hash->{startup_run}];
  235. my $subpage = "";
  236. $subpage = '?gatewayId='.$hash->{gatewayId} if (substr($page, -13) eq 'ProtocolsType');
  237. $subpage = '?quotaId=smsCredit' if ($page eq 'getSetupQuota');
  238. $subpage = '/'.$hash->{gatewayId}.'/version' if (substr($page, -8) eq 'gateways');
  239. tahoma_UserAgent_NonblockingGet({
  240. timeout => 10,
  241. noshutdown => 1,
  242. hash => $hash,
  243. page => $page,
  244. subpage => $subpage,
  245. callback => \&tahoma_dispatch,
  246. nonblocking => 1,
  247. });
  248. }
  249. sub tahoma_refreshAllStates($)
  250. {
  251. my ($hash) = @_;
  252. my $name = $hash->{NAME};
  253. Log3 $name, 4, "$name: tahoma_refreshAllStates";
  254. tahoma_UserAgent_NonblockingGet({
  255. timeout => 10,
  256. noshutdown => 1,
  257. hash => $hash,
  258. page => 'refreshAllStates',
  259. callback => \&tahoma_dispatch,
  260. nonblocking => 1,
  261. });
  262. }
  263. sub tahoma_getEvents($)
  264. {
  265. my ($hash) = @_;
  266. my $name = $hash->{NAME};
  267. Log3 $name, 4, "$name: tahoma_getEvents";
  268. return if(!$hash->{logged_in} && !$hash->{startup_done});
  269. tahoma_UserAgent_NonblockingGet({
  270. timeout => 10,
  271. noshutdown => 1,
  272. hash => $hash,
  273. page => 'getEvents',
  274. callback => \&tahoma_dispatch,
  275. nonblocking => 1,
  276. });
  277. }
  278. sub tahoma_readStatusTimer($)
  279. {
  280. my $timestart = time;
  281. my $timeinfo = "tahoma_readStatusTimer";
  282. my ($hash) = @_;
  283. my $name = $hash->{NAME};
  284. RemoveInternalTimer($hash);
  285. my ($seconds) = gettimeofday();
  286. $hash->{refreshStateTimer} = $seconds + 10 if ( (!defined($hash->{refreshStateTimer})) || (!$hash->{logged_in}) );
  287. if( $hash->{request_active} ) {
  288. Log3 $name, 3, "$name: request active";
  289. if( ($timestart - $hash->{request_time}) > 10)
  290. {
  291. Log3 $name, 2, "$name: timeout, close ua";
  292. $hash->{socket} = undef;
  293. $hash->{request_active} = 0;
  294. $hash->{logged_in} = 0;
  295. $hash->{startup_done} = 0;
  296. }
  297. }
  298. elsif( !$hash->{logged_in} ) {
  299. tahoma_login($hash);
  300. $timeinfo = "tahoma_login";
  301. }
  302. elsif( !$hash->{startup_done} ) {
  303. tahoma_startup($hash);
  304. $timeinfo = "tahoma_startup";
  305. if ( $hash->{startup_done} ) {
  306. tahoma_getStates($hash) ;
  307. $hash->{refreshStateTimer} = $seconds + $hash->{INTERVAL};
  308. $timeinfo = "tahoma_getStates";
  309. }
  310. }
  311. elsif( ($seconds < $hash->{refreshStateTimer}) || ($hash->{INTERVAL} <= 0) )
  312. {
  313. Log3 $name, 4, "$name: refreshing event";
  314. tahoma_getEvents($hash);
  315. $timeinfo = "tahoma_getEvents";
  316. }
  317. else
  318. {
  319. Log3 $name, 4, "$name: refreshing state";
  320. tahoma_refreshAllStates($hash);
  321. tahoma_getStates($hash);
  322. $hash->{refreshStateTimer} = $seconds + $hash->{INTERVAL};
  323. $timeinfo = "tahoma_refreshAllStates tahoma_getStates";
  324. }
  325. my $timedelta = time -$timestart;
  326. if ($timedelta > 0.5)
  327. {
  328. $timedelta *= 1000;
  329. Log3 $name, 3, "$name: $timeinfo took $timedelta ms"
  330. }
  331. InternalTimer(gettimeofday()+2, "tahoma_readStatusTimer", $hash, 0);
  332. }
  333. sub tahoma_connect($)
  334. {
  335. my ($hash) = @_;
  336. my $name = $hash->{NAME};
  337. Log3 $name, 3, "$name: tahoma_connect";
  338. RemoveInternalTimer($hash);
  339. tahoma_login($hash);
  340. my ($seconds) = gettimeofday();
  341. $hash->{refreshStateTimer} = $seconds + 10;
  342. tahoma_readStatusTimer($hash);
  343. }
  344. sub tahoma_initDevice($)
  345. {
  346. my ($hash) = @_;
  347. my $name = $hash->{NAME};
  348. my $subtype = $hash->{SUBTYPE};
  349. AssignIoPort($hash);
  350. if(defined($hash->{IODev}->{NAME})) {
  351. Log3 $name, 3, "$name: I/O device is " . $hash->{IODev}->{NAME};
  352. } else {
  353. Log3 $name, 1, "$name: no I/O device";
  354. }
  355. my $device;
  356. if( defined($hash->{device}) ) {
  357. $device = tahoma_getDeviceDetail( $hash, $hash->{device} );
  358. #Log3 $name, 4, Dumper($device);
  359. } elsif( defined($hash->{oid}) ) {
  360. $device = tahoma_getDeviceDetail( $hash, $hash->{oid} );
  361. #Log3 $name, 4, Dumper($device);
  362. }
  363. if( defined($device) && ($subtype eq 'DEVICE') ) {
  364. Log3 $name, 4, "$name: I/O device is label=".$device->{label};
  365. $hash->{inType} = $device->{type};
  366. $hash->{inLabel} = $device->{label};
  367. $hash->{inControllable} = $device->{controllableName};
  368. $hash->{inPlaceOID} = $device->{placeOID};
  369. $hash->{inClass} = $device->{uiClass};
  370. $device->{levelInvert} = $attr{$hash->{NAME}}{levelInvert} if (defined $attr{$hash->{NAME}}{levelInvert});
  371. }
  372. elsif( defined($device) && ($subtype eq 'PLACE') ) {
  373. Log3 $name, 4, "$name: I/O device is label=".$device->{label};
  374. $hash->{inType} = $device->{type};
  375. $hash->{inLabel} = $device->{label};
  376. $hash->{inOID} = $device->{oid};
  377. $hash->{inClass} = 'RollerShutter';
  378. $hash->{inClass} = $attr{$hash->{NAME}}{placeClasses} if (defined $attr{$hash->{NAME}}{placeClasses});
  379. }
  380. elsif( defined($device) && ($subtype eq 'SCENE') ) {
  381. Log3 $name, 4, "$name: I/O device is label=".$device->{label};
  382. $hash->{inLabel} = $device->{label};
  383. $hash->{inOID} = $device->{oid};
  384. }
  385. else
  386. {
  387. my $device=$hash->{device};
  388. $device ||= 'undefined';
  389. $subtype ||= 'undefined';
  390. Log3 $name, 3, "$name: unknown device=$device, subtype=$subtype";
  391. }
  392. }
  393. sub tahoma_updateDevices($)
  394. {
  395. my ($hash) = @_;
  396. my $name = $hash->{NAME};
  397. Log3 $name, 3, "$name: tahoma_updateDevices";
  398. return undef if( !$hash->{helper}{devices} ) ;
  399. $hash = $hash->{IODev} if( defined($hash->{IODev}) );
  400. foreach my $module (keys %{$modules{$hash->{TYPE}}{defptr}}) {
  401. my $def = $modules{$hash->{TYPE}}{defptr}{"$module"};
  402. my $subtype = $def->{SUBTYPE};
  403. if (defined($def->{oid}) && !defined($def->{inType}))
  404. {
  405. Log3 $name, 3, "$name: updateDevices oid=$def->{oid}";
  406. my $device = tahoma_getDeviceDetail( $hash, $def->{oid} );
  407. if( defined($device) && ($subtype eq 'PLACE') ) {
  408. Log3 $name, 4, "$name: I/O device is label=".$device->{label};
  409. $def->{inType} = $device->{type};
  410. $def->{inLabel} = $device->{label};
  411. $def->{inOID} = $device->{oid};
  412. $def->{inClass} = 'RollerShutter';
  413. $def->{inClass} = $attr{$def->{NAME}}{placeClasses} if (defined $attr{$def->{NAME}}{placeClasses});
  414. }
  415. elsif( defined($device) && ($subtype eq 'SCENE') ) {
  416. Log3 $name, 4, "$name: I/O device is label=".$device->{label};
  417. $def->{inLabel} = $device->{label};
  418. $def->{inOID} = $device->{oid};
  419. }
  420. }
  421. elsif (defined($def->{device}) && !defined($def->{inType}))
  422. {
  423. Log3 $name, 3, "$name: updateDevices device=$def->{device}";
  424. my $device = tahoma_getDeviceDetail( $hash, $def->{device} );
  425. if( defined($device) && ($subtype eq 'DEVICE') ) {
  426. Log3 $name, 4, "$name: I/O device is label=".$device->{label};
  427. $def->{inType} = $device->{type};
  428. $def->{inLabel} = $device->{label};
  429. $def->{inControllable} = $device->{controllableName};
  430. $def->{inPlaceOID} = $device->{placeOID};
  431. $def->{inClass} = $device->{uiClass};
  432. $device->{levelInvert} = $attr{$def->{NAME}}{levelInvert} if (defined $attr{$def->{NAME}}{levelInvert});
  433. }
  434. }
  435. }
  436. return undef;
  437. }
  438. sub tahoma_getDevices($$)
  439. {
  440. my ($hash,$nonblocking) = @_;
  441. my $name = $hash->{NAME};
  442. Log3 $name, 4, "$name: tahoma_getDevices";
  443. tahoma_UserAgent_NonblockingGet({
  444. timeout => 10,
  445. noshutdown => 1,
  446. hash => $hash,
  447. page => 'getSetup',
  448. callback => \&tahoma_dispatch,
  449. nonblocking => $nonblocking,
  450. });
  451. return $hash->{helper}{devices};
  452. }
  453. sub tahoma_getDeviceDetail($$)
  454. {
  455. my ($hash,$id) = @_;
  456. my $name = $hash->{NAME};
  457. Log3 $name, 4, "$name: tahoma_getDeviceDetails $id";
  458. $hash = $hash->{IODev} if( defined($hash->{IODev}) );
  459. foreach my $device (@{$hash->{helper}{devices}}) {
  460. return $device if( defined($device->{deviceURL}) && ($device->{deviceURL} eq $id) );
  461. return $device if( defined($device->{oid}) && ($device->{oid} eq $id) );
  462. }
  463. Log3 $name, 4, "$name: getDeviceDetails $id not found";
  464. return undef;
  465. }
  466. sub tahoma_getStates($)
  467. {
  468. my ($hash) = @_;
  469. my $name = $hash->{NAME};
  470. Log3 $name, 4, "$name: tahoma_getStates";
  471. my $data = '[';
  472. foreach my $device (@{$hash->{helper}{devices}}) {
  473. if( defined($device->{deviceURL}) && defined($device->{states}) )
  474. {
  475. $data .= ',' if (substr($data, -1) eq '}');
  476. $data .= '{"deviceURL":"'.$device->{deviceURL}.'","states":[';
  477. foreach my $state (@{$device->{states}}) {
  478. $data .= ',' if (substr($data, -1) eq '}');
  479. $data .= '{"name":"' . $state->{name} . '"}';
  480. }
  481. $data .= ']}';
  482. }
  483. }
  484. $data .= ']';
  485. Log3 $name, 5, "$name: tahoma_getStates data=".$data;
  486. tahoma_UserAgent_NonblockingGet({
  487. timeout => 10,
  488. noshutdown => 1,
  489. hash => $hash,
  490. page => 'getStates',
  491. data => tahoma_encode_utf8($data),
  492. callback => \&tahoma_dispatch,
  493. nonblocking => 1,
  494. });
  495. }
  496. sub tahoma_getDeviceList($$$$);
  497. sub tahoma_getDeviceList($$$$)
  498. {
  499. my ($hash,$oid,$placeClasses,$deviceList) = @_;
  500. #print "tahoma_getDeviceList oid=$oid devices=".scalar @{$deviceList}."\n";
  501. my @classes = split(' ',$placeClasses);
  502. my $devices = $hash->{helper}{devices};
  503. foreach my $device (@{$devices}) {
  504. if ( defined($device->{deviceURL}) && defined($device->{placeOID}) && defined($device->{uiClass}) ) {
  505. if (( grep { $_ eq $device->{uiClass}} @classes ) && ($device->{placeOID} eq $oid)) {
  506. push ( @{$deviceList}, { device => $device->{deviceURL}, class => $device->{uiClass}, levelInvert => $device->{levelInvert} } ) ;
  507. #print "tahoma_getDeviceList url=$device->{deviceURL} devices=".scalar @{$deviceList}."\n";
  508. }
  509. } elsif ( defined($device->{oid}) && defined($device->{subPlaces}) ) {
  510. if ($device->{oid} eq $oid)
  511. {
  512. foreach my $place (@{$device->{subPlaces}}) {
  513. tahoma_getDeviceList($hash,$place->{oid},$placeClasses,$deviceList);
  514. }
  515. }
  516. }
  517. }
  518. }
  519. sub tahoma_checkCommand($$$$)
  520. {
  521. my ($hash,$device,$command,$value) = @_;
  522. my $name = $hash->{NAME};
  523. Log3 $name, 4, "$name: tahoma_checkCommand";
  524. if (($command eq 'setClosure') && (defined ($device->{levelInvert})))
  525. {
  526. $value = 100 - $value if ($device->{levelInvert} && ($value >= 0) && ($value <= 100));
  527. }
  528. if (($command eq 'setClosure') && ($value == 100) && (index($hash->{COMMANDS}," close:") > 0))
  529. {
  530. $command = 'close';
  531. $value = '';
  532. }
  533. if (($command eq 'setClosure') && ($value == 0) && (index($hash->{COMMANDS}," open:") > 0))
  534. {
  535. $command = 'open';
  536. $value = '';
  537. }
  538. return ($command,$value);
  539. }
  540. sub tahoma_applyRequest($$$)
  541. {
  542. my ($hash,$command,$value) = @_;
  543. my $name = $hash->{NAME};
  544. Log3 $name, 4, "$name: tahoma_applyRequest";
  545. if ( !defined($hash->{IODev}) || !(defined($hash->{device}) || defined($hash->{oid})) || !defined($hash->{inLabel}) || !defined($hash->{inClass}) ) {
  546. Log3 $name, 4, "$name: tahoma_applyRequest failed - define error";
  547. return;
  548. }
  549. my @devices = ();
  550. if ( defined($hash->{device}) ) {
  551. push ( @devices, { device => $hash->{device}, class => $hash->{inClass}, commands => $hash->{COMMANDS}, levelInvert => $attr{$hash->{NAME}}{levelInvert} } );
  552. } else {
  553. tahoma_getDeviceList($hash->{IODev},$hash->{oid},$hash->{inClass},\@devices);
  554. }
  555. Log3 $name, 4, "$name: tahoma_applyRequest devices=".scalar @devices;
  556. foreach my $dev (@devices) {
  557. Log3 $name, 4, "$name: tahoma_applyRequest devices=$dev->{device} class=$dev->{class}";
  558. }
  559. return if (scalar @devices < 1);
  560. my $data = '';
  561. $value = '' if (!defined($value));
  562. my $commandChecked = $command;
  563. my $valueChecked = $value;
  564. foreach my $device (@devices) {
  565. ($commandChecked, $valueChecked) = tahoma_checkCommand($hash,$device,$command,$value);
  566. if (defined($commandChecked) && defined($valueChecked))
  567. {
  568. $data .= ',' if substr($data, -1) eq '}';
  569. $data .= '{"deviceURL":"'.$device->{device}.'",';
  570. $data .= '"commands":[{"name":"'.$commandChecked.'","parameters":['.$valueChecked.']}]}';
  571. }
  572. }
  573. return if (length $data < 20);
  574. my $dataHead = '{"label":"' . $hash->{inLabel};
  575. if ($commandChecked eq 'setClosure') {
  576. $dataHead .= ' - Positionieren auf '.$valueChecked.' % - iPhone","actions":[';
  577. } elsif ($commandChecked eq 'close') {
  578. $dataHead .= ' - Schliessen - iPhone","actions":[';
  579. } elsif ($commandChecked eq 'open') {
  580. $dataHead .= ' - Oeffnen - iPhone","actions":[';
  581. } else {
  582. $dataHead .= " - $commandChecked $valueChecked".' - iPhone","actions":[';
  583. }
  584. $data = $dataHead . $data . ']}';
  585. Log3 $name, 3, "$name: tahoma_applyRequest data=".$data;
  586. tahoma_UserAgent_NonblockingGet({
  587. timeout => 10,
  588. noshutdown => 1,
  589. hash => $hash,
  590. page => 'apply',
  591. data => tahoma_encode_utf8($data),
  592. callback => \&tahoma_dispatch,
  593. nonblocking => 1,
  594. });
  595. }
  596. sub tahoma_scheduleActionGroup($$)
  597. {
  598. my ($hash,$delay) = @_;
  599. my $name = $hash->{NAME};
  600. Log3 $name, 4, "$name: tahoma_scheduleActionGroup";
  601. if ( !defined($hash->{IODev}) || !defined($hash->{oid}) ) {
  602. Log3 $name, 3, "$name: tahoma_scheduleActionGroup failed - define error";
  603. return;
  604. }
  605. $delay = 0 if(!defined($delay));
  606. tahoma_UserAgent_NonblockingGet({
  607. timeout => 10,
  608. noshutdown => 1,
  609. hash => $hash,
  610. page => 'scheduleActionGroup',
  611. subpage => '?oid='.$hash->{oid}.'&delay='.$delay,
  612. callback => \&tahoma_dispatch,
  613. nonblocking => 1,
  614. });
  615. }
  616. sub tahoma_launchActionGroup($)
  617. {
  618. my ($hash) = @_;
  619. my $name = $hash->{NAME};
  620. Log3 $name, 4, "$name: tahoma_launchActionGroup";
  621. if ( !defined($hash->{IODev}) || !defined($hash->{oid}) ) {
  622. Log3 $name, 3, "$name: tahoma_launchActionGroup failed - define error";
  623. return;
  624. }
  625. tahoma_UserAgent_NonblockingGet({
  626. timeout => 10,
  627. noshutdown => 1,
  628. hash => $hash,
  629. page => 'launchActionGroup',
  630. subpage => '?oid='.$hash->{oid},
  631. callback => \&tahoma_dispatch,
  632. nonblocking => 1,
  633. });
  634. }
  635. sub tahoma_cancelExecutions($)
  636. {
  637. my ($hash) = @_;
  638. my $name = $hash->{NAME};
  639. Log3 $name, 4, "$name: tahoma_cancelExecutions";
  640. my $subpage = '';
  641. if (defined($hash->{IODev}))
  642. {
  643. if (defined($hash->{inExecId}) && (length $hash->{inExecId} > 20))
  644. {
  645. $subpage = '?execId='.$hash->{inExecId};
  646. }
  647. elsif (defined($hash->{inTriggerId}) && (length $hash->{inTriggerId} > 20))
  648. {
  649. $subpage = '?triggerId='.$hash->{inTriggerId};
  650. }
  651. else
  652. {
  653. Log3 $name, 3, "$name: tahoma_cancelExecutions failed - no valid execId or triggerId found";
  654. return;
  655. }
  656. }
  657. Log3 $name, 3, "$name: tahoma_cancelExecutions subpage=$subpage";
  658. tahoma_UserAgent_NonblockingGet({
  659. timeout => 10,
  660. noshutdown => 1,
  661. hash => $hash,
  662. page => 'cancelExecutions',
  663. subpage => $subpage,
  664. callback => \&tahoma_dispatch,
  665. nonblocking => 1,
  666. });
  667. }
  668. sub tahoma_dispatch($$$)
  669. {
  670. my ($param, $err, $data) = @_;
  671. my $hash = $param->{hash};
  672. my $name = $hash->{NAME};
  673. my $hashIn = $hash;
  674. $hash = $hash->{IODev} if (defined($hash->{IODev}));
  675. $hash->{request_active} = 0;
  676. if( $err ) {
  677. Log3 $name, 2, "$name: tahoma_dispatch http request failed: $err";
  678. $hash->{logged_in} = 0;
  679. return;
  680. }
  681. if( $data ) {
  682. tahoma_GetCookies($hash,$param->{httpheader}) if (!$hash->{logged_in});
  683. $data =~ tr/\r\n//d;
  684. $data =~ s/\h+/ /g;
  685. $data =~ s/\\\//\//g;
  686. Log3 $name, (length $data > 120)?4:5, "$name: tahoma_dispatch data=".decode_utf8($data);
  687. # perl exception while parsing json string captured
  688. my $json = {};
  689. eval { $json = JSON->new->utf8(0)->decode($data); };
  690. if ($@) {
  691. Log3 $name, 3, "$name: tahoma_dispatch json string is faulty";
  692. $hash->{lastError} = 'json string is faulty';
  693. $hash->{logged_in} = 0;
  694. return;
  695. }
  696. if( (ref $json ne 'ARRAY') && ($json->{errorResponse}) ) {
  697. $hash->{lastError} = $json->{errorResponse}{message};
  698. $hash->{logged_in} = 0;
  699. return;
  700. }
  701. if( $param->{page} eq 'getEvents' ) {
  702. tahoma_parseGetEvents($hash,$json);
  703. } elsif( $param->{page} eq 'apply' ) {
  704. tahoma_parseApplyRequest($hashIn,$json);
  705. } elsif( $param->{page} eq 'getSetup' ) {
  706. tahoma_parseGetSetup($hash,$json);
  707. } elsif( $param->{page} eq 'refreshAllStates' ) {
  708. tahoma_parseRefreshAllStates($hash,$json);
  709. } elsif( $param->{page} eq 'getStates' ) {
  710. tahoma_parseGetStates($hash,$json);
  711. } elsif( $param->{page} eq 'login' ) {
  712. tahoma_parseLogin($hash,$json);
  713. } elsif( $param->{page} eq 'getActionGroups' ) {
  714. tahoma_parseGetActionGroups($hash,$json);
  715. } elsif( $param->{page} eq '../../enduserAPI/setup/gateways' ) {
  716. tahoma_parseEnduserAPISetupGateways($hash,$json);
  717. } elsif( $param->{page} eq 'getCurrentExecutions' ) {
  718. tahoma_parseGetCurrentExecutions($hash,$json);
  719. } elsif( $param->{page} eq 'scheduleActionGroup' ) {
  720. tahoma_parseScheduleActionGroup($hashIn,$json);
  721. } elsif( $param->{page} eq 'launchActionGroup' ) {
  722. tahoma_parseLaunchActionGroup($hashIn,$json);
  723. } elsif( $param->{page} eq 'cancelExecutions' ) {
  724. tahoma_parseCancelExecutions($hash,$json);
  725. }
  726. }
  727. }
  728. sub tahoma_autocreate($)
  729. {
  730. my($hash) = @_;
  731. my $name = $hash->{NAME};
  732. if( !$hash->{helper}{devices} ) {
  733. tahoma_getDevices($hash,1);
  734. return undef;
  735. }
  736. foreach my $d (keys %defs) {
  737. next if($defs{$d}{TYPE} ne "autocreate");
  738. return undef if(AttrVal($defs{$d}{NAME},"disable",undef));
  739. }
  740. Log3 $name, 2, "$name: tahoma_autocreate begin";
  741. my $autocreated = 0;
  742. my $devices = $hash->{helper}{devices};
  743. foreach my $device (@{$devices}) {
  744. my ($id, $fid, $devname, $define);
  745. if ($device->{deviceURL}) {
  746. $id = $device->{deviceURL};
  747. $fid = (split("/",$id))[-1];
  748. $devname = "tahoma_". $fid;
  749. $define = "$devname tahoma DEVICE $id";
  750. if( defined($modules{$hash->{TYPE}}{defptr}{"$fid"}) ) {
  751. Log3 $name, 4, "$name: device '$fid' already defined";
  752. next;
  753. }
  754. } elsif ( $device->{oid} ) {
  755. $id = $device->{oid};
  756. $fid = (split("-",$id))[0];
  757. $devname = "tahoma_". $fid;
  758. $define = "$devname tahoma PLACE $id" if (!defined $device->{actions});
  759. $define = "$devname tahoma SCENE $id" if (defined $device->{actions});
  760. if( defined($modules{$hash->{TYPE}}{defptr}{"$fid"}) ) {
  761. Log3 $name, 4, "$name: device '$fid' already defined";
  762. next;
  763. }
  764. }
  765. Log3 $name, 3, "$name: create new device '$devname' for device '$id'";
  766. my $cmdret= CommandDefine(undef,$define);
  767. if($cmdret) {
  768. Log3 $name, 1, "$name: Autocreate: An error occurred while creating device for id '$id': $cmdret";
  769. } else {
  770. $cmdret= CommandAttr(undef,"$devname alias room ".$device->{label}) if( defined($device->{label}) && defined($device->{oid}) && !defined($device->{actions}) );
  771. $cmdret= CommandAttr(undef,"$devname alias scene ".$device->{label}) if( defined($device->{label}) && defined($device->{oid}) && defined($device->{actions}) );
  772. $cmdret= CommandAttr(undef,"$devname alias $device->{uiClass} ".$device->{label}) if( defined($device->{label}) && defined($device->{states}) );
  773. $cmdret= CommandAttr(undef,"$devname room tahoma");
  774. $cmdret= CommandAttr(undef,"$devname IODev $name");
  775. $cmdret= CommandAttr(undef,"$devname webCmd dim") if( defined($device->{uiClass}) && ($device->{uiClass} eq "RollerShutter") );
  776. $autocreated++;
  777. }
  778. }
  779. CommandSave(undef,undef) if( $autocreated && AttrVal( "autocreate", "autosave", 1 ) );
  780. Log3 $name, 2, "$name: tahoma_autocreate end, new=$autocreated";
  781. }
  782. sub tahoma_defineCommands($)
  783. {
  784. my($hash) = @_;
  785. my $name = $hash->{NAME};
  786. Log3 $name, 4, "$name: tahoma_defineCommands";
  787. my $devices = $hash->{helper}{devices};
  788. foreach my $device (@{$devices}) {
  789. my ($id, $fid, $devname, $define);
  790. if ($device->{deviceURL}) {
  791. $id = $device->{deviceURL};
  792. $fid = (split("/",$id))[-1];
  793. $devname = "tahoma_". $fid;
  794. $define = "$devname tahoma DEVICE $id";
  795. if( defined $device->{definition}{commands}[0]{commandName} ) {
  796. my $commandlist = "dim:slider,0,1,100 cancel:noArg";
  797. foreach my $command (@{$device->{definition}{commands}}) {
  798. $commandlist .= " " . $command->{commandName};
  799. $commandlist .= ":noArg" if ($command->{nparams} == 0);
  800. }
  801. if( defined($modules{$hash->{TYPE}}{defptr}{"$fid"}) ) {
  802. $modules{$hash->{TYPE}}{defptr}{"$fid"}{COMMANDS} = $commandlist;
  803. Log3 $name, 4, "$name: tahoma_defineCommands fid=$fid commandlist=$commandlist";
  804. }
  805. }
  806. }
  807. }
  808. }
  809. sub tahoma_parseLogin($$)
  810. {
  811. my($hash, $json) = @_;
  812. my $name = $hash->{NAME};
  813. Log3 $name, 4, "$name: tahoma_parseLogin";
  814. if (defined $json->{errorResponse}) {
  815. $hash->{logged_in} = 0;
  816. $hash->{STATE} = $json->{errorResponse}{message};
  817. } else {
  818. $hash->{inVersion} = $json->{version};
  819. $hash->{logged_in} = 1;
  820. }
  821. Log3 $name, 2, "$name: login end, logged_in=".$hash->{logged_in};
  822. }
  823. sub tahoma_parseGetEvents($$)
  824. {
  825. my($hash, $json) = @_;
  826. my $name = $hash->{NAME};
  827. Log3 $name, 5, "$name: tahoma_parseGetEvent";
  828. $hash->{refresh_event} = $json;
  829. if( $hash->{logged_in} ) {
  830. $hash->{STATE} = "Connected";
  831. } else {
  832. $hash->{STATE} = "Disconnected";
  833. }
  834. if( ref $json eq 'ARRAY' ) {
  835. #print Dumper($json);
  836. foreach my $devices ( @{$json} ) {
  837. if( defined($devices->{deviceURL}) ) {
  838. #print "\nDevice=$devices->{deviceURL} found\n";
  839. my $id = $devices->{deviceURL};
  840. my $fid = (split("/",$id))[-1];
  841. my $devname = "tahoma_". $fid;
  842. my $d = $modules{$hash->{TYPE}}{defptr}{"$fid"};
  843. if( defined($d) )# && $d->{NAME} eq $devname )
  844. {
  845. #print "\nDevice=$devices->{deviceURL} updated\n";
  846. readingsBeginUpdate($d);
  847. foreach my $state (@{$devices->{deviceStates}}) {
  848. #print "$devname $state->{name} = $state->{value}\n";
  849. next if (!defined($state->{name}) || !defined($state->{value}));
  850. if ($state->{name} eq "core:ClosureState") {
  851. $state->{value} = 100 - $state->{value} if ($attr{$d->{NAME}}{levelInvert});
  852. readingsBulkUpdate($d, "state", "dim".$state->{value});
  853. } elsif ($state->{name} eq "core:OpenClosedState") {
  854. readingsBulkUpdate($d, "devicestate", $state->{value});
  855. }
  856. readingsBulkUpdate($d, (split(":",$state->{name}))[-1], $state->{value});
  857. }
  858. my ($seconds) = gettimeofday();
  859. readingsBulkUpdate( $d, ".lastupdate", $seconds, 0 );
  860. readingsEndUpdate($d,1);
  861. }
  862. }
  863. elsif( defined($devices->{name}) && (defined($devices->{execId}) || defined($devices->{triggerId})) )
  864. {
  865. foreach my $module (keys %{$modules{$hash->{TYPE}}{defptr}})
  866. {
  867. my $def = $modules{$hash->{TYPE}}{defptr}{"$module"};
  868. if (defined($def->{inExecId}) && ($def->{inExecId} eq $devices->{execId}))
  869. {
  870. if ($devices->{name} eq 'ExecutionStateChangedEvent')
  871. {
  872. $def->{inExecState} = $devices->{newState};
  873. $def->{inExecId} = 'finished' if ($devices->{newState} == 4);
  874. $def->{inExecId} = 'canceled' if ($devices->{newState} == 5);
  875. }
  876. }
  877. elsif (defined($def->{inTriggerId}) && ($def->{inTriggerId} eq $devices->{triggerId}))
  878. {
  879. $def->{inTriggerState} = $devices->{name};
  880. $def->{inTriggerId} = 'finished' if ($devices->{name} eq '4');
  881. $def->{inTriggerId} = 'canceled' if ($devices->{name} eq '5');
  882. }
  883. }
  884. }
  885. }
  886. }
  887. }
  888. sub tahoma_parseApplyRequest($$)
  889. {
  890. my($hash, $json) = @_;
  891. my $name = $hash->{NAME};
  892. Log3 $name, 4, "$name: tahoma_parseApplyRequest";
  893. $hash->{inExecState} = 0;
  894. if (defined($json->{execId})) {
  895. $hash->{inExecId} = $json->{execId};
  896. } else {
  897. $hash->{inExecId} = "undefined";
  898. }
  899. if (defined($json->{events}) && defined($hash->{IODev}))
  900. {
  901. tahoma_parseGetEvents($hash->{IODev},$json->{events})
  902. }
  903. }
  904. sub tahoma_parseGetSetup($$)
  905. {
  906. my($hash, $json) = @_;
  907. my $name = $hash->{NAME};
  908. $hash->{gatewayId} = $json->{setup}{gateways}[0]{gatewayId};
  909. my @devices = ();
  910. foreach my $device (@{$json->{setup}{devices}}) {
  911. Log3 $name, 4, "$name: tahoma_parseGetSetup device = $device->{label}";
  912. push( @devices, $device );
  913. }
  914. $hash->{helper}{devices} = \@devices;
  915. if ($json->{setup}{rootPlace}) {
  916. my $places = $json->{setup}{rootPlace};
  917. #Log3 $name, 4, "$name: tahoma_parseGetSetup places= " . Dumper($places);
  918. tahoma_parseGetSetupPlaces($hash, $places);
  919. }
  920. tahoma_autocreate($hash);
  921. tahoma_updateDevices($hash);
  922. tahoma_defineCommands($hash);
  923. }
  924. sub tahoma_parseGetSetupPlaces($$)
  925. {
  926. my($hash, $places) = @_;
  927. my $name = $hash->{NAME};
  928. #Log3 $name, 4, "$name: tahoma_parseGetSetupPlaces " . Dumper($places);
  929. my $devices = $hash->{helper}{devices};
  930. if (ref $places eq 'ARRAY') {
  931. #Log3 $name, 4, "$name: tahoma_parseGetSetupPlaces isArray";
  932. foreach my $place (@{$places}) {
  933. push( @{$devices}, $place );
  934. my $placesNext = $place->{subPlaces};
  935. tahoma_parseGetSetupPlaces($hash, $placesNext ) if ($placesNext);
  936. }
  937. }
  938. else {
  939. #Log3 $name, 4, "$name: tahoma_parseGetSetupPlaces isScalar";
  940. push( @{$devices}, $places );
  941. my $placesNext = $places->{subPlaces};
  942. tahoma_parseGetSetupPlaces($hash, $placesNext) if ($placesNext);
  943. }
  944. }
  945. sub tahoma_parseGetActionGroups($$)
  946. {
  947. my($hash, $json) = @_;
  948. my $name = $hash->{NAME};
  949. Log3 $name, 4, "$name: tahoma_parseGetActionGroups";
  950. my $devices = $hash->{helper}{devices};
  951. foreach my $action (@{$json->{actionGroups}}) {
  952. push( @{$devices}, $action );
  953. }
  954. tahoma_autocreate($hash);
  955. tahoma_updateDevices($hash);
  956. tahoma_defineCommands($hash);
  957. }
  958. sub tahoma_parseRefreshAllStates($$)
  959. {
  960. my($hash, $json) = @_;
  961. my $name = $hash->{NAME};
  962. Log3 $name, 4, "$name: tahoma_parseRefreshAllStates";
  963. }
  964. sub tahoma_parseGetStates($$)
  965. {
  966. my($hash, $states) = @_;
  967. my $name = $hash->{NAME};
  968. Log3 $name, 4, "$name: tahoma_parseGetStates";
  969. if( defined($states->{devices}) ) {
  970. foreach my $devices ( @{$states->{devices}} ) {
  971. if( defined($devices->{deviceURL}) ) {
  972. my $id = $devices->{deviceURL};
  973. my $fid = (split("/",$id))[-1];
  974. my $devname = "tahoma_". $fid;
  975. my $d = $modules{$hash->{TYPE}}{defptr}{"$fid"};
  976. if( defined($d) )# && $d->{NAME} eq $devname )
  977. {
  978. readingsBeginUpdate($d);
  979. foreach my $state (@{$devices->{states}}) {
  980. next if (!defined($state->{name}) || !defined($state->{value}));
  981. if ($state->{name} eq "core:ClosureState") {
  982. $state->{value} = 100 - $state->{value} if ($attr{$d->{NAME}}{levelInvert});
  983. readingsBulkUpdate($d, "state", "dim".$state->{value});
  984. } elsif ($state->{name} eq "core:OpenClosedState") {
  985. readingsBulkUpdate($d, "devicestate", $state->{value});
  986. }
  987. readingsBulkUpdate($d, (split(":",$state->{name}))[-1], $state->{value});
  988. }
  989. my ($seconds) = gettimeofday();
  990. readingsBulkUpdate( $d, ".lastupdate", $seconds, 0 );
  991. readingsEndUpdate($d,1);
  992. }
  993. }
  994. }
  995. }
  996. }
  997. sub tahoma_parseEnduserAPISetupGateways($$)
  998. {
  999. my($hash, $json) = @_;
  1000. my $name = $hash->{NAME};
  1001. Log3 $name, 4, "$name: tahoma_parseEnduserAPISetupGateways";
  1002. eval { $hash->{inGateway} = $json->{result}; };
  1003. eval { $hash->{inGateway} = $json->[0]{gatewayId}; };
  1004. }
  1005. sub tahoma_parseGetCurrentExecutions($$)
  1006. {
  1007. my($hash, $json) = @_;
  1008. my $name = $hash->{NAME};
  1009. Log3 $name, 4, "$name: tahoma_parseGetCurrentExecutions";
  1010. }
  1011. sub tahoma_parseScheduleActionGroup($$)
  1012. {
  1013. my($hash, $json) = @_;
  1014. my $name = $hash->{NAME};
  1015. Log3 $name, 4, "$name: tahoma_parseScheduleActionGroup";
  1016. if (defined $json->{actionGroup})
  1017. {
  1018. $hash->{inTriggerState} = 0;
  1019. if (defined($json->{actionGroup}[0]{triggerId})) {
  1020. $hash->{inTriggerId} = $json->{actionGroup}[0]{triggerId};
  1021. } else {
  1022. $hash->{inTriggerId} = "undefined";
  1023. }
  1024. }
  1025. if (defined($json->{events}) && defined($hash->{IODev}))
  1026. {
  1027. tahoma_parseGetEvents($hash->{IODev},$json->{events})
  1028. }
  1029. }
  1030. sub tahoma_parseLaunchActionGroup($$)
  1031. {
  1032. my($hash, $json) = @_;
  1033. my $name = $hash->{NAME};
  1034. Log3 $name, 4, "$name: tahoma_parseLaunchActionGroup";
  1035. if (defined $json->{actionGroup})
  1036. {
  1037. $hash->{inExecState} = 0;
  1038. if (defined($json->{actionGroup}[0]{execId})) {
  1039. $hash->{inExecId} = $json->{actionGroup}[0]{execId};
  1040. } else {
  1041. $hash->{inExecId} = "undefined";
  1042. }
  1043. }
  1044. if (defined($json->{events}) && defined($hash->{IODev}))
  1045. {
  1046. tahoma_parseGetEvents($hash->{IODev},$json->{events})
  1047. }
  1048. }
  1049. sub tahoma_parseCancelExecutions($$)
  1050. {
  1051. my($hash, $json) = @_;
  1052. my $name = $hash->{NAME};
  1053. Log3 $name, 4, "$name: tahoma_parseCancelExecutions";
  1054. }
  1055. sub tahoma_Get($$@)
  1056. {
  1057. my ($hash, $name, $cmd) = @_;
  1058. my $list;
  1059. if( $hash->{SUBTYPE} eq "DEVICE" ) {
  1060. $list = "updateAll:noArg";
  1061. if( $cmd eq "updateAll" ) {
  1062. my ($seconds) = gettimeofday();
  1063. $hash->{refreshStateTimer} = $seconds;
  1064. return undef;
  1065. }
  1066. } elsif( $hash->{SUBTYPE} eq "SCENE"
  1067. || $hash->{SUBTYPE} eq "PLACE" ) {
  1068. $list = "";
  1069. } elsif( $hash->{SUBTYPE} eq "ACCOUNT" ) {
  1070. $list = "devices:noArg reset:noArg";
  1071. if( $cmd eq "devices" ) {
  1072. my $devices = tahoma_getDevices($hash,0);
  1073. my $ret;
  1074. foreach my $device (@{$devices}) {
  1075. $ret .= "$device->{deviceURL}\t".$device->{label}."\t$device->{uiClass}\t$device->{controllable}\t\n" if ($device->{deviceURL});
  1076. $ret .= "$device->{oid}\t".$device->{label}."\n" if ($device->{oid});
  1077. }
  1078. $ret = "id\t\t\t\tname\t\t\tuiClass\t\tcontrollable\n" . $ret if( $ret );
  1079. $ret = "no devices found" if( !$ret );
  1080. return $ret;
  1081. }
  1082. elsif( $cmd eq "reset" ) {
  1083. HttpUtils_Close($hash);
  1084. $hash->{logged_in} = undef;
  1085. return "connection closed";
  1086. }
  1087. }
  1088. return "Unknown argument $cmd, choose one of $list";
  1089. }
  1090. sub tahoma_Set($$@)
  1091. {
  1092. my ($hash, $name, $cmd, $val) = @_;
  1093. my $list = "";
  1094. if( $hash->{SUBTYPE} eq "DEVICE" ||
  1095. $hash->{SUBTYPE} eq "PLACE" ) {
  1096. $list = "dim:slider,0,1,100 setClosure open:noArg close:noArg my:noArg stop:noArg cancel:noArg";
  1097. $list = $hash->{COMMANDS} if (defined $hash->{COMMANDS});
  1098. if( $cmd eq "cancel" ) {
  1099. tahoma_cancelExecutions($hash);
  1100. return undef;
  1101. }
  1102. $cmd = "setClosure" if( $cmd eq "dim" );
  1103. my @commands = split(" ",$list);
  1104. foreach my $command (@commands)
  1105. {
  1106. if( $cmd eq (split(":",$command))[0])
  1107. {
  1108. tahoma_applyRequest($hash,$cmd,$val);
  1109. return undef;
  1110. }
  1111. }
  1112. }
  1113. if( $hash->{SUBTYPE} eq "SCENE") {
  1114. $list = "start:noArg startAt cancel:noArg";
  1115. if( $cmd eq "start" ) {
  1116. tahoma_launchActionGroup($hash);
  1117. return undef;
  1118. }
  1119. if( $cmd eq "startAt" ) {
  1120. tahoma_scheduleActionGroup($hash,$val);
  1121. return undef;
  1122. }
  1123. if( $cmd eq "cancel" ) {
  1124. tahoma_cancelExecutions($hash);
  1125. return undef;
  1126. }
  1127. }
  1128. if( $hash->{SUBTYPE} eq "ACCOUNT") {
  1129. $list = "cancel:noArg";
  1130. if( $cmd eq "cancel" ) {
  1131. tahoma_cancelExecutions($hash);
  1132. return undef;
  1133. }
  1134. }
  1135. return "Unknown argument $cmd, choose one of $list";
  1136. }
  1137. sub tahoma_Attr($$$)
  1138. {
  1139. my ($cmd, $name, $attrName, $attrVal) = @_;
  1140. my $orig = $attrVal;
  1141. if( $attrName eq "interval" ) {
  1142. my $hash = $defs{$name};
  1143. $attrVal = int($attrVal);
  1144. $attrVal = 60*5 if ($attrVal < 60*5 && $attrVal != 0);
  1145. $hash->{INTERVAL} = $attrVal;
  1146. } elsif( $attrName eq "disable" ) {
  1147. my $hash = $defs{$name};
  1148. RemoveInternalTimer($hash);
  1149. if( $cmd eq "set" && $attrVal ne "0" ) {
  1150. } else {
  1151. $attr{$name}{$attrName} = 0;
  1152. }
  1153. } elsif( $attrName eq "blocking" ) {
  1154. my $hash = $defs{$name};
  1155. $hash->{BLOCKING} = $attrVal;
  1156. } elsif( $attrName eq "placeClasses" ) {
  1157. my $hash = $defs{$name};
  1158. $hash->{inClass} = $attrVal if $hash->{SUBTYPE} eq "PLACE";
  1159. }
  1160. elsif ( $attrName eq "levelInvert" ) {
  1161. my $hash = $defs{$name};
  1162. my $device = tahoma_getDeviceDetail( $hash, $hash->{device} );
  1163. $device->{levelInvert} = $attrVal if (defined $device);
  1164. }
  1165. if( $cmd eq "set" ) {
  1166. if( $orig ne $attrVal ) {
  1167. $attr{$name}{$attrName} = $attrVal;
  1168. return $attrName ." set to ". $attrVal;
  1169. }
  1170. }
  1171. return;
  1172. }
  1173. sub tahoma_UserAgent_NonblockingGet($)
  1174. {
  1175. my ($param) = @_;
  1176. my ($hash) = $param->{hash};
  1177. $hash = $hash->{IODev} if (defined ($hash->{IODev}));
  1178. my $name = $hash->{NAME};
  1179. Log3 $name, 5, "$name: tahoma_UserAgent_NonblockingGet page=$param->{page}";
  1180. #"User-Agent":"TaHoma/7980 CFNetwork/758.5.3 Darwin/15.6.0","Proxy-Connection":"keep-alive","Accept":"*/*","Connection":"keep-alive","Content-Length":"49","Accept-Encoding":"gzip, deflate","Content-Type":"application/x-www-form-urlencoded","Accept-Language":"de-de","Host":"www.tahomalink.com"
  1181. $param->{header} = {'User-Agent' => $hash->{userAgent} }; #, 'Accept-Language' => "de-de", 'Accept-Encoding' => "gzip, deflate"};
  1182. $param->{header}{Cookie} = $hash->{HTTPCookies} if ($hash->{HTTPCookies});
  1183. $param->{compress} = 1;
  1184. $param->{keepalive} = 1;
  1185. if (index($hash->{url},'file:') == 0)
  1186. {
  1187. $param->{url} = $hash->{url} . $param->{page} . '.json';
  1188. }
  1189. else
  1190. {
  1191. $param->{url} = $hash->{url} . $param->{page};
  1192. $param->{url} .= $param->{subpage} if ($param->{subpage});
  1193. }
  1194. $hash->{request_active} = 1;
  1195. $hash->{request_time} = time;
  1196. if ($param->{blocking})
  1197. {
  1198. my($err,$data) = HttpUtils_BlockingGet($param);
  1199. $param->{callback}($param, $err, $data, length $data) if($param->{callback});
  1200. }
  1201. else
  1202. {
  1203. my($err,$data) = HttpUtils_NonblockingGet($param);
  1204. }
  1205. }
  1206. sub tahoma_encode_utf8($)
  1207. {
  1208. my ($text) = @_;
  1209. $text =~ s/Ä/Ae/g;
  1210. $text =~ s/Ö/Oe/g;
  1211. $text =~ s/Ü/Ue/g;
  1212. $text =~ s/ä/ae/g;
  1213. $text =~ s/ö/oe/g;
  1214. $text =~ s/ü/ue/g;
  1215. $text =~ s/ß/ss/g;
  1216. return $text;
  1217. }
  1218. sub tahoma_GetCookies($$)
  1219. {
  1220. my ($hash, $header) = @_;
  1221. my $name = $hash->{NAME};
  1222. Log3 $name, 5, "$name: tahoma_GetCookies looking for Cookies in header";
  1223. foreach my $cookie ($header =~ m/set-cookie: ?(.*)/gi) {
  1224. Log3 $name, 5, "$name: Set-Cookie: $cookie";
  1225. $cookie =~ /([^,; ]+)=([^,; ]+)[;, ]*(.*)/;
  1226. Log3 $name, 4, "$name: Cookie: $1 Wert $2 Rest $3";
  1227. $hash->{HTTPCookieHash}{$1}{Value} = $2;
  1228. $hash->{HTTPCookieHash}{$1}{Options} = ($3 ? $3 : "");
  1229. }
  1230. $hash->{HTTPCookies} = join ("; ", map ($_ . "=".$hash->{HTTPCookieHash}{$_}{Value},
  1231. sort keys %{$hash->{HTTPCookieHash}}));
  1232. }
  1233. 1;
  1234. =pod
  1235. =item summary commumication modul for io-homecontrol&reg gateway TaHoma&reg
  1236. =item summary_DE Kommunicationsmodul f&uuml;er io-homecontrol&reg Gateway TaHoma&reg
  1237. =begin html
  1238. <a name="tahoma"></a>
  1239. <h3>tahoma</h3>
  1240. <ul>
  1241. The module realizes the communication with io-homecontrol&reg; Devices e.g. from Somfy&reg; or Velux&reg;<br>
  1242. A registered TaHoma&reg; Connect gateway from Overkiz&reg; sold by Somfy&reg; which is continously connected to the internet is necessary for the module.<br>
  1243. <br><br>
  1244. Notes:
  1245. <ul>
  1246. <li>JSON has to be installed on the FHEM host.</li>
  1247. <li>on problems refer also the fhem forum <a href="https://forum.fhem.de/index.php/topic,28045.0.html">IO-Homecontrol Devices &uuml;ber Tahoma Box einbinden</a></li>
  1248. </ul><br>
  1249. <a name="tahoma_Define"></a>
  1250. <b>Define</b>
  1251. <ul>
  1252. <code>define &lt;name&gt; tahoma ACCOUNT &lt;username&gt; &lt;password&gt;</code><br>
  1253. <code>define &lt;name&gt; tahoma DEVICE &lt;DeviceURL&gt;</code><br>
  1254. <code>define &lt;name&gt; tahoma PLACE &lt;oid&gt;</code><br>
  1255. <code>define &lt;name&gt; tahoma SCENE &lt;oid&gt;</code><br>
  1256. <br>
  1257. <br>
  1258. A definition is only necessary for a tahoma device:<br>
  1259. <code>define &lt;name&gt; tahoma ACCOUNT &lt;username&gt; &lt;password&gt;</code><br>
  1260. <b>If a tahoma device of the type ACCOUNT is created, all other devices acessable by the tahoma gateway are automaticaly created!</b><br>
  1261. If the account is valid, the setup will be read from the server.<br>
  1262. All registrated devices are automatically created with name tahoma_12345 (device number 12345 is used from setup)<br>
  1263. All defined rooms will be are automatically created.<br>
  1264. Also all defined scenes will be automatically created.<br>
  1265. <br>
  1266. <br>
  1267. <b>global Attributes for ACCOUNT:</b>
  1268. <ul>
  1269. If autocreate is disabled, no devices, places and scenes will be created automatically:<br>
  1270. <code>attr autocreate disable</code><br>
  1271. </ul>
  1272. <br>
  1273. <b>local Attributes for ACCOUNT:</b>
  1274. <ul>
  1275. Normally, the web commands will be send asynchron, and this can be forced to wait of the result by blocking=1<br>
  1276. <code>attr tahoma1 blocking 1</code><br>
  1277. </ul>
  1278. <br>
  1279. <b>local Attributes for DEVICE:</b>
  1280. <ul>
  1281. If the closure value 0..100 should be 100..0, the level can be inverted:<br>
  1282. <code>attr tahoma_23234545 levelInvert 1</code><br>
  1283. </ul>
  1284. <br>
  1285. <b>local Attributes for PLACE:</b>
  1286. <ul>
  1287. The commands in a room will only affect the devices in the room with inClass=RollerShutter.<br>
  1288. This can be extend or changed by setting the placeClasses attribut:<br>
  1289. <code>attr tahoma_abc12345 placeClasses RollerShutter ExteriorScreen Window</code><br>
  1290. </ul>
  1291. <br>
  1292. <b>Examples:</b>
  1293. <ul>
  1294. <code>define tahoma1 tahoma ACCOUNT abc@test.com myPassword</code><br>
  1295. <code>attr tahoma1 blocking 0</code><br>
  1296. <code>attr tahoma1 room tahoma</code><br>
  1297. <br>
  1298. <br>Automatic created device e.g.:<br>
  1299. <code>define tahoma_23234545 tahoma DEVICE io://0234-5678-9012/23234545</code><br>
  1300. <code>attr tahoma_23234545 IODev tahoma1</code><br>
  1301. <code>attr tahoma_23234545 alias RollerShutter Badezimmer</code><br>
  1302. <code>attr tahoma_23234545 room tahoma</code><br>
  1303. <code>attr tahoma_23234545 webCmd dim</code><br>
  1304. <br>
  1305. <br>Automatic created place e.g.:<br>
  1306. <code>define tahoma_abc12345 tahoma PLACE abc12345-0a23-0b45-0c67-d5e6f7a1b2c3</code><br>
  1307. <code>attr tahoma_abc12345 IODev tahoma1</code><br>
  1308. <code>attr tahoma_abc12345 alias room Wohnzimmer</code><br>
  1309. <code>attr tahoma_abc12345 room tahoma</code><br>
  1310. <br>
  1311. <br>Automatic created scene e.g.:<br>
  1312. <code>define tahoma_4ef30a23 tahoma SCENE 4ef30a23-0b45-0c67-d5e6-f7a1b2c32e3f</code><br>
  1313. <code>attr tahoma_4ef30a23 IODev tahoma1</code><br>
  1314. <code>attr tahoma_4ef30a23 alias scene Rolladen S&uuml;dfenster zu</code><br>
  1315. <code>attr tahoma_4ef30a23 room tahoma</code><br>
  1316. </ul>
  1317. </ul><br>
  1318. </ul>
  1319. =end html
  1320. =cut