huawei-lte-api-ts icon indicating copy to clipboard operation
huawei-lte-api-ts copied to clipboard

Question - How to use "logout/close" to run a api call every 5 sec.

Open grawsom opened this issue 3 years ago • 4 comments

Let's take ref. in you example/device_info.js. If i execute that every 5 sec. after 4 or 5 times I get a ResponseErrorException: 108003: Already login Just used this one for test, but if that one is not working, then the ones I want to use will neither.

Believe there must be a way to close connection/logout or something like that, but I don't know how to program that.

What I am after is a way to receive SMS's, but for that I need to "check" every X sec. I have the flow and nodes in place, but it's not bulletproff. I check every 10 sec. (but want to go lower) if there is new sms, I save that one in a folder, then I monitor that folder, and gets the information in sms, and then call the delete function

I'm trying to get it to work in the Node-red package, but must be sure that it works in the API, before taking it further.

grawsom avatar Jan 09 '22 14:01 grawsom

You need to reuse the connection save it to a variable. E.g. only call the function with login credentials once.

Another repo but line 30 shows running the logout function.

zinen avatar Jan 11 '22 16:01 zinen

@zinen I'm no programmer, but maybe you can help me, or show me in the right direction. I have forked you and made my own changes to try somthing out. You can get it here

grawsom avatar Jan 12 '22 09:01 grawsom

This code you should be able to run over and over again without hitting the routers limit of 5 active sessions due to the logout at the end. Tests on my router proves that any sessions is killed after 5 minutes of inactivity.

const huaweiLteApi = require('huawei-lte-api')
// Varible globaly scoped
let connection
async function sendSMS(inputText) {
    try {
        connection = new huaweiLteApi.Connection('http://admin:[email protected]/')
        await connection.ready
        // Phone number must be an array of strings
        const resultSendSMS = await new huaweiLteApi.Sms(connection).sendSms(['12345678'], inputText)
        console.log('result for send SMS =', resultSendSMS)
    } catch (error) {
        console.error('Ended in error:')
        console.error(error)
    } finally {
        // finallyCode - Code block to be executed regardless of the try result
        const resultLogout = await new huaweiLteApi.User(connection).logout()
        console.log('result from logout=',resultLogout)
        connection = null
    }
}
sendSMS('My message to send')

zinen avatar Jan 12 '22 18:01 zinen

@Salamek When looking at the normal API, there is a command client.user.logout(). Where is that in TS package? Because I think maybe this is my problem here. @zinen

grawsom avatar Mar 27 '22 14:03 grawsom