Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
grawsom opened this issue Jan 9, 2022 · 4 comments
Open

Comments

@grawsom
Copy link

grawsom commented Jan 9, 2022

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.

@zinen
Copy link
Contributor

zinen commented Jan 11, 2022

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.

@grawsom
Copy link
Author

grawsom commented Jan 12, 2022

@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

@zinen
Copy link
Contributor

zinen commented Jan 12, 2022

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:MY_SUPER_TRUPER_PASSWORD@192.168.8.1/')
        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')

@grawsom
Copy link
Author

grawsom commented Mar 27, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants