auctifera-josed / starprnt

Cordova plugin for the Star micronics printers
MIT License
40 stars 39 forks source link

checkStatus send StarIOPortException instead of PrinterStatus #40

Open MMartinenqInfonial opened 5 years ago

MMartinenqInfonial commented 5 years ago

I'm using this sample code:

this.selectedPrinterStatus = null;
this.starPRNT.checkStatus(this.selectedPrinter.portName, StarPRNT.Emulation.StarPRNT)
    .then((printerStatus: PrinterStatus) => {
        this.selectedPrinterStatus = printerStatus;
     })
    .catch((error: any) => {
        console.log(error);
    });

With it, I want to get the status of a printer, and it works when there isn't any error on the printer (so when the cover is closed, the paper isn't empty and the cutter is ready).

But when I test to run this code when I open the cover, of anything that make the status offline, I get a StarIOPortException ('printer is offline') instead of the status with the flag corresponding to the offline causes.

To resolve that, in the file 'cordova-plugin-starprnt/src/android/StarPRNT.java', I tried to edit the checkStatus method :

public void checkStatus(String portName, String portSettings, CallbackContext callbackContext) {

        final Context context = this.cordova.getActivity();
        final CallbackContext _callbackContext = callbackContext;

        final String _portName = portName;
        final String _portSettings = portSettings;

        cordova.getThreadPool()
                .execute(new Runnable() {
                    public void run() {

                        StarIOPort port = null;
                        try {

                            port = StarIOPort.getPort(_portName, _portSettings, 10000, context);

                            // A sleep is used to get time for the socket to completely open
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                            }

                            StarPrinterStatus status;
                            Map<String, String> firmwareInformationMap = port.getFirmwareInformation();
                            status = port.retreiveStatus();

                            JSONObject json = new JSONObject();
                            try {
                                json.put("offline", status.offline);
                                json.put("coverOpen", status.coverOpen);
                                json.put("cutterError", status.cutterError);
                                json.put("receiptPaperEmpty", status.receiptPaperEmpty);
                                json.put("ModelName", firmwareInformationMap.get("ModelName"));
                                json.put("FirmwareVersion", firmwareInformationMap.get("FirmwareVersion"));

                                // TEST
                                //json.put("ModelName", "model");
                                //json.put("FirmwareVersion", "version");
                            } catch (JSONException ex) {

                            } finally {
                                _callbackContext.success(json);
                            }

                        } catch (StarIOPortException e) {
                            _callbackContext.error("Failed to connect to printer :" + e.getMessage()); 
                        } finally {

                            if (port != null) {
                                try {

                                    StarIOPort.releasePort(port);
                                } catch (StarIOPortException e) {
                                    _callbackContext.error("Failed to connect to printer" + e.getMessage());
                                }
                            }

                        }

                    }
                });
    }

I identified (by comment / decomment) that my issue is caused by the getFirmwareInformation method, which it's called to get the ModelName and the FirmwareVersion. It's a StarIO method, so I can't modify its behaviour.

Have you any ideas to solve this problem?