From 79c7d6be88c959da5d086e210246c90ceb8f8632 Mon Sep 17 00:00:00 2001 From: jpawlowski Date: Sat, 20 Oct 2018 15:15:48 +0000 Subject: [PATCH] RESIDENTStk: Support for updated GEOFANCY version git-svn-id: https://svn.fhem.de/fhem/trunk@17575 2b470e98-0d58-463d-a4d8-8e2adae1ed80 --- fhem/CHANGED | 5 +- fhem/FHEM/98_GEOFANCY.pm | 46 +++++++++-------- fhem/FHEM/RESIDENTStk.pm | 106 ++++++++++++++++++++++++++++++++------- 3 files changed, 116 insertions(+), 41 deletions(-) diff --git a/fhem/CHANGED b/fhem/CHANGED index 3817d9f98..1b0ff261a 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,9 +1,10 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - change: ROOMMATE, GUEST: Support for updated GEOFANCY version - feature: 98_GEOFANCY: add new reading LocTravDist; add support for - Geofency.app webhook attributes currentLatitude, + Geofency.app webhook attributes radius, currentLatitude, currentLongitude, motion, wifiSSID and wifiBSSID. - New readings: PosBSSID, PosLat, PosHomeDist, PosLocDist, + New readings: PosBSSID, PosLat, PosDistHome, PosDistLoc, PosLong, PosMotion, PosSSID, PosTravDist - bugfix: 98_GEOFANCY: fixed last* readings - feature: 89_FULLY: New commands for sound playback and photo. diff --git a/fhem/FHEM/98_GEOFANCY.pm b/fhem/FHEM/98_GEOFANCY.pm index b0df2d537..f7eb03bd7 100755 --- a/fhem/FHEM/98_GEOFANCY.pm +++ b/fhem/FHEM/98_GEOFANCY.pm @@ -116,8 +116,9 @@ sub GEOFANCY_CGI() { my $date = ""; my $time = ""; my $locName = ""; - my $posLocDist = ""; - my $posHomeDist = ""; + my $radius = ""; + my $posDistLoc = ""; + my $posDistHome = ""; my $locTravDist = ""; my $motion = ""; my $wifiSSID = ""; @@ -314,10 +315,10 @@ 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 "unknown", "stationary", "walking", "running", "automotive", "cycling" ); - my $motion = lc( $webArgs->{motion} ); + my $motionLc = lc( $webArgs->{motion} ); return ( "text/plain; charset=utf-8", "NOK Unknown motion type '" . $webArgs->{motion} . "'" ) - if ( !grep( /^$motion$/, @motions ) ); + if ( !grep( /^$motionLc$/, @motions ) ); } # Locative.app @@ -345,6 +346,8 @@ 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 $date = GEOFANCY_ISO8601UTCtoLocal( $webArgs->{date} ); $lat = $webArgs->{latitude}; $long = $webArgs->{longitude}; + $radius = $webArgs->{radius} + if ( defined( $webArgs->{radius} ) ); $address = $webArgs->{address} if ( defined( $webArgs->{address} ) ); $device = $webArgs->{device}; @@ -493,18 +496,18 @@ 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 if ( $homeLat && $homeLong ) { Debug "$homeLat $homeLong"; if ( $posLat ne "" && $posLong ne "" ) { - $posHomeDist = + $posDistHome = UConv::distance( $posLat, $posLong, $homeLat, $homeLong, 2 ); } elsif ( $lat ne "" && $long ne "" ) { - $posHomeDist = + $posDistHome = UConv::distance( $lat, $long, $homeLat, $homeLong, 2 ); } } # distance between location and position if ( $lat ne "" && $long ne "" && $posLat ne "" && $posLong ne "" ) { - $posLocDist = UConv::distance( $posLat, $posLong, $lat, $long, 2 ); + $posDistLoc = UConv::distance( $posLat, $posLong, $lat, $long, 2 ); } # travelled distance for location @@ -579,15 +582,16 @@ 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 # backup last known position foreach ( - 'PosSSID', 'PosBSSID', 'PosMotion', - 'PosLat', 'PosLong', 'PosHomeDist', - 'PosLocDist', 'PosTravDist', 'LocTravDist' + 'PosSSID', 'PosBSSID', 'PosMotion', 'PosLat', + 'PosLong', 'PosDistHome', 'PosDistLoc', 'PosTravDist', + 'LocTravDist', 'LocRadius' ) { $currReading = "curr" . $_ . "_" . $deviceAlias; $lastReading = "last" . $_ . "_" . $deviceAlias; - $currVal = ReadingsVal( $name, $currReading, "" ); - readingsBulkUpdate( $hash, $lastReading, $currVal ); + $currVal = ReadingsVal( $name, $currReading, undef ); + readingsBulkUpdate( $hash, $lastReading, $currVal ) + if ( defined($currVal) ); } readingsBulkUpdate( $hash, "currPosSSID_" . $deviceAlias, $wifiSSID ); @@ -595,15 +599,17 @@ 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 readingsBulkUpdate( $hash, "currPosMotion_" . $deviceAlias, $motion ); readingsBulkUpdate( $hash, "currPosLat_" . $deviceAlias, $posLat ); readingsBulkUpdate( $hash, "currPosLong_" . $deviceAlias, $posLong ); - readingsBulkUpdate( $hash, "currPosHomeDist_" . $deviceAlias, - $posHomeDist ); - readingsBulkUpdate( $hash, "currPosLocDist_" . $deviceAlias, - $posLocDist ); + readingsBulkUpdate( $hash, "currPosDistHome_" . $deviceAlias, + $posDistHome ); + readingsBulkUpdate( $hash, "currPosDistLoc_" . $deviceAlias, + $posDistLoc ); readingsBulkUpdate( $hash, "currLocTravDist_" . $deviceAlias, $locTravDist ); readingsBulkUpdate( $hash, "currPosTravDist_" . $deviceAlias, $posTravDist ); + readingsBulkUpdate( $hash, "currLocRadius_" . $deviceAlias, $radius ); + if ( lc($entry) eq "enter" || lc($entry) eq "1" || lc($entry) eq "entered" @@ -666,10 +672,10 @@ 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 $locName = $id if ( $locName eq "" ); RESIDENTStk_SetLocation( - $deviceAlias, $locName, $trigger, $id, - $time, $lat, $long, $address, - $device, $posLat, $posLong, $posLocDist, - $motion, $wifiSSID, $wifiBSSID + $deviceAlias, $locName, $trigger, $id, + $time, $lat, $long, $address, + $device, $radius, $posLat, $posLong, $posDistHome, + $posDistLoc, $motion, $wifiSSID, $wifiBSSID ) if ( IsDevice( $deviceAlias, "ROOMMATE|GUEST" ) ); } diff --git a/fhem/FHEM/RESIDENTStk.pm b/fhem/FHEM/RESIDENTStk.pm index 45d2899a4..6e888936e 100644 --- a/fhem/FHEM/RESIDENTStk.pm +++ b/fhem/FHEM/RESIDENTStk.pm @@ -1478,29 +1478,40 @@ sub RESIDENTStk_DurationTimer($;$) { sub RESIDENTStk_SetLocation(@) { my ( - $name, $location, $trigger, $id, $time, - $lat, $long, $address, $device, $posLat, - $posLong, $posLocDist, $motion, $wifiSSID, $wifiBSSID + $name, $location, $trigger, $id, $time, + $lat, $long, $address, $device, $radius, + $posLat, $posLong, $posDistHome, $posDistLoc, $motion, + $wifiSSID, $wifiBSSID ) = @_; - my $hash = $defs{$name}; - my $TYPE = GetType($name); - my $prefix = RESIDENTStk_GetPrefixFromType($name); - my $state = ReadingsVal( $name, "state", "initialized" ); - my $presence = ReadingsVal( $name, "presence", "present" ); - my $currLocation = ReadingsVal( $name, "location", "-" ); - my $currWayhome = ReadingsVal( $name, "wayhome", "0" ); - my $currLat = ReadingsVal( $name, "locationLat", "-" ); - my $currLong = ReadingsVal( $name, "locationLong", "-" ); - my $currAddr = ReadingsVal( $name, "locationAddr", "" ); + my $hash = $defs{$name}; + my $TYPE = GetType($name); + my $prefix = RESIDENTStk_GetPrefixFromType($name); + my $state = ReadingsVal( $name, "state", "initialized" ); + my $presence = ReadingsVal( $name, "presence", "present" ); + my $currLocation = ReadingsVal( $name, "location", "-" ); + my $currWayhome = ReadingsVal( $name, "wayhome", "0" ); + my $currLat = ReadingsVal( $name, "locationLat", "-" ); + my $currLong = ReadingsVal( $name, "locationLong", "-" ); + my $currRadius = ReadingsVal( $name, "locationRadius", "" ); + my $currAddr = ReadingsVal( $name, "locationAddr", "" ); + my $currPosLat = ReadingsVal( $name, "positionLat", "" ); + my $currPosLong = ReadingsVal( $name, "positionLong", "" ); + my $currPosDistHome = ReadingsVal( $name, "positionDistHome", "" ); + my $currPosDistLoc = ReadingsVal( $name, "positionDistLocation", "" ); + my $currPosMotion = ReadingsVal( $name, "positionMotion", "" ); + my $currPosSSID = ReadingsVal( $name, "positionSSID", "" ); + my $currPosBSSID = ReadingsVal( $name, "positionBSSID", "" ); $id = "-" if ( !$id || $id eq "" ); $lat = "-" if ( !$lat || $lat eq "" ); $long = "-" if ( !$long || $long eq "" ); $address = "" if ( !$address ); $time = "" if ( !$time ); $device = "" if ( !$device ); + $posLat = "" if ( !$posLat || $posLat eq "-" ); + $posLong = "" if ( !$posLong || $posLong eq "-" ); Log3 $name, 5, -"$TYPE $name: received location information: id=$id name=$location trig=$trigger date=$time lat=$lat long=$long address:$address device=$device"; +"$TYPE $name: received location information: id=$id name=$location trig=$trigger date=$time lat=$lat long=$long posLat=$posLat posLong=$posLong address:$address device=$device"; my $searchstring; @@ -1522,6 +1533,61 @@ sub RESIDENTStk_SetLocation(@) { readingsBulkUpdate( $hash, "locationPresence", "absent" ) if ( $trigger == 0 ); + # travelled distance for location + my $locTravDist = ""; + if ( $lat ne "" && $long ne "" ) { + my $locLatVal = ReadingsVal( $name, "locationLat", "-" ); + $locLatVal = ReadingsVal( $name, "lastLocationLat", "-" ) + if ( $locLatVal eq "-" ); + my $locLongVal = ReadingsVal( $name, "locationLong", "-" ); + $locLongVal = ReadingsVal( $name, "lastLocationLong", "-" ) + if ( $locLongVal eq "-" ); + + if ( $locLatVal ne "-" && $locLongVal ne "-" ) { + $locTravDist = + UConv::distance( $lat, $long, $locLatVal, $locLongVal, 2 ); + } + } + + # travelled distance for position + my $posTravDist = ""; + if ( $posLat ne "" && $posLong ne "" ) { + my $currPosLatVal = ReadingsVal( $name, "positionLat", "" ); + my $currPosLongVal = ReadingsVal( $name, "positionLong", "" ); + + if ( $currPosLatVal ne "" && $currPosLongVal ne "" ) { + $posTravDist = UConv::distance( $posLat, $posLong, $currPosLatVal, + $currPosLongVal, 2 ); + } + } + + # backup last known position + foreach ( + 'positionLat', 'positionLong', + 'positionDistHome', 'positionDistLocation', + 'positionMotion', 'positionSSID', + 'positionBSSID', 'positionTravDistance', + 'locationTravDistance' + ) + { + my $currReading = $_; + my $lastReading = "last" . $_; + my $currVal = ReadingsVal( $name, $currReading, undef ); + readingsBulkUpdate( $hash, $lastReading, $currVal ) + if ( defined($currVal) ); + } + + # update position based readings + readingsBulkUpdate( $hash, "positionLat", $posLat ); + readingsBulkUpdate( $hash, "positionLong", $posLong ); + readingsBulkUpdate( $hash, "positionDistHome", $posDistHome ); + readingsBulkUpdate( $hash, "positionDistLocation", $posDistLoc ); + readingsBulkUpdate( $hash, "positionMotion", $motion ); + readingsBulkUpdate( $hash, "positionSSID", $wifiSSID ); + readingsBulkUpdate( $hash, "positionBSSID", $wifiBSSID ); + readingsBulkUpdate( $hash, "positionTravDistance", $posTravDist ); + readingsBulkUpdate( $hash, "locationTravDistance", $locTravDist ); + # check for implicit state change # my $stateChange = 0; @@ -1633,15 +1699,17 @@ sub RESIDENTStk_SetLocation(@) { ) { Log3 $name, 5, "$TYPE $name: archiving last known location"; - readingsBulkUpdate( $hash, "lastLocationLat", $currLat ); - readingsBulkUpdate( $hash, "lastLocationLong", $currLong ); - readingsBulkUpdate( $hash, "lastLocationAddr", $currAddr ) + readingsBulkUpdate( $hash, "lastLocationLat", $currLat ); + readingsBulkUpdate( $hash, "lastLocationLong", $currLong ); + readingsBulkUpdate( $hash, "lastLocationRadius", $currRadius ); + readingsBulkUpdate( $hash, "lastLocationAddr", $currAddr ) if ( $currAddr ne "" ); readingsBulkUpdate( $hash, "lastLocation", $currLocation ); } - readingsBulkUpdate( $hash, "locationLat", $lat ); - readingsBulkUpdate( $hash, "locationLong", $long ); + readingsBulkUpdate( $hash, "locationLat", $lat ); + readingsBulkUpdate( $hash, "locationLong", $long ); + readingsBulkUpdate( $hash, "locationRadius", $radius ); if ( $address ne "" ) { readingsBulkUpdate( $hash, "locationAddr", $address );