Commit a846e83f220e23200c2188c7596367ccc4befd31

Authored by Adhidarma Hadiwinoto
1 parent aec1110166
Exists in master

register modem on startup

Showing 3 changed files with 44 additions and 1 deletions Side-by-side Diff

... ... @@ -7,7 +7,8 @@
7 7 }
8 8 },
9 9 "report_url": {
10   - "incoming_sms": "http://localhost:16481/apikey/PLEASE_CHANGE_ME/on-sms"
  10 + "incoming_sms": "http://localhost:16481/apikey/PLEASE_CHANGE_ME/on-sms",
  11 + "register_modem": "http://localhost:16481/apikey/PLEASE_CHANGE_ME/modems/set"
11 12 },
12 13 "http_command_server": {
13 14 "apikey": "PLEASE_CHANGE_ME",
... ... @@ -20,6 +20,7 @@ const sms = require('./sms');
20 20 const dbCops = require('./db-cops');
21 21 const reportSender = require('./report-sender');
22 22 const msisdn = require('./msisdn');
  23 +const registerModem = require('./register-modem');
23 24  
24 25 const modemInfo = {
25 26 device: config.modem.device,
... ... @@ -344,6 +345,7 @@ function init() {
344 345 mutex.releaseLockWaitForCommand();
345 346 logger.verbose('Init completed');
346 347  
  348 + registerModem();
347 349 registerSignalStrengthBackgroundQuery();
348 350 });
349 351 }
lib/register-modem.js
... ... @@ -0,0 +1,40 @@
  1 +'use strict';
  2 +
  3 +const path = require('path');
  4 +const request = require('request');
  5 +
  6 +const config = require('komodo-sdk/config');
  7 +const logger = require('komodo-sdk/logger');
  8 +
  9 +function reportUrl() {
  10 + if (config.report_url.register_modem) {
  11 + return config.report_url.register_modem;
  12 + }
  13 +
  14 + const baseUrl = path.dirname(config.report_url.incoming_sms);
  15 + return `${baseUrl}/modems/set`;
  16 +}
  17 +
  18 +module.exports = (modemInfo) => {
  19 + const requestOptions = {
  20 + url: reportUrl(),
  21 + qs: {
  22 + modem: config.name,
  23 + modem_imsi: modemInfo.imsi,
  24 + modem_msisdn: modemInfo.msisdn,
  25 + modem_device: config.modem.device,
  26 + uptime: Math.floor(process.uptime()),
  27 + report_port: config.http_command_server.listen_port,
  28 + report_apikey: config.http_command_server.apikey,
  29 + report_path_sms: '/sms',
  30 + },
  31 + };
  32 +
  33 + request(requestOptions, (err, res) => {
  34 + if (err) {
  35 + logger.warn(`Error registering modem. ${err.toString()}`);
  36 + } else if (res.statusCode !== 200) {
  37 + logger.warn(`SMS center respond with HTTP status code ${res.statusCode}.`);
  38 + }
  39 + });
  40 +};