diff --git a/app/Http/Controllers/Main.php b/app/Http/Controllers/Main.php index 7d998af..ab54e98 100755 --- a/app/Http/Controllers/Main.php +++ b/app/Http/Controllers/Main.php @@ -109,13 +109,10 @@ class Main extends Controller // Header("Content-type: image/png"); imagepng($output, $fileName, 9); //to print to screen - - $connector = new FilePrintConnector("/dev/usb/lp0"); - $printer = new Printer($connector); $img = EscposImage::load($fileName, false); - $printer->bitImage($img); + $this->paper->getPrinter()->bitImage($img); - $printer->feed(4); + $this->paper->getPrinter()->feed(4); return back(); } @@ -152,13 +149,15 @@ class Main extends Controller return back(); } - public function printNote($noteId){ + public function printNote($noteId) + { $note = DB::table('note')->where('id', $noteId)->first(); $this->paper->sendPrint($note->topic, $note->text, $note->icon); return $note; } - public function noteLast(){ + public function noteLast() + { $note = DB::table('note')->orderBy('id', 'desc')->first(); $this->printNote($note->id); } diff --git a/app/Paper/HtmlToPos.php b/app/Paper/HtmlToPos.php index 0b2de8e..942f1fe 100644 --- a/app/Paper/HtmlToPos.php +++ b/app/Paper/HtmlToPos.php @@ -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; } - } \ No newline at end of file diff --git a/app/Paper/Paper.php b/app/Paper/Paper.php index 10df14b..80b06b4 100644 --- a/app/Paper/Paper.php +++ b/app/Paper/Paper.php @@ -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); } diff --git a/app/Paper/Traficar.php b/app/Paper/Traficar.php new file mode 100644 index 0000000..42d4bc3 --- /dev/null +++ b/app/Paper/Traficar.php @@ -0,0 +1,60 @@ + 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]; + } +} \ No newline at end of file