1

JS API: Will it work in nodejs?

(06/10/2014 21:34:12 отредактировано sergej.shirokov)

Тема: 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!

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
2

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

Hi

Check this  repo. It contains wialon.js build for Node.js
Its quite old, but i think its enough for your purposes

3

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

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?

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
4

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

Difficult to answer without code. As any Linux user i think that problem in Windows wink
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

5

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

The code exactly the same as you pointed (the repo), plus I've tried to add logout as well.

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
6

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

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);
        }
    });
});
7

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

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.

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
8

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

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

9

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

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.

Sergei Shirokov / GoGPS Service
Senior Software Engineer
www.gogps.eu
Skype: sergei.shirokov
10

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

Обновленного wialon.js для nodejs не появилось? В репах npm модуль для remote api 2 летней давности, он актуален?

11

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

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);
        });
    });
  }
});
12

JS API: Will it work in nodejs?

Re: JS API: Will it work in nodejs?

Спасибо!