|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Posprint\Connectors; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Buffer |
|
7
|
|
|
* In principle, the entire assembly of RAW commands must be made for this buffer |
|
8
|
|
|
* that will be used later for sending to the appropriate connector set |
|
9
|
|
|
* by calling class |
|
10
|
|
|
* this is necessary to make to enable: |
|
11
|
|
|
* 1 - debug the assembly of commands |
|
12
|
|
|
* 2 - allow the use of qz.io for printing by using a browser and a cloud server |
|
13
|
|
|
* |
|
14
|
|
|
* @category NFePHP |
|
15
|
|
|
* @package Posprint |
|
16
|
|
|
* @author Roberto L. Machado <[email protected]> |
|
17
|
|
|
* @copyright 2016 Roberto L. Machado |
|
18
|
|
|
* @license http://www.gnu.org/licenses/lesser.html LGPL v3 |
|
19
|
|
|
* @link http://github.com/nfephp-org/posprint |
|
20
|
|
|
* for the canonical source repository |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
use Posprint\Connectors\ConnectorInterface; |
|
24
|
|
|
|
|
25
|
|
|
final class Buffer implements ConnectorInterface |
|
26
|
|
|
{ |
|
27
|
|
|
private $ctrlCodes = [ |
|
28
|
|
|
' [NUL] ' => 0, //Nulo |
|
29
|
|
|
' [EOT] ' => 4, //EOT fim da transmissão |
|
30
|
|
|
' [ENQ] ' => 5, //ENQ colocar na fila Pedido de status 1 |
|
31
|
|
|
' [BEL] ' => 7, //BEL sinal sonoro |
|
32
|
|
|
' [HT] ' => 9, //tabulação horizontal |
|
33
|
|
|
' [LF] ' => 10, //Inicia a impressão e avança uma linha |
|
34
|
|
|
' [VT] ' => 11, //tabulação vertical |
|
35
|
|
|
' [FF] ' => 12, //avança pagina |
|
36
|
|
|
' [CR] ' => 13, //retorno de carro |
|
37
|
|
|
' [SO] ' => 14, //SO Inicia modo expandido |
|
38
|
|
|
' [SI] ' => 15, //Seleciona modo condensado |
|
39
|
|
|
' [DLE] ' => 16, //Data Link Escape |
|
40
|
|
|
' [DC1] ' => 17, //DC1 Inicia modo enfatizado |
|
41
|
|
|
' [DC2] ' => 18, //DC2 Cancela modo condensado |
|
42
|
|
|
' [DC3] ' => 19, //DC3 Cancela modo enfatizado |
|
43
|
|
|
' [DC4] ' => 20, //DC4 Controle de dispositivo 4 Inicia modo normal |
|
44
|
|
|
' [NAK] ' => 21, // NAK |
|
45
|
|
|
' [SYN] ' => 22, //Sincronismo |
|
46
|
|
|
' [CAN] ' => 24, //CAN Cancela linha enviada |
|
47
|
|
|
' [EM] ' => 25, //Avança 4 linhas |
|
48
|
|
|
' [ESC] ' => 27, //escape |
|
49
|
|
|
' [FS] ' => 28, //FS |
|
50
|
|
|
' [GS] ' => 29, //GS |
|
51
|
|
|
' [DEL] ' => 127 //Cancela último caracter |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Buffer of accumulated raw data. |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
private $buffer = null; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Create new print connector |
|
62
|
|
|
* and set $buffer property as empty array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function __construct() |
|
65
|
28 |
|
{ |
|
66
|
|
|
$this->buffer = array(); |
|
67
|
28 |
|
} |
|
68
|
28 |
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Destruct print connection |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __destruct() |
|
73
|
27 |
|
{ |
|
74
|
|
|
$this->close(); |
|
75
|
27 |
|
} |
|
76
|
27 |
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Send data to buffer porperty |
|
79
|
|
|
* @param string $data |
|
80
|
|
|
*/ |
|
81
|
|
|
public function write($data) |
|
82
|
|
|
{ |
|
83
|
26 |
|
$this->buffer[] = $data; |
|
84
|
|
|
} |
|
85
|
26 |
|
|
|
86
|
26 |
|
/** |
|
87
|
|
|
* Read data form buffer |
|
88
|
|
|
* @param int $len |
|
89
|
|
|
* @return string|array |
|
90
|
|
|
*/ |
|
91
|
|
|
public function read($len = null) |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->getDataReadable(false); |
|
94
|
1 |
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
/** |
|
97
|
|
|
* Finalize printer connection |
|
98
|
|
|
* and clear buffer property |
|
99
|
|
|
*/ |
|
100
|
|
|
public function close() |
|
101
|
|
|
{ |
|
102
|
|
|
$this->buffer = array(); |
|
103
|
29 |
|
} |
|
104
|
|
|
|
|
105
|
29 |
|
/** |
|
106
|
29 |
|
* Return the accumulated raw data that has been sent to this buffer. |
|
107
|
|
|
* |
|
108
|
|
|
* @param bool $retArray Enable return as array, otherwise will return a string |
|
109
|
|
|
* @return string|array |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getDataBinary($retArray = true) |
|
112
|
|
|
{ |
|
113
|
|
|
if (! $retArray) { |
|
114
|
21 |
|
return implode($this->buffer); |
|
115
|
|
|
} |
|
116
|
21 |
|
return $this->buffer; |
|
117
|
20 |
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
/** |
|
120
|
|
|
* Return the data buffer in base64-encoded for transmission over TCP/IP, |
|
121
|
|
|
* specifically for use qz.io or other similar system. |
|
122
|
|
|
* |
|
123
|
|
|
* @param boolean $retArray Enable return as array, otherwise will return a string |
|
124
|
|
|
* @return array|string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getDataBase64($retArray = true) |
|
127
|
|
|
{ |
|
128
|
|
|
$lbuff = $this->convertBuffer('B'); |
|
129
|
2 |
|
if (!$retArray) { |
|
130
|
|
|
return implode("\n", $lbuff); |
|
131
|
2 |
|
} |
|
132
|
2 |
|
return $lbuff; |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
/** |
|
136
|
|
|
* Returns the buffer data in JSON format |
|
137
|
|
|
* for use with the java ajax |
|
138
|
|
|
* It must be tested because there may be binary data |
|
139
|
|
|
* that can not travel on GET or POST requests over TCP/IP |
|
140
|
|
|
* |
|
141
|
|
|
* @param bool $retArray Enable return as array, otherwise will return a string |
|
142
|
|
|
* @return string |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getDataJson($retArray = true) |
|
145
|
|
|
{ |
|
146
|
|
|
return json_encode($this->getDataBinary($retArray)); |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
/** |
|
150
|
|
|
* getDataReadable |
|
151
|
|
|
* Return buffer data converted into a readable string. |
|
152
|
|
|
* For testing and debbuging only, this format should not be sent to printer |
|
153
|
|
|
* @param bool $retArray Enable return as array, otherwise will return a string |
|
154
|
|
|
* @return string|array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getDataReadable($retArray = true) |
|
157
|
|
|
{ |
|
158
|
|
|
$ret = $this->convertBuffer('R'); |
|
159
|
|
|
if (!$retArray) { |
|
160
|
2 |
|
$ret = implode("\n", $ret); |
|
161
|
|
|
} |
|
162
|
2 |
|
return $ret; |
|
163
|
2 |
|
} |
|
164
|
2 |
|
|
|
165
|
2 |
|
/** |
|
166
|
2 |
|
* Convert buffer content |
|
167
|
|
|
* @param string $type |
|
168
|
|
|
* @return array |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function convertBuffer($type) |
|
171
|
|
|
{ |
|
172
|
|
|
$ret = []; |
|
173
|
|
|
foreach ($this->buffer as $data) { |
|
174
|
|
|
if ($type == 'R') { |
|
175
|
|
|
$ret[] = $this->friendlyBinary($data); |
|
176
|
|
|
} |
|
177
|
|
|
if ($type == 'B') { |
|
178
|
|
|
$ret[] = base64_encode($data); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
return $ret; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Converts unprintable characters in screen-printing characters |
|
186
|
|
|
* used for debugging purpose only |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $input |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
|
|
protected function friendlyBinary($input) |
|
192
|
|
|
{ |
|
193
|
|
|
// Print out binary data with PHP \x00 escape codes, |
|
194
|
|
|
// for builting test cases. |
|
195
|
|
|
$chars = str_split($input); |
|
196
|
2 |
|
foreach ($chars as $index => $byte) { |
|
197
|
|
|
$code = ord($byte); |
|
198
|
|
|
$key = array_search($code, $this->ctrlCodes, true); |
|
199
|
|
|
if ($key !== false) { |
|
200
|
2 |
|
$chars[$index] = $key; |
|
201
|
2 |
|
} elseif ($code < 32 || $code > 126) { |
|
202
|
2 |
|
$chars[$index] = " (" . bin2hex($byte) . "h) "; |
|
203
|
2 |
|
} |
|
204
|
2 |
|
} |
|
205
|
2 |
|
return implode($chars); |
|
206
|
2 |
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|