Geo-fencing for GPS enabled devices


#1

You can now define a geo-fenced area by drawing a circle, or outlining a path on a map.
Whenever a device enters or leaves a geo-fenced area, InControl will trigger an alert.

After selecting “Geo-Fencing” you can add a new Geo-fence option.
The window below will show up, in which you can add a “Ring” or “Path” to create a “fence”.
This “fence” can then be applied to all or some devices (using “tags”).

When the GPS-enabled Peplink router leaves the geo-fenced area the following actions can be triggered:

Email-notification
-HTTP /HTTPS notification
-Enable / Disable Wi-FI AP


MAX Roters add Geo-fencing capability
Tagging in InControl 2
#2

Hi, I’m trying to setup the geo-fence to use a custom php script on my server.

However when I put variables in the url similar to those in the email setting, I get a message saying:

Please enter a valid URL.

This is what I am trying to accomplish:

http://site.local/geofence.php?device_name={device_name}&entered_or_left={entered_or_left}&fence_name={fence_name}&date_time={date_time}&latitude={latitude}&longitude={longitude}&location=${location}

Or do these variables all get pushed in the full http request?

Thanks


#3

I did a packet sniff on my web server and found that it is doing a HTTP POST with JSON, but it doesn’t have all the same info that the variables do, I would have to parse the data which is ugly… It would be nice if one of those values was the name of the geo fence and not just part of the ‘detail’

{“networkId”:1,
“deviceId”:3,
“datetime”:“2017-04-26T16:06:08”,
“unixtime”:1493237168,
“eventType”:“Geofence”,
“detail”:“BUS_2_282 left the geo-fence mac”,
“longitude”:-73.57382,
“latitude”:45.50302,
“gpsUnixtime”:1493237168,
“result”:“LEFT”,
“conditionType”:“geofence”}


#4

This is awesome!
One though, could you add scheduling to it?
i.e. only send emails between certain hours and email sent if device is not back within a zone with in a certain time limit?
Thank You


#5

Your mentioned JSON file should be received from “HTTP/HTTPS Notifications” enabled in the Group Settings page. But you should rather enable the “HTTP/HTTPS Notification” field in a gen-fence profile. The JSON file posted will contain device name and geo-fence name.

The events posted from the group settings’ “HTTP/HTTPS Notifications” will be revised so that geo-fencing events will no longer be included. Only Device, WAN, PepVPN, IPsec status and AirProbe Alarms will be posted.


#6

We will review if a Silence Period setting could be implemented in the next release.

Currently a geo-fence event will not be triggered if a device has left or entered a zone for less than 30 secs. This is to avoid false alerts generated from momentary GPS errors in poor reception areas.


#7

We have changed the notification data format to below fields, hopefully it will fit your needs

{
“organization_id”: “”,
“fence_name”: “”,
“sn”: “”,
“device_name”: “”,
“device_url”: “”,
“event_type”: “Geofence”,
“longitude”: 0,
“latitude”: 0,
“gps_unixtime”: 0,
“entered_or_left”: “entered”,
“date_time”: “1970-01-01T00:00:00”,
“unixtime”: 0
}


#8

Wow awesome I see the new data coming in already! Now I just have to finish writing my script! :slight_smile:


#9

I thought I would share my code with you all to parse the post data via php into a mysql database:

table geo_fence

CREATE TABLE `geo_fence` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	`device_name` VARCHAR(50) NOT NULL,
	`entered_or_left` VARCHAR(50) NOT NULL,
	`fence_name` VARCHAR(50) NOT NULL,
	`date_time` DATETIME NOT NULL,
	`latitude` DECIMAL(10,8) NULL DEFAULT NULL,
	`longitude` DECIMAL(10,8) NULL DEFAULT NULL,
	PRIMARY KEY (`id`)
);

table geo_fence_live

