Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lam-station-visualization
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
data-analysis-and-ai
lam-station-visualization
Commits
358cd15d
Commit
358cd15d
authored
5 years ago
by
Janne Alatalo
Browse files
Options
Downloads
Patches
Plain Diff
Update the app to use the new mqtt websocket api
parent
54af653e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/App.js
+3
-3
3 additions, 3 deletions
src/App.js
src/utils/api-client.js
+27
-7
27 additions, 7 deletions
src/utils/api-client.js
with
30 additions
and
10 deletions
src/App.js
+
3
−
3
View file @
358cd15d
...
@@ -27,10 +27,10 @@ class App extends Component {
...
@@ -27,10 +27,10 @@ class App extends Component {
let
width
=
window
.
innerWidth
;
let
width
=
window
.
innerWidth
;
let
height
=
window
.
innerHeight
;
let
height
=
window
.
innerHeight
;
this
.
state
=
{
lam_points
:
Map
(),
mapSize
:
{
width
,
height
}
};
this
.
state
=
{
lam_points
:
Map
(),
mapSize
:
{
width
,
height
}
};
get_lam_points
(
"
http://tie.digitraffic.fi
"
,
this
.
update_state
.
bind
(
this
))
get_lam_points
(
"
http
s
://tie.digitraffic.fi
"
,
this
.
update_state
.
bind
(
this
))
.
then
(()
=>
{
.
then
(()
=>
{
connect_lam_ws
(
"
w
s://tie.digitraffic.fi
"
,
this
.
update_state
.
bind
(
this
));
return
connect_lam_ws
(
"
mqtt
s://tie.digitraffic.fi
:61619
"
,
this
.
update_state
.
bind
(
this
));
})
})
;
this
.
handleResize
=
this
.
handleResize
.
bind
(
this
);
this
.
handleResize
=
this
.
handleResize
.
bind
(
this
);
}
}
...
...
This diff is collapsed.
Click to expand it.
src/utils/api-client.js
+
27
−
7
View file @
358cd15d
import
{
Map
}
from
'
immutable
'
;
import
{
Map
}
from
'
immutable
'
;
import
mqtt
from
'
mqtt
'
;
import
LamStation
from
'
./lam-station.js
'
;
import
LamStation
from
'
./lam-station.js
'
;
...
@@ -86,20 +87,38 @@ export function get_lam_station_data(api_url, set_state) {
...
@@ -86,20 +87,38 @@ export function get_lam_station_data(api_url, set_state) {
export
function
connect_lam_ws
(
api_url
,
update_state
)
{
export
function
connect_lam_ws
(
api_url
,
update_state
)
{
let
data_buff
=
[];
let
data_buff
=
[];
let
url
=
`
${
api_url
}
/api/v1/plain-websockets/tmsdata`
;
let
url
=
`
${
api_url
}
/mqtt`
;
let
socket
=
new
WebSocket
(
url
);
// The username and password is documented on digitraffic api documentation site here:
socket
.
addEventListener
(
'
open
'
,
()
=>
{
// https://www.digitraffic.fi/tieliikenne/#websocket-rajapinnat
console
.
info
(
`WebSocket open to
${
url
}
`
);
let
client
=
mqtt
.
connect
(
url
,
{
username
:
'
digitraffic
'
,
password
:
'
digitrafficPassword
'
,
});
});
socket
.
addEventListener
(
'
message
'
,
(
msg
)
=>
{
client
.
on
(
'
connect
'
,
()
=>
{
let
data
=
JSON
.
parse
(
msg
.
data
).
sensorValue
;
console
.
info
(
`MQTT connection open to
${
url
}
`
);
const
topic
=
"
tms/#
"
;
client
.
subscribe
(
topic
,
(
err
)
=>
{
if
(
err
)
{
console
.
error
(
`Subscription error:
${
err
}
`
);
}
else
{
console
.
log
(
`Subscribed to
${
topic
}
`
);
}
});
});
client
.
on
(
'
error
'
,
(
err
)
=>
{
console
.
error
(
`error:
${
err
}
`
);
});
client
.
on
(
'
message
'
,
(
topic
,
msg
)
=>
{
let
data
=
JSON
.
parse
(
msg
.
toString
());
if
(
!
data
)
{
if
(
!
data
)
{
return
return
}
}
let
id
=
data
.
roadStationId
;
let
id
=
data
.
roadStationId
;
let
value
=
data
.
sensorValue
;
let
value
=
data
.
sensorValue
;
let
name
=
data
.
name
;
let
name
=
data
.
name
;
data_buff
.
push
({
id
,
value
,
name
})
if
(
id
&&
value
&&
name
)
{
data_buff
.
push
({
id
,
value
,
name
});
}
});
});
function
buffer_flusher
()
{
function
buffer_flusher
()
{
...
@@ -111,6 +130,7 @@ export function connect_lam_ws(api_url, update_state) {
...
@@ -111,6 +130,7 @@ export function connect_lam_ws(api_url, update_state) {
let
{
id
,
value
,
name
}
=
obj
;
let
{
id
,
value
,
name
}
=
obj
;
let
point
=
lam_points
.
get
(
id
);
let
point
=
lam_points
.
get
(
id
);
if
(
!
point
)
{
if
(
!
point
)
{
console
.
error
(
`point not found
${
id
}
`
);
continue
;
continue
;
}
}
if
(
name
===
"
KESKINOPEUS_5MIN_LIUKUVA_SUUNTA1
"
)
{
if
(
name
===
"
KESKINOPEUS_5MIN_LIUKUVA_SUUNTA1
"
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment