比较最后的GPS和当前的GPS数据

时间:2016-01-06 10:23:17

标签: php mysql sockets session caching

我有一个项目,我需要通过发送GPS坐标。每6秒插上一次。坐标存储在MYsql数据库中。我每隔6秒运行一次查询,如果最后一个位置与当前位置不同,则应用程序将数据发送到远程服务器。在浏览器中它就像一个魅力,但在终端我不能使用Sessions。

我试过apc_add,但根据PHP手册,它很久以前被删除了。

这种比较最常见的方法是什么?将最后的坐标存储到数据库或文本文件中?或者有没有办法在运行时间内进行调整?

**这是我的主要代码:**

<?php 
require 'bootstrap.php';

use App\Libs\appServiceProvider;
use App\Libs\socketServiceProvider;
use Socket\Raw\Factory;
use App\Models\Koordinata;

$app = new appServiceProvider;


if (empty($_SESSION['lat']) || empty($_SESSION['lon'])) {
    $_SESSION['lat'] = 0;
    $_SESSION['lon'] = 0;
}

$lastLat = $_SESSION['lat'];
$lastLon = $_SESSION['lon'];

$currentLat = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lat;
$currentLon = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lon;

if ($lastLat != $currentLat && $lastLon != $currentLon) {
    $factory = new Factory();
    $socket = $factory->createClient('REMOTEADDRESSE');
    echo "Kapcsolat létrehozva\n";
    $socket->write("MESSAGE");
    echo "Üzenet elküldve\n";
    var_dump("Válasz: " . $socket->read(8192));
    $socket->close();
} else {
    echo "Idle";
    $log->addDebug("GPS data NOT CHANGED! STATUS IDLE!");
}

$_SESSION['lat'] = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lat;
$_SESSION['lon'] = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lon;

 ?>

1 个答案:

答案 0 :(得分:0)

好的,我用数据库做了,工作正常。这是代码:

<?php 
require 'bootstrap.php';

use App\Libs\appServiceProvider;
use App\Libs\socketServiceProvider;
use Socket\Raw\Factory;
use App\Models\Koordinata;
use App\Models\TempKoordinata;

$app = new appServiceProvider;

$last = TempKoordinata::find(1);
if(!empty($last)) { 
    $lastLat = $last->lat;
    $lastLon = $last->lon;
} else {
    $temp = new TempKoordinata();
        $temp->id = 1;
        $temp->lat = 0;
        $temp->lon = 0;
    $temp->save();
    $log->addDebug('No data to compare! Empty tempCoordinate table! Set values to ZERO!');
}

$currentLat = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lat;
$currentLon = $app->getAllCoordinatesByFszgId($application['fszgId'])->last()->lon;

if ($lastLat != $currentLat && $lastLon != $currentLon) {
    /*$factory = new Factory();
    $socket = $factory->createClient('REMOTE');
    echo "Kapcsolat létrehozva\n";
    $socket->write("MESSAGE");
    echo "Üzenet elküldve\n";
    var_dump("Válasz: " . $socket->read(8192));
    $socket->close();*/
    echo "Sending\n";
} else {
    echo "Idle\n";
    $log->addDebug("GPS data NOT CHANGED! STATUS IDLE!");
}

TempKoordinata::destroy(1);

//Elmentjük a mostani GPS koordinátát
$count = TempKoordinata::all();
//Ha üres az adatbázis akkor elmentjük a koordinátákat
if ($count->count() == 0) {
    $temp = new TempKoordinata();
        $temp->id = 1;
        $temp->lat = $currentLat;
        $temp->lon = $currentLon;
    $temp->save();
} else {
    $log->addDebug("More than one item in the temp table!");
}

 ?>