-
Notifications
You must be signed in to change notification settings - Fork 0
/
printerclient.go
397 lines (344 loc) · 9.7 KB
/
printerclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
import (
"encoding/binary"
"errors"
"fmt"
"golang.org/x/sys/unix"
"log"
m "pConv/models"
"strconv"
"strings"
"syscall"
"time"
)
type PrinterClient struct {
fd int
Packetbuf []byte
}
// InfoEnum is an enumeration for different types of information.
type InfoEnum int
const (
DENSITY InfoEnum = 1
PRINTSPEED InfoEnum = 2
LABELTYPE InfoEnum = 3
LANGUAGETYPE InfoEnum = 6
AUTOSHUTDOWNTIME InfoEnum = 7
DEVICETYPE InfoEnum = 8
SOFTVERSION InfoEnum = 9
BATTERY InfoEnum = 10
DEVICESERIAL InfoEnum = 11
HARDVERSION InfoEnum = 12
)
// RequestCodeEnum is an enumeration for different request codes.
type RequestCodeEnum int
const (
GET_INFO RequestCodeEnum = 64
GET_RFID RequestCodeEnum = 26
HEARTBEAT RequestCodeEnum = 220
SET_LABEL_TYPE RequestCodeEnum = 35
SET_LABEL_DENSITY RequestCodeEnum = 33
START_PRINT RequestCodeEnum = 1
END_PRINT RequestCodeEnum = 243
START_PAGE_PRINT RequestCodeEnum = 3
END_PAGE_PRINT RequestCodeEnum = 227
ALLOW_PRINT_CLEAR RequestCodeEnum = 32
SET_DIMENSION RequestCodeEnum = 19
SET_QUANTITY RequestCodeEnum = 21
GET_PRINT_STATUS RequestCodeEnum = 163
)
// _packetToInt is a helper function to convert packet data to an integer (big-endian).
func _packetToInt(data []byte) int {
return int(binary.BigEndian.Uint16(data))
}
// NewPrinterClient creates a new PrinterClient and connects to the given address.
func NewPrinterClient(address string) (*PrinterClient, error) {
mac, err := parseMac(address)
if err != nil {
return nil, err
}
fd, err := unix.Socket(syscall.AF_BLUETOOTH, syscall.SOCK_STREAM, unix.BTPROTO_RFCOMM)
if err != nil {
return nil, err
}
log.Println("unix socket returned a file descriptor: ", fd)
err = unix.Connect(fd, &unix.SockaddrRFCOMM{Addr: mac, Channel: 1})
if err != nil {
return nil, err
}
return &PrinterClient{
fd: fd,
Packetbuf: make([]byte, 0),
}, nil
}
func parseMac(addr string) ([6]uint8, error) {
var mac [6]uint8
s := strings.Split(addr, ":")
if len(s) != 6 {
return mac, errors.New("invalid MAC address")
}
for i, si := range s {
n, err := strconv.ParseInt(si, 16, 8)
if err != nil {
return mac, fmt.Errorf("invalid character: %s", err)
}
mac[i] = uint8(n)
}
return mac, nil
}
// recv receives and deserializes packets from the connection.
func (c *PrinterClient) recv() ([]*m.NiimbotPacket, error) {
packets := make([]*m.NiimbotPacket, 0)
buffer := make([]byte, 1024)
for {
n, err := unix.Read(c.fd, buffer)
if err != nil {
return nil, err
}
c.Packetbuf = append(c.Packetbuf, buffer[:n]...)
for len(c.Packetbuf) > 4 {
pktLen := int(c.Packetbuf[3]) + 7
if len(c.Packetbuf) >= pktLen {
packet, err := FromBytes(c.Packetbuf[:pktLen])
if err != nil {
return nil, err
}
packets = append(packets, packet)
c.Packetbuf = c.Packetbuf[pktLen:]
} else {
break
}
}
}
}
// send sends a packet to the connection.
func (c *PrinterClient) send(packet *m.NiimbotPacket) {
unix.Write(c.fd, ToBytes(packet))
}
// transceive sends a request packet and waits for a response.
func (c *PrinterClient) transceive(reqCode RequestCodeEnum, data []byte, respOffset byte) (*m.NiimbotPacket, error) {
respCode := RequestCodeEnum(int(reqCode) + int(respOffset))
packet := NewNiimbotPacket(byte(reqCode), data)
c.send(packet)
for i := 0; i < 6; i++ {
packets, err := c.recv()
if err != nil {
return nil, err
}
for _, packet := range packets {
if packet.Type == 219 {
return nil, fmt.Errorf("response error")
} else if packet.Type == 0 {
return nil, fmt.Errorf("not implemented")
} else if packet.Type == byte(respCode) {
return packet, nil
}
}
time.Sleep(100 * time.Millisecond)
}
return nil, fmt.Errorf("response not received")
}
// GetInfo gets the specified information from the printer.
func (c *PrinterClient) GetInfo(key InfoEnum) (interface{}, error) {
packet, err := c.transceive(GET_INFO, []byte{byte(key)}, byte(key))
if err != nil {
return nil, err
}
switch key {
case DEVICESERIAL:
return fmt.Sprintf("%x", packet.Data), nil
case SOFTVERSION:
return _packetToInt(packet.Data) / 100.0, nil
case HARDVERSION:
return _packetToInt(packet.Data) / 100.0, nil
default:
return _packetToInt(packet.Data), nil
}
}
// GetRFID gets RFID information from the printer.
func (c *PrinterClient) GetRFID() (map[string]interface{}, error) {
packet, err := c.transceive(GET_RFID, []byte{0x01}, 0)
if err != nil {
return nil, err
}
data := packet.Data
if data[0] == 0 {
return nil, nil
}
idx := 0
uuid := fmt.Sprintf("%x", data[idx:idx+8])
idx += 8
barcodeLen := int(data[idx])
idx++
barcode := string(data[idx : idx+barcodeLen])
idx += barcodeLen
serialLen := int(data[idx])
idx++
serial := string(data[idx : idx+serialLen])
idx += serialLen
totalLen := binary.BigEndian.Uint16(data[idx:])
idx += 2
usedLen := binary.BigEndian.Uint16(data[idx:])
idx += 2
type_ := data[idx]
return map[string]interface{}{
"uuid": uuid,
"barcode": barcode,
"serial": serial,
"used_len": usedLen,
"total_len": totalLen,
"type": type_,
}, nil
}
// Heartbeat sends a heartbeat request and returns the response.
func (c *PrinterClient) Heartbeat() map[string]interface{} {
packet, err := c.transceive(HEARTBEAT, []byte{0x01}, 0)
if err != nil {
return nil
}
data := packet.Data
heartbeatData := make(map[string]interface{})
switch len(data) {
case 20:
heartbeatData["paperstate"] = int(data[18])
heartbeatData["rfidreadstate"] = int(data[19])
case 13:
heartbeatData["closingstate"] = int(data[9])
heartbeatData["powerlevel"] = int(data[10])
heartbeatData["paperstate"] = int(data[11])
heartbeatData["rfidreadstate"] = int(data[12])
case 19:
heartbeatData["closingstate"] = int(data[15])
heartbeatData["powerlevel"] = int(data[16])
heartbeatData["paperstate"] = int(data[17])
heartbeatData["rfidreadstate"] = int(data[18])
case 10:
heartbeatData["closingstate"] = int(data[8])
heartbeatData["powerlevel"] = int(data[9])
heartbeatData["rfidreadstate"] = int(data[8])
case 9:
heartbeatData["closingstate"] = int(data[8])
}
return heartbeatData
}
// SetLabelType sets the label type.
func (c *PrinterClient) SetLabelType(n byte) bool {
assert := func(value bool) {
if !value {
panic("assertion failed")
}
}
assert(1 <= n && n <= 3)
packet, err := c.transceive(SET_LABEL_TYPE, []byte{byte(n)}, 16)
if err != nil {
return false
}
return packet.Data[0] != 0
}
// SetLabelDensity sets the label density.
func (c *PrinterClient) SetLabelDensity(n byte) bool {
assert := func(value bool) {
if !value {
panic("assertion failed")
}
}
assert(1 <= n && n <= 3)
packet, err := c.transceive(SET_LABEL_DENSITY, []byte{byte(n)}, 16)
if err != nil {
return false
}
return packet.Data[0] != 0
}
// StartPrint sends a start print request and returns the response.
func (c *PrinterClient) StartPrint() bool {
packet, err := c.transceive(START_PRINT, []byte{0x01}, 0)
if err != nil {
return false
}
return packet.Data[0] != 0
}
// EndPrint sends an end print request and returns the response.
func (c *PrinterClient) EndPrint() bool {
packet, err := c.transceive(END_PRINT, []byte{0x01}, 0)
if err != nil {
return false
}
return packet.Data[0] != 0
}
// StartPagePrint sends a start page print request and returns the response.
func (c *PrinterClient) StartPagePrint() bool {
packet, err := c.transceive(START_PAGE_PRINT, []byte{0x01}, 0)
if err != nil {
return false
}
return packet.Data[0] != 0
}
// EndPagePrint sends an end page print request and returns the response.
func (c *PrinterClient) EndPagePrint() bool {
packet, err := c.transceive(END_PAGE_PRINT, []byte{0x01}, 0)
if err != nil {
return false
}
return packet.Data[0] != 0
}
// AllowPrintClear sends an allow print clear request and returns the response.
func (c *PrinterClient) AllowPrintClear() bool {
var packet, err = c.transceive(ALLOW_PRINT_CLEAR, []byte{0x01}, 16)
if err != nil {
return false
}
// Check if the response is valid and return the result
if len(packet.Data) > 0 && packet.Data[0] == 0x01 {
return true
}
return false
}
func (c *PrinterClient) SetDimension(width, height int) bool {
data := make([]byte, 4)
binary.BigEndian.PutUint16(data, uint16(width))
binary.BigEndian.PutUint16(data[2:], uint16(height))
packet, err := c.transceive(SET_DIMENSION, data, 16)
if err != nil {
return false
}
// Check if the response is valid and return the result
if len(packet.Data) > 0 && packet.Data[0] == 0x01 {
return true
}
return false
}
func (c *PrinterClient) SetQuantity(quantity uint16) bool {
data := make([]byte, 2)
binary.BigEndian.PutUint16(data, quantity)
packet, err := c.transceive(SET_QUANTITY, data, 16)
if err != nil {
return false
}
// Check if the response is valid and return the result
if len(packet.Data) > 0 && packet.Data[0] == 0x01 {
return true
}
return false
}
func (c *PrinterClient) GetPrintStatus() m.PrintStatus {
packet, err := c.transceive(GET_PRINT_STATUS, []byte{0x01}, 16)
if err != nil {
// Handle the error, e.g., return a default or error status
return m.PrintStatus{}
}
// Ensure that the packet data contains enough bytes to extract the print status.
if len(packet.Data) < 4 {
// Handle the case where the packet data is too short to parse.
// For example, return a default or error status.
return m.PrintStatus{}
}
// Extract the print status information from the packet data.
pageNumber := binary.BigEndian.Uint16(packet.Data)
progress1 := packet.Data[2]
progress2 := packet.Data[3]
// Create and return the PrintStatus struct.
return m.PrintStatus{
Page: pageNumber,
Progress1: progress1,
Progress2: progress2,
}
}