123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- var express = require('express')
- var app = express()
- app.use(express.json()) // for parsing application/json
-
- var request = require('request');
- const si = require('systeminformation');
- var argv = require('yargs').argv;
- // --local_ip
- // --local_port
- // --local_name
- // --remote_ip
- // --remote_port
- // --remote_name
-
- var LOCAL_ENDPOINT = {IP : argv.local_ip, PORT : argv.local_port, NAME : argv.local_name};
- var REMOTE_ENDPOINT = {IP : argv.remote_ip, PORT : argv.remote_port, NAME : argv.remote_name};
-
- const E_OK = 200;
- const E_CREATED = 201;
- const E_FORBIDDEN = 403;
- const E_NOT_FOUND = 404;
- const E_ALREADY_EXIST = 500;
-
-
- var db = {
- gateways : new Map()
- };
-
- function addNewGateway(gw) {
- var res = -1;
- if (!db.gateways.get(gw.Name)) {
- db.gateways.set(gw.Name, gw);
- res = 0;
- }
- return res;
- }
-
- function removeGateway(gw) {
- if (db.gateways.get(gw.Name))
- db.gateways.delete(gw.Name);
- }
-
-
- function doPOST(uri, body, onResponse) {
- request({method: 'POST', uri: uri, json : body}, onResponse);
- }
-
- function register() {
- doPOST(
- 'http://' + REMOTE_ENDPOINT.IP + ':' + REMOTE_ENDPOINT.PORT + '/gateways/register',
- {
- Name : LOCAL_ENDPOINT.NAME,
- PoC : 'http://' + LOCAL_ENDPOINT.IP + ':' + LOCAL_ENDPOINT.PORT,
- },
- function(error, response, respBody) {
- console.log(respBody);
- }
- );
- }
-
-
- app.post('/gateways/register', function(req, res) {
- console.log(req.body);
- var result = addNewGateway(req.body);
- if (result === 0)
- res.sendStatus(E_CREATED);
- else
- res.sendStatus(E_ALREADY_EXIST);
- });
- app.post('/devices/register', function(req, res) {
- console.log(req.body);
- doPOST(
- 'http://' + REMOTE_ENDPOINT.IP + ':' +REMOTE_ENDPOINT.PORT + '/devices/register',
- req.body,
- function(error, response, respBody) {
- console.log(respBody);
- res.sendStatus(E_OK);
- }
- )
- });
- app.post('/device/:dev/data', function(req, res) {
- console.log(req.body);
- var dev = req.params.dev;
- doPOST(
- 'http://' + REMOTE_ENDPOINT.IP + ':' +REMOTE_ENDPOINT.PORT + '/device/' + dev + '/data',
- req.body,
- function(error, response, respBody) {
- console.log(respBody);
- res.sendStatus(E_OK);
- }
- )
- });
- app.get('/gateways', function(req, res) {
- console.log(req.body);
- let resObj = [];
- db.gateways.forEach((v,k) => {
- resObj.push(v);
- });
- res.send(resObj);
- });
- app.get('/gateway/:gw', function(req, res) {
- console.log(req.body);
- var gw = req.params.gw;
- var gateway = db.gateways.get(gw);
- if (gateway)
- res.status(E_OK).send(JSON.stringify(gateway));
- else
- res.sendStatus(E_NOT_FOUND);
- });
-
- app.get('/ping', function(req, res) {
- console.log(req.body);
- res.status(E_OK).send({pong: Date.now()});
- });
- app.get('/health', function(req, res) {
- console.log(req.body);
- si.currentLoad((d) => {
- console.log(d);
- res.status(E_OK).send(JSON.stringify(d));
- })
- });
-
-
- register();
- app.listen(LOCAL_ENDPOINT.PORT , function () {
- console.log(LOCAL_ENDPOINT.NAME + ' listening on : ' + LOCAL_ENDPOINT.PORT );
- });
|