Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery
/
devops
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5ee7a320
authored
Nov 15, 2017
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
add random for fields and timestamp
parent
f76e858c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
6 deletions
tools/wsEventEmulator/.vscode/launch.json
tools/wsEventEmulator/app.js
tools/wsEventEmulator/.vscode/launch.json
View file @
5ee7a320
...
...
@@ -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
tools/wsEventEmulator/app.js
View file @
5ee7a320
...
...
@@ -7,7 +7,7 @@ const schedule = require('node-schedule');
const
request
=
require
(
'request'
);
var
myArgs
=
require
(
'optimist'
).
argv
,
help
=
'
./websocketExpres
s -p port -f file-path [-d delay-between-samples --postUrl http url to post instead of websocket]'
;
help
=
'
node app.j
s -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
)
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment