Merged older paper pi with the newer, with some recent updates.

This commit is contained in:
kplaczek
2018-04-02 22:34:15 +02:00
parent 3c0ebd26ed
commit ad53e0c861
19 changed files with 1071 additions and 160 deletions

78
app/Paper/Airly.php Normal file
View File

@@ -0,0 +1,78 @@
<?php
namespace App\Paper;
class Airly
{
private $apiKey = '8b6d77b2950e4e018b0684912bf7b9ed';
private $stations = ['2210', '2256', '2180'];
private $airlyApi = 'https://airapi.airly.eu/v1';
public function getStations()
{
return $this->stations;
}
function getStationInfo($stationId)
{
return json_decode(file_get_contents(sprintf('%s/sensors/%d?apikey=%s', $this->airlyApi, $stationId, $this->apiKey)), 1);
}
function getStationMeasurements($stationId)
{
return json_decode(file_get_contents(sprintf('%s/sensor/measurements?sensorId=%d&apikey=%s', $this->airlyApi, $stationId, $this->apiKey)), 1);
}
public function getPollutionLevelToText($pollutionLevel)
{
return ["Wspaniałe powietrze! Idealny dzień na aktywność na świeżym powietrzu",
"Dobre powietrze. Możesz bez obaw wyjść na zewnątrz i cieszyć się dniem",
"Bywało lepiej… To nie jest najlepszy dzień na aktywność poza domem",
"Zła jakość powietrza! Lepiej zostań dzisiaj w domu",
"Zła jakość powietrza! Lepiej zostań dzisiaj w domu",
"Bardzo zła jakość powietrza! Zostań dziś w domu"][$pollutionLevel - 1];
}
public function getInformationText($stationId)
{
$stationMeasurements = $this->getStationMeasurements($stationId);
if ($stationMeasurements['currentMeasurements'] === []) {
$dataText = 'Brak aktualnych danych ze stacji';
} else {
$dataText = "Aktualne warunki:" . PHP_EOL;
if (isset($stationMeasurements['currentMeasurements']['temperature'])) {
$dataText .= "Temperatura: " . round((int)$stationMeasurements['currentMeasurements']['temperature'], 0) . "°C" . PHP_EOL;
}
if (isset($stationMeasurements['currentMeasurements']['pressure'])) {
$dataText .= "Ciśnienie: " . round(((float)$stationMeasurements['currentMeasurements']['pressure'] / 100), 2) . "hPa" . PHP_EOL;
}
if (isset($stationMeasurements['currentMeasurements']['humidity'])) {
$dataText .= "Wilgotność: " . round((int)$stationMeasurements['currentMeasurements']['humidity'], 2) . "%" . PHP_EOL . PHP_EOL;
}
if (isset($stationMeasurements['currentMeasurements']['pm1'])) {
$dataText .= "PMI 1: " . (int)$stationMeasurements['currentMeasurements']['pm1'] . "ppm" . PHP_EOL;
}
if (isset($stationMeasurements['currentMeasurements']['pm25'])) {
$dataText .= "PMI 2.5: " . (int)$stationMeasurements['currentMeasurements']['pm25'] . "ppm / " . round((int)$stationMeasurements['currentMeasurements']['pm25'] / 0.25, 0) . '%' . PHP_EOL;
}
if (isset($stationMeasurements['currentMeasurements']['pm10'])) {
$dataText .= "PMI 10: " . (int)$stationMeasurements['currentMeasurements']['pm10'] . "ppm / " . round((int)$stationMeasurements['currentMeasurements']['pm10'] / 0.50, 0) . '%' . PHP_EOL;
}
if (isset($stationMeasurements['currentMeasurements']['airQualityIndex'])) {
$dataText .= "Ogólna jakość powietrza: " . (int)$stationMeasurements['currentMeasurements']['airQualityIndex'] . '/100' . PHP_EOL;
}
if (isset($stationMeasurements['currentMeasurements']['pollutionLevel'])) {
$dataText .= "Stopień zanieczyszczeń: " . (int)$stationMeasurements['currentMeasurements']['pollutionLevel'] . '/6' . PHP_EOL;
$dataText .= $this->getPollutionLevelToText((int)$stationMeasurements['currentMeasurements']['pollutionLevel']);
}
}
return $dataText;
}
}

View File

@@ -13,6 +13,7 @@ class CinemaMultikino implements Cinema
{
$moviePath = 'https://multikino.pl/data/filmswithshowings/' . $cinemaId;
$movieContent = file_get_contents($moviePath);
print_r($movieContent);
$movieJson = json_decode($movieContent, 0);
return $movieJson;
}
@@ -21,6 +22,8 @@ class CinemaMultikino implements Cinema
{
$movieJson = $this->fetchRepertorire($day, $cinemaId);
echo $day;
// print_r($movieJson);
$filmy = [];
foreach ($movieJson->films as $movie) {
$film = [];
@@ -49,6 +52,9 @@ class CinemaMultikino implements Cinema
$filmy[] = $film;
}
}
// print_r($filmy);
die();
return $filmy;
}

