seven365-zyprinter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/Package.swift +43 -0
  2. package/README.md +186 -0
  3. package/Seven365Zyprinter.podspec +27 -0
  4. package/android/build.gradle +58 -0
  5. package/android/src/main/AndroidManifest.xml +2 -0
  6. package/android/src/main/java/com/mycompany/plugins/example/Example.java +342 -0
  7. package/android/src/main/java/com/mycompany/plugins/example/ExamplePlugin.java +161 -0
  8. package/android/src/main/res/.gitkeep +0 -0
  9. package/dist/docs.json +229 -0
  10. package/dist/esm/definitions.d.ts +56 -0
  11. package/dist/esm/definitions.js +2 -0
  12. package/dist/esm/definitions.js.map +1 -0
  13. package/dist/esm/index.d.ts +4 -0
  14. package/dist/esm/index.js +7 -0
  15. package/dist/esm/index.js.map +1 -0
  16. package/dist/esm/web.d.ts +49 -0
  17. package/dist/esm/web.js +40 -0
  18. package/dist/esm/web.js.map +1 -0
  19. package/dist/plugin.cjs.js +54 -0
  20. package/dist/plugin.cjs.js.map +1 -0
  21. package/dist/plugin.js +57 -0
  22. package/dist/plugin.js.map +1 -0
  23. package/ios/Seven365Zyprinter.podspec +28 -0
  24. package/ios/Sources/Plugin/ZyprintPlugin.swift +161 -0
  25. package/ios/Sources/Plugin/ZywellSDK.swift +358 -0
  26. package/ios/Sources/Seven365Zyprinter-Umbrella.h +16 -0
  27. package/ios/Sources/module.modulemap +12 -0
  28. package/ios/Sources/sources/BLEManager.h +658 -0
  29. package/ios/Sources/sources/BLEManager.m +2842 -0
  30. package/ios/Sources/sources/GCD/Documentation.html +47 -0
  31. package/ios/Sources/sources/GCD/GCDAsyncSocket.h +1226 -0
  32. package/ios/Sources/sources/GCD/GCDAsyncSocket.m +8560 -0
  33. package/ios/Sources/sources/GCD/GCDAsyncUdpSocket.h +1036 -0
  34. package/ios/Sources/sources/GCD/GCDAsyncUdpSocket.m +5632 -0
  35. package/ios/Sources/sources/GCD/PrinterManager.h +91 -0
  36. package/ios/Sources/sources/GCD/PrinterManager.m +513 -0
  37. package/ios/Sources/sources/GCD/WifiManager.h +91 -0
  38. package/ios/Sources/sources/GCD/WifiManager.m +510 -0
  39. package/ios/Sources/sources/ImageTranster.h +38 -0
  40. package/ios/Sources/sources/ImageTranster.m +389 -0
  41. package/ios/Sources/sources/POSBLEManager.h +759 -0
  42. package/ios/Sources/sources/POSBLEManager.m +834 -0
  43. package/ios/Sources/sources/POSSDK.h +93 -0
  44. package/ios/Sources/sources/POSWIFIManager.h +116 -0
  45. package/ios/Sources/sources/POSWIFIManager.m +260 -0
  46. package/ios/Sources/sources/POSWIFIManagerAsync.h +745 -0
  47. package/ios/Sources/sources/POSWIFIManagerAsync.m +1847 -0
  48. package/ios/Sources/sources/PosCommand.h +633 -0
  49. package/ios/Sources/sources/PosCommand.m +1019 -0
  50. package/ios/Sources/sources/TscCommand.h +723 -0
  51. package/ios/Sources/sources/TscCommand.m +566 -0
  52. package/ios/Tests/ExamplePluginTests/ExamplePluginTests.swift +15 -0
  53. package/package.json +339 -0
