Working implementation for printing with the API

This commit is contained in:
Marc Ole Bulling 2021-01-15 00:51:22 +01:00
parent 2f86611bb5
commit 1e8bfcab15
No known key found for this signature in database
GPG Key ID: C126AFC2A47B06FF
4 changed files with 39 additions and 35 deletions

30
composer.lock generated
View File

@ -2205,16 +2205,16 @@
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "39d483bdf39be819deabf04ec872eb0b2410b531"
"reference": "f377a3dd1fde44d37b9831d68dc8dea3ffd28e13"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/39d483bdf39be819deabf04ec872eb0b2410b531",
"reference": "39d483bdf39be819deabf04ec872eb0b2410b531",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/f377a3dd1fde44d37b9831d68dc8dea3ffd28e13",
"reference": "f377a3dd1fde44d37b9831d68dc8dea3ffd28e13",
"shasum": ""
},
"require": {
@ -2226,7 +2226,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -2264,23 +2264,20 @@
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.20.0"
},
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T16:49:33+00:00"
},
{
"name": "symfony/polyfill-php80",
"version": "v1.20.0",
"version": "v1.22.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php80.git",
"reference": "e70aa8b064c5b72d3df2abd5ab1e90464ad009de"
"reference": "dc3063ba22c2a1fd2f45ed856374d79114998f91"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/e70aa8b064c5b72d3df2abd5ab1e90464ad009de",
"reference": "e70aa8b064c5b72d3df2abd5ab1e90464ad009de",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/dc3063ba22c2a1fd2f45ed856374d79114998f91",
"reference": "dc3063ba22c2a1fd2f45ed856374d79114998f91",
"shasum": ""
},
"require": {
@ -2289,7 +2286,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.20-dev"
"dev-main": "1.22-dev"
},
"thanks": {
"name": "symfony/polyfill",
@ -2333,10 +2330,7 @@
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-php80/tree/v1.20.0"
},
"time": "2020-10-23T14:02:19+00:00"
"time": "2021-01-07T16:49:33+00:00"
},
{
"name": "symfony/translation",

View File

@ -95,6 +95,18 @@ Setting('MEAL_PLAN_FIRST_DAY_OF_WEEK', '');
// see the file controllers/Users/User.php for possible values
Setting('DEFAULT_PERMISSIONS', ['ADMIN']);
// When using a thermal printer (thermal printers are receipt printers, not regular printers)
// The printer must support the ESC/POS protocol, see https://github.com/mike42/escpos-php
Setting('TPRINTER_IS_NETWORK_PRINTER', false); // Set to true if it is a network printer
//Configuration below for network printers. If you are using a USB/serial printer, skip to next section
Setting('TPRINTER_IP', '127.0.0.1'); // IP of the network printer
Setting('TPRINTER_PORT', 9100); // Port of printer, eg. 9100
//Configuration below if you are using a USB or serial printer
Setting('TPRINTER_CONNECTOR', '/dev/usb/lp0'); // Location of printer. For USB on Linux this is often '/dev/usb/lp0',
// for serial printers it could be similar to '/dev/ttyS0'
// Make sure that the user that runs the webserver has permissions to write to the printer!
// Default user settings
// These settings can be changed per user, here the defaults
// are defined which are used when the user has not changed the setting so far

View File

@ -3,19 +3,12 @@
namespace Grocy\Services;
use Exception;
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
class PrintService extends BaseService {
/**
* Checks if a thermal printer has been configured
* @return bool
*/
private static function isThermalPrinterEnabled(): bool {
//TODO
return true;
}
/**
* Initialises the printer
@ -23,7 +16,11 @@ class PrintService extends BaseService {
* @throws Exception if unable to connect to printer
*/
private static function getPrinterHandle() {
$connector = new FilePrintConnector("php://stdout");
if (GROCY_TPRINTER_IS_NETWORK_PRINTER) {
$connector = new NetworkPrintConnector(GROCY_TPRINTER_IP, GROCY_TPRINTER_PORT);
} else {
$connector = new FilePrintConnector(GROCY_TPRINTER_CONNECTOR);
}
return new Printer($connector);
}
@ -42,7 +39,7 @@ class PrintService extends BaseService {
$printer->setTextSize(1, 1);
$printer->setReverseColors(false);
$printer->selectPrintMode();
$printer->feed(6);
$printer->feed(3);
}
/**
@ -52,9 +49,6 @@ class PrintService extends BaseService {
* @throws Exception
*/
public function printShoppingList(bool $printHeader, array $items): array {
if (!self::isThermalPrinterEnabled()) {
throw new Exception("Printer is not setup enabled in configuration");
}
$printer = self::getPrinterHandle();
if ($printer === false)
throw new Exception("Unable to connect to printer");
@ -72,8 +66,7 @@ class PrintService extends BaseService {
$printer->cut();
$printer->close();
return [
'result' => "OK",
'printed' => $items
'result' => "OK"
];
}
}

View File

@ -1,6 +1,5 @@
<?php
// @formatter:off
namespace Grocy\Services;
class StockService extends BaseService
@ -936,6 +935,12 @@ class StockService extends BaseService
}
}
/**
* Returns the shoppinglist as an array with lines for a printer
* @param int $listId ID of shopping list
* @return string[] Returns an array in the format "[amount] [name of product]"
* @throws \Exception
*/
public function GetShoppinglistInPrintableStrings($listId = 1): array {
if (!$this->ShoppingListExists($listId)) {
throw new \Exception('Shopping list does not exist');
@ -1405,7 +1410,7 @@ class StockService extends BaseService
private function LocationExists($locationId)
{
$locationRow = $this->getDatabase()->locations()->where('id = :1', $locationId)->fetchAll();
$locationRow = $this->getDatabase()->locations()->where('id = :1', $locationId)->fetch();
return $locationRow !== null;
}