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

View File

@@ -7,8 +7,10 @@
*/
namespace App\Http\Controllers;
set_time_limit(-1);
use App\Paper\HtmlToPos;
use App\Paper\Airly;
use App\Paper\Paper;
use Illuminate\Support\Facades\DB;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
@@ -19,6 +21,20 @@ use Illuminate\Http\Request;
class Main extends Controller
{
private $paper = null;
const TEMPLATE = 'template';
const NOTE = 'note';
public function __construct()
{
$this->paper = new Paper();
}
/**
* post::/printImage
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function imagePrint(Request $request)
{
@@ -103,89 +119,130 @@ class Main extends Controller
return back();
}
/**
* get:/
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function listView(Request $request)
{
$notes = DB::select('SELECT * FROM note ORDER BY updated_at DESC');
$notes = DB::select('SELECT * FROM note WHERE type = "note" ORDER BY updated_at DESC');
$templates = DB::select('SELECT * FROM note WHERE type = "template" ORDER BY updated_at DESC');
return view('list', [
'notes' => $notes,
'templates' => $templates,
'title' => $request->old('title'),
'text' => $request->old('text')
'text' => $request->old('text'),
'icon_selected' => $request->old('icon'),
'icons' => $this->paper->getIcons()
]);
}
public function form(Request $request)
/**
* get:/airly
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function airly(Request $request)
{
return view('create', [
'title' => $request->old('title'),
'text' => $request->old('text')
]);
$airly = new Airly();
$this->paper->sendImagePrint('cloud.png');
$this->paper->sendHeaderPrint('Jakość powietrza:' . PHP_EOL . date('H:i d-m-Y'));
foreach ($airly->getStations() as $stationId) {
$stationInfo = $airly->getStationInfo($stationId);
$this->paper->sendHeaderPrint($stationInfo['address']['locality'] . ' ' . $stationInfo['address']['route']);
$dataText = $airly->getInformationText($stationId);
$this->paper->sendPrint('', $dataText);
}
return back();
}
/**
* post::/print/{id}
* @param $id
* @return \Illuminate\Http\RedirectResponse
*/
public function printText($id)
{
$note = DB::table('note')->where('id', $id)->first();
$this->sendPrint($note->topic, $note->text);
$this->paper->sendPrint($note->topic, $note->text, $note->icon);
return back();
}
private function templateReplace($input){
return str_replace(['[d]', '[m]', '[y]', '[h]', '[i]', '[s]'],
[date('d'), date('m'), date('Y'), date('H'), date('i'), date('s')],
$input);
}
/**
* get::/edit/{id}/{slug}
* post::/edit/{id}/{slug}
* @param Request $request
* @param $id
* @param $slug
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
*/
public function edit(Request $request, $id, $slug)
{
$note = DB::table('note')->where('id', $id)->first();
if ($request->isMethod('post')) {
if ($request->exists('save')) {
DB::table('note')
->where('id', $note->id)
->update([
'topic' => $request->input('title'),
'topic_slug' => str_slug($request->input('title'), '_'),
'text' => $request->input('text'),
'updated_at' => time(),
]);
if ($request->exists('save') ||
$request->exists('save_template')) {
if ($note->type == $this::TEMPLATE) {
$id = DB::table('note')
->insertGetId([
'topic' => $this->templateReplace($request->input('title')),
'topic_slug' => str_slug($request->input('title'), '_'),
'text' => $this->templateReplace($request->input('text')),
'icon' => $request->input('icon'),
'type' => self::NOTE,
'created_at' => time(),
'updated_at' => time()
]);
$note = DB::table('note')->where('id', $id)->first();
} else {
DB::table('note')
->where('id', $note->id)
->update([
'topic' => $request->input('title'),
'topic_slug' => str_slug($request->input('title'), '_'),
'text' => $request->input('text'),
'icon' => $request->input('icon'),
'updated_at' => time(),
]);
}
} elseif ($request->exists('delete')) {
DB::table('note')
->where('id', $note->id)
->delete();
return redirect()->route('list');
} elseif ($request->exists('print')) {
$this->sendPrint($request->input('title'), $request->input('text'));
$this->paper->sendPrint($request->input('title'), $request->input('text'), $request->input('icon'));
}
return redirect()->route('edit', ['id' => $note->id, 'slug' => $note->topic_slug]);
} else {
return view('edit', [
'title' => $note->topic,
'text' => $note->text,
'icon_selected' => $note->icon,
'type' => $note->type,
'id' => $note->id,
'icons' => $this->paper->getIcons(),
'topic_slug' => $note->topic_slug,
]);
}
}
public function sendPrint($title, $text)
{
$connector = new FilePrintConnector("/dev/usb/lp0");
$printer = new Printer($connector);
// $printer->setFont(Printer::FONT_B);
if (strlen($title)) {
$printer->setDoubleStrike(true);
$printer->setJustification(Printer::JUSTIFY_CENTER);
$printer->setEmphasis(true);
$printer->text($title . "\n\n");
$printer->setEmphasis(false);
$printer->setJustification(Printer::JUSTIFY_LEFT);
$printer->setDoubleStrike(false);
}
$htmlToPos = new HtmlToPos();
$printer->text($htmlToPos->convert($text));
$printer->feed(4);
$printer->close();
}
/**
* post::/
* @param Request $request
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function main(Request $request)
{
if ($request->exists('save')) {
@@ -194,13 +251,28 @@ class Main extends Controller
'topic' => $request->input('title'),
'topic_slug' => str_slug($request->input('title'), '_'),
'text' => $request->input('text'),
'icon' => $request->input('icon'),
'type' => self::NOTE,
'created_at' => time(),
'updated_at' => time()
]);
$note = DB::table('note')->where('id', $id)->first();
return redirect()->route('edit', ['id' => $note->id, 'slug' => $note->topic_slug]);
} elseif ($request->exists('save_template')) {
$id = DB::table('note')
->insertGetId([
'topic' => $request->input('title'),
'topic_slug' => str_slug($request->input('title'), '_'),
'text' => $request->input('text'),
'icon' => $request->input('icon'),
'type' => self::TEMPLATE,
'created_at' => time(),
'updated_at' => time()
]);
$note = DB::table('note')->where('id', $id)->first();
return redirect()->route('edit', ['id' => $note->id, 'slug' => $note->topic_slug]);
} else {
$this->sendPrint($request->input('title'), $request->input('text'));
$this->paper->sendPrint($request->input('title'), $request->input('text'), $request->input('icon'));
return back()->withInput();
}