@@ -0,0 +1,91 @@
1
+ //
2
+ // PrinterManager.h
3
+ // Printer
4
+ //
5
+ // Created by ding on 2021/12/14.
6
+ // Copyright © 2021 Admin. All rights reserved.
7
+ //
8
+
9
+ #import <Foundation/Foundation.h>
10
+ #import "GCDAsyncSocket.h"
11
+ #import "BLEManager.h"
12
+ NS_ASSUME_NONNULL_BEGIN
13
+
14
+ @interface WifiManager : NSObject
15
+ #pragma mark
16
+ //PrinterType
17
+ typedef enum {
18
+ WifiPrinter=0,
19
+ BlePrinter
20
+ } PrinterType;
21
+ //PrinterStatus
22
+ typedef enum {
23
+ Normal=0,
24
+ Error,
25
+ Printing,
26
+ Busy,
27
+ CashDrawerOpened,
28
+ CoverOpened,
29
+ PaperNearEnd,
30
+ NoPaper,
31
+ CutPaper,
32
+ FeedPaper,
33
+ PrintCompleted,
34
+ Disconnected
35
+ } PrinterStatus;
36
+ //Printer IP
37
+ @property (nonatomic,copy) NSString *ip;
38
+ //Printer Port
39
+ @property (nonatomic,assign) UInt16 port;
40
+ //Connection Status
41
+ @property (nonatomic,assign) BOOL isConnected;
42
+ //Received data
43
+ @property(nonatomic,copy)NSData* receivedData;
44
+ //Whether the status of the printer is successfully monitored
45
+ @property (nonatomic,assign) BOOL isReceivedData;
46
+ //Printer Type
47
+ @property(nonatomic,assign)NSUInteger printerType;
48
+ //Printer Status
49
+ @property(nonatomic,assign)int printerStatus;
50
+ @property(strong,nonatomic) NSTimer *timer;
51
+ //Monitor Port
52
+ @property(nonatomic,assign)int monitorPort;
53
+ //Print Port
54
+ @property(nonatomic,assign)int printPort;
55
+ //Print Succeed
56
+ @property(nonatomic,assign)bool printSucceed;
57
+ //Whether to automatically reconnect
58
+ @property(nonatomic,assign)bool isAutoRecon;
59
+ //Whether the user actively disconnected
60
+ @property(nonatomic,assign)bool isUserDiscon;
61
+ @property(nonatomic,assign)bool isFirstRece;
62
+ //Whether it is a machine manufactured after 2022/3/28
63
+ @property(nonatomic,assign)bool isNewPrinter;
64
+ #pragma mark
65
+ //+ (instancetype)shareManager:(int)printerType;
66
+ + (instancetype)shareManager:(int)printerType threadID:(NSString*)thread;
67
+ //connect wifi printer
68
+ -(bool)ConnectWifiPrinter:(NSString *)ip port:(UInt16)port;
69
+ //start printer monitor
70
+ -(void)StartMonitor;
71
+ //Exit the print prohibition state
72
+ -(NSData*)exitForbidPrinting;
73
+ //No Lost Order
74
+ -(void)freeLostOrder;
75
+ //connect ble printer
76
+ -(void)ConnectBlePrinter:(CBPeripheral *)peripheral;
77
+ //disconnect printer
78
+ - (void)DisConnectPrinter;
79
+ //send data to printer
80
+ -(bool)SendDataToPrinter:(NSData *)data;
81
+ //Send the receipt to the printer, and judge the status of the printer
82
+ -(bool)SendReceiptToPrinter:(NSData *)data;
83
+ //Read the data returned by the printer
84
+ -(void)ReadDataFromPrinter;
85
+ //Get the current status of the printer
86
+ -(NSString*)GetPrinterStatus;
87
+ //Whether the printing is complete or not
88
+ -(BOOL)IsPrintCompletely;
89
+ @end
90
+
91
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,510 @@
1
+ //
2
+ // WifiManager.m
3
+ // Printer
4
+ //
5
+ // Created by ding on 2021/12/14.
6
+ // Copyright © 2021 Admin. All rights reserved.
7
+ //
8
+
9
+ #import "WifiManager.h"
10
+
11
+ static WifiManager *shareManager = nil;
12
+ @interface WifiManager ()<GCDAsyncSocketDelegate,BLEManagerDelegate>
13
+ //connected wifi socket object
14
+ @property (nonatomic,strong) GCDAsyncSocket *wifiPrinter;
15
+ @property (nonatomic,strong) GCDAsyncSocket *wifiPrinterMonitor;
16
+ //ble printer object
17
+ @property (nonatomic,strong) BLEManager *blePrinter;
18
+ @end
19
+ @implementation WifiManager
20
+ + (instancetype)shareManager:(int)printerType threadID:(NSString*)thread{
21
+ shareManager = [[WifiManager alloc] init:printerType threadID:thread];
22
+ return shareManager;
23
+ }
24
+
25
+ - (instancetype)init:(int)printerType threadID:(NSString*)thread{
26
+ if (self = [super init]) {
27
+ _isConnected=false;
28
+ _printerType=printerType;
29
+ _printerStatus=Disconnected;
30
+ _printSucceed=false;
31
+ _monitorPort=9100;
32
+ _printPort=9100;
33
+ _isAutoRecon=false;
34
+ _isFirstRece=true;
35
+ _isReceivedData=false;
36
+ _isNewPrinter=false;
37
+ switch (printerType) {
38
+ case WifiPrinter:
39
+ {
40
+ dispatch_queue_t queue = dispatch_queue_create([thread UTF8String], DISPATCH_QUEUE_SERIAL);
41
+ _wifiPrinter=[[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:queue];
42
+ if(_monitorPort==4000){
43
+ dispatch_queue_t queue2 = dispatch_queue_create("com.printer.monitor", DISPATCH_QUEUE_SERIAL);
44
+ _wifiPrinterMonitor=[[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:queue2];
45
+ }
46
+ }
47
+ break;
48
+ case BlePrinter:
49
+ {
50
+
51
+ }
52
+ break;
53
+ default:
54
+ break;
55
+ }
56
+ }
57
+ return self;
58
+ }
59
+ -(bool)ConnectWifiPrinter:(NSString *)ip port:(UInt16)port{
60
+ if(!_isConnected){
61
+ _ip=ip;
62
+ _port=port;
63
+ NSError *err = nil;
64
+ _isFirstRece=true;
65
+ _isUserDiscon=false;
66
+ _isReceivedData=false;
67
+ _isNewPrinter=false;
68
+ _isConnected=[_wifiPrinter connectToHost:ip onPort:port withTimeout:-1 error:&err];
69
+ if(!_isConnected){
70
+ _isConnected=false;
71
+ _printerStatus=Disconnected;
72
+ _isUserDiscon=true;
73
+ NSLog(@"connect failed\n");
74
+ }else{
75
+ _isConnected=true;
76
+ //NSLog(@"connect success\n");
77
+ _printerStatus=Normal;
78
+ }
79
+ if(_monitorPort==4000&&_isConnected){
80
+ [_wifiPrinterMonitor connectToHost:ip onPort:4000 error:&err];
81
+ }
82
+ NSDate* tmpStartData = [NSDate date];
83
+ while(_isFirstRece){
84
+ double timeout = [[NSDate date] timeIntervalSinceDate:tmpStartData];
85
+ if(timeout>2){//connect timeout
86
+ _isConnected=false;
87
+ _printerStatus=Disconnected;
88
+ _isUserDiscon=true;
89
+ NSLog(@"connect timeout,connect failed\n");
90
+ break;
91
+ }
92
+ }
93
+ if(!_isFirstRece){
94
+ _isConnected=true;
95
+ NSLog(@"didconnect,connect success\n");
96
+ _printerStatus=Normal;
97
+ [self exitForbidPrinting];
98
+ }
99
+ }
100
+ return _isConnected;
101
+ }
102
+ -(void)StartMonitor{
103
+ //Turn on the function of avoiding lost orders, send monitoring commands, and obtain the status of the printer through the timer
104
+ //Set a timer to execute the OnclickStart method regularly
105
+ //The timer needs to be added to the current sub-thread
106
+ _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(ReadDataFromPrinter) userInfo:nil repeats:YES];
107
+ NSRunLoop *runloop = [NSRunLoop currentRunLoop];
108
+ // [runloop addTimer:_timer forMode:NSDefaultRunLoopMode];
109
+ [runloop addTimer:_timer forMode:NSRunLoopCommonModes];
110
+ if(_monitorPort==_printPort&&_isConnected){
111
+ //free lost order,It needs to be set once, restarting the printer will take effect
112
+ Byte free[]={0x1B, 0x73, 0x42, 0x45, 0x92, 0x9A, 0x01, 0x00, 0x5F, 0x0A};
113
+ //9100 port monitor cmd
114
+ Byte b[]={0x1d,0x61,0x1f};
115
+ [_wifiPrinter writeData:[[NSData alloc]initWithBytes:b length:3] withTimeout:-1 tag:1];
116
+ //2022/7/5 DING Added the function of getting the firmware version
117
+ //NSLog(@"Get the printer firmware version");
118
+ [_wifiPrinter writeData:[self GetFirmwareVersion] withTimeout:-1 tag:2];
119
+ [_wifiPrinter readDataWithTimeout:-1 tag:2];
120
+ }
121
+ [runloop run];
122
+ }
123
+ -(NSData*)exitForbidPrinting{
124
+ Byte exitForbidPrinting[]={0x1b,0x41};//Exit the print prohibition state
125
+ return [[NSData alloc] initWithBytes:exitForbidPrinting length:sizeof(exitForbidPrinting)];
126
+ }
127
+ -(void)freeLostOrder{
128
+ Byte freeLostOrder[]={0x1B, 0x73, 0x42, 0x45, 0x92, 0x9A, 0x01, 0x00, 0x5F, 0x0A};//No Lost Order Order
129
+ [_wifiPrinter writeData:[[NSData alloc]initWithBytes:freeLostOrder length:freeLostOrder] withTimeout:-1 tag:0];
130
+ }
131
+ -(bool)ReconWifiPrinter:(NSString *)ip port:(UInt16)port{
132
+ if(!_isConnected){
133
+ _ip=ip;
134
+ _port=port;
135
+ NSError *err = nil;
136
+ _isFirstRece=true;
137
+ _isConnected=[_wifiPrinter connectToHost:ip onPort:port withTimeout:-1 error:&err];
138
+ if(!_isConnected){
139
+ _isConnected=false;
140
+ _printerStatus=Disconnected;
141
+ //NSLog(@"connect failed\n");
142
+ }else{
143
+ _isConnected=true;
144
+ //NSLog(@"connect success\n");
145
+ _printerStatus=Normal;
146
+ }
147
+ }
148
+ return _isConnected;
149
+ }
150
+ //connect ble printer
151
+ -(void)ConnectBlePrinter:(CBPeripheral *)peripheral{
152
+
153
+ }
154
+ //Disconnect the printer
155
+ - (void)DisConnectPrinter{
156
+ NSLog(@"disconnect printer");
157
+ _isUserDiscon=true;
158
+ switch (_printerType) {
159
+ case WifiPrinter:
160
+ {
161
+ [_wifiPrinter disconnect];
162
+ }
163
+ break;
164
+ case BlePrinter:
165
+ {
166
+
167
+ }
168
+ break;
169
+ default:
170
+ break;
171
+ }
172
+ }
173
+ //send data to printer
174
+ -(bool)SendReceiptToPrinter:(NSData *)data{
175
+ switch (_printerType) {
176
+ case WifiPrinter:
177
+ {
178
+ //Differentiate the printing process of old and new printers, and the old printers do not perform status query processing
179
+ _printSucceed=false;
180
+ if(_isNewPrinter){//New machine delivery process
181
+ if((_printerStatus==Normal||_printerStatus==PrintCompleted)&&_isConnected){
182
+ NSMutableData* dataM=[[NSMutableData alloc] init];
183
+ Byte init[]={0x1b,0x40};
184
+ [dataM appendBytes:init length:sizeof(init)];
185
+ [dataM appendData:data];
186
+ Byte cutPaper[]={0x1D,0x56,0x42,0x00,0x0A,0x0A,0x00};//The paper cutting instruction needs to be before the order instruction
187
+ Byte b[]={0x1D,0x28,0x48,0x06,0x00,0x30,0x30,0x30,0x30,0x30,0x31};//The order number will be returned to the client after printing the data
188
+ [dataM appendBytes:cutPaper length:sizeof(cutPaper)];
189
+ [dataM appendBytes:b length:sizeof(b)];
190
+ [_wifiPrinter writeData:dataM withTimeout:-1 tag:0];
191
+ NSDate* tmpStartData = [NSDate date];
192
+ while(!_printSucceed){
193
+ double timeout = [[NSDate date] timeIntervalSinceDate:tmpStartData];
194
+ if(timeout>60||_printerStatus==CoverOpened||_printerStatus==NoPaper||_printerStatus==Error||_printerStatus==Disconnected){//Print data timeout, the default is 1 minute; printing fails in error conditions
195
+ Byte exitForbidPrinting[]={0x1b,0x41};//Exit the print prohibition state
196
+ [_wifiPrinter writeData:[[NSData alloc]initWithBytes:exitForbidPrinting length:2] withTimeout:-1 tag:0];
197
+ NSLog(@"timeout,cost time = %f seconds", timeout);
198
+ _printSucceed=false;
199
+ if (_printerStatus==Normal) {
200
+ _isConnected=false;
201
+ _printerStatus=Disconnected;
202
+ }
203
+ break;
204
+ }
205
+ }
206
+ }else{
207
+ Byte exitForbidPrinting[]={0x1b,0x41};//Exit the print prohibition state
208
+ [_wifiPrinter writeData:[[NSData alloc]initWithBytes:exitForbidPrinting length:2] withTimeout:-1 tag:0];
209
+ _printSucceed=false;
210
+ NSLog(@"Printer status %@\n",[self statusToString:_printerStatus]);
211
+ }
212
+ }else{
213
+ [self SendDataToPrinter:data];
214
+ }
215
+ }
216
+ break;
217
+ case BlePrinter:
218
+ {
219
+
220
+ }
221
+ break;
222
+ default:
223
+ break;
224
+ }
225
+ return _printSucceed;
226
+ }
227
+ //Old machine sending process
228
+ -(bool)SendDataToPrinter:(NSData *)data{
229
+ switch (_printerType) {
230
+ case WifiPrinter:
231
+ {
232
+ _printSucceed=true;
233
+ if((_printerStatus==Normal)&&_isConnected){
234
+ //NSMutableData* dataM=[[NSMutableData alloc] initWithData:data];
235
+ //[_wifiPrinter writeData:dataM withTimeout:-1 tag:0];
236
+ NSMutableData* dataM=[[NSMutableData alloc] init];
237
+ Byte init[]={0x1b,0x40};
238
+ [dataM appendBytes:init length:sizeof(init)];
239
+ [dataM appendData:data];
240
+ Byte cutPaper[]={0x1D,0x56,0x42,0x00,0x0A,0x0A,0x00};
241
+ [dataM appendBytes:cutPaper length:sizeof(cutPaper)];
242
+ [_wifiPrinter writeData:dataM withTimeout:-1 tag:0];
243
+ NSDate* tmpStartData = [NSDate date];
244
+ while(!_isReceivedData){
245
+ double timeout = [[NSDate date] timeIntervalSinceDate:tmpStartData];
246
+ if(timeout>3||_printerStatus==CoverOpened||_printerStatus==NoPaper||_printerStatus==Error||_printerStatus==Disconnected){//Print data timeout, the default is 3 seconds; printing failed in error condition
247
+ Byte exitForbidPrinting[]={0x1b,0x41};//Exit the print prohibition state
248
+ [_wifiPrinter writeData:[[NSData alloc]initWithBytes:exitForbidPrinting length:sizeof(exitForbidPrinting)] withTimeout:-1 tag:0];
249
+ NSLog(@"timeout,cost time = %f seconds", timeout);
250
+ _printSucceed=false;
251
+ if (_printerStatus==Normal) {
252
+ _isConnected=false;
253
+ _printerStatus=Disconnected;
254
+ }
255
+ break;
256
+ }
257
+ }
258
+ }else{
259
+ Byte exitForbidPrinting[]={0x1b,0x41};//Exit the print prohibition state
260
+ [_wifiPrinter writeData:[[NSData alloc]initWithBytes:exitForbidPrinting length:sizeof(exitForbidPrinting)] withTimeout:-1 tag:0];
261
+ _printSucceed=false;
262
+ NSLog(@"Printer status %@\n",[self statusToString:_printerStatus]);
263
+ }
264
+ }
265
+ break;
266
+ case BlePrinter:
267
+ {
268
+
269
+ }
270
+ break;
271
+ default:
272
+ break;
273
+ }
274
+ return _printSucceed;
275
+ }
276
+ //Read the data returned by the printer
277
+ -(void)ReadDataFromPrinter{
278
+ _receivedData=nil;
279
+ if(_monitorPort==4000){
280
+ //4000 port monitoring command needs to be sent every time the printer status is read
281
+ Byte b[]={0x1b,0x76};
282
+ [_wifiPrinterMonitor writeData:[[NSData alloc]initWithBytes:b length:2] withTimeout:-1 tag:1];
283
+ //Call the read function and return via didread
284
+ [_wifiPrinterMonitor readDataWithTimeout:-1 tag:1];
285
+ }else{
286
+ //Call the read function and return via didread
287
+ [_wifiPrinter readDataWithTimeout:-1 tag:1];
288
+ }
289
+ }
290
+ //Get the current status of the printer
291
+ -(NSString*)GetPrinterStatus{
292
+ if(!_isReceivedData){
293
+ _printerStatus=Error;
294
+ }
295
+ return [self statusToString:_printerStatus];
296
+ }
297
+ //Whether the printing is complete or not
298
+ -(BOOL)IsPrintCompletely{
299
+ return false;
300
+ }
301
+ //printer status string
302
+ - (NSString*)statusToString:(PrinterStatus)printerStatus {
303
+ NSString *result = nil;
304
+ switch(printerStatus) {
305
+ case Normal:
306
+ result = @"Normal";
307
+ break;
308
+ case Error:
309
+ result = @"Error";
310
+ break;
311
+ case Printing:
312
+ result = @"Printing";
313
+ break;
314
+ case Busy:
315
+ result = @"Busy";
316
+ break;
317
+ case CashDrawerOpened:
318
+ result = @"CashDrawerOpened";
319
+ break;
320
+ case CoverOpened:
321
+ result = @"CoverOpened";
322
+ break;
323
+ case PaperNearEnd:
324
+ result = @"PaperNearEnd";
325
+ break;
326
+ case NoPaper:
327
+ result = @"NoPaper";
328
+ break;
329
+ case CutPaper:
330
+ result = @"CutPaper";
331
+ break;
332
+ case FeedPaper:
333
+ result = @"FeedPaper";
334
+ break;
335
+ case PrintCompleted:
336
+ result=@"PrintCompleted";
337
+ break;
338
+ case Disconnected:
339
+ result=@"Printer Disconnected";
340
+ break;
341
+ default:
342
+ [NSException raise:NSGenericException format:@"Unexpected status."];
343
+ }
344
+
345
+ return result;
346
+ }
347
+ //2022/7/5 DING Get the firmware version of the printer Only port 9100 is valid
348
+ -(NSData*)GetFirmwareVersion{
349
+ Byte data[] = {0x1d,0x49,0xd9};
350
+ return [[NSData alloc] initWithBytes:data length:sizeof(data)];
351
+ }
352
+ #pragma mark wifi
353
+ - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port{
354
+ if(sock.isConnected){
355
+ //NSLog(@"didconnect to host");
356
+ _isConnected=true;
357
+ _printerStatus=Normal;
358
+ _isFirstRece=false;
359
+ }
360
+ }
361
+ - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
362
+ if(sock.connectedPort==_monitorPort||sock.connectedPort==_printPort){
363
+ NSLog(@"data=%@,tag=%ld", data,tag);
364
+ _receivedData=data;
365
+ _isReceivedData=true;
366
+ Byte b[data.length];
367
+ bool printCompelte=false;
368
+ Byte jobID[]={0x37,0x22,0x30,0x30,0x30,0x31,0x00};
369
+ for (int i = 0 ; i < data.length; i++) {
370
+ NSData *idata = [data subdataWithRange:NSMakeRange(i, 1)];
371
+ b[i] =((Byte*)[idata bytes])[0];
372
+ }
373
+ //The order number of the printer may be returned together with the status, so it is necessary to judge the substring more accurately
374
+ for(int i=0,j=0;i<data.length&&data.length>=7&&j<sizeof(jobID);i++){
375
+ if(b[i]==jobID[j]){
376
+ j++;
377
+ if(j==sizeof(jobID)){
378
+ printCompelte=true;
379
+ _printSucceed=true;
380
+ _printerStatus=PrintCompleted;
381
+ }
382
+ }
383
+ }
384
+ if(data.length>=29){//Get the firmware version of the printer
385
+ //First remove the order number and status value that may be included in the string
386
+ NSString* printerInfo=[[NSString alloc] initWithData: [data subdataWithRange:NSMakeRange(data.length-29,29)] encoding:NSASCIIStringEncoding];
387
+ NSString* printerDate=[printerInfo substringToIndex:6];
388
+ if(printerDate!=nil && [printerDate compare:@"220401"]!=NSOrderedAscending){//2022.4.01 after
389
+ //NSLog(@"Printers after 2022.4.01");
390
+ _isNewPrinter=true;
391
+ }else{
392
+ //NSLog(@"Printers before 2022.4.01");
393
+ _isNewPrinter=false;
394
+ }
395
+ }
396
+ if(data.length==4){
397
+ //0x1400602f cant print
398
+ Byte nomralBytes[]={0x14,0x00,0x00,0x0f};
399
+ if(data.bytes==nomralBytes){
400
+ _printerStatus=Normal;
401
+ }
402
+ else{
403
+ //first byte
404
+ if ((b[0] & (0x04)) != 0) {//cash
405
+ _printerStatus=CashDrawerOpened;
406
+ _printerStatus=Normal;
407
+ } else {
408
+ //n
409
+ }
410
+ if ((b[0] & (0x08)) != 0) {// busy
411
+ _printerStatus=Busy;
412
+ } else {
413
+ //n
414
+ }
415
+ if ((b[0] & (0x20)) != 0) { // cover
416
+ _printerStatus=CoverOpened;
417
+ } else {
418
+ //n
419
+ }
420
+ if (((b[0] & (0x40)) != 0)){//feedpaper
421
+ _printerStatus=FeedPaper;
422
+ } else {
423
+ //n
424
+ }
425
+ //second byte
426
+ if ((b[1] & (0x08)) != 0) {//cutpaper
427
+ _printerStatus=CutPaper;
428
+ NSLog(@"Printer status %@\n",[self statusToString:_printerStatus]);
429
+ } else {
430
+ //n
431
+ }
432
+ if ((b[1] & (0x20)) != 0) {//fatal
433
+ _printerStatus=Error;
434
+ } else {
435
+ //n
436
+ }
437
+ if ((b[1] & (0x40)) != 0) {//autorestor
438
+ //y
439
+ } else {
440
+ //n
441
+ }
442
+
443
+ //third byte
444
+ if ((b[2] & (0x03)) != 0) {//papernearend
445
+ _printerStatus=PaperNearEnd;
446
+ } else {
447
+ //n
448
+ }
449
+
450
+ if ((b[2] & (0x0C)) != 0) {//nopaper
451
+ _printerStatus=NoPaper;
452
+ } else {
453
+ //n
454
+ }
455
+
456
+ if ((b[2] & (0x40)) != 0) {//printpaper
457
+ _printerStatus=Printing;
458
+ _printerStatus=Normal;
459
+ } else {
460
+ //n
461
+ }
462
+ }
463
+ }
464
+
465
+ }
466
+ }
467
+ - (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag{
468
+ if(sock.isDisconnected){
469
+ _isConnected=false;
470
+ _printerStatus=Disconnected;
471
+ }
472
+ }
473
+ - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(nullable NSError *)err{
474
+ if(sock.isDisconnected){
475
+ _isConnected=false;
476
+ _printerStatus=Disconnected;
477
+ // NSLog(@"printer disconnected");
478
+ if(_isAutoRecon&&!_isUserDiscon){
479
+ //Do not repeat if reconnection fails
480
+ _isAutoRecon=[self ReconWifiPrinter:_ip port:_port];
481
+ if(_isAutoRecon){
482
+ //NSLog(@"reconnect success");
483
+ }else{
484
+ NSLog(@"reconnect failed,check printer ip or power");
485
+ }
486
+ }
487
+ }
488
+ }
489
+ #pragma mark ble
490
+ - (void)BLEManagerDelegate:(BLEManager *)BLEmanager connectPeripheral:(CBPeripheral *)peripheral {
491
+
492
+ }
493
+
494
+ - (void)BLEManagerDelegate:(BLEManager *)BLEmanager didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
495
+
496
+ }
497
+
498
+ - (void)BLEManagerDelegate:(BLEManager *)BLEmanager didWriteValueForCharacteristic:(CBCharacteristic *)character error:(NSError *)error {
499
+
500
+ }
501
+
502
+ - (void)BLEManagerDelegate:(BLEManager *)BLEmanager disconnectPeripheral:(CBPeripheral *)peripheral isAutoDisconnect:(BOOL)isAutoDisconnect {
503
+
504
+ }
505
+
506
+ - (void)BLEManagerDelegate:(BLEManager *)BLEmanager updatePeripheralList:(NSArray *)peripherals RSSIList:(NSArray *)RSSIArr {
507
+
508
+ }
509
+
510
+ @end
@@ -0,0 +1,38 @@
1
+ //
2
+ // ImageTranster.h
3
+ // Printer
4
+ //
5
+ // Created by LeeLee on 16/7/19.
6
+ // Copyright © 2016年 Admin. All rights reserved.
7
+ //
8
+
9
+ #import <Foundation/Foundation.h>
10
+ #import <UIKit/UIKit.h>
11
+ @interface ImageTranster : NSObject
12
+
13
+ typedef enum {
14
+ Dithering=0,//图片单色处理的方式:二值法
15
+ Threshold//抖动算法
16
+ } BmpType;
17
+
18
+ typedef enum {
19
+ RasterNolmorWH=0,//打印光栅位图的模式:正常大小
20
+ RasterDoubleWidth,//倍宽
21
+ RasterDoubleHeight,//倍高
22
+ RasterDoubleWH//倍宽高
23
+ } PrintRasterType;
24
+
25
+
26
+
27
+ -(UIImage *)covertToGrayScale:(UIImage*)image;
28
+ /// Convert the picture to grayscale and then into printer format data
29
+ /// @param mImage The picture to be converted
30
+ /// @param bmptype Image conversion algorithm type
31
+ +(NSData *)Imagedata:(UIImage *) mImage andType:(BmpType) bmptype;
32
+ /// Convert pictures to raster bitmap format
33
+ /// @param mIamge The picture to be converted
34
+ /// @param bmptype Image conversion algorithm type
35
+ /// @param type The type of picture print size
36
+ +(NSData *)rasterImagedata:(UIImage *) mIamge andType:(BmpType) bmptype andPrintRasterType:(PrintRasterType) type;
37
+ +(UIImage *) imageCompressForWidthScale:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth;
38
+ @end