| 1 |
<?php |
| 2 |
/** |
| 3 |
* Epson Server Direct Print provider adapter. |
| 4 |
* |
| 5 |
* @package WCPOS\WooCommercePOS\Services\Providers |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WCPOS\WooCommercePOS\Services\Providers; |
| 9 |
|
| 10 |
use WCPOS\WooCommercePOS\Interfaces\Provider_Adapter_Interface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Epson_Sdp_Adapter class. |
| 14 |
*/ |
| 15 |
class Epson_Sdp_Adapter implements Provider_Adapter_Interface { |
| 16 |
/** |
| 17 |
* Return the ePOS-Print XML content type. |
| 18 |
* |
| 19 |
* @return string |
| 20 |
*/ |
| 21 |
public function content_type(): string { |
| 22 |
return 'application/xml'; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Resolve ePOS-Print XML format data. |
| 27 |
* |
| 28 |
* @param array $printer Printer configuration. |
| 29 |
* @param array $template Template configuration. |
| 30 |
* |
| 31 |
* @return array{kind:string, content_type:string} |
| 32 |
*/ |
| 33 |
public function format( array $printer, array $template ): array { |
| 34 |
if ( 'thermal' !== (string) ( $template['engine'] ?? '' ) ) { |
| 35 |
return array( |
| 36 |
'kind' => '', |
| 37 |
'content_type' => '', |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
return array( |
| 42 |
'kind' => 'epos-xml', |
| 43 |
'content_type' => $this->content_type(), |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Build an ePOS-Print XML diagnostic. |
| 49 |
* |
| 50 |
* @param string $printer_name Printer display name. |
| 51 |
* |
| 52 |
* @return array{content_type:string, payload:string} |
| 53 |
*/ |
| 54 |
public function diagnostic( string $printer_name ): array { |
| 55 |
$name = (string) preg_replace( '/[\x00-\x1F\x7F]/', '', $printer_name ); |
| 56 |
$text = "WCPOS - Cloud Print Test\nPrinter: " . $name . "\n"; |
| 57 |
$text .= 'Date: ' . gmdate( 'Y-m-d H:i' ) . "\nIf you can read this, printing works!\n"; |
| 58 |
// Three blank lines before the cut, the same bottom margin the receipt |
| 59 |
// templates use, so the last line is not flush against the blade. |
| 60 |
$xml = '<epos-print xmlns="http://www.epson-pos.com/schemas/2011/03/epos-print">' |
| 61 |
. '<text>' . esc_html( $text ) . '</text><feed line="3"/><cut type="feed"/></epos-print>'; |
| 62 |
|
| 63 |
return array( |
| 64 |
'content_type' => $this->content_type(), |
| 65 |
'payload' => base64_encode( $xml ), |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Resolve polling status. |
| 71 |
* |
| 72 |
* @param array $printer Printer configuration. |
| 73 |
* @param array $context Runtime status context. |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function status( array $printer, array $context ): string { |
| 78 |
$relay = $context['relay_status'] ?? null; |
| 79 |
if ( \is_array( $relay ) && 'blocked' === ( $relay['origin_status'] ?? '' ) ) { |
| 80 |
return 'blocked'; |
| 81 |
} |
| 82 |
if ( \is_array( $relay ) && null !== ( $relay['last_seen_seconds_ago'] ?? null ) && (int) $relay['last_seen_seconds_ago'] <= (int) $context['seen_ttl'] ) { |
| 83 |
return 'connected'; |
| 84 |
} |
| 85 |
|
| 86 |
$seen = (int) ( $context['seen'] ?? 0 ); |
| 87 |
if ( 0 === $seen ) { |
| 88 |
return 'waiting'; |
| 89 |
} |
| 90 |
|
| 91 |
return ( (int) $context['now'] - $seen ) <= (int) $context['seen_ttl'] ? 'connected' : 'offline'; |
| 92 |
} |
| 93 |
} |
| 94 |
|