View File

@@ -12,17 +12,10 @@ class HtmlToPos
return str_replace(['<br>', '<br/>', '<br />'], "\n", $text);
}
public function convert($html)
public function convert($html = '')
{
$posText = $this->handleNewLine($html);
// print_r($posText);
// die();
return $posText;
}
}

105
app/Paper/Paper.php Normal file
View File

@@ -0,0 +1,105 @@
<?php
namespace App\Paper;
use Illuminate\Support\Facades\DB;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
use App\Paper\HtmlToPos;
use Mike42\Escpos\EscposImage;
use Mockery\Exception;
class Paper
{
private $imageDirectory = 'large/';
private $imageDirectorySmall = 'small/';
private $printer;
private $connector;
public function __construct()
{
try {
$this->connector = new FilePrintConnector("/dev/usb/lp0");
$this->printer = new Printer($this->connector);
}catch (Exception $e){
die($e->getMessage());
}
}
public function sendHeaderPrint($title)
{
if (strlen($title)) {
$this->printer->setDoubleStrike(true);
$this->printer->setJustification(Printer::JUSTIFY_CENTER);
$this->printer->setEmphasis(true);
$this->printer->text($title . "\n\n");
$this->printer->setEmphasis(false);
$this->printer->setJustification(Printer::JUSTIFY_LEFT);
$this->printer->setDoubleStrike(false);
}
}
public function sendImagePrint($image, $imageLocal = true)
{
if ($image) {
if ($imageLocal) {
$img = EscposImage::load($this->imageDirectory . basename($image).'.png');
} else { //image not local so then remote image
$extension = strtolower(pathinfo($image, PATHINFO_EXTENSION));
$tmpFile = storage_path() . '/image_packt.' . $extension;
file_put_contents($tmpFile, file_get_contents($image));
$img = EscposImage::load($tmpFile);
}
$this->printer->setJustification(Printer::JUSTIFY_CENTER);
$this->printer->bitImageColumnFormat($img);
$this->printer->feed(2);
$this->printer->setJustification(Printer::JUSTIFY_LEFT);
}
}
public function sendPrint($title, $text = '', $image = false, $imageLocal = true)
{
$this->sendImagePrint($image, $imageLocal);
$this->sendHeaderPrint($title);
$htmlToPos = new HtmlToPos();
$this->printer->text($htmlToPos->convert($text));
$this->printer->feed(4);
}
/**
*
* pobieranie ikon z bazy w kolejności ilości użyć, ikon z dysku a następnie
* usunięcie pozdbioru ikon z bazy ze wszystkich ikon a następnie dodanie
* ich na początek listy ikon
*
* @return array
*/
public function getIcons()
{
// $icons = array_diff(scandir($this->imageDirectorySmall), ['.', '..']);
$icons = ["address-card-o","anchor","archive-3","at","balance-scale","ban","bar-chart-o","barcode","battery-empty","battery-full","battery-half","battery-quarter","battery-three-quarters","bed","beer","bell-o","bell-slash-o","bicycle","birthday-cake","bolt","bomb","book","bug","building-o","bullhorn","bus","camera","car","chain","chat-2","check","cloud","code","coffee","cog","cutlery","dashboard","database","diamond","dollar","dribbble","envelope-o","envira","exclamation-triangle","female","file-text-o","film","fingerprint","fire-extinguisher","fire","flag-o","flask","floppy-o","folder-o","folder-open-o","frown-o","gamepad","gift","git","glass","graduation-cap","grav","group","hand-o-left","heart-o","home","lemon-o","lightbulb-o","list-alt","location-arrow","lock","male","map-1","map-marker","microchip","money","moon-o","music","paper-plane","paperclip","paw","pencil","phone","pie-chart","piggy-bank","plane","question-circle-o","rocket","search","ship","shopping-cart","smile-o","snowflake-o","steam","subway","success","support","thermometer-2","thumbs-o-down","thumbs-o-up","ticket","times","trash-o","tree","trophy","truck","umbrella","usd","warning","wifi","wpexplorer","wrench","youtube-play"];
$iconsDatabase = [];
$iconsDb = DB::select('SELECT icon FROM note WHERE icon is not null group by icon ORDER BY count(icon) DESC ');
foreach ($iconsDb as $icon) {
$iconsDatabase[] = pathinfo(basename($icon->icon), PATHINFO_FILENAME);
}
return (array_merge($iconsDatabase, array_diff($icons, $iconsDatabase)));
}
public function __destruct()
{
$this->printer->close();
}
}