NativeFastPrinterModule.java
15.9 KB
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
package com.foodlabel.nativeprinter;
import com.alibaba.fastjson.JSONObject;
import com.foodlabel.nativeprinter.debug.NativePrintDebugState;
import com.foodlabel.nativeprinter.support.PluginResult;
import com.foodlabel.nativeprinter.support.SafeJson;
import com.foodlabel.nativeprinter.support.ThrowableUtils;
import com.foodlabel.nativeprinter.template.NativeTemplateCommandBuilder;
import com.foodlabel.nativeprinter.transport.GprinterBluetoothTransport;
import com.foodlabel.nativeprinter.transport.UposBuiltinPrinterTransport;
import com.foodlabel.nativeprinter.transport.UposSerialPortTransport;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.util.Base64;
import io.dcloud.feature.uniapp.common.UniModule;
public class NativeFastPrinterModule extends UniModule {
private static final String BACKEND = "gprinter-sdk";
private static final String PLUGIN_VERSION = "1.2.0";
private static final Object LOCK = new Object();
private static final ExecutorService PRINT_EXECUTOR = Executors.newSingleThreadExecutor();
private static final NativePrintDebugState DEBUG_STATE = new NativePrintDebugState(BACKEND, PLUGIN_VERSION);
private static final GprinterBluetoothTransport BLUETOOTH_TRANSPORT = new GprinterBluetoothTransport();
private static final UposBuiltinPrinterTransport UPOS_BUILTIN_TRANSPORT = new UposBuiltinPrinterTransport();
private static final UposSerialPortTransport UPOS_SERIAL_TRANSPORT = new UposSerialPortTransport();
private static final String[] SERIAL_PORT_CANDIDATES = new String[]{
"/dev/ttyS1",
"/dev/ttyS0",
"/dev/ttyMSM0",
"/dev/ttyMSM1",
"/dev/ttyUSB0",
"/dev/ttyACM0"
};
@JSMethod(uiThread = false)
public void connect(JSONObject params, JSCallback callback) {
String deviceId = SafeJson.getString(params, "deviceId", "");
String deviceName = SafeJson.getString(params, "deviceName", "");
PluginResult result = ensureConnected(deviceId, deviceName);
if (callback != null) {
callback.invoke(result.toJsonString());
}
}
@JSMethod(uiThread = false)
public void disconnect(JSCallback callback) {
synchronized (LOCK) {
BLUETOOTH_TRANSPORT.disconnect();
DEBUG_STATE.clearCurrentDevice();
DEBUG_STATE.setStage("disconnect:ok");
DEBUG_STATE.clearError();
}
if (callback != null) {
callback.invoke(debugResult(PluginResult.ok(false, "", "", "disconnect:ok")).toJsonString());
}
}
@JSMethod(uiThread = false)
public void isConnected(JSCallback callback) {
boolean connected;
synchronized (LOCK) {
connected = BLUETOOTH_TRANSPORT.isConnected();
}
if (callback != null) {
callback.invoke(debugResult(PluginResult.ok(connected, DEBUG_STATE.getCurrentDeviceId(), DEBUG_STATE.getCurrentDeviceName(), "isConnected:ok")).toJsonString());
}
}
@JSMethod(uiThread = false)
public void getDebugInfo(JSCallback callback) {
boolean connected;
synchronized (LOCK) {
connected = BLUETOOTH_TRANSPORT.isConnected();
}
if (callback != null) {
callback.invoke(debugResult(PluginResult.ok(connected, DEBUG_STATE.getCurrentDeviceId(), DEBUG_STATE.getCurrentDeviceName(), "debug:ok")).toJsonString());
}
}
@JSMethod(uiThread = false)
public void printTemplate(JSONObject params, JSCallback callback) {
String deviceId = SafeJson.getString(params, "deviceId", "");
String deviceName = SafeJson.getString(params, "deviceName", "");
String templateJson = SafeJson.getString(params, "templateJson", "");
String dataJson = SafeJson.getString(params, "dataJson", "{}");
int dpi = SafeJson.getInt(params, "dpi", 203);
int printQty = Math.max(1, SafeJson.getInt(params, "printQty", 1));
if (templateJson == null || templateJson.trim().isEmpty()) {
if (callback != null) {
callback.invoke(errorResult(9011006, "Template json is empty.").toJsonString());
}
return;
}
PluginResult connectResult = ensureConnected(deviceId, deviceName);
if (!connectResult.success) {
if (callback != null) callback.invoke(connectResult.toJsonString());
return;
}
DEBUG_STATE.setStage("printTemplate:queued");
if (callback != null) {
callback.invoke(debugResult(PluginResult.ok(true, DEBUG_STATE.getCurrentDeviceId(), DEBUG_STATE.getCurrentDeviceName(), "printTemplate:queued")).toJsonString());
}
PRINT_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
try {
DEBUG_STATE.resetBuildMetrics();
DEBUG_STATE.setStage("build-command");
long buildStarted = System.currentTimeMillis();
NativeTemplateCommandBuilder.BuildResult buildResult =
NativeTemplateCommandBuilder.buildWithStats(templateJson, dataJson, dpi, printQty);
DEBUG_STATE.setBuildMs(Math.max(0L, System.currentTimeMillis() - buildStarted));
DEBUG_STATE.setBuildResult(buildResult);
long writeStarted = System.currentTimeMillis();
synchronized (LOCK) {
if (!BLUETOOTH_TRANSPORT.isConnected()) {
errorResult(9011005, "Bluetooth printer transport is not ready.");
return;
}
DEBUG_STATE.setStage("write-command");
boolean ok = BLUETOOTH_TRANSPORT.write(buildResult.bytes);
if (!ok) {
errorResult(9011011, "Printer writeDataImmediately returned false.");
return;
}
}
DEBUG_STATE.setWriteMs(Math.max(0L, System.currentTimeMillis() - writeStarted));
DEBUG_STATE.markPrinted(System.currentTimeMillis());
DEBUG_STATE.setStage("printTemplate:ok");
DEBUG_STATE.clearError();
} catch (Throwable e) {
DEBUG_STATE.setError(ThrowableUtils.unwrap(e));
DEBUG_STATE.setStage("printTemplate:error");
}
}
});
}
/**
* 将 JS 侧已生成的 TSC/指令字节(Base64)经佳博 SDK 写出。
* 用于整页光栅等路径,避免再走 JS 经典蓝牙 socket(慢、易超时)。
*/
@JSMethod(uiThread = false)
public void printCommandBytes(JSONObject params, JSCallback callback) {
String deviceId = SafeJson.getString(params, "deviceId", "");
String deviceName = SafeJson.getString(params, "deviceName", "");
String base64 = SafeJson.getString(params, "base64", "");
if (base64 == null || base64.trim().isEmpty()) {
if (callback != null) {
callback.invoke(errorResult(9011012, "base64 is empty.").toJsonString());
}
return;
}
PluginResult connectResult = ensureConnected(deviceId, deviceName);
if (!connectResult.success) {
if (callback != null) {
callback.invoke(connectResult.toJsonString());
}
return;
}
DEBUG_STATE.setStage("printCommandBytes:queued");
if (callback != null) {
callback.invoke(debugResult(PluginResult.ok(true, DEBUG_STATE.getCurrentDeviceId(), DEBUG_STATE.getCurrentDeviceName(), "printCommandBytes:queued")).toJsonString());
}
PRINT_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
try {
byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
if (bytes == null || bytes.length == 0) {
DEBUG_STATE.setError("decoded bytes empty");
DEBUG_STATE.setStage("printCommandBytes:error");
return;
}
long writeStarted = System.currentTimeMillis();
synchronized (LOCK) {
if (!BLUETOOTH_TRANSPORT.isConnected()) {
errorResult(9011005, "Bluetooth printer transport is not ready.");
return;
}
DEBUG_STATE.setStage("write-raw-command");
boolean ok = BLUETOOTH_TRANSPORT.write(bytes);
if (!ok) {
errorResult(9011011, "Printer writeDataImmediately returned false.");
return;
}
}
DEBUG_STATE.setWriteMs(Math.max(0L, System.currentTimeMillis() - writeStarted));
DEBUG_STATE.markPrinted(System.currentTimeMillis());
DEBUG_STATE.setStage("printCommandBytes:ok");
DEBUG_STATE.clearError();
} catch (Throwable e) {
DEBUG_STATE.setError(ThrowableUtils.unwrap(e));
DEBUG_STATE.setStage("printCommandBytes:error");
}
}
});
}
/**
* UnifiedPOS SDK: built-in printer / serial port printing (no Bluetooth pairing).
*
* Params:
* - base64: required, raw command bytes
* - prefer: optional, "builtin" | "serial" (default builtin)
* - serialPath: optional, e.g. "/dev/ttyS1"
* - baudrate: optional, default 9600
*/
@JSMethod(uiThread = false)
public void printUposCommandBytes(JSONObject params, JSCallback callback) {
String base64 = SafeJson.getString(params, "base64", "");
String prefer = SafeJson.getString(params, "prefer", "builtin");
String serialPath = SafeJson.getString(params, "serialPath", "");
int baudrate = Math.max(1200, SafeJson.getInt(params, "baudrate", 9600));
if (base64 == null || base64.trim().isEmpty()) {
if (callback != null) {
callback.invoke(errorResult(9011012, "base64 is empty.").toJsonString());
}
return;
}
DEBUG_STATE.setStage("printUposCommandBytes:queued");
PRINT_EXECUTOR.execute(new Runnable() {
@Override
public void run() {
try {
byte[] bytes = Base64.decode(base64, Base64.DEFAULT);
if (bytes == null || bytes.length == 0) {
DEBUG_STATE.setError("decoded bytes empty");
DEBUG_STATE.setStage("printUposCommandBytes:error");
return;
}
long writeStarted = System.currentTimeMillis();
boolean ok;
if ("serial".equalsIgnoreCase(prefer)) {
ok = writeBySerial(bytes, serialPath, baudrate);
} else {
ok = writeByBuiltinPrinter(bytes);
if (!ok) {
// fallback to serial if builtin printer open/print failed
ok = writeBySerial(bytes, serialPath, baudrate);
}
}
DEBUG_STATE.setWriteMs(Math.max(0L, System.currentTimeMillis() - writeStarted));
DEBUG_STATE.markPrinted(System.currentTimeMillis());
if (ok) {
DEBUG_STATE.setStage("printUposCommandBytes:ok");
DEBUG_STATE.clearError();
if (callback != null) {
callback.invoke(debugResult(PluginResult.ok(true, "", "", "printUposCommandBytes:ok")).toJsonString());
}
} else {
DEBUG_STATE.setStage("printUposCommandBytes:error");
if (DEBUG_STATE.getLastError() == null || DEBUG_STATE.getLastError().isEmpty()) {
DEBUG_STATE.setError("upos write failed");
}
if (callback != null) {
callback.invoke(errorResult(9011020, "UPOS print failed: " + DEBUG_STATE.getLastError()).toJsonString());
}
}
} catch (Throwable e) {
DEBUG_STATE.setError(ThrowableUtils.unwrap(e));
DEBUG_STATE.setStage("printUposCommandBytes:error");
if (callback != null) {
callback.invoke(errorResult(9011021, "UPOS print exception: " + DEBUG_STATE.getLastError()).toJsonString());
}
}
}
});
}
private static boolean writeByBuiltinPrinter(byte[] bytes) {
try {
DEBUG_STATE.setStage("upos:builtin:write");
boolean ok = UPOS_BUILTIN_TRANSPORT.write(bytes);
if (!ok) {
DEBUG_STATE.setError("upos builtin failed: " + UPOS_BUILTIN_TRANSPORT.getLastError());
}
return ok;
} catch (Throwable e) {
DEBUG_STATE.setError(ThrowableUtils.unwrap(e));
return false;
}
}
private static boolean writeBySerial(byte[] bytes, String path, int baudrate) {
try {
String usePath = (path != null && !path.trim().isEmpty()) ? path.trim() : null;
if (usePath != null) {
DEBUG_STATE.setStage("upos:serial:open:" + usePath);
UPOS_SERIAL_TRANSPORT.close();
if (!UPOS_SERIAL_TRANSPORT.open(usePath, baudrate)) {
DEBUG_STATE.setError("upos serial open failed: " + UPOS_SERIAL_TRANSPORT.getLastError());
return false;
}
DEBUG_STATE.setStage("upos:serial:write");
boolean ok = UPOS_SERIAL_TRANSPORT.write(bytes);
if (!ok) DEBUG_STATE.setError("upos serial write failed: " + UPOS_SERIAL_TRANSPORT.getLastError());
UPOS_SERIAL_TRANSPORT.close();
return ok;
}
for (String candidate : SERIAL_PORT_CANDIDATES) {
DEBUG_STATE.setStage("upos:serial:open:" + candidate);
UPOS_SERIAL_TRANSPORT.close();
if (!UPOS_SERIAL_TRANSPORT.open(candidate, baudrate)) {
continue;
}
DEBUG_STATE.setStage("upos:serial:write");
boolean ok = UPOS_SERIAL_TRANSPORT.write(bytes);
if (!ok) {
UPOS_SERIAL_TRANSPORT.close();
continue;
}
UPOS_SERIAL_TRANSPORT.close();
return true;
}
DEBUG_STATE.setError("upos serial open failed: no candidate ports worked");
return false;
} catch (Throwable e) {
DEBUG_STATE.setError(ThrowableUtils.unwrap(e));
return false;
}
}
private PluginResult ensureConnected(String deviceId, String deviceName) {
synchronized (LOCK) {
PluginResult result = BLUETOOTH_TRANSPORT.ensureConnected(deviceId, deviceName, DEBUG_STATE);
if (!result.success) {
return debugResult(result);
}
return debugResult(PluginResult.ok(true, DEBUG_STATE.getCurrentDeviceId(), DEBUG_STATE.getCurrentDeviceName(), "connect:ok"));
}
}
private static PluginResult errorResult(int code, String message) {
DEBUG_STATE.setError(message == null ? "" : message);
return debugResult(PluginResult.error(code, message));
}
private static PluginResult debugResult(PluginResult result) {
return DEBUG_STATE.attachTo(result);
}
}