Тема: JS API: Will it work in nodejs?
Hello! I need to write simple utility to export all messages from all objects to files. Have you tried your JS API (wialon.js) in nodejs environment? Does it work? Thanks!
Вы не вошли. Пожалуйста, войдите или зарегистрируйтесь.
Hello! I need to write simple utility to export all messages from all objects to files. Have you tried your JS API (wialon.js) in nodejs environment? Does it work? Thanks!
Hi
Check this repo. It contains wialon.js build for Node.js
Its quite old, but i think its enough for your purposes
Thanks! - it works at least with login, getting units and following logout. The only problem I currently have is that node.exe never finished. So my question, is it possible to somehow un-initialize the session? Even after calling "session.logout" NodeJS still thinks that there is deffered/asynchronous action to be done. Any timers?
Difficult to answer without code. As any Linux user i think that problem in Windows
In any case you can follow Node.js documentation and execute
process.exit(0);
Also you can provide problematic code and i'll try to debug it and help asap
The code exactly the same as you pointed (the repo), plus I've tried to add logout as well.
I checked repo more precisely. Following code works if you're going exec program time-to-time
wialon.core.Session.getInstance().login("wialon_test", "test", "", function() {
var user = wialon.core.Session.getInstance().getCurrUser();
console.log('Login:', user.getName());
wialon.core.Session.getInstance().logout(function (code) {
if (code === 0) {
console.log('Logout');
process.exit(0);
}
});
});
Surely process.exit() works, but then I encounter with following : https://github.com/joyent/node/issues/8329 (which is not only Windows issue). Will be better if you find the root cause, namely why nodejs with wialon continues to run. I have just reproduced this on lubuntu 14.04 as well.
I thought a little more and realized that Wialon SDK should not exit after logout in common case. Simple example: login-logout in loop for several users - program should not exit after first iteration.
From other side, console.log issue that you mentioned almost unreachable in production - you'll rather store data to a file, not to console.
Also you can code your task using Node.js without any SDK, here is sample
And even more there are number of different languages repos that helps work with Wialon, you can try one of them for your task
Thanks for the tips about different languages, but I've already invested so many hours to learn Wialon JS API so I won't change horses as the crossing.
Обновленного wialon.js для nodejs не появилось? В репах npm модуль для remote api 2 летней давности, он актуален?
i.zubakin
Официальной библиотеки для nodejs нет и не планируется
Для большинства задач для работы с RemoteAPI хватает стандартного модуля request. Например, вот так
const request = require('request');
const token = 'AUTH TOKEN HERE';
var SID = '';
/** Perform Remote API request
*/
var remoteApi = function(svc, params, cb) {
params = params || {};
var form = {
svc: svc,
params: JSON.stringify(params)
};
if (SID) {
form.sid = SID;
}
request.post({
url: 'https://hst-api.wialon.com/wialon/ajax.html',
json: true,
form: form
}, cb);
};
// login
remoteApi('token/login', {token: token}, function(error, response, body) {
console.log('token/login', body);
if (body.eid) {
SID = body.eid;
// get current account details
remoteApi('core/get_account_data', {type: 1}, function(error, response, body) {
console.log('core/get_account_data', body);
// logout
remoteApi('core/logout', {}, function(error, response, body) {
SID = '';
console.log('core/logout', body);
});
});
}
});