ajout scripts js
This commit is contained in:
parent
863bef86d8
commit
706f1545b3
5 changed files with 369 additions and 0 deletions
59
jsfiles/device.js
Normal file
59
jsfiles/device.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* Author: Samir MEDJIAH medjiah@laas.fr
|
||||
* File : device.js
|
||||
* Version : 0.1.0
|
||||
*/
|
||||
|
||||
var express = require('express')
|
||||
var app = express()
|
||||
var request = require('request');
|
||||
|
||||
var argv = require('yargs').argv;
|
||||
// --local_ip
|
||||
// --local_port
|
||||
// --local_name
|
||||
// --remote_ip
|
||||
// --remote_port
|
||||
// --remote_name
|
||||
// --send_period
|
||||
|
||||
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};
|
||||
|
||||
var DATA_PERIOD = argv.send_period;
|
||||
|
||||
function doPOST(uri, body, onResponse) {
|
||||
request({method: 'POST', uri: uri, json : body}, onResponse);
|
||||
}
|
||||
|
||||
function register() {
|
||||
doPOST(
|
||||
'http://' + REMOTE_ENDPOINT.IP + ':' + REMOTE_ENDPOINT.PORT + '/devices/register',
|
||||
{
|
||||
Name : LOCAL_ENDPOINT.NAME,
|
||||
PoC : 'http://' + LOCAL_ENDPOINT.IP + ':' + LOCAL_ENDPOINT.PORT,
|
||||
},
|
||||
function(error, response, respBody) {
|
||||
console.log(respBody);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
var dataItem = 0;
|
||||
function sendData() {
|
||||
doPOST(
|
||||
'http://' + REMOTE_ENDPOINT.IP + ':' + REMOTE_ENDPOINT.PORT + '/device/'+ LOCAL_ENDPOINT.NAME + '/data',
|
||||
{
|
||||
Name : LOCAL_ENDPOINT.NAME,
|
||||
Data : dataItem++,
|
||||
Time : Date.now(),
|
||||
},
|
||||
function(error, response, respBody) {
|
||||
console.log(respBody);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
register();
|
||||
|
||||
setInterval(sendData, DATA_PERIOD);
|
133
jsfiles/gateway.js
Normal file
133
jsfiles/gateway.js
Normal file
|
@ -0,0 +1,133 @@
|
|||
/**
|
||||
* Author: Samir MEDJIAH medjiah@laas.fr
|
||||
* File : gateway.js
|
||||
* Version : 0.1.0
|
||||
*/
|
||||
|
||||
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 );
|
||||
});
|
3
jsfiles/scripts.txt
Normal file
3
jsfiles/scripts.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
http://homepages.laas.fr/smedjiah/tmp/*.js
|
||||
|
||||
with * = server OR gateway OR device
|
174
jsfiles/server.js
Normal file
174
jsfiles/server.js
Normal file
|
@ -0,0 +1,174 @@
|
|||
/**
|
||||
* Author: Samir MEDJIAH medjiah@laas.fr
|
||||
* File : server.js
|
||||
* Version : 0.1.0
|
||||
*/
|
||||
|
||||
|
||||
var express = require('express')
|
||||
var app = express()
|
||||
app.use(express.json())
|
||||
|
||||
var argv = require('yargs').argv;
|
||||
// --local_ip
|
||||
// --local_port
|
||||
// --local_name
|
||||
const si = require('systeminformation');
|
||||
|
||||
var LOCAL_ENDPOINT = {IP : argv.local_ip, PORT : argv.local_port, NAME : argv.local_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 = {
|
||||
devices : new Map(),
|
||||
data : new Map(),
|
||||
gateways : new Map()
|
||||
};
|
||||
|
||||
function addNewDevice(dev) {
|
||||
var result = -1;
|
||||
if (!db.devices.get(dev.Name)) {
|
||||
db.devices.set(dev.Name, dev);
|
||||
|
||||
if (db.devices.get(dev.Name))
|
||||
db.data.delete(dev.Name);
|
||||
db.data.set(dev.Name, []);
|
||||
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function addNewGateway(gw) {
|
||||
var result = -1;
|
||||
if (!db.gateways.get(gw.Name)) {
|
||||
db.gateways.set(gw.Name, gw);
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function removeDevice(dev) {
|
||||
if (db.devices.get(dev.Name)) {
|
||||
db.devices.delete(dev.Name);
|
||||
if (db.devices.get(dev.Name))
|
||||
db.data.delete(dev.Name);
|
||||
}
|
||||
}
|
||||
|
||||
function removeGateway(gw) {
|
||||
if (db.gateways.get(gw.Name))
|
||||
db.gateways.delete(gw.Name);
|
||||
}
|
||||
|
||||
function addDeviceData(dev, data) {
|
||||
var result = -1;
|
||||
var device = db.devices.get(dev);
|
||||
if (device) {
|
||||
db.data.get(dev).push(data);
|
||||
result = 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
app.get('/devices', function(req, res) {
|
||||
console.log(req.body);
|
||||
let resObj = [];
|
||||
db.devices.forEach((v,k) => {
|
||||
resObj.push(v);
|
||||
});
|
||||
res.status(E_OK).send(resObj);
|
||||
});
|
||||
app.get('/device/:dev', function(req, res) {
|
||||
console.log(req.body);
|
||||
var dev = req.params.dev;
|
||||
var device = db.devices.get(dev);
|
||||
if (device)
|
||||
res.status(E_OK).send(JSON.stringify(device));
|
||||
else
|
||||
res.sendStatus(E_NOT_FOUND);
|
||||
});
|
||||
app.post('/device/:dev/data', function(req, res) {
|
||||
console.log(req.body);
|
||||
var dev = req.params.dev;
|
||||
var result = addDeviceData(dev, req.body);
|
||||
if (result === 0)
|
||||
res.sendStatus(E_CREATED);
|
||||
else
|
||||
res.sendStatus(E_NOT_FOUND);
|
||||
});
|
||||
app.get('/device/:dev/data', function(req, res) {
|
||||
console.log(req.body);
|
||||
var dev = req.params.dev;
|
||||
var device = db.devices.get(dev);
|
||||
if (device){
|
||||
var data = db.data.get(dev);
|
||||
if (data) {
|
||||
let resObj = [];
|
||||
data.forEach((v,k) => {
|
||||
resObj.push(v);
|
||||
});
|
||||
res.status(E_OK).send(JSON.stringify(resObj));
|
||||
}
|
||||
else
|
||||
res.sendStatus(E_NOT_FOUND);
|
||||
}
|
||||
else
|
||||
res.sendStatus(E_NOT_FOUND);
|
||||
});
|
||||
app.post('/devices/register', function(req, res) {
|
||||
console.log(req.body);
|
||||
var result = addNewDevice(req.body);
|
||||
if ( result === 0)
|
||||
res.sendStatus(E_CREATED);
|
||||
else
|
||||
res.sendStatus(E_ALREADY_EXIST);
|
||||
});
|
||||
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.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.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));
|
||||
})
|
||||
});
|
||||
|
||||
app.listen(LOCAL_ENDPOINT.PORT , function () {
|
||||
console.log(LOCAL_ENDPOINT.NAME + ' listening on : ' + LOCAL_ENDPOINT.PORT );
|
||||
});
|
Loading…
Reference in a new issue