98_GEOFANCY.pm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. ###############################################################################
  2. # $Id: 98_GEOFANCY.pm 14110 2017-04-26 06:54:58Z loredo $
  3. package main;
  4. use strict;
  5. use warnings;
  6. use Data::Dumper;
  7. use Time::Local;
  8. use HttpUtils;
  9. # initialize ##################################################################
  10. sub GEOFANCY_Initialize($) {
  11. my ($hash) = @_;
  12. $hash->{DefFn} = "GEOFANCY_Define";
  13. $hash->{UndefFn} = "GEOFANCY_Undefine";
  14. $hash->{SetFn} = "GEOFANCY_Set";
  15. $hash->{AttrList} = "devAlias disable:0,1 " . $readingFnAttributes;
  16. }
  17. # regular Fn ##################################################################
  18. sub GEOFANCY_Define($$) {
  19. my ( $hash, $def ) = @_;
  20. my @a = split( "[ \t]+", $def, 5 );
  21. return "Usage: define <name> GEOFANCY <infix>"
  22. if ( int(@a) != 3 );
  23. my $name = $a[0];
  24. my $infix = $a[2];
  25. $hash->{fhem}{infix} = $infix;
  26. GEOFANCY_addExtension( $name, "GEOFANCY_CGI", $infix );
  27. readingsBeginUpdate($hash);
  28. readingsBulkUpdate( $hash, "state", "initialized" );
  29. readingsEndUpdate( $hash, 1 );
  30. return undef;
  31. }
  32. sub GEOFANCY_Undefine($$) {
  33. my ( $hash, $name ) = @_;
  34. GEOFANCY_removeExtension( $hash->{fhem}{infix} );
  35. return undef;
  36. }
  37. sub GEOFANCY_Set($@) {
  38. my ( $hash, @a ) = @_;
  39. my $name = $hash->{NAME};
  40. my $state = $hash->{STATE};
  41. Log3 $name, 5, "GEOFANCY $name: called function GEOFANCY_Set()";
  42. return "No Argument given" if ( !defined( $a[1] ) );
  43. my $usage = "Unknown argument " . $a[1] . ", choose one of clear:readings";
  44. # clear
  45. if ( $a[1] eq "clear" ) {
  46. Log3 $name, 2, "GEOFANCY set $name " . $a[1];
  47. if ( $a[2] ) {
  48. # readings
  49. if ( $a[2] eq "readings" ) {
  50. delete $hash->{READINGS};
  51. readingsBeginUpdate($hash);
  52. readingsBulkUpdate( $hash, "state", "clearedReadings" );
  53. readingsEndUpdate( $hash, 1 );
  54. }
  55. }
  56. else {
  57. return "No Argument given, choose one of readings ";
  58. }
  59. }
  60. # return usage hint
  61. else {
  62. return $usage;
  63. }
  64. return undef;
  65. }
  66. # module Fn ####################################################################
  67. sub GEOFANCY_CGI() {
  68. # Locative.app (https://itunes.apple.com/us/app/locative/id725198453?mt=8)
  69. # /$infix?device=UUIDdev&id=UUIDloc&latitude=xx.x&longitude=xx.x&trigger=(enter|exit)
  70. #
  71. # Geofency.app (https://itunes.apple.com/us/app/geofency-time-tracking-automatic/id615538630?mt=8)
  72. # /$infix?id=UUIDloc&name=locName&entry=(1|0)&date=DATE&latitude=xx.x&longitude=xx.x&device=UUIDdev
  73. #
  74. # SMART Geofences.app (https://www.microsoft.com/en-us/store/apps/smart-geofences/9nblggh4rk3k)
  75. # /$infix?device=UUIDdev&name=UUIDloc&latitude=xx.x&longitude=xx.x&type=(Entered|Leaving)&date=DATE
  76. #
  77. my ($request) = @_;
  78. my $hash;
  79. my $name = "";
  80. my $link = "";
  81. my $URI = "";
  82. my $device = "";
  83. my $deviceAlias = "-";
  84. my $id = "";
  85. my $lat = "";
  86. my $long = "";
  87. my $address = "-";
  88. my $entry = "";
  89. my $msg = "";
  90. my $date = "";
  91. my $time = "";
  92. my $locName = "";
  93. # data received
  94. if ( $request =~ m,^(\/[^/]+?)(?:\&|\?|\/\?|\/)(.*)?$, ) {
  95. $link = $1;
  96. $URI = $2;
  97. # get device name
  98. $name = $data{FWEXT}{$link}{deviceName} if ( $data{FWEXT}{$link} );
  99. # return error if no such device
  100. return ( "text/plain; charset=utf-8",
  101. "NOK No GEOFANCY device for webhook $link" )
  102. unless ($name);
  103. # return error if no such device
  104. return ( "text/plain; charset=utf-8", "NOK disabled" )
  105. if ( IsDisabled($name) );
  106. # extract values from URI
  107. my $webArgs;
  108. foreach my $pv ( split( "&", $URI ) ) {
  109. next if ( $pv eq "" );
  110. $pv =~ s/\+/ /g;
  111. $pv =~ s/%([\dA-F][\dA-F])/chr(hex($1))/ige;
  112. my ( $p, $v ) = split( "=", $pv, 2 );
  113. $webArgs->{$p} = trim($v);
  114. }
  115. # validate id
  116. # does not exist in "SMART Geofences.app"
  117. return ( "text/plain; charset=utf-8",
  118. "NOK Expected value for 'id' cannot be empty" )
  119. if ( ( !defined( $webArgs->{id} ) || $webArgs->{id} eq "" )
  120. && !defined( $webArgs->{type} ) );
  121. return ( "text/plain; charset=utf-8",
  122. "NOK No whitespace allowed in id '" . $webArgs->{id} . "'" )
  123. if ( defined( $webArgs->{id} ) && $webArgs->{id} =~ m/(?:\s)/ );
  124. # validate locName
  125. return ( "text/plain; charset=utf-8",
  126. "NOK No whitespace allowed in id '" . $webArgs->{locName} . "'" )
  127. if ( defined( $webArgs->{locName} )
  128. && $webArgs->{locName} =~ m/(?:\s)/ );
  129. # require entry or trigger
  130. return ( "text/plain; charset=utf-8",
  131. "NOK Neither 'entry' nor 'trigger' nor 'type' was specified" )
  132. if ( !defined( $webArgs->{entry} )
  133. && !defined( $webArgs->{trigger} )
  134. && !defined( $webArgs->{type} ) );
  135. # validate entry
  136. return ( "text/plain; charset=utf-8",
  137. "NOK Expected value for 'entry' cannot be empty" )
  138. if ( defined( $webArgs->{entry} ) && $webArgs->{entry} eq "" );
  139. return ( "text/plain; charset=utf-8",
  140. "NOK Value for 'entry' can only be: 1 0" )
  141. if ( defined( $webArgs->{entry} )
  142. && $webArgs->{entry} ne 0
  143. && $webArgs->{entry} ne 1 );
  144. # validate trigger
  145. return ( "text/plain; charset=utf-8",
  146. "NOK Expected value for 'trigger' cannot be empty" )
  147. if ( defined( $webArgs->{trigger} ) && $webArgs->{trigger} eq "" );
  148. return ( "text/plain; charset=utf-8",
  149. "NOK Value for 'trigger' can only be: enter|test exit" )
  150. if ( defined( $webArgs->{trigger} )
  151. && $webArgs->{trigger} ne "enter"
  152. && $webArgs->{trigger} ne "test"
  153. && $webArgs->{trigger} ne "exit" );
  154. # validate type
  155. return ( "text/plain; charset=utf-8",
  156. "NOK Expected value for 'type' cannot be empty" )
  157. if ( defined( $webArgs->{type} ) && $webArgs->{type} eq "" );
  158. return ( "text/plain; charset=utf-8",
  159. "NOK Value for 'type' can only be: Entered Leaving" )
  160. if ( defined( $webArgs->{type} )
  161. && lc( $webArgs->{type} ) ne "entered"
  162. && lc( $webArgs->{type} ) ne "leaving" );
  163. # validate date
  164. return (
  165. "text/plain; charset=utf-8",
  166. "NOK Specified date '"
  167. . $webArgs->{date} . "'"
  168. . " does not match ISO8601 UTC format (1970-01-01T00:00:00Z)"
  169. )
  170. if ( defined( $webArgs->{date} )
  171. && $webArgs->{date} !~
  172. 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/
  173. );
  174. # validate timestamp
  175. return (
  176. "text/plain; charset=utf-8",
  177. "NOK Specified timestamp '"
  178. . $webArgs->{timestamp} . "'"
  179. . " does not seem to be a valid Unix timestamp"
  180. )
  181. if (
  182. defined( $webArgs->{timestamp} )
  183. && ( $webArgs->{timestamp} !~ m/^\d+(\.\d+)?$/
  184. || $webArgs->{timestamp} > time() + 300 )
  185. );
  186. # validate locName
  187. return ( "text/plain; charset=utf-8",
  188. "NOK No whitespace allowed in id '" . $webArgs->{locName} . "'" )
  189. if ( defined( $webArgs->{locName} )
  190. && $webArgs->{locName} =~ m/(?:\s)/ );
  191. # validate LAT
  192. return (
  193. "text/plain; charset=utf-8",
  194. "NOK Specified latitude '"
  195. . $webArgs->{latitude}
  196. . "' has unexpected format"
  197. )
  198. if (
  199. defined $webArgs->{latitude}
  200. && ( $webArgs->{latitude} !~ m/^-?\d+(\.\d+)?$/
  201. || $webArgs->{latitude} < -90
  202. || $webArgs->{latitude} > 90 )
  203. );
  204. # validate LONG
  205. return (
  206. "text/plain; charset=utf-8",
  207. "NOK Specified longitude '"
  208. . $webArgs->{longitude}
  209. . "' has unexpected format"
  210. )
  211. if (
  212. defined $webArgs->{longitude}
  213. && ( $webArgs->{longitude} !~ m/^-?\d+(\.\d+)?$/
  214. || $webArgs->{longitude} < -180
  215. || $webArgs->{longitude} > 180 )
  216. );
  217. # validate device
  218. return ( "text/plain; charset=utf-8",
  219. "NOK Expected value for 'device' cannot be empty" )
  220. if ( !defined( $webArgs->{device} ) || $webArgs->{device} eq "" );
  221. return (
  222. "text/plain; charset=utf-8",
  223. "NOK No whitespace allowed in device '" . $webArgs->{device} . "'"
  224. )
  225. if ( defined( $webArgs->{device} )
  226. && $webArgs->{device} =~ m/(?:\s)/ );
  227. # Locative.app
  228. if ( defined $webArgs->{trigger} ) {
  229. Log3 $name, 5, "GEOFANCY $name: detected data format: Locative.app";
  230. $id = $webArgs->{id};
  231. $entry = $webArgs->{trigger};
  232. $lat = $webArgs->{latitude};
  233. $long = $webArgs->{longitude};
  234. $device = $webArgs->{device};
  235. if ( defined( $webArgs->{timestamp} ) ) {
  236. my ( $sec, $min, $hour, $d, $m, $y ) =
  237. localtime( $webArgs->{timestamp} );
  238. $date = timelocal( $sec, $min, $hour, $d, $m, $y );
  239. }
  240. }
  241. # Geofency.app
  242. elsif ( defined $webArgs->{entry} ) {
  243. Log3 $name, 5, "GEOFANCY $name: detected data format: Geofency.app";
  244. $id = $webArgs->{id};
  245. $locName = $webArgs->{name};
  246. $entry = $webArgs->{entry};
  247. $date = GEOFANCY_ISO8601UTCtoLocal( $webArgs->{date} );
  248. $lat = $webArgs->{latitude};
  249. $long = $webArgs->{longitude};
  250. $address = $webArgs->{address}
  251. if ( defined( $webArgs->{address} ) );
  252. $device = $webArgs->{device};
  253. }
  254. # SMART Geofences.app
  255. elsif ( defined $webArgs->{type} ) {
  256. Log3 $name, 5,
  257. "GEOFANCY $name: detected data format: SMART Geofences.app";
  258. $id = $webArgs->{name};
  259. $locName = $webArgs->{name};
  260. $entry = $webArgs->{type};
  261. $date = GEOFANCY_ISO8601UTCtoLocal( $webArgs->{date} );
  262. $lat = $webArgs->{latitude};
  263. $long = $webArgs->{longitude};
  264. $address = $webArgs->{address}
  265. if ( defined( $webArgs->{address} ) );
  266. $device = $webArgs->{device};
  267. }
  268. else {
  269. return "fatal error";
  270. }
  271. }
  272. # no data received
  273. else {
  274. Log3 undef, 5, "GEOFANCY: No data received";
  275. return ( "text/plain; charset=utf-8", "NOK No data received" );
  276. }
  277. # return error if unknown trigger
  278. return ( "text/plain; charset=utf-8", "$entry NOK" )
  279. if ( lc($entry) ne "enter"
  280. && lc($entry) ne "1"
  281. && lc($entry) ne "exit"
  282. && lc($entry) ne "0"
  283. && lc($entry) ne "test"
  284. && lc($entry) ne "entered"
  285. && lc($entry) ne "leaving" );
  286. $hash = $defs{$name};
  287. # update ROOMMATE devices associated with this device UUID
  288. my $matchingResident = 0;
  289. delete $hash->{ROOMMATES};
  290. foreach my $gdev ( devspec2array("rr_geofenceUUIDs=.+") ) {
  291. next unless ( IsDevice( $gdev, "ROOMMATE" ) );
  292. Log3 $name, 5, "GEOFANCY $name: Checking rr_geofenceUUIDs for $gdev";
  293. my $geofenceUUIDs = AttrVal( $gdev, "rr_geofenceUUIDs", undef );
  294. $hash->{ROOMMATES} .= ",$gdev" if $hash->{ROOMMATES};
  295. $hash->{ROOMMATES} = $gdev if !$hash->{ROOMMATES};
  296. my @UUIDs = split( ',', $geofenceUUIDs );
  297. if (@UUIDs) {
  298. foreach (@UUIDs) {
  299. if ( $_ eq $device ) {
  300. Log3 $name, 4,
  301. "GEOFANCY $name: "
  302. . "Found matching UUID at ROOMMATE device $gdev";
  303. $deviceAlias = $gdev;
  304. $matchingResident = 1;
  305. last;
  306. }
  307. }
  308. }
  309. }
  310. # update GUEST devices associated with this device UUID
  311. delete $hash->{GUESTS};
  312. foreach my $gdev ( devspec2array("rg_geofenceUUIDs=.+") ) {
  313. next unless ( IsDevice( $gdev, "GUEST" ) );
  314. Log3 $name, 5, "GEOFANCY $name: Checking rg_geofenceUUIDs for $gdev";
  315. my $geofenceUUIDs = AttrVal( $gdev, "rg_geofenceUUIDs", undef );
  316. $hash->{GUESTS} .= ",$gdev" if $hash->{GUESTS};
  317. $hash->{GUESTS} = $gdev if !$hash->{GUESTS};
  318. my @UUIDs = split( ',', $geofenceUUIDs );
  319. if (@UUIDs) {
  320. foreach (@UUIDs) {
  321. if ( $_ eq $device ) {
  322. Log3 $name, 4,
  323. "GEOFANCY $name: "
  324. . "Found matching UUID at GUESTS device $gdev";
  325. $deviceAlias = $gdev;
  326. $matchingResident = 1;
  327. last;
  328. }
  329. }
  330. }
  331. }
  332. # Device alias handling
  333. #
  334. delete $hash->{helper}{device_aliases}
  335. if $hash->{helper}{device_aliases};
  336. delete $hash->{helper}{device_names}
  337. if $hash->{helper}{device_names};
  338. my @devices = split( ' ', AttrVal( $name, "devAlias", "" ) );
  339. foreach (@devices) {
  340. my @device = split( ':', $_ );
  341. $hash->{helper}{device_aliases}{ $device[0] } =
  342. $device[1];
  343. $hash->{helper}{device_names}{ $device[1] } =
  344. $device[0];
  345. }
  346. $deviceAlias = $hash->{helper}{device_aliases}{$device}
  347. if ( $hash->{helper}{device_aliases}{$device} && $matchingResident == 0 );
  348. Log3 $name, 4,
  349. "GEOFANCY $name: id=$id name=$locName trig=$entry date=$date lat=$lat long=$long address:$address dev=$device devAlias=$deviceAlias";
  350. Log3 $name, 3,
  351. "GEOFANCY $name: Unknown device UUID $device: Set attribute devAlias for $name or assign $device to any ROOMMATE or GUEST device using attribute r*_geofenceUUIDs"
  352. if ( $deviceAlias eq "-" );
  353. readingsBeginUpdate($hash);
  354. # use date for readings
  355. if ( $date ne "" ) {
  356. $hash->{".updateTime"} = $date;
  357. $hash->{".updateTimestamp"} = FmtDateTime( $hash->{".updateTime"} );
  358. $time = $hash->{".updateTimestamp"};
  359. }
  360. # use local FHEM time
  361. else {
  362. $time = TimeNow();
  363. }
  364. # General readings
  365. readingsBulkUpdate( $hash, "state",
  366. "id:$id trig:$entry date:$date lat:$lat long:$long dev:$device devAlias=$deviceAlias"
  367. );
  368. readingsBulkUpdate( $hash, "lastDeviceUUID", $device );
  369. readingsBulkUpdate( $hash, "lastDevice", $deviceAlias );
  370. # update local device readings if
  371. # - UUID was not assigned to any resident device
  372. # - UUID has a defined devAlias
  373. if ( $matchingResident == 0 && $deviceAlias ne "-" ) {
  374. $id = $locName if ( defined($locName) && $locName ne "" );
  375. readingsBulkUpdate( $hash, "lastArr", $deviceAlias . " " . $id )
  376. if ( lc($entry) eq "enter"
  377. || lc($entry) eq "1"
  378. || lc($entry) eq "entered" );
  379. readingsBulkUpdate( $hash, "lastDep", $deviceAlias . " " . $id )
  380. if ( lc($entry) eq "exit"
  381. || lc($entry) eq "0"
  382. || lc($entry) eq "leaving" );
  383. if ( lc($entry) eq "enter"
  384. || lc($entry) eq "1"
  385. || lc($entry) eq "entered"
  386. || lc($entry) eq "test" )
  387. {
  388. Log3 $name, 4, "GEOFANCY $name: $deviceAlias arrived at $id";
  389. readingsBulkUpdate( $hash, $deviceAlias, "arrived " . $id );
  390. readingsBulkUpdate( $hash, "currLoc_" . $deviceAlias, $id );
  391. readingsBulkUpdate( $hash, "currLocLat_" . $deviceAlias, $lat );
  392. readingsBulkUpdate( $hash, "currLocLong_" . $deviceAlias, $long );
  393. readingsBulkUpdate( $hash, "currLocAddr_" . $deviceAlias,
  394. $address );
  395. readingsBulkUpdate( $hash, "currLocTime_" . $deviceAlias, $time );
  396. }
  397. elsif (lc($entry) eq "exit"
  398. || lc($entry) eq "0"
  399. || lc($entry) eq "leaving" )
  400. {
  401. my $currReading;
  402. my $lastReading;
  403. Log3 $name, 4,
  404. "GEOFANCY $name: $deviceAlias left $id and is in transit";
  405. # backup last known location if not "underway"
  406. $currReading = "currLoc_" . $deviceAlias;
  407. my $currVal = ReadingsVal( $name, $currReading, undef );
  408. if ( $currVal && $currVal ne "underway" ) {
  409. foreach ( 'Loc', 'LocLat', 'LocLong', 'LocAddr' ) {
  410. $currReading = "curr" . $_ . "_" . $deviceAlias;
  411. $lastReading = "last" . $_ . "_" . $deviceAlias;
  412. readingsBulkUpdate( $hash, $lastReading, $currVal );
  413. }
  414. $currReading = "currLocTime_" . $deviceAlias;
  415. readingsBulkUpdate( $hash, "lastLocArr_" . $deviceAlias,
  416. $currVal );
  417. readingsBulkUpdate( $hash, "lastLocDep_" . $deviceAlias,
  418. $time );
  419. }
  420. readingsBulkUpdate( $hash, $deviceAlias, "left " . $id );
  421. readingsBulkUpdate( $hash, "currLoc_" . $deviceAlias, "underway" );
  422. readingsBulkUpdate( $hash, "currLocLat_" . $deviceAlias, "-" );
  423. readingsBulkUpdate( $hash, "currLocLong_" . $deviceAlias, "-" );
  424. readingsBulkUpdate( $hash, "currLocAddr_" . $deviceAlias, "-" );
  425. readingsBulkUpdate( $hash, "currLocTime_" . $deviceAlias, $time );
  426. }
  427. }
  428. readingsEndUpdate( $hash, 1 );
  429. # trigger update of resident device readings
  430. if ( $matchingResident == 1 ) {
  431. my $trigger = 0;
  432. $trigger = 1
  433. if ( lc($entry) eq "enter"
  434. || lc($entry) eq "1"
  435. || lc($entry) eq "entered"
  436. || lc($entry) eq "test" );
  437. $locName = $id if ( $locName eq "" );
  438. RESIDENTStk_SetLocation(
  439. $deviceAlias, $locName, $trigger, $id, $time,
  440. $lat, $long, $address, $device
  441. ) if ( IsDevice( $deviceAlias, "ROOMMATE|GUEST" ) );
  442. }
  443. $msg = lc($entry) . " OK";
  444. $msg .= "\ndevice=$device id=$id lat=$lat long=$long trig=lc($entry)"
  445. if ( lc($entry) eq "test" );
  446. return ( "text/plain; charset=utf-8", $msg );
  447. }
  448. sub GEOFANCY_addExtension($$$) {
  449. my ( $name, $func, $link ) = @_;
  450. my $url = "/$link";
  451. Log3 $name, 2, "Registering GEOFANCY $name for URL $url...";
  452. $data{FWEXT}{$url}{deviceName} = $name;
  453. $data{FWEXT}{$url}{FUNC} = $func;
  454. $data{FWEXT}{$url}{LINK} = $link;
  455. }
  456. sub GEOFANCY_removeExtension($) {
  457. my ($link) = @_;
  458. my $url = "/$link";
  459. my $name = $data{FWEXT}{$url}{deviceName};
  460. Log3 $name, 2, "Unregistering GEOFANCY $name for URL $url...";
  461. delete $data{FWEXT}{$url};
  462. }
  463. sub GEOFANCY_ISO8601UTCtoLocal ($) {
  464. my ($datetime) = @_;
  465. $datetime =~ s/T/ /g if ( defined( $datetime && $datetime ne "" ) );
  466. $datetime =~ s/Z//g if ( defined( $datetime && $datetime ne "" ) );
  467. my (
  468. $date, $time, $y, $m, $d, $hour,
  469. $min, $sec, $hours, $minutes, $seconds, $timestamp
  470. );
  471. ( $date, $time ) = split( ' ', $datetime );
  472. ( $y, $m, $d ) = split( '-', $date );
  473. ( $hour, $min, $sec ) = split( ':', $time );
  474. $m -= 01;
  475. $timestamp = timegm( $sec, $min, $hour, $d, $m, $y );
  476. ( $sec, $min, $hour, $d, $m, $y ) = localtime($timestamp);
  477. $timestamp = timelocal( $sec, $min, $hour, $d, $m, $y );
  478. return $timestamp;
  479. }
  480. 1;
  481. =pod
  482. =item helper
  483. =item summary Geofencing for specific iOS, Android or Windows 10 apps
  484. =item summary_DE Geofencing f&uuml;r spezielle iOS, Android und Windows 10 Apps
  485. =begin html
  486. <p>
  487. <a name="GEOFANCY" id="GEOFANCY"></a>
  488. </p>
  489. <h3>
  490. GEOFANCY
  491. </h3>
  492. <ul>
  493. <li>Provides a webhook receiver for geofencing, e.g. via the following apps:<br>
  494. <br>
  495. </li>
  496. <li>
  497. <a href="https://itunes.apple.com/app/id615538630">Geofency (iOS)</a>
  498. </li>
  499. <li>
  500. <a href="https://itunes.apple.com/app/id725198453">Locative (iOS)</a>
  501. </li>
  502. <li>
  503. <a href="http://www.egigeozone.de">EgiGeoZone (Android)</a>
  504. </li>
  505. <li>
  506. <a href="https://www.microsoft.com/en-us/store/apps/smart-geofences/9nblggh4rk3k">SMART Geofences (Windows 10, Windows 10 Mobile)</a>
  507. </li>
  508. <li>
  509. <p>
  510. Note: GEOFANCY is an extension to <a href="#FHEMWEB">FHEMWEB</a>. You need to install FHEMWEB to use GEOFANCY.
  511. </p><a name="GEOFANCYdefine" id="GEOFANCYdefine"></a> <b>Define</b>
  512. <ul>
  513. <code>define &lt;name&gt; GEOFANCY &lt;infix&gt;</code><br>
  514. <br>
  515. 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>
  516. <br>
  517. Example:
  518. <ul>
  519. <code>define geofancy GEOFANCY geo</code><br>
  520. </ul><br>
  521. The webhook will be reachable at http://hostname:8083/fhem/geo in that case.<br>
  522. <br>
  523. </ul><a name="GEOFANCYset" id="GEOFANCYset"></a> <b>Set</b>
  524. <ul>
  525. <li>
  526. <b>clear</b> &nbsp;&nbsp;readings&nbsp;&nbsp; can be used to cleanup auto-created readings from deprecated devices.
  527. </li>
  528. </ul><br>
  529. <br>
  530. <a name="GEOFANCYattr" id="GEOFANCYattr"></a> <b>Attributes</b><br>
  531. <br>
  532. <ul>
  533. <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>
  534. Separate using <i>blank</i> to rename multiple device UUIDs.<br>
  535. <br>
  536. 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.
  537. </li>
  538. </ul><br>
  539. <br>
  540. <b>Usage information / Hints on Security</b><br>
  541. <br>
  542. <ul>
  543. Likely your FHEM installation is not reachable directly from the internet (good idea!).<br>
  544. 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>
  545. <br>
  546. 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>
  547. <br>
  548. 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>
  549. <br>
  550. To make that reverse proxy available from the internet, just forward the appropriate port via your internet router.<br>
  551. <br>
  552. 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.
  553. </ul><br>
  554. <br>
  555. <b>Integration with Home Automation</b><br>
  556. <br>
  557. <ul>
  558. 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.
  559. </ul>
  560. </li>
  561. </ul>
  562. =end html
  563. =begin html_DE
  564. <p>
  565. <a name="GEOFANCY" id="GEOFANCY"></a>
  566. </p>
  567. <h3>
  568. GEOFANCY
  569. </h3>
  570. <ul>
  571. Eine deutsche Version der Dokumentation ist derzeit nicht vorhanden. Die englische Version ist hier zu finden:
  572. </ul>
  573. <ul>
  574. <a href='http://fhem.de/commandref.html#GEOFANCY'>GEOFANCY</a>
  575. </ul>
  576. =end html_DE
  577. =cut