Code coverage report for lib/iotagent-thinking-things.js

Statements: 76.19% (32 / 42)      Branches: 58.33% (7 / 12)      Functions: 66.67% (4 / 6)      Lines: 76.19% (32 / 42)     

All files » lib/ » iotagent-thinking-things.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                                              1   1                               1                       1                                 1 22   22 22     22           22 22   22 22 22   22       22 22     22 1     22 22   22   22   22           1 22                 1 22   22 22       1 1  
/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of iotagent-thinking-things
 *
 * iotagent-thinking-things is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * iotagent-thinking-things is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with iotagent-thinking-things.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[iot_support@tid.es]
 */
 
'use strict';
 
var logger = require('fiware-node-logger'),
    http = require('http'),
    express = require('express'),
    thinkingListener = require('./middlewares/thinkingListener'),
    iotAgentLib = require('iotagent-node-lib'),
    bodyParser = require('body-parser'),
    async = require('async'),
    apply = async.apply,
    context = {
      op: 'IoTAgentTT.SouthBoundServer'
    },
    southboundServer;
 
/**
 * This middleware can be used to trace the request contents in debug mode.
 */
function traceRequest(req, res, next) {
  logger.debug(context, 'Request for path [%s] from [%s]', req.path, req.get('host'));
 
  logger.debug(context, 'Headers:\n %s', JSON.stringify(req.headers, null, 4));
  logger.debug(context, 'RawBody:\n\n%s\n\n', JSON.stringify(req.body, null, 4));
 
  next();
}
 
/**
 * Generic error handler for errors raised in the southbound.
 */
function handleError(error, req, res, next) {
  var code = 500;
 
  logger.debug(context, 'Error [%s] handing request: %s', error.name, error.message);
 
  if (error.code) {
    code = error.code;
  }
  res.status(code).json({
    name: error.name,
    message: error.message
  });
}
 
/**
 * Start the southbound server to listen for Thinking Things requests.
 */
function startServer(config, callback) {
  var baseRoot = '/';
 
  Eif (config.thinkingThings.logLevel) {
    logger.setLevel(config.thinkingThings.logLevel);
  }
 
  southboundServer = {
    server: null,
    app: express(),
    router: express.Router()
  };
 
  logger.info(context, 'Starting IoT Agent listening on port [%s]', config.thinkingThings.port);
  logger.debug(context, 'Using config:\n\n%s\n', JSON.stringify(config, null, 4));
 
  southboundServer.app.set('port', config.thinkingThings.port);
  southboundServer.app.set('host', '0.0.0.0');
  southboundServer.app.use(bodyParser.urlencoded());
 
  Iif (config.thinkingThings.logLevel && config.thinkingThings.logLevel === 'DEBUG') {
    southboundServer.app.use(traceRequest);
  }
 
  Eif (config.thinkingThings.root) {
    baseRoot = config.thinkingThings.root;
  }
 
    if (config.thinkingThings.mappingFile) {
        config.thinkingThings.idMapping = require('../' + config.thinkingThings.mappingFile);
    }
 
  southboundServer.app.use(baseRoot, southboundServer.router);
  thinkingListener.loadContextRoutes(southboundServer.router, config);
 
  southboundServer.app.use(handleError);
 
  southboundServer.server = http.createServer(southboundServer.app);
 
  southboundServer.server.listen(southboundServer.app.get('port'), southboundServer.app.get('host'), callback);
}
 
/**
 * Start the IoT Agent.
 */
function start(config, callback) {
  async.series([
      apply(startServer, config),
      apply(iotAgentLib.activate, config.ngsi)
  ], callback);
}
 
/**
 * Stop the IoT Agent.
 */
function stop(callback) {
  logger.info(context, 'Stopping IoT Agent');
 
  iotAgentLib.deactivate(function() {
      southboundServer.server.close(callback);
  });
}
 
exports.start = start;
exports.stop = stop;