CREATE TABLE `geo_fence_live` (
	`id` INT(11) NOT NULL AUTO_INCREMENT,
	`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
	`device_name` VARCHAR(50) NOT NULL,
	`entered_or_left` VARCHAR(50) NOT NULL,
	`fence_name` VARCHAR(50) NOT NULL,
	`date_time` DATETIME NOT NULL,
	`latitude` DECIMAL(10,8) NULL DEFAULT NULL,
	`longitude` DECIMAL(10,8) NULL DEFAULT NULL,
	PRIMARY KEY (`id`)
);

mysql.php

<?php

// MYSQL Config
$mysql_server           = "localhost" ;
$mysql_user             = "tracker" ;
$mysql_pass             = "changeme" ;
$mysql_db               = "tracker" ;
$mysql_table_geo        = "geo_fence";
$mysql_table_geo_live   = "geo_fence_live";

?>

geo_fence.php

<?php

ini_set('display_errors', 'On');

// If you want to see output on the HTTP return turn this into a 1
$output_debug = 0;

// MYSQL database/username/password configuration
require("config/mysql.php");

// Make sure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
    throw new Exception('Error');
}

// Make sure that the content type of the POST request has been set and contains application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strpos($contentType, 'application/json') != 0){
    throw new Exception('Error');
}

// Receive the RAW post data.
$json_data = trim(file_get_contents("php://input"));

// Attempt to decode the incoming RAW post data from JSON.
$data = json_decode($json_data, true);

// If json_decode failed, the JSON is invalid.
if(!is_array($data)){
    throw new Exception('Received content contained invalid JSON!');
}

$device_name            = $data['device_name'];
$entered_or_left        = $data['entered_or_left'];
$fence_name             = $data['fence_name'];
$date_time              = $data['date_time'];
$latitude               = $data['latitude'];
$longitude              = $data['longitude'];

// Insert data into database
geo_mysql_insert($device_name,$entered_or_left,$fence_name,$date_time,$latitude,$longitude);

function geo_mysql_insert ($device_name,$entered_or_left,$fence_name,$date_time,$latitude,$longitude){
        global $mysql_server;
        global $mysql_user;
        global $mysql_pass;
        global $mysql_db;
        global $mysql_table_geo;
        global $mysql_table_geo_live;
        global $output_debug;

        // Opens a connection to a MySQL server
        $conn=new mysqli($mysql_server, $mysql_user, $mysql_pass, $mysql_db);

        // Check connection
        if ($conn->connect_error) {
                die("Database connection failed: ". $conn->connect_error);
        }

        $sql = "INSERT INTO ". $mysql_table_geo ." (
                device_name,
                entered_or_left,
                fence_name,
                date_time,
                latitude,
                longitude
        ) VALUES (
                '$device_name',
                '$entered_or_left',
                '$fence_name',
                '$date_time',
                '$latitude',
                '$longitude'
        )";

        $sql_update = "UPDATE ". $mysql_table_geo_live ."
                SET
                        timestamp=now(),
                        device_name='$device_name',
                        entered_or_left='$entered_or_left',
                        fence_name='$fence_name',
                        date_time='$date_time',
                        latitude='$latitude',
                        longitude='$longitude'
                WHERE
                        device_name='$device_name'
                LIMIT 1
        ";

        if ($output_debug == 1){
                echo "SQL Query:\n$sql\n";
                echo "SQL Update:\n$sql_update\n";
        }

        if ($conn->query($sql) === TRUE) {
                if ($output_debug == 1){
                        echo "New record created successfully\n";
                }
        } else {
                if ($output_debug == 1){
                        echo "Error: " . $sql . "<br>" . $conn->error;
                }
        }

        if ($conn->query($sql_update) === TRUE) {
                if ($output_debug == 1){
                        echo "Record updated successfully\n";
                }
        } else {
                if ($output_debug == 1){
                        echo "Error: " . $sql_update . "<br>" . $conn->error;
                }
        }

        $conn->close();
}

#10

Hi Team,

Is there any way that an SMS notification can also be configured with this, aside from email?


#11

Hi paulbryan,

This is currently not possible.
Please add your request to the “feature request category”.
If there is enough support for this feature, it could be added to our roadmap.