Commit 5ee7a320 by amir

add random for fields and timestamp

parent f76e858c
......@@ -9,7 +9,13 @@
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js"
"program": "${workspaceFolder}/app.js",
"args": [
"-p","8053",
"-f","/home/amir/git/ipgallery/transportation/doc/events.json",
"-d", "10"
]
// "--postUrl", "http://172.16.1.33:50035/transportation/api/v1/traffic/san-diego/event"
}
]
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ const schedule = require('node-schedule');
const request = require('request');
var myArgs = require('optimist').argv,
help = './websocketExpress -p port -f file-path [-d delay-between-samples --postUrl http url to post instead of websocket]';
help = 'node app.js -p port -f file-path [-d delay-between-samples --postUrl http url to post instead of websocket]';
/**
* some validation
......@@ -38,13 +38,65 @@ const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
var index = 0;
// Math.floor((Math.random() * 10)); 0 - 10
function randomFloatBetween(minValue,maxValue,precision){
if(typeof(precision) == 'undefined'){
precision = 2;
}
return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)),maxValue).toFixed(precision));
}
function randomIntBetween(minValue,maxValue){
return parseInt(Math.min(minValue + (Math.random() * (maxValue - minValue)),maxValue));
}
function handleChild(child, parent) {
var obj = parent[child];
switch (typeof obj){
case 'object':
handleChildren(obj);
break;
case 'string':
var args = obj.split(":");
if (args.length == 4){
if (args[0] == 'R'){
var startRange = args[2];
var endRange = args[3];
var type = args[1];
switch (type){
case 'I':
parent[child] = randomIntBetween(startRange,endRange);
break;
case 'F':
parent[child] = randomFloatBetween(startRange,endRange);
break;
}
}
}
break;
case 'number':
if (child == 'timestamp')
parent[child] = Date.now();
break;
}
}
function handleChildren(parent) {
for (var child in parent) {
handleChild(child, parent);
}
}
var normalyzeEvent = function(eventObj){
handleChildren(eventObj);
}
var sendJson = function(){
wss.clients.forEach(function each(client) {
var eventObj = jsonFile[index];
normalyzeEvent(eventObj);
console.log("sending: " + JSON.stringify(eventObj));
if (myArgs.postUrl == null){
if (client.readyState === WebSocket.OPEN) {
var currentObj = jsonFile[index];
client.send(JSON.stringify(currentObj));
index = ++index % jsonFile.length;
client.send(JSON.stringify(eventObj));
}
} else {
request.post({
......@@ -52,7 +104,7 @@ var sendJson = function(){
headers: {
"Content-Type": "application/json"
},
body: jsonFile[index],
body: eventObj,
json:true
}, function(error, response, body){
console.log(error);
......@@ -61,6 +113,7 @@ var sendJson = function(){
});
}
});
index = ++index % jsonFile.length;
}
fs.readFile(myArgs.f, (err, data) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment