98_GEOFANCY.pm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. # $Id: 98_GEOFANCY.pm 13333 2017-02-05 10:45:36Z loredo $
  2. ##############################################################################
  3. #
  4. # 98_GEOFANCY.pm
  5. # An FHEM Perl module to receive geofencing webhooks from geofancy.com.
  6. #
  7. # Copyright by Julian Pawlowski
  8. # e-mail: julian.pawlowski at gmail.com
  9. #
  10. # Based on HTTPSRV from Dr. Boris Neubert
  11. #
  12. # This file is part of fhem.
  13. #
  14. # Fhem is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation, either version 2 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # Fhem is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU General Public License
  25. # along with fhem. If not, see <http://www.gnu.org/licenses/>.
  26. #
  27. ##############################################################################
  28. package main;
  29. use strict;
  30. use warnings;
  31. use vars qw(%data);
  32. use HttpUtils;
  33. use Time::Local;
  34. use Data::Dumper;
  35. sub GEOFANCY_Set($@);
  36. sub GEOFANCY_Define($$);
  37. sub GEOFANCY_Undefine($$);
  38. #########################
  39. sub GEOFANCY_addExtension($$$) {
  40. my ( $name, $func, $link ) = @_;
  41. my $url = "/$link";
  42. Log3 $name, 2, "Registering GEOFANCY $name for URL $url...";
  43. $data{FWEXT}{$url}{deviceName} = $name;
  44. $data{FWEXT}{$url}{FUNC} = $func;
  45. $data{FWEXT}{$url}{LINK} = $link;
  46. }
  47. #########################
  48. sub GEOFANCY_removeExtension($) {
  49. my ($link) = @_;
  50. my $url = "/$link";
  51. my $name = $data{FWEXT}{$url}{deviceName};
  52. Log3 $name, 2, "Unregistering GEOFANCY $name for URL $url...";
  53. delete $data{FWEXT}{$url};
  54. }
  55. ###################################
  56. sub GEOFANCY_Initialize($) {
  57. my ($hash) = @_;
  58. Log3 $hash, 5, "GEOFANCY_Initialize: Entering";
  59. $hash->{SetFn} = "GEOFANCY_Set";
  60. $hash->{DefFn} = "GEOFANCY_Define";
  61. $hash->{UndefFn} = "GEOFANCY_Undefine";
  62. $hash->{AttrList} = "devAlias disable:0,1 " . $readingFnAttributes;
  63. }
  64. ###################################
  65. sub GEOFANCY_Define($$) {
  66. my ( $hash, $def ) = @_;
  67. my @a = split( "[ \t]+", $def, 5 );
  68. return "Usage: define <name> GEOFANCY <infix>"
  69. if ( int(@a) != 3 );
  70. my $name = $a[0];
  71. my $infix = $a[2];
  72. $hash->{fhem}{infix} = $infix;
  73. GEOFANCY_addExtension( $name, "GEOFANCY_CGI", $infix );
  74. readingsBeginUpdate($hash);
  75. readingsBulkUpdate( $hash, "state", "initialized" );
  76. readingsEndUpdate( $hash, 1 );
  77. return undef;
  78. }
  79. ###################################
  80. sub GEOFANCY_Undefine($$) {
  81. my ( $hash, $name ) = @_;
  82. GEOFANCY_removeExtension( $hash->{fhem}{infix} );
  83. return undef;
  84. }
  85. ###################################
  86. sub GEOFANCY_Set($@) {
  87. my ( $hash, @a ) = @_;
  88. my $name = $hash->{NAME};
  89. my $state = $hash->{STATE};
  90. Log3 $name, 5, "GEOFANCY $name: called function GEOFANCY_Set()";
  91. return "No Argument given" if ( !defined( $a[1] ) );
  92. my $usage = "Unknown argument " . $a[1] . ", choose one of clear:readings";
  93. # clear
  94. if ( $a[1] eq "clear" ) {
  95. Log3 $name, 2, "GEOFANCY set $name " . $a[1];
  96. if ( $a[2] ) {
  97. # readings
  98. if ( $a[2] eq "readings" ) {
  99. delete $hash->{READINGS};
  100. readingsBeginUpdate($hash);
  101. readingsBulkUpdate( $hash, "state", "clearedReadings" );
  102. readingsEndUpdate( $hash, 1 );
  103. }
  104. }
  105. else {
  106. return "No Argument given, choose one of readings ";
  107. }
  108. }
  109. # return usage hint
  110. else {
  111. return $usage;
  112. }
  113. return undef;
  114. }
  115. ############################################################################################################
  116. #
  117. # Begin of helper functions
  118. #
  119. ############################################################################################################
  120. ###################################
  121. sub GEOFANCY_CGI() {
  122. # Locative.app (https://itunes.apple.com/us/app/locative/id725198453?mt=8)
  123. # /$infix?device=UUIDdev&id=UUIDloc&latitude=xx.x&longitude=xx.x&trigger=(enter|exit)
  124. #
  125. # Geofency.app (https://itunes.apple.com/us/app/geofency-time-tracking-automatic/id615538630?mt=8)
  126. # /$infix?id=UUIDloc&name=locName&entry=(1|0)&date=DATE&latitude=xx.x&longitude=xx.x&device=UUIDdev
  127. #
  128. # SMART Geofences.app (https://www.microsoft.com/en-us/store/apps/smart-geofences/9nblggh4rk3k)
  129. # /$infix?device=UUIDdev&name=UUIDloc&latitude=xx.x&longitude=xx.x&type=(Entered|Leaving)&date=DATE
  130. #
  131. my ($request) = @_;
  132. my $hash;
  133. my $name = "";
  134. my $link = "";
  135. my $URI = "";
  136. my $device = "";
  137. my $deviceAlias = "-";
  138. my $id = "";
  139. my $lat = "";
  140. my $long = "";
  141. my $address = "-";
  142. my $entry = "";
  143. my $msg = "";
  144. my $date = "";
  145. my $time = "";
  146. my $locName = "";
  147. # data received
  148. if ( $request =~ m,^(\/[^/]+?)(?:\&|\?|\/\?|\/)(.*)?$, ) {
  149. $link = $1;
  150. $URI = $2;
  151. # get device name
  152. $name = $data{FWEXT}{$link}{deviceName} if ( $data{FWEXT}{$link} );
  153. # return error if no such device
  154. return ( "text/plain; charset=utf-8",
  155. "NOK No GEOFANCY device for webhook $link" )
  156. unless ($name);
  157. # return error if no such device
  158. return ( "text/plain; charset=utf-8", "NOK disabled" )
  159. if ( IsDisabled($name) );
  160. # extract values from URI
  161. my $webArgs;
  162. foreach my $pv ( split( "&", $URI ) ) {
  163. next if ( $pv eq "" );
  164. $pv =~ s/\+/ /g;
  165. $pv =~ s/%([\dA-F][\dA-F])/chr(hex($1))/ige;
  166. my ( $p, $v ) = split( "=", $pv, 2 );
  167. $webArgs->{$p} = trim($v);
  168. }
  169. # validate id
  170. # does not exist in "SMART Geofences.app"
  171. return ( "text/plain; charset=utf-8",
  172. "NOK Expected value for 'id' cannot be empty" )
  173. if ( ( !defined( $webArgs->{id} ) || $webArgs->{id} eq "" )
  174. && !defined( $webArgs->{type} ) );
  175. return ( "text/plain; charset=utf-8",
  176. "NOK No whitespace allowed in id '" . $webArgs->{id} . "'" )
  177. if ( defined( $webArgs->{id} ) && $webArgs->{id} =~ m/(?:\s)/ );
  178. # validate locName
  179. return ( "text/plain; charset=utf-8",
  180. "NOK No whitespace allowed in id '" . $webArgs->{locName} . "'" )
  181. if ( defined( $webArgs->{locName} )
  182. && $webArgs->{locName} =~ m/(?:\s)/ );
  183. # require entry or trigger
  184. return ( "text/plain; charset=utf-8",
  185. "NOK Neither 'entry' nor 'trigger' nor 'type' was specified" )
  186. if ( !defined( $webArgs->{entry} )
  187. && !defined( $webArgs->{trigger} )
  188. && !defined( $webArgs->{type} ) );
  189. # validate entry
  190. return ( "text/plain; charset=utf-8",
  191. "NOK Expected value for 'entry' cannot be empty" )
  192. if ( defined( $webArgs->{entry} ) && $webArgs->{entry} eq "" );
  193. return ( "text/plain; charset=utf-8",
  194. "NOK Value for 'entry' can only be: 1 0" )
  195. if ( defined( $webArgs->{entry} )
  196. && $webArgs->{entry} ne 0
  197. && $webArgs->{entry} ne 1 );
  198. # validate trigger
  199. return ( "text/plain; charset=utf-8",
  200. "NOK Expected value for 'trigger' cannot be empty" )
  201. if ( defined( $webArgs->{trigger} ) && $webArgs->{trigger} eq "" );
  202. return ( "text/plain; charset=utf-8",
  203. "NOK Value for 'trigger' can only be: enter|test exit" )
  204. if ( defined( $webArgs->{trigger} )
  205. && $webArgs->{trigger} ne "enter"
  206. && $webArgs->{trigger} ne "test"
  207. && $webArgs->{trigger} ne "exit" );
  208. # validate type
  209. return ( "text/plain; charset=utf-8",
  210. "NOK Expected value for 'type' cannot be empty" )
  211. if ( defined( $webArgs->{type} ) && $webArgs->{type} eq "" );
  212. return ( "text/plain; charset=utf-8",
  213. "NOK Value for 'type' can only be: Entered Leaving" )
  214. if ( defined( $webArgs->{type} )
  215. && lc( $webArgs->{type} ) ne "entered"
  216. && lc( $webArgs->{type} ) ne "leaving" );
  217. # validate date
  218. return (
  219. "text/plain; charset=utf-8",
  220. "NOK Specified date '"
  221. . $webArgs->{date} . "'"
  222. . " does not match ISO8601 UTC format (1970-01-01T00:00:00Z)"
  223. )
  224. if ( defined( $webArgs->{date} )
  225. && $webArgs->{date} !~
  226. m/(19|20)\d\d-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]\.?[0-9]*)Z/
  227. );
  228. # validate timestamp
  229. return (
  230. "text/plain; charset=utf-8",
  231. "NOK Specified timestamp '"
  232. . $webArgs->{timestamp} . "'"
  233. . " does not seem to be a valid Unix timestamp"
  234. )
  235. if (
  236. defined( $webArgs->{timestamp} )
  237. && ( $webArgs->{timestamp} !~ m/^\d+(\.\d+)?$/
  238. || $webArgs->{timestamp} > time() + 300 )
  239. );
  240. # validate locName
  241. return ( "text/plain; charset=utf-8",
  242. "NOK No whitespace allowed in id '" . $webArgs->{locName} . "'" )
  243. if ( defined( $webArgs->{locName} )
  244. && $webArgs->{locName} =~ m/(?:\s)/ );
  245. # validate LAT
  246. return (
  247. "text/plain; charset=utf-8",
  248. "NOK Specified latitude '"
  249. . $webArgs->{latitude}
  250. . "' has unexpected format"
  251. )
  252. if (
  253. defined $webArgs->{latitude}
  254. && ( $webArgs->{latitude} !~ m/^-?\d+(\.\d+)?$/
  255. || $webArgs->{latitude} < -90
  256. || $webArgs->{latitude} > 90 )
  257. );
  258. # validate LONG
  259. return (
  260. "text/plain; charset=utf-8",
  261. "NOK Specified longitude '"
  262. . $webArgs->{longitude}
  263. . "' has unexpected format"
  264. )
  265. if (
  266. defined $webArgs->{longitude}
  267. && ( $webArgs->{longitude} !~ m/^-?\d+(\.\d+)?$/
  268. || $webArgs->{longitude} < -180
  269. || $webArgs->{longitude} > 180 )
  270. );
  271. # validate device
  272. return ( "text/plain; charset=utf-8",
  273. "NOK Expected value for 'device' cannot be empty" )
  274. if ( !defined( $webArgs->{device} ) || $webArgs->{device} eq "" );
  275. return (
  276. "text/plain; charset=utf-8",
  277. "NOK No whitespace allowed in device '" . $webArgs->{device} . "'"
  278. )
  279. if ( defined( $webArgs->{device} )
  280. && $webArgs->{device} =~ m/(?:\s)/ );
  281. # Locative.app
  282. if ( defined $webArgs->{trigger} ) {
  283. Log3 $name, 5, "GEOFANCY $name: detected data format: Locative.app";
  284. $id = $webArgs->{id};
  285. $entry = $webArgs->{trigger};
  286. $lat = $webArgs->{latitude};
  287. $long = $webArgs->{longitude};
  288. $device = $webArgs->{device};
  289. if ( defined( $webArgs->{timestamp} ) ) {
  290. my ( $sec, $min, $hour, $d, $m, $y ) =
  291. localtime( $webArgs->{timestamp} );
  292. $date = timelocal( $sec, $min, $hour, $d, $m, $y );
  293. }
  294. }
  295. # Geofency.app
  296. elsif ( defined $webArgs->{entry} ) {
  297. Log3 $name, 5, "GEOFANCY $name: detected data format: Geofency.app";
  298. $id = $webArgs->{id};
  299. $locName = $webArgs->{name};
  300. $entry = $webArgs->{entry};
  301. $date = GEOFANCY_ISO8601UTCtoLocal( $webArgs->{date} );
  302. $lat = $webArgs->{latitude};
  303. $long = $webArgs->{longitude};
  304. $address = $webArgs->{address}
  305. if ( defined( $webArgs->{address} ) );
  306. $device = $webArgs->{device};
  307. }
  308. # SMART Geofences.app
  309. elsif ( defined $webArgs->{type} ) {
  310. Log3 $name, 5,
  311. "GEOFANCY $name: detected data format: SMART Geofences.app";
  312. $id = $webArgs->{name};
  313. $locName = $webArgs->{name};
  314. $entry = $webArgs->{type};
  315. $date = GEOFANCY_ISO8601UTCtoLocal( $webArgs->{date} );
  316. $lat = $webArgs->{latitude};
  317. $long = $webArgs->{longitude};
  318. $address = $webArgs->{address}
  319. if ( defined( $webArgs->{address} ) );
  320. $device = $webArgs->{device};
  321. }
  322. else {
  323. return "fatal error";
  324. }
  325. }
  326. # no data received
  327. else {
  328. Log3 undef, 5, "GEOFANCY: No data received";
  329. return ( "text/plain; charset=utf-8", "NOK No data received" );
  330. }
  331. # return error if unknown trigger
  332. return ( "text/plain; charset=utf-8", "$entry NOK" )
  333. if ( lc($entry) ne "enter"
  334. && lc($entry) ne "1"
  335. && lc($entry) ne "exit"
  336. && lc($entry) ne "0"
  337. && lc($entry) ne "test"
  338. && lc($entry) ne "entered"
  339. && lc($entry) ne "leaving" );
  340. $hash = $defs{$name};
  341. # update ROOMMATE devices associated with this device UUID
  342. my $matchingResident = 0;
  343. delete $hash->{ROOMMATES};
  344. if ( defined( $modules{ROOMMATE}{defptr} ) ) {
  345. Log3 $name, 5, "GEOFANCY $name: found defptr for ROOMMATE\n"
  346. . Dumper( $modules{ROOMMATE}{defptr} );
  347. while ( my ( $key, $value ) = each %{ $modules{ROOMMATE}{defptr} } ) {
  348. Log3 $name, 5, "GEOFANCY $name: Checking rr_geofenceUUIDs for $key";
  349. my $geofenceUUIDs = AttrVal( $key, "rr_geofenceUUIDs", undef );
  350. next if !$geofenceUUIDs;
  351. Log3 $name, 5,
  352. "GEOFANCY $name: ROOMMATE device $key has assigned UUIDs: $geofenceUUIDs";
  353. $hash->{ROOMMATES} .= ",$key" if $hash->{ROOMMATES};
  354. $hash->{ROOMMATES} = $key if !$hash->{ROOMMATES};
  355. my @UUIDs = split( ',', $geofenceUUIDs );
  356. if (@UUIDs) {
  357. foreach (@UUIDs) {
  358. if ( $_ eq $device ) {
  359. Log3 $name, 4,
  360. "GEOFANCY $name: Found matching UUID at ROOMMATE device $key";
  361. $deviceAlias = $key;
  362. $matchingResident = 1;
  363. last;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. delete $hash->{GUESTS};
  370. # update GUEST devices associated with this device UUID
  371. if ( $matchingResident == 0 && defined( $modules{GUEST}{defptr} ) ) {
  372. while ( my ( $key, $value ) = each %{ $modules{GUEST}{defptr} } ) {
  373. my $geofenceUUIDs = AttrVal( $key, "rg_geofenceUUIDs", undef );
  374. next if !$geofenceUUIDs;
  375. Log3 $name, 5,
  376. "GEOFANCY $name: GUEST device $key has assigned UUIDs: $geofenceUUIDs";
  377. $hash->{GUESTS} .= ",$key" if $hash->{GUESTS};
  378. $hash->{GUESTS} = $key if !$hash->{GUESTS};
  379. my @UUIDs = split( ',', $geofenceUUIDs );
  380. if (@UUIDs) {
  381. foreach (@UUIDs) {
  382. if ( $_ eq $device ) {
  383. Log3 $name, 4,
  384. "GEOFANCY $name: Found matching UUID at GUEST device $key";
  385. $deviceAlias = $key;
  386. $matchingResident = 1;
  387. last;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. # Device alias handling
  394. #
  395. delete $hash->{helper}{device_aliases}
  396. if $hash->{helper}{device_aliases};
  397. delete $hash->{helper}{device_names}
  398. if $hash->{helper}{device_names};
  399. if ( defined( $attr{$name}{devAlias} ) ) {
  400. my @devices = split( ' ', $attr{$name}{devAlias} );
  401. if (@devices) {
  402. foreach (@devices) {
  403. my @device = split( ':', $_ );
  404. $hash->{helper}{device_aliases}{ $device[0] } =
  405. $device[1];
  406. $hash->{helper}{device_names}{ $device[1] } =
  407. $device[0];
  408. }
  409. }
  410. }
  411. $deviceAlias = $hash->{helper}{device_aliases}{$device}
  412. if ( $hash->{helper}{device_aliases}{$device} && $matchingResident == 0 );
  413. Log3 $name, 4,
  414. "GEOFANCY $name: id=$id name=$locName trig=$entry date=$date lat=$lat long=$long address:$address dev=$device devAlias=$deviceAlias";
  415. Log3 $name, 3,
  416. "GEOFANCY $name: Unknown device UUID $device: Set attribute devAlias for $name or assign $device to any ROOMMATE or GUEST device using attribute r*_geofenceUUIDs"
  417. if ( $deviceAlias eq "-" );
  418. readingsBeginUpdate($hash);
  419. # use date for readings
  420. if ( $date ne "" ) {
  421. $hash->{".updateTime"} = $date;
  422. $hash->{".updateTimestamp"} = FmtDateTime( $hash->{".updateTime"} );
  423. $time = $hash->{".updateTimestamp"};
  424. }
  425. # use local FHEM time
  426. else {
  427. $time = TimeNow();
  428. }
  429. # General readings
  430. readingsBulkUpdate( $hash, "state",
  431. "id:$id trig:$entry date:$date lat:$lat long:$long dev:$device devAlias=$deviceAlias"
  432. );
  433. readingsBulkUpdate( $hash, "lastDeviceUUID", $device );
  434. readingsBulkUpdate( $hash, "lastDevice", $deviceAlias );
  435. # update local device readings if
  436. # - UUID was not assigned to any resident device
  437. # - UUID has a defined devAlias
  438. if ( $matchingResident == 0 && $deviceAlias ne "-" ) {
  439. $id = $locName if ( defined($locName) && $locName ne "" );
  440. readingsBulkUpdate( $hash, "lastArr", $deviceAlias . " " . $id )
  441. if ( lc($entry) eq "enter"
  442. || lc($entry) eq "1"
  443. || lc($entry) eq "entered" );
  444. readingsBulkUpdate( $hash, "lastDep", $deviceAlias . " " . $id )
  445. if ( lc($entry) eq "exit"
  446. || lc($entry) eq "0"
  447. || lc($entry) eq "leaving" );
  448. if ( lc($entry) eq "enter"
  449. || lc($entry) eq "1"
  450. || lc($entry) eq "entered"
  451. || lc($entry) eq "test" )
  452. {
  453. Log3 $name, 4, "GEOFANCY $name: $deviceAlias arrived at $id";
  454. readingsBulkUpdate( $hash, $deviceAlias, "arrived " . $id );
  455. readingsBulkUpdate( $hash, "currLoc_" . $deviceAlias, $id );
  456. readingsBulkUpdate( $hash, "currLocLat_" . $deviceAlias, $lat );
  457. readingsBulkUpdate( $hash, "currLocLong_" . $deviceAlias, $long );
  458. readingsBulkUpdate( $hash, "currLocAddr_" . $deviceAlias,
  459. $address );
  460. readingsBulkUpdate( $hash, "currLocTime_" . $deviceAlias, $time );
  461. }
  462. elsif (lc($entry) eq "exit"
  463. || lc($entry) eq "0"
  464. || lc($entry) eq "leaving" )
  465. {
  466. my $currReading;
  467. my $lastReading;
  468. Log3 $name, 4,
  469. "GEOFANCY $name: $deviceAlias left $id and is in transit";
  470. # backup last known location if not "underway"
  471. $currReading = "currLoc_" . $deviceAlias;
  472. if ( defined( $hash->{READINGS}{$currReading}{VAL} )
  473. && $hash->{READINGS}{$currReading}{VAL} ne "underway" )
  474. {
  475. foreach ( 'Loc', 'LocLat', 'LocLong', 'LocAddr' ) {
  476. $currReading = "curr" . $_ . "_" . $deviceAlias;
  477. $lastReading = "last" . $_ . "_" . $deviceAlias;
  478. readingsBulkUpdate( $hash, $lastReading,
  479. $hash->{READINGS}{$currReading}{VAL} )
  480. if ( defined( $hash->{READINGS}{$currReading}{VAL} ) );
  481. }
  482. $currReading = "currLocTime_" . $deviceAlias;
  483. readingsBulkUpdate(
  484. $hash,
  485. "lastLocArr_" . $deviceAlias,
  486. $hash->{READINGS}{$currReading}{VAL}
  487. ) if ( defined( $hash->{READINGS}{$currReading}{VAL} ) );
  488. readingsBulkUpdate( $hash, "lastLocDep_" . $deviceAlias,
  489. $time );
  490. }
  491. readingsBulkUpdate( $hash, $deviceAlias, "left " . $id );
  492. readingsBulkUpdate( $hash, "currLoc_" . $deviceAlias, "underway" );
  493. readingsBulkUpdate( $hash, "currLocLat_" . $deviceAlias, "-" );
  494. readingsBulkUpdate( $hash, "currLocLong_" . $deviceAlias, "-" );
  495. readingsBulkUpdate( $hash, "currLocAddr_" . $deviceAlias, "-" );
  496. readingsBulkUpdate( $hash, "currLocTime_" . $deviceAlias, $time );
  497. }
  498. }
  499. readingsEndUpdate( $hash, 1 );
  500. # trigger update of resident device readings
  501. if ( $matchingResident == 1 ) {
  502. my $trigger = 0;
  503. $trigger = 1
  504. if ( lc($entry) eq "enter"
  505. || lc($entry) eq "1"
  506. || lc($entry) eq "entered"
  507. || lc($entry) eq "test" );
  508. $locName = $id if ( $locName eq "" );
  509. ROOMMATE_SetLocation(
  510. $deviceAlias, $locName, $trigger, $id, $time,
  511. $lat, $long, $address, $device
  512. ) if ( $defs{$deviceAlias}{TYPE} eq "ROOMMATE" );
  513. GUEST_SetLocation(
  514. $deviceAlias, $locName, $trigger, $id, $time,
  515. $lat, $long, $address, $device
  516. ) if ( $defs{$deviceAlias}{TYPE} eq "GUEST" );
  517. }
  518. $msg = lc($entry) . " OK";
  519. $msg .= "\ndevice=$device id=$id lat=$lat long=$long trig=lc($entry)"
  520. if ( lc($entry) eq "test" );
  521. return ( "text/plain; charset=utf-8", $msg );
  522. }
  523. sub GEOFANCY_ISO8601UTCtoLocal ($) {
  524. my ($datetime) = @_;
  525. $datetime =~ s/T/ /g if ( defined( $datetime && $datetime ne "" ) );
  526. $datetime =~ s/Z//g if ( defined( $datetime && $datetime ne "" ) );
  527. my (
  528. $date, $time, $y, $m, $d, $hour,
  529. $min, $sec, $hours, $minutes, $seconds, $timestamp
  530. );
  531. ( $date, $time ) = split( ' ', $datetime );
  532. ( $y, $m, $d ) = split( '-', $date );
  533. ( $hour, $min, $sec ) = split( ':', $time );
  534. $m -= 01;
  535. $timestamp = timegm( $sec, $min, $hour, $d, $m, $y );
  536. ( $sec, $min, $hour, $d, $m, $y ) = localtime($timestamp);
  537. $timestamp = timelocal( $sec, $min, $hour, $d, $m, $y );
  538. return $timestamp;
  539. }
  540. 1;
  541. =pod
  542. =item helper
  543. =item summary Geofencing for specific iOS, Android or Windows 10 apps
  544. =item summary_DE Geofencing f&uuml;r spezielle iOS, Android und Windows 10 Apps
  545. =begin html
  546. <p>
  547. <a name="GEOFANCY" id="GEOFANCY"></a>
  548. </p>
  549. <h3>
  550. GEOFANCY
  551. </h3>
  552. <ul>
  553. <li>Provides a webhook receiver for geofencing, e.g. via the following apps:<br>
  554. <br>
  555. </li>
  556. <li>
  557. <a href="https://itunes.apple.com/app/id615538630">Geofency (iOS)</a>
  558. </li>
  559. <li>
  560. <a href="https://itunes.apple.com/app/id725198453">Locative (iOS)</a>
  561. </li>
  562. <li>
  563. <a href="http://www.egigeozone.de">EgiGeoZone (Android)</a>
  564. </li>
  565. <li>
  566. <a href="https://www.microsoft.com/en-us/store/apps/smart-geofences/9nblggh4rk3k">SMART Geofences (Windows 10, Windows 10 Mobile)</a>
  567. </li>
  568. <li>
  569. <p>
  570. Note: GEOFANCY is an extension to <a href="#FHEMWEB">FHEMWEB</a>. You need to install FHEMWEB to use GEOFANCY.
  571. </p><a name="GEOFANCYdefine" id="GEOFANCYdefine"></a> <b>Define</b>
  572. <ul>
  573. <code>define &lt;name&gt; GEOFANCY &lt;infix&gt;</code><br>
  574. <br>
  575. Defines the webhook server. <code>&lt;infix&gt;</code> is the portion behind the FHEMWEB base URL (usually <code>http://hostname:8083/fhem</code>)<br>
  576. <br>
  577. Example:
  578. <ul>
  579. <code>define geofancy GEOFANCY geo</code><br>
  580. </ul><br>
  581. The webhook will be reachable at http://hostname:8083/fhem/geo in that case.<br>
  582. <br>
  583. </ul><a name="GEOFANCYset" id="GEOFANCYset"></a> <b>Set</b>
  584. <ul>
  585. <li>
  586. <b>clear</b> &nbsp;&nbsp;readings&nbsp;&nbsp; can be used to cleanup auto-created readings from deprecated devices.
  587. </li>
  588. </ul><br>
  589. <br>
  590. <a name="GEOFANCYattr" id="GEOFANCYattr"></a> <b>Attributes</b><br>
  591. <br>
  592. <ul>
  593. <li>devAlias: Mandatory attribute to assign device name alias to an UUID in the format DEVICEUUID:Aliasname (most readings will only be created if devAlias was defined).<br>
  594. Separate using <i>blank</i> to rename multiple device UUIDs.<br>
  595. <br>
  596. Should you be using GEOFANCY together with <a href="#ROOMMATE">ROOMMATE</a> or <a href="#GUEST">GUEST</a> you might consider using attribute r*_geofenceUUIDs directly at those devices instead.
  597. </li>
  598. </ul><br>
  599. <br>
  600. <b>Usage information / Hints on Security</b><br>
  601. <br>
  602. <ul>
  603. Likely your FHEM installation is not reachable directly from the internet (good idea!).<br>
  604. It is recommended to have a reverse proxy like <a href="http://loredo.me/post/116633549315/geeking-out-with-haproxy-on-pfsense-the-ultimate">HAproxy</a>, <a href="http://www.apsis.ch/pound/">Pound</a> or <a href="https://www.varnish-cache.org/">Varnish</a> in front of FHEM where you can make sure access is only possible to a specific URI like /fhem/geo. Apache or Nginx might do as well. However, in case you have Apache or Nginx running already you should still consider one of the named reverse proxies in front of it for fine-grain security configuration.<br>
  605. <br>
  606. You might also want to think about protecting the access by using HTTP Basic Authentication and encryption via TLS/SSL. Using TLS offloading in the reverse proxy software is highly recommended and software like HAproxy provides high control of data flow for TLS.<br>
  607. <br>
  608. Also the definition of a dedicated FHEMWEB instance for that purpose together with <a href="#allowed">allowed</a> might help to restrict FHEM's functionality (e.g. set attributes allowedCommands and allowedDevices to ",". Note that attributes <i>hiddengroup</i> and <i>hiddenroom</i> of FHEMWEB do NOT protect from just guessing/knowing the correct URI but would help tremendously to prevent easy inspection of your FHEM setup.)<br>
  609. <br>
  610. To make that reverse proxy available from the internet, just forward the appropriate port via your internet router.<br>
  611. <br>
  612. The actual solution on how you can securely make your GEOFANCY webhook available to the internet is not part of this documentation and depends on your own skills.
  613. </ul><br>
  614. <br>
  615. <b>Integration with Home Automation</b><br>
  616. <br>
  617. <ul>
  618. You might want to have a look to the module family of <a href="#ROOMMATE">ROOMMATE</a>, <a href="#GUEST">GUEST</a> and <a href="#RESIDENTS">RESIDENTS</a> for an easy processing of GEOFANCY events.
  619. </ul>
  620. </li>
  621. </ul>
  622. =end html
  623. =begin html_DE
  624. <p>
  625. <a name="GEOFANCY" id="GEOFANCY"></a>
  626. </p>
  627. <h3>
  628. GEOFANCY
  629. </h3>
  630. <ul>
  631. Eine deutsche Version der Dokumentation ist derzeit nicht vorhanden. Die englische Version ist hier zu finden:
  632. </ul>
  633. <ul>
  634. <a href='http://fhem.de/commandref.html#GEOFANCY'>GEOFANCY</a>
  635. </ul>
  636. =end html_DE
  637. =cut