From 6b09f01b4f674180e3bd246159899355ed59b530 Mon Sep 17 00:00:00 2001 From: Evan Moore Date: Thu, 4 Apr 2019 11:52:01 -0400 Subject: [PATCH] iOS update * fix printer reconnect issue * implement printer status check --- .gitignore | 4 +- native/index.d.ts | 3 +- native/index.js | 7 - native/package-lock.json | 39 ++++ src/ios/ZebraPrinter-Bridging-Header.h | 4 +- src/ios/ZebraPrinterPlugin.swift | 248 ++++++++++++++++++------- www/ZebraPrinter.js | 8 +- 7 files changed, 227 insertions(+), 86 deletions(-) create mode 100644 native/package-lock.json diff --git a/.gitignore b/.gitignore index b512c09..a296600 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -node_modules \ No newline at end of file +node_modules +src/ios/.DS_Store +src/ios/lib/.DS_Store diff --git a/native/index.d.ts b/native/index.d.ts index 4d82ad8..f15e4cf 100644 --- a/native/index.d.ts +++ b/native/index.d.ts @@ -37,10 +37,9 @@ export interface PrinterStatus { * ``` */ export declare class ZebraPrinter extends IonicNativePlugin { - echo(value: string): Promise; print(cpcl: string): Promise; isConnected(): Promise; - printerStatus(adderss: string): Promise; + printerStatus(): Promise; connect(adress: string): Promise; disconnect(): Promise; discover(): Promise>; diff --git a/native/index.js b/native/index.js index 3425f0e..dbbd7bc 100644 --- a/native/index.js +++ b/native/index.js @@ -56,7 +56,6 @@ var ZebraPrinter = (function (_super) { function ZebraPrinter() { return _super !== null && _super.apply(this, arguments) || this; } - ZebraPrinter.prototype.echo = function (value) { return; }; ZebraPrinter.prototype.print = function (cpcl) { return; }; ZebraPrinter.prototype.isConnected = function () { return; }; ZebraPrinter.prototype.printerStatus = function (adderss) { return; }; @@ -68,12 +67,6 @@ var ZebraPrinter = (function (_super) { ]; /** @nocollapse */ ZebraPrinter.ctorParameters = function () { return []; }; - __decorate([ - Cordova(), - __metadata("design:type", Function), - __metadata("design:paramtypes", [String]), - __metadata("design:returntype", Promise) - ], ZebraPrinter.prototype, "echo", null); __decorate([ Cordova(), __metadata("design:type", Function), diff --git a/native/package-lock.json b/native/package-lock.json new file mode 100644 index 0000000..d73a2a5 --- /dev/null +++ b/native/package-lock.json @@ -0,0 +1,39 @@ +{ + "name": "@ionic-native/zebra-printer", + "version": "4.7.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@angular/core": { + "version": "7.2.11", + "resolved": "https://registry.npmjs.org/@angular/core/-/core-7.2.11.tgz", + "integrity": "sha512-2FUgXbGn75D6HQESVVmnrjqP1l2YNwwIZISembzpr4WvTm0lxzq/9WsuPFJNWggwCerajyjYz+kDJT3RsonGZg==", + "requires": { + "tslib": "^1.9.0" + } + }, + "@ionic-native/core": { + "version": "4.20.0", + "resolved": "https://registry.npmjs.org/@ionic-native/core/-/core-4.20.0.tgz", + "integrity": "sha512-8ppRT4zXKwtalv9HJomLQjDnMfPAiKdNUQSSKpwZePmI+8TpYRL7UNr0o0hmjiTXx1oGcKQFzHpDc1M2yeR3BA==" + }, + "rxjs": { + "version": "5.5.12", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-5.5.12.tgz", + "integrity": "sha512-xx2itnL5sBbqeeiVgNPVuQQ1nC8Jp2WfNJhXWHmElW9YmrpS9UVnNzhP3EH3HFqexO5Tlp8GhYY+WEcqcVMvGw==", + "requires": { + "symbol-observable": "1.0.1" + } + }, + "symbol-observable": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/symbol-observable/-/symbol-observable-1.0.1.tgz", + "integrity": "sha1-g0D8RwLDEi310iKI+IKD9RPT/dQ=" + }, + "tslib": { + "version": "1.9.3", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", + "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==" + } + } +} diff --git a/src/ios/ZebraPrinter-Bridging-Header.h b/src/ios/ZebraPrinter-Bridging-Header.h index d17fa00..d52dce8 100644 --- a/src/ios/ZebraPrinter-Bridging-Header.h +++ b/src/ios/ZebraPrinter-Bridging-Header.h @@ -1,6 +1,8 @@ #import "ZebraPrinterConnection.h" #import "ZebraPrinter.h" #import "ZebraPrinterFactory.h" +#import "PrinterStatus.h" +#import "PrinterStatusMessages.h" #import "TcpPrinterConnection.h" #import "MFiBtPrinterConnection.h" -#import "SGD.h" \ No newline at end of file +#import "SGD.h" diff --git a/src/ios/ZebraPrinterPlugin.swift b/src/ios/ZebraPrinterPlugin.swift index 7d499b3..112da82 100644 --- a/src/ios/ZebraPrinterPlugin.swift +++ b/src/ios/ZebraPrinterPlugin.swift @@ -4,23 +4,12 @@ import ExternalAccessory @objc(ZebraPrinterPlugin) class ZebraPrinterPlugin: CDVPlugin { var printerConnection: ZebraPrinterConnection? + var printer: ZebraPrinter? - @objc func echo(_ command: CDVInvokedUrlCommand) { - var pluginResult = CDVPluginResult( - status: CDVCommandStatus_ERROR - ) - let value = command.arguments[0] as? String ?? "" - pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK, - messageAs: value - ) - - self.commandDelegate!.send( - pluginResult, - callbackId: command.callbackId - ) - } - + /** + * Discover connectable zebra printers + * + */ @objc func discover(_ command: CDVInvokedUrlCommand){ DispatchQueue.global(qos: .background).async { let manager = EAAccessoryManager.shared() @@ -48,6 +37,93 @@ class ZebraPrinterPlugin: CDVPlugin { } } + /** + * Get the status of the printer we are currently connected to + * + */ + @objc func printerStatus(_ command: CDVInvokedUrlCommand){ + DispatchQueue.global(qos: .background).async { + var status = [String: Any]() + status["connected"] = false + status["isReadyToPrint"] = false + status["isPaused"] = false + status["isReceiveBufferFull"] = false + status["isRibbonOut"] = false + status["isPaperOut"] = false + status["isHeadTooHot"] = false + status["isHeadOpen"] = false + status["isHeadCold"] = false + status["isPartialFormatInProgress"] = false + + if(self.printerConnection != nil && self.printerConnection!.isConnected() && self.printer != nil){ + let zebraPrinterStatus = try? self.printer?.getCurrentStatus() + if(zebraPrinterStatus != nil){ + NSLog("Got Printer Status") + if(zebraPrinterStatus!.isReadyToPrint) { NSLog("Read To Print"); } + else { + let message = PrinterStatusMessages.init(printerStatus: zebraPrinterStatus) + if(message != nil) + { + NSLog("Printer Not Ready. " + (message!.getStatusMessage() as! [String]).joined(separator: ", ")) + }else{ + NSLog("Printer Not Ready.") + } + } + + status["connected"] = true + status["isReadyToPrint"] = zebraPrinterStatus?.isReadyToPrint + status["isPaused"] = zebraPrinterStatus?.isPaused + status["isReceiveBufferFull"] = zebraPrinterStatus?.isReceiveBufferFull + status["isRibbonOut"] = zebraPrinterStatus?.isRibbonOut + status["isPaperOut"] = zebraPrinterStatus?.isPaperOut + status["isHeadTooHot"] = zebraPrinterStatus?.isHeadTooHot + status["isHeadOpen"] = zebraPrinterStatus?.isHeadOpen + status["isHeadCold"] = zebraPrinterStatus?.isHeadCold + status["isPartialFormatInProgress"] = zebraPrinterStatus?.isPartialFormatInProgress + + NSLog("ZebraPrinter:: returning status") + let pluginResult = CDVPluginResult( + status: CDVCommandStatus_OK, + messageAs: status + ) + self.commandDelegate!.send( + pluginResult, + callbackId: command.callbackId + ) + return + }else{ + // printer has no status... this happens when the printer turns off, but the driver still thinks it is connected + NSLog("ZebraPrinter:: Got a printer but no status. Sadness.") + let pluginResult = CDVPluginResult( + status: CDVCommandStatus_ERROR, + messageAs: "Printer Has No Status" + ) + self.commandDelegate!.send( + pluginResult, + callbackId: command.callbackId + ) + return + } + }else{ + NSLog("ZebraPrinter:: status of disconnected printer") + // if the printer isn't connected return success with disconnect status + let pluginResult = CDVPluginResult( + status: CDVCommandStatus_OK, + messageAs: status + ) + self.commandDelegate!.send( + pluginResult, + callbackId: command.callbackId + ) + return + } + } + } + + /** + * Print the cpcl + * + */ @objc func print(_ command: CDVInvokedUrlCommand) { DispatchQueue.global(qos: .background).async { let cpcl = command.arguments[0] as? String ?? "" @@ -60,6 +136,7 @@ class ZebraPrinterPlugin: CDVPlugin { self.printerConnection!.open() self.printerConnection!.write(data, error:&error) if error != nil{ + NSLog("ZebraPrinter:: error printing -> " + (error?.localizedDescription ?? "Unknonwn Error")) let pluginResult = CDVPluginResult( status: CDVCommandStatus_ERROR ) @@ -67,28 +144,34 @@ class ZebraPrinterPlugin: CDVPlugin { pluginResult, callbackId: command.callbackId ) - return + }else{ + NSLog("ZebraPrinter:: print completed") + let pluginResult = CDVPluginResult( + status: CDVCommandStatus_OK + ) + self.commandDelegate!.send( + pluginResult, + callbackId: command.callbackId + ) } }else{ + NSLog("ZebraPrinter:: not connected") let pluginResult = CDVPluginResult( - status: CDVCommandStatus_ERROR + status: CDVCommandStatus_ERROR, + messageAs: "Printer Not Connected" ) self.commandDelegate!.send( pluginResult, callbackId: command.callbackId ) - return } - let pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK - ) - self.commandDelegate!.send( - pluginResult, - callbackId: command.callbackId - ) } } + /** + * Check if we are connectd to the printer or not + * + */ @objc func isConnected(_ command: CDVInvokedUrlCommand){ let pluginResult = CDVPluginResult( status: CDVCommandStatus_OK, @@ -99,78 +182,104 @@ class ZebraPrinterPlugin: CDVPlugin { callbackId: command.callbackId ) } - + + /** + * Check if we are connectd to the printer or not + * + */ private func isConnected() -> Bool{ - return (printerConnection != nil && printerConnection!.isConnected()) + //printerConnection!.isConnected lies, it says it's open when it isn't + return self.printerConnection != nil && (self.printerConnection?.isConnected() ?? false) } - - // @objc func printerStatus(_ call:CAPPluginCall){ - // //TODO - // //let address = call.getString("MACAddress") ?? "" - // //Return status - // call.success([ - // "connected": true, - // "isReadyToPrint": false, - // "isPaused": false, - // "isReceiveBufferFull": false, - // "isRibbonOut": false, - // "isPaperOut": false, - // "isHeadTooHot": false, - // "isHeadOpen": false, - // "isHeadCold": false, - // "isPartialFormatInProgress": false, - // ]) - // } - + + /** + * Connect to a printer by serialNumber + * + */ @objc func connect(_ command: CDVInvokedUrlCommand){ DispatchQueue.global(qos: .background).async { - var pluginResult = CDVPluginResult( - status: CDVCommandStatus_ERROR - ) let address = command.arguments[0] as? String ?? "" if(address == ""){ + NSLog("ZebraPrinter:: empty printer address") + let pluginResult = CDVPluginResult( + status: CDVCommandStatus_ERROR, + messageAs: "Invalid Address" + ) self.commandDelegate!.send( pluginResult, callbackId: command.callbackId ) return } + + NSLog("ZebraPrinter:: connecting to " + address) + + //try to close an existing connection + if(self.printerConnection != nil){ + self.printerConnection?.close() + } + + //clear out our existing printer & connection + self.printerConnection = nil; + self.printer = nil; + + //create and open a new connection self.printerConnection = MfiBtPrinterConnection(serialNumber: address) + NSLog("ZebraPrinter:: got connection. opening...") self.printerConnection?.open() + NSLog("ZebraPrinter:: opened connection") + if( self.isConnected()){ - let printer = try? ZebraPrinterFactory.getInstance(self.printerConnection as! NSObjectProtocol & ZebraPrinterConnection) + NSLog("ZebraPrinter:: getting printer") + self.printer = try? ZebraPrinterFactory.getInstance(self.printerConnection as? NSObjectProtocol & ZebraPrinterConnection) + NSLog("ZebraPrinter:: got printer") - if(printer == nil) + if(self.printer == nil) { + NSLog("ZebraPrinter:: nil printer") + let pluginResult = CDVPluginResult( + status: CDVCommandStatus_ERROR, + messageAs: "Printer Null" + ) + self.commandDelegate!.send( + pluginResult, + callbackId: command.callbackId + ) + }else{ + NSLog("ZebraPrinter:: connected") + let pluginResult = CDVPluginResult( + status: CDVCommandStatus_OK, + messageAs: "Printer Connected" + ) self.commandDelegate!.send( pluginResult, callbackId: command.callbackId ) - return } }else{ + NSLog("ZebraPrinter:: not connected") + let pluginResult = CDVPluginResult( + status: CDVCommandStatus_ERROR, + messageAs: "Printer Not Connected" + ) self.commandDelegate!.send( pluginResult, callbackId: command.callbackId ) - return - } - - pluginResult = CDVPluginResult( - status: CDVCommandStatus_OK - ) - self.commandDelegate!.send( - pluginResult, - callbackId: command.callbackId - ) } } - + + /** + * Disconnect fromt the currently connected printer + * + */ @objc func disconnect(_ command: CDVInvokedUrlCommand){ - //TODO - if(isConnected()){ - printerConnection?.close() + //close the connection and set it to nil + if(self.printerConnection != nil){ + self.printerConnection?.close() + self.printerConnection = nil + self.printer = nil } let pluginResult = CDVPluginResult( @@ -180,8 +289,5 @@ class ZebraPrinterPlugin: CDVPlugin { pluginResult, callbackId: command.callbackId ) - } - - - + } } diff --git a/www/ZebraPrinter.js b/www/ZebraPrinter.js index 532f5cc..57bad32 100644 --- a/www/ZebraPrinter.js +++ b/www/ZebraPrinter.js @@ -1,9 +1,5 @@ var exec = require('cordova/exec'); -exports.echo = function (arg0, success, error) { - exec(success, error, 'ZebraPrinter', 'echo', [arg0]); -}; - exports.discover = function (success, error) { exec(success, error, 'ZebraPrinter', 'discover', []); }; @@ -16,6 +12,10 @@ exports.disconnect = function (success, error) { exec(success, error, 'ZebraPrinter', 'disconnect', []); }; +exports.printerStatus = function(success, error){ + exec(success, error, 'ZebraPrinter', 'printerStatus', []); +} + exports.isConnected = function (success, error) { exec(success, error, 'ZebraPrinter', 'isConnected', []); };