| 1 |
<?php |
| 2 |
/** |
| 3 |
* Star CloudPRNT 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 |
* Star_Cloudprnt_Adapter class. |
| 14 |
*/ |
| 15 |
class Star_Cloudprnt_Adapter implements Provider_Adapter_Interface { |
| 16 |
/** |
| 17 |
* Return the StarPRNT content type. |
| 18 |
* |
| 19 |
* @return string |
| 20 |
*/ |
| 21 |
public function content_type(): string { |
| 22 |
return 'application/vnd.star.starprnt'; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Resolve StarPRNT 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' => 'starprnt', |
| 43 |
'content_type' => $this->content_type(), |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Build a native StarPRNT 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 |
$date = gmdate( 'Y-m-d H:i' ); |
| 57 |
$bytes = "\x1B\x1D\x29\x55\x02\x00\x30\x01"; |
| 58 |
$bytes .= "\x1B\x1D\x29\x55\x02\x00\x40\x00"; |
| 59 |
$bytes .= "\x1B\x1D\x61\x01WCPOS\nCloud Print Test\n\x1B\x1D\x61\x00"; |
| 60 |
$bytes .= 'Printer: ' . $name . "\nDate: " . $date . "\nIf you can read this, printing works!\n\n\n"; |
| 61 |
$bytes .= "\x1B\x64\x03"; |
| 62 |
|
| 63 |
return array( |
| 64 |
'content_type' => $this->content_type(), |
| 65 |
'payload' => base64_encode( $bytes ), |
| 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 |
|