iOS update

* fix printer reconnect issue
 * implement printer status check
This commit is contained in:
Evan Moore 2019-04-04 11:52:01 -04:00
parent 5adcb23dd0
commit 6b09f01b4f
7 changed files with 227 additions and 86 deletions

4
.gitignore vendored
View File

@ -1 +1,3 @@
node_modules node_modules
src/ios/.DS_Store
src/ios/lib/.DS_Store

3
native/index.d.ts vendored
View File

@ -37,10 +37,9 @@ export interface PrinterStatus {
* ``` * ```
*/ */
export declare class ZebraPrinter extends IonicNativePlugin { export declare class ZebraPrinter extends IonicNativePlugin {
echo(value: string): Promise<any>;
print(cpcl: string): Promise<any>; print(cpcl: string): Promise<any>;
isConnected(): Promise<boolean>; isConnected(): Promise<boolean>;
printerStatus(adderss: string): Promise<PrinterStatus>; printerStatus(): Promise<PrinterStatus>;
connect(adress: string): Promise<any>; connect(adress: string): Promise<any>;
disconnect(): Promise<any>; disconnect(): Promise<any>;
discover(): Promise<Array<Printer>>; discover(): Promise<Array<Printer>>;

View File

@ -56,7 +56,6 @@ var ZebraPrinter = (function (_super) {
function ZebraPrinter() { function ZebraPrinter() {
return _super !== null && _super.apply(this, arguments) || this; return _super !== null && _super.apply(this, arguments) || this;
} }
ZebraPrinter.prototype.echo = function (value) { return; };
ZebraPrinter.prototype.print = function (cpcl) { return; }; ZebraPrinter.prototype.print = function (cpcl) { return; };
ZebraPrinter.prototype.isConnected = function () { return; }; ZebraPrinter.prototype.isConnected = function () { return; };
ZebraPrinter.prototype.printerStatus = function (adderss) { return; }; ZebraPrinter.prototype.printerStatus = function (adderss) { return; };
@ -68,12 +67,6 @@ var ZebraPrinter = (function (_super) {
]; ];
/** @nocollapse */ /** @nocollapse */
ZebraPrinter.ctorParameters = function () { return []; }; ZebraPrinter.ctorParameters = function () { return []; };
__decorate([
Cordova(),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", Promise)
], ZebraPrinter.prototype, "echo", null);
__decorate([ __decorate([
Cordova(), Cordova(),
__metadata("design:type", Function), __metadata("design:type", Function),

39
native/package-lock.json generated Normal file
View File

@ -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=="
}
}
}

View File

@ -1,6 +1,8 @@
#import "ZebraPrinterConnection.h" #import "ZebraPrinterConnection.h"
#import "ZebraPrinter.h" #import "ZebraPrinter.h"
#import "ZebraPrinterFactory.h" #import "ZebraPrinterFactory.h"
#import "PrinterStatus.h"
#import "PrinterStatusMessages.h"
#import "TcpPrinterConnection.h" #import "TcpPrinterConnection.h"
#import "MFiBtPrinterConnection.h" #import "MFiBtPrinterConnection.h"
#import "SGD.h" #import "SGD.h"

View File

@ -4,23 +4,12 @@ import ExternalAccessory
@objc(ZebraPrinterPlugin) @objc(ZebraPrinterPlugin)
class ZebraPrinterPlugin: CDVPlugin { class ZebraPrinterPlugin: CDVPlugin {
var printerConnection: ZebraPrinterConnection? var printerConnection: ZebraPrinterConnection?
var printer: ZebraPrinter?
@objc func echo(_ command: CDVInvokedUrlCommand) { /**
var pluginResult = CDVPluginResult( * Discover connectable zebra printers
status: CDVCommandStatus_ERROR *
) */
let value = command.arguments[0] as? String ?? ""
pluginResult = CDVPluginResult(
status: CDVCommandStatus_OK,
messageAs: value
)
self.commandDelegate!.send(
pluginResult,
callbackId: command.callbackId
)
}
@objc func discover(_ command: CDVInvokedUrlCommand){ @objc func discover(_ command: CDVInvokedUrlCommand){
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
let manager = EAAccessoryManager.shared() 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) { @objc func print(_ command: CDVInvokedUrlCommand) {
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
let cpcl = command.arguments[0] as? String ?? "" let cpcl = command.arguments[0] as? String ?? ""
@ -60,6 +136,7 @@ class ZebraPrinterPlugin: CDVPlugin {
self.printerConnection!.open() self.printerConnection!.open()
self.printerConnection!.write(data, error:&error) self.printerConnection!.write(data, error:&error)
if error != nil{ if error != nil{
NSLog("ZebraPrinter:: error printing -> " + (error?.localizedDescription ?? "Unknonwn Error"))
let pluginResult = CDVPluginResult( let pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR status: CDVCommandStatus_ERROR
) )
@ -67,28 +144,34 @@ class ZebraPrinterPlugin: CDVPlugin {
pluginResult, pluginResult,
callbackId: command.callbackId callbackId: command.callbackId
) )
return }else{
NSLog("ZebraPrinter:: print completed")
let pluginResult = CDVPluginResult(
status: CDVCommandStatus_OK
)
self.commandDelegate!.send(
pluginResult,
callbackId: command.callbackId
)
} }
}else{ }else{
NSLog("ZebraPrinter:: not connected")
let pluginResult = CDVPluginResult( let pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR status: CDVCommandStatus_ERROR,
messageAs: "Printer Not Connected"
) )
self.commandDelegate!.send( self.commandDelegate!.send(
pluginResult, pluginResult,
callbackId: command.callbackId 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){ @objc func isConnected(_ command: CDVInvokedUrlCommand){
let pluginResult = CDVPluginResult( let pluginResult = CDVPluginResult(
status: CDVCommandStatus_OK, status: CDVCommandStatus_OK,
@ -99,78 +182,104 @@ class ZebraPrinterPlugin: CDVPlugin {
callbackId: command.callbackId callbackId: command.callbackId
) )
} }
/**
* Check if we are connectd to the printer or not
*
*/
private func isConnected() -> Bool{ 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 * Connect to a printer by serialNumber
// //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,
// ])
// }
@objc func connect(_ command: CDVInvokedUrlCommand){ @objc func connect(_ command: CDVInvokedUrlCommand){
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
var pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR
)
let address = command.arguments[0] as? String ?? "" let address = command.arguments[0] as? String ?? ""
if(address == ""){ if(address == ""){
NSLog("ZebraPrinter:: empty printer address")
let pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR,
messageAs: "Invalid Address"
)
self.commandDelegate!.send( self.commandDelegate!.send(
pluginResult, pluginResult,
callbackId: command.callbackId callbackId: command.callbackId
) )
return 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) self.printerConnection = MfiBtPrinterConnection(serialNumber: address)
NSLog("ZebraPrinter:: got connection. opening...")
self.printerConnection?.open() self.printerConnection?.open()
NSLog("ZebraPrinter:: opened connection")
if( self.isConnected()){ 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( self.commandDelegate!.send(
pluginResult, pluginResult,
callbackId: command.callbackId callbackId: command.callbackId
) )
return
} }
}else{ }else{
NSLog("ZebraPrinter:: not connected")
let pluginResult = CDVPluginResult(
status: CDVCommandStatus_ERROR,
messageAs: "Printer Not Connected"
)
self.commandDelegate!.send( self.commandDelegate!.send(
pluginResult, pluginResult,
callbackId: command.callbackId 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){ @objc func disconnect(_ command: CDVInvokedUrlCommand){
//TODO //close the connection and set it to nil
if(isConnected()){ if(self.printerConnection != nil){
printerConnection?.close() self.printerConnection?.close()
self.printerConnection = nil
self.printer = nil
} }
let pluginResult = CDVPluginResult( let pluginResult = CDVPluginResult(
@ -180,8 +289,5 @@ class ZebraPrinterPlugin: CDVPlugin {
pluginResult, pluginResult,
callbackId: command.callbackId callbackId: command.callbackId
) )
} }
} }

View File

@ -1,9 +1,5 @@
var exec = require('cordova/exec'); var exec = require('cordova/exec');
exports.echo = function (arg0, success, error) {
exec(success, error, 'ZebraPrinter', 'echo', [arg0]);
};
exports.discover = function (success, error) { exports.discover = function (success, error) {
exec(success, error, 'ZebraPrinter', 'discover', []); exec(success, error, 'ZebraPrinter', 'discover', []);
}; };
@ -16,6 +12,10 @@ exports.disconnect = function (success, error) {
exec(success, error, 'ZebraPrinter', 'disconnect', []); exec(success, error, 'ZebraPrinter', 'disconnect', []);
}; };
exports.printerStatus = function(success, error){
exec(success, error, 'ZebraPrinter', 'printerStatus', []);
}
exports.isConnected = function (success, error) { exports.isConnected = function (success, error) {
exec(success, error, 'ZebraPrinter', 'isConnected', []); exec(success, error, 'ZebraPrinter', 'isConnected', []);
}; };