Nearest traficar inforations.
This commit is contained in:
@@ -73,6 +73,27 @@ class HtmlToPos
|
||||
return $posText;
|
||||
}
|
||||
|
||||
public function traficar($posText)
|
||||
{
|
||||
$data = $posText;
|
||||
if (strstr($posText, '[traficar]')) {
|
||||
$data = [];
|
||||
$traficar = new Traficar();
|
||||
$nearest = $traficar->nearestCar();
|
||||
|
||||
$data['text'] = "Najbliższy traficar:\n" .
|
||||
'Model: ' . $nearest['car']['model'] . "\n" .
|
||||
'Numer boczny: ' . $nearest['car']['orderNumber'] . "\n" .
|
||||
'Rejestracja: ' . $nearest['car']['regNumber'] . "\n" .
|
||||
'Lokalizacja: ' . $nearest['car']['location'] . "\n" .
|
||||
'Odległość: ' . $nearest['distance'] . "km\n";
|
||||
// $data['image'] = $nearest['imageName'];
|
||||
// $data['image'] = 'https://staticmapmaker.com/img/cartodb_placeholder.png';
|
||||
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function convert($html = '')
|
||||
{
|
||||
$posText = $this->handleNewLine($html);
|
||||
@@ -80,7 +101,10 @@ class HtmlToPos
|
||||
$posText = $this->pickRandomElement($posText);
|
||||
$posText = $this->randomNumber($posText);
|
||||
$posText = $this->vocabularyWord($posText);
|
||||
$posText = $this->traficar($posText);
|
||||
|
||||
// print_r($posText);
|
||||
// die();
|
||||
return $posText;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,6 +28,16 @@ class Paper
|
||||
}
|
||||
}
|
||||
|
||||
public function getConnector()
|
||||
{
|
||||
return $this->connector;
|
||||
}
|
||||
|
||||
public function getPrinter()
|
||||
{
|
||||
return $this->printer;
|
||||
}
|
||||
|
||||
|
||||
public function sendHeaderPrint($title)
|
||||
{
|
||||
@@ -42,11 +52,15 @@ class Paper
|
||||
}
|
||||
}
|
||||
|
||||
public function sendImagePrint($image, $imageLocal = true)
|
||||
public function sendImagePrint($image, $imageLocal = true, $icon = true)
|
||||
{
|
||||
if ($image) {
|
||||
if ($imageLocal) {
|
||||
$img = EscposImage::load($this->imageDirectory . basename($image) . '.png');
|
||||
if ($icon) {
|
||||
$img = EscposImage::load($this->imageDirectory . basename($image) . '.png');
|
||||
} else {
|
||||
$img = EscposImage::load($image);
|
||||
}
|
||||
} else { //image not local so then remote image
|
||||
$extension = strtolower(pathinfo($image, PATHINFO_EXTENSION));
|
||||
|
||||
@@ -67,7 +81,13 @@ class Paper
|
||||
$this->sendImagePrint($image, $imageLocal);
|
||||
$this->sendHeaderPrint($title);
|
||||
$htmlToPos = new HtmlToPos();
|
||||
$this->printer->text($htmlToPos->convert($text));
|
||||
$convertedData = $htmlToPos->convert($text);
|
||||
if (is_array($convertedData)) {
|
||||
$this->printer->text($convertedData['text']);
|
||||
} else {
|
||||
$this->printer->text($htmlToPos->convert($text));
|
||||
|
||||
}
|
||||
$this->printer->feed(3);
|
||||
}
|
||||
|
||||
|
||||
60
app/Paper/Traficar.php
Normal file
60
app/Paper/Traficar.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 30.06.2018
|
||||
* Time: 18:29
|
||||
*/
|
||||
|
||||
namespace App\Paper;
|
||||
|
||||
|
||||
class Traficar
|
||||
{
|
||||
|
||||
private $homeCoords = ['lat' => 54.417475, 'lng' => 18.481913];
|
||||
private $staticMapsKey = 'AIzaSyBPb9wXT2BcK8EH_3FFWVc2MLuDprj7YXw';
|
||||
|
||||
/**
|
||||
* @param $lat1
|
||||
* @param $lon1
|
||||
* @param $lat2
|
||||
* @param $lon2
|
||||
* @return float zwracana jest odległość w kilometrach
|
||||
*/
|
||||
private function distance($lat1, $lon1, $lat2, $lon2)
|
||||
{
|
||||
|
||||
$theta = $lon1 - $lon2;
|
||||
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
|
||||
$dist = acos($dist);
|
||||
$dist = rad2deg($dist);
|
||||
$miles = $dist * 60 * 1.1515;
|
||||
return ($miles * 1.609344);
|
||||
}
|
||||
|
||||
|
||||
public function nearestCar()
|
||||
{
|
||||
$trafcarData = json_decode(file_get_contents('https://api.traficar.pl/eaw-rest-api/car?shapeId=5'), 1);
|
||||
$closestCar = [];
|
||||
$closestDistance = 1000;
|
||||
foreach ($trafcarData['cars'] as $car) {
|
||||
|
||||
$distance = $this->distance($this->homeCoords['lat'], $this->homeCoords['lng'], $car['latitude'], $car['longitude']);
|
||||
if ($distance < $closestDistance) {
|
||||
$closestDistance = $distance;
|
||||
$closestCar = $car;
|
||||
}
|
||||
}
|
||||
|
||||
// $imgNoKey = "https://maps.googleapis.com/maps/api/staticmap?center=" . $latHome . "," . $lngHome . "&size=300x300&maptype=roadmap&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0x000000%7C" . $closestCar['latitude'] . "," . $closestCar['longitude'];
|
||||
// $img = "https://maps.googleapis.com/maps/api/staticmap?center=" . $this->homeCoords['lat'] . "," . $this->homeCoords['lng'] . "&size=300x300&maptype=roadmap&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0x000000%7C" . $closestCar['latitude'] . "," . $closestCar['longitude'] . '&key=' . $this->staticMapsKey;
|
||||
// $tmpFile = storage_path() . '/traficar_map.png';
|
||||
// file_put_contents($tmpFile, file_get_contents($img));
|
||||
|
||||
|
||||
return ['car' => $closestCar, 'distance' => round($closestDistance, 2),];
|
||||
// 'imageName' => $tmpFile];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user