Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1aee7cdc02 |
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 20:47
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
|
||||
class HideLinesDecorator extends TextConverterDecorator
|
||||
{
|
||||
|
||||
/**
|
||||
* usuwa linijki z tekstu które zaczynają zię od znaku #
|
||||
* @return string
|
||||
*/
|
||||
public function getText(): string
|
||||
{
|
||||
return preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $this->text->getText());
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 19:03
|
||||
*/
|
||||
|
||||
namespace App\Decorators\Interfaces;
|
||||
|
||||
|
||||
interface TextConverterDecoratorInterface
|
||||
{
|
||||
public function getText(): string;
|
||||
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 20:23
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
|
||||
class NewLineDecorator extends TextConverterDecorator
|
||||
{
|
||||
private $replaceElements = ['<br>', '<br />', '<br/>'];
|
||||
|
||||
public function getText(): string
|
||||
{
|
||||
return str_replace($this->replaceElements, "\n", $this->text->getText());
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 20:52
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
|
||||
class RandomElementsDecorator extends TextConverterDecorator
|
||||
{
|
||||
|
||||
/**
|
||||
* poniższy kod wybieraz tekstu fragmenty w podwójnych nawiasach kwadratowych, rozdziela je po przecinkach
|
||||
* następnie losuje element i podmienia go w miejsce oryginalnego fragmentu
|
||||
*/
|
||||
public function getText(): string
|
||||
{
|
||||
$re = "#\[\[(.*?)\]\]#s";
|
||||
$newText = $this->text->getText();
|
||||
preg_match_all($re, $newText, $found, PREG_SET_ORDER, 0);
|
||||
foreach ($found as $elements) {
|
||||
$data = explode(',', $elements[1]);
|
||||
$data = array_filter($data, function ($el) {
|
||||
return strlen(trim($el));
|
||||
});
|
||||
$data = array_values($data);
|
||||
$pos = strpos($newText, $elements[0]);
|
||||
$newText = substr_replace($newText, $data[mt_rand(0, count($data) - 1)], $pos, strlen($elements[0]));
|
||||
}
|
||||
return $newText;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 21:28
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
|
||||
class RandomNumberDecorator extends TextConverterDecorator
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* przeszukiwanie tekstu pod katem zawartości [ liczba, liczba2 ] i zamienianiu tego na randomową liczbę z przedziału
|
||||
* liczba - liczba2
|
||||
* obojętnie czy ujemną czy nie
|
||||
*/
|
||||
public function getText(): string
|
||||
{
|
||||
$text = $this->text->getText();
|
||||
$re = '/\[\s*(-?\d+)\s*\,\s*(-?\d+)\s*\]/';
|
||||
preg_match_all($re, $text, $matches, PREG_SET_ORDER, 0);
|
||||
|
||||
foreach ($matches as $match) {
|
||||
$pos = strpos($text, $match[0]);
|
||||
$text = substr_replace($text, mt_rand($match[1], $match[2]), $pos, strlen($match[0]));
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 20:35
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
|
||||
use App\Decorators\Interfaces\TextConverterDecoratorInterface;
|
||||
|
||||
class Text implements TextConverterDecoratorInterface
|
||||
{
|
||||
|
||||
private $text;
|
||||
|
||||
public function __construct(string $text)
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
|
||||
public function getText(): string
|
||||
{
|
||||
return $this->text;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 21:32
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
|
||||
class TextBreakDecorator extends TextConverterDecorator
|
||||
{
|
||||
|
||||
private $lineLength = 32;
|
||||
|
||||
/**
|
||||
* łamie tekst w miejscu spacji do długości 32 znaki na linię
|
||||
* @return string
|
||||
*/
|
||||
public function getText(): string
|
||||
{
|
||||
return wordwrap($this->text->getText(), $this->lineLength);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 19:06
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
use App\Decorators\Interfaces\TextConverterDecoratorInterface;
|
||||
|
||||
abstract class TextConverterDecorator implements TextConverterDecoratorInterface
|
||||
{
|
||||
protected $text;
|
||||
|
||||
public function __construct(TextConverterDecoratorInterface $text)
|
||||
{
|
||||
$this->text = $text;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 22:20
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
|
||||
use App\Paper\Traficar;
|
||||
|
||||
class TraficarDecorator extends TextConverterDecorator
|
||||
{
|
||||
|
||||
public function getText(): string
|
||||
{
|
||||
$text = $this->text->getText();
|
||||
if (strstr($text, '[traficar]')) {
|
||||
$traficar = new Traficar();
|
||||
$nearest = $traficar->nearestCarText();
|
||||
$text = str_replace('[traficar]', $nearest, $text);
|
||||
}
|
||||
return $text;
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 28.02.2019
|
||||
* Time: 22:20
|
||||
*/
|
||||
|
||||
namespace App\Decorators;
|
||||
|
||||
|
||||
use App\Paper\Vocabulary;
|
||||
|
||||
class VocabularyWordDecorator extends TextConverterDecorator
|
||||
{
|
||||
|
||||
public function getText(): string
|
||||
{
|
||||
$text = $this->text->getText();
|
||||
if (strstr($text, '[vocabulary_word]')
|
||||
|| strpos($text, '[vocabulary_short]')
|
||||
|| strpos($text, '[vocabulary_long]')) {
|
||||
$voc = new Vocabulary();
|
||||
$randomWord = $voc->getRandomWord();
|
||||
$word = $voc->getWord($randomWord);
|
||||
|
||||
$text = str_replace('[vocabulary_word]', $word->word, $text);
|
||||
$text = str_replace('[vocabulary_short]', $word->short, $text);
|
||||
$text = str_replace('[vocabulary_long]', $word->long, $text);
|
||||
}
|
||||
|
||||
return $text;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Paper\InteractiveMenu;
|
||||
use App\Paper\Paper;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -18,6 +19,14 @@ class Keyboard extends Controller
|
||||
|
||||
$keyboardAction = explode('_', $keyboardData->action);
|
||||
|
||||
$redis = new \Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
if($keyboardAction[0] == 'interactivemenu' || $redis->get('interactivemenu')){
|
||||
$interactiveMenu = new InteractiveMenu();
|
||||
$interactiveMenu->pressed($key);
|
||||
return;
|
||||
}
|
||||
|
||||
switch ($keyboardAction[0]) {
|
||||
case 'settings':
|
||||
$settings = new Settings();
|
||||
|
||||
@@ -230,7 +230,7 @@ class Main extends Controller
|
||||
'topic_slug' => str_slug($request->input('title'), '_'),
|
||||
'text' => $request->input('text'),
|
||||
'icon' => $request->input('icon'),
|
||||
'private' => $request->input('private', 0),
|
||||
'private' => $request->input('private',0),
|
||||
'updated_at' => time(),
|
||||
]);
|
||||
}
|
||||
@@ -274,7 +274,7 @@ class Main extends Controller
|
||||
'text' => $request->input('text'),
|
||||
'icon' => $request->input('icon'),
|
||||
'type' => self::NOTE,
|
||||
'private' => $request->input('private', 0),
|
||||
'private' => $request->input('private',0),
|
||||
'created_at' => time(),
|
||||
'updated_at' => time()
|
||||
]);
|
||||
@@ -288,7 +288,7 @@ class Main extends Controller
|
||||
'text' => $request->input('text'),
|
||||
'icon' => $request->input('icon'),
|
||||
'type' => self::TEMPLATE,
|
||||
'private' => $request->input('private', 0),
|
||||
'private' => $request->input('private',0),
|
||||
'created_at' => time(),
|
||||
'updated_at' => time()
|
||||
]);
|
||||
|
||||
@@ -76,6 +76,9 @@ class Settings extends Controller
|
||||
$actions['settings_list'] = 'Lista zmapowanych klawiszy';
|
||||
|
||||
$actions['separator1'] = 'separator';
|
||||
$actions['interactivemenu'] = 'Interaktywne menu';
|
||||
|
||||
$actions['separator2'] = 'separator';
|
||||
|
||||
$actions['repertoire_all_today'] = 'Repertuar wszystkie kina dzisiaj';
|
||||
$actions['repertoire_all_tomorrow'] = 'Repertuar wszystkie kina jutro';
|
||||
@@ -92,7 +95,7 @@ class Settings extends Controller
|
||||
$actions['repertoire_gdynskiecentrumfilmowe_today'] = 'Repertuar Gdyńskie Centrum filmowe dzisiaj';
|
||||
$actions['repertoire_gdynskiecentrumfilmowe_tomorrow'] = 'Repertuar Gdyńskie Centrum filmowe jutro';
|
||||
|
||||
$actions['separator2'] = 'separator';
|
||||
$actions['separator3'] = 'separator';
|
||||
$actions['airly_all'] = 'Airly wszystkie stacje';
|
||||
$actions['airly_rzeczypospolitej'] = 'Airly (Rzeczypospolitej)';
|
||||
$actions['airly_grunwaldzka'] = 'Airly (Grunwaldzka)';
|
||||
@@ -103,11 +106,11 @@ class Settings extends Controller
|
||||
// $actions['spacex_prev_flight'] = 'Poprzedni lot SpaceX';
|
||||
// $actions['spacex_all_next_summary'] = 'Wszystkie zaplanowane loty SpaceX (skrót)';
|
||||
|
||||
$actions['separator4'] = 'separator';
|
||||
$actions['separator5'] = 'separator';
|
||||
$actions['note_last'] = 'Ostatnia zapisana notatka';
|
||||
|
||||
|
||||
$actions['separator5'] = 'separator';
|
||||
$actions['separator6'] = 'separator';
|
||||
|
||||
$notes = DB::select('SELECT * FROM note WHERE type = "note" ORDER BY updated_at DESC');
|
||||
foreach ($notes as $note) {
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 27.02.2019
|
||||
* Time: 23:41
|
||||
*/
|
||||
|
||||
namespace App\Interfaces;
|
||||
|
||||
|
||||
interface PrinterElementInterface
|
||||
{
|
||||
public function render(): string;
|
||||
}
|
||||
@@ -3,28 +3,113 @@
|
||||
namespace App\Paper;
|
||||
|
||||
|
||||
use App\Decorators\HideLinesDecorator;
|
||||
use App\Decorators\NewLineDecorator;
|
||||
use App\Decorators\RandomElementsDecorator;
|
||||
use App\Decorators\RandomNumberDecorator;
|
||||
use App\Decorators\Text;
|
||||
use App\Decorators\TextBreakDecorator;
|
||||
use App\Decorators\TraficarDecorator;
|
||||
use App\Decorators\VocabularyWordDecorator;
|
||||
|
||||
class HtmlToPos
|
||||
{
|
||||
|
||||
|
||||
private function handleNewLine($text)
|
||||
{
|
||||
return str_replace(['<br>', '<br/>', '<br />'], "\n", $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* usuwa linijki które na początku mają ! albo #
|
||||
* @param $posText
|
||||
* @return string
|
||||
*/
|
||||
private function hideLines($posText): string
|
||||
{
|
||||
return preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $posText);
|
||||
}
|
||||
|
||||
|
||||
private function pickRandomElement($posText)
|
||||
{
|
||||
/**
|
||||
* poniższy kod wybieraz tekstu fragmenty w podwójnych nawiasach kwadratowych, rozdziela je po przecinkach
|
||||
* następnie losuje element i podmienia go w miejsce oryginalnego fragmentu
|
||||
*/
|
||||
$re = "#\[\[(.*?)\]\]#s";
|
||||
preg_match_all($re, $posText, $found, PREG_SET_ORDER, 0);
|
||||
foreach ($found as $elements) {
|
||||
$data = explode(',', $elements[1]);
|
||||
$data = array_filter($data, function ($el) {
|
||||
return strlen(trim($el));
|
||||
});
|
||||
$data = array_values($data);
|
||||
$pos = strpos($posText, $elements[0]);
|
||||
$posText = substr_replace($posText, $data[mt_rand(0, count($data) - 1)], $pos, strlen($elements[0]));
|
||||
}
|
||||
return $posText;
|
||||
}
|
||||
|
||||
private function randomNumber($posText)
|
||||
{
|
||||
|
||||
/**
|
||||
* przeszukiwanie tekstu pod katem zawartości [ liczba, liczba2 ] i zamienianiu tego na randomową liczbę z przedziału
|
||||
* liczba - liczba2
|
||||
* obojętnie czy ujemną czy nie
|
||||
*/
|
||||
$re = '/\[\s*(-?\d+)\s*\,\s*(-?\d+)\s*\]/';
|
||||
preg_match_all($re, $posText, $matches, PREG_SET_ORDER, 0);
|
||||
foreach ($matches as $match) {
|
||||
$pos = strpos($posText, $match[0]);
|
||||
$posText = substr_replace($posText, mt_rand($match[1], $match[2]), $pos, strlen($match[0]));
|
||||
}
|
||||
return $posText;
|
||||
}
|
||||
|
||||
public function vocabularyWord($posText)
|
||||
{
|
||||
if (strstr($posText, '[vocabulary_word]')
|
||||
|| strpos($posText, '[vocabulary_short]')
|
||||
|| strpos($posText, '[vocabulary_long]')) {
|
||||
$voc = new Vocabulary();
|
||||
$randomWord = $voc->getRandomWord();
|
||||
$word = $voc->getWord($randomWord);
|
||||
|
||||
$posText = str_replace('[vocabulary_word]', $word->word, $posText);
|
||||
$posText = str_replace('[vocabulary_short]', $word->short, $posText);
|
||||
$posText = str_replace('[vocabulary_long]', $word->long, $posText);
|
||||
}
|
||||
|
||||
return $posText;
|
||||
}
|
||||
|
||||
public function traficar($posText)
|
||||
{
|
||||
if (strstr($posText, '[traficar]')) {
|
||||
$traficar = new Traficar();
|
||||
$nearest = $traficar->nearestCarText();
|
||||
$posText = str_replace('[traficar]', $nearest, $posText);
|
||||
}
|
||||
return $posText;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* łamie tekst w miejscu spacji do długości 32 znaki na linię
|
||||
* @param $posText
|
||||
* @return string
|
||||
*/
|
||||
public function textBreak($posText)
|
||||
{
|
||||
$posText = wordwrap($posText, 32);
|
||||
return $posText;
|
||||
}
|
||||
|
||||
public function convert($html = '')
|
||||
{
|
||||
$text = new Text($html);
|
||||
$text = new NewLineDecorator($text);
|
||||
$text = new HideLinesDecorator($text);
|
||||
$text = new RandomElementsDecorator($text);
|
||||
$text = new RandomNumberDecorator($text);
|
||||
$text = new VocabularyWordDecorator($text);
|
||||
$text = new TraficarDecorator($text);
|
||||
$text = new TextBreakDecorator($text);
|
||||
return $text->getText();
|
||||
$posText = $this->handleNewLine($html);
|
||||
$posText = $this->hideLines($posText);
|
||||
$posText = $this->pickRandomElement($posText);
|
||||
$posText = $this->randomNumber($posText);
|
||||
$posText = $this->vocabularyWord($posText);
|
||||
$posText = $this->traficar($posText);
|
||||
|
||||
$posText = $this->textBreak($posText);
|
||||
|
||||
return $posText;
|
||||
}
|
||||
}
|
||||
42
app/Paper/InteractiveMenu.php
Normal file
42
app/Paper/InteractiveMenu.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Paper;
|
||||
|
||||
|
||||
class InteractiveMenu
|
||||
{
|
||||
|
||||
private $apiKey;
|
||||
|
||||
private $paper;
|
||||
|
||||
private $redis;
|
||||
|
||||
private $structure = [
|
||||
0 =>
|
||||
[
|
||||
'title' => 'tytuł pierwszego menu',
|
||||
1 => ['name' => 'to jest podtytuł pierwszego menu', 'action' => '', 'type' => 'output'],
|
||||
2 => ['name' => 'to jest podtytuł drugiego menu', 'action' => '', 'type' => 'input'],
|
||||
3 => ['name' => 'to jest podtytuł trzeciego menu', 'action' => '', 'type' => 'outputandinput'],
|
||||
4 => ['name' => 'to jest podtytuł czwartego menu', 'action' => '', 'type' => 'inputandoutput'],
|
||||
]
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->paper = new Paper();
|
||||
$this->redis = new \Redis();
|
||||
$this->redis->connect('127.0.0.1', 6379);
|
||||
$this->redis->setex('interactivemenu', 10, 1);
|
||||
}
|
||||
|
||||
public function pressed($key)
|
||||
{
|
||||
$this->paper->sendPrint($key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,9 @@ 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
|
||||
{
|
||||
@@ -21,7 +23,7 @@ class Paper
|
||||
try {
|
||||
$this->connector = new FilePrintConnector("/dev/usb/lp0");
|
||||
$this->printer = new Printer($this->connector);
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
die($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 27.02.2019
|
||||
* Time: 23:49
|
||||
*/
|
||||
|
||||
namespace App\Paper\PrinterElements;
|
||||
|
||||
|
||||
use App\Interfaces\PrinterElementInterface;
|
||||
|
||||
class PrinterDocument implements PrinterElementInterface
|
||||
{
|
||||
|
||||
private $elements = [];
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
foreach ($this->elements as $element) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function addElement(PrinterElementInterface $element)
|
||||
{
|
||||
$this->elements[] = $element;
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: k
|
||||
* Date: 27.02.2019
|
||||
* Time: 23:45
|
||||
*/
|
||||
|
||||
namespace App\Paper\PrinterElements;
|
||||
|
||||
|
||||
use App\Interfaces\PrinterElementInterface;
|
||||
|
||||
class TitlePrinterElement implements PrinterElementInterface
|
||||
{
|
||||
private $title;
|
||||
|
||||
public function __construct($title)
|
||||
{
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
public function render(): string
|
||||
{
|
||||
return $this->title;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user