gnaudio / jabra-node-sdk

Jabra Node.Js SDK based on N-API
https://www.npmjs.com/package/@gnaudio/jabra-node-sdk
MIT License
0 stars 1 forks source link

isBatterySupportedAsync, getBatteryStatusAsync sometimes not responding back #136

Open mavitm opened 2 years ago

mavitm commented 2 years ago

hi i am using electron js

"electron": "^11.4.5",
const j = require("@gnaudio/jabra-node-sdk");

class Jabra {
    constructor() {
        this.jabra              = null;
        this.device             = null;
        this.isBatterySupport   = false;
        this.batteryInfo        = {};
        this.attached           = false;

        this.connectResolve     = function () {};
        this.connectReject      = function () {};
    }

    async connect(){
        let that = this;
        return new Promise(function (resolve, reject) {

            that.connectResolve = resolve;
            that.connectReject  = reject;

            if(that.jabra !== null){
               that.connectResolve(that.device);
            }
            else {
                that._connect();
            }
        });
    }

    _connect(){
        let that = this;
        j.createJabraApplication('123').then((jabra) => {
            that.jabra = jabra;

            this.jabra.on('attach', this.attachDevice.bind(that));
            this.jabra.on('detach', this.detachDevice.bind(that));

            setTimeout(() => {
                if (that.attached !== true) {
                    that.connectReject("jabra sdk time out");
                    that.close();
                }
            }, 10000);

        }).catch(function (e) {
            that.connectReject("jabra sdk connection error");
            console.log(e);
        });
    }

    attachDevice(device){
        this.attached = true;
        this.device = device;
        this.connectResolve(this.device);
    }

    async detachDevice(device){
        if (this.jabra.getAttachedDevices().length < 1) {
            await this.close();
        }
    }

    async requestBatteryInfo(){
        let that = this;
        console.log("being pronounced battery ...");
        return new Promise(function (resolve, reject) {
            let getInfo = false;
            that.device.isBatterySupportedAsync().then(async (supported)=>{
                that.isBatterySupport = supported;
                if(that.isBatterySupport) {
                    that.device.getBatteryStatusAsync().then(function (b1) {
                        that.batteryInfo = b1;
                        console.log("BATTERY", that.batteryInfo);
                        resolve(that);
                        getInfo = true;
                    }).catch(function (e) {
                        getInfo = true;
                        reject(e);
                    });
                }else{
                    getInfo = true;
                    reject("device not supported battery info");
                }
            }).catch(function (e) {
                console.log(e);
                reject(e);
                getInfo = true;
            });

            setTimeout(() => {
                if (getInfo !== true) {
                    reject("jabra device time out");
                }
            }, 25000);
        });
    }

    async close(){
        let that = this;
        try {
            await that.jabra.disposeAsync();
            that.clear();
        }catch (e) {
            //console.log(e);
            that.clear();
        }
    }

    clear(){
        this.attached           = false;
        this.device             = null;
        this.jabra              = null;
    }
}
export default Jabra;

while using

app.on('ready', async () => {
       let that = this;
       let sendData = {battery: null};

       if (that.jabraSdk === null) {
            that.jabraSdk = new Jabra();
        }

        that.jabraSdk.connect().then(async (device) => {
            console.log("jabra device connected");

            that.jabraSdk.requestBatteryInfo().then(function (jabra) {
                if (jabra.isBatterySupport) {
                    sendData = {battery: jabra.batteryInfo};
                    app.events.emit("headset_battery_info", sendData);
                } else {
                    app.events.emit("headset_battery_info", sendData);
                }
            }).catch(function (e) {
                console.log(e);
                app.events.emit("headset_battery_info", sendData);
            });
        }).catch((e) => {
            console.log(e);
            app.events.emit("headset_battery_info", sendData);
        });
});

When the application first starts, there is no problem, I get the battery information, but if I refresh a few times, I start to get time out after a while.

requestBatteryInfo method waits for 25 seconds and gives time error, when I exit the application, zombie processes remain in the task manager and the application does not kill properly.

TypeError: Cannot read property 'getBatteryStatusAsync' of null
    at eval (webpack:///./src/background/headset/Jabra.js?:158:33)
TypeError: Cannot read property 'getBatteryStatusAsync' of null
    at eval (webpack:///./src/background/headset/Jabra.js?:158:33)
TypeError: Cannot read property 'getBatteryStatusAsync' of null
    at eval (webpack:///./src/background/headset/Jabra.js?:158:33)

sometimes it can also point to the isBatterySupportedAsync method