Skip to content

Commit 31f6cec

Browse files
committed
added support for MQTT reload and update
1 parent f23270e commit 31f6cec

File tree

7 files changed

+190
-1
lines changed

7 files changed

+190
-1
lines changed

config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,12 @@
1616
"github":{
1717
"image":"./media/github.png",
1818
"link":"https://github.com/NetworkGraphs/graphysics"
19+
},
20+
"mqtt":{
21+
"host":"10.0.0.42",
22+
"port":1884,
23+
"client":"graphysics",
24+
"subscribe":"graph/#",
25+
"enabled":true
1926
}
2027
}

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<title>Graph with physics</title>
44
<link rel='shortcut icon' href='favicon.ico' type='image/x-icon' />
55
<script src="./libs/matter.min.js"></script>
6+
<script src="./libs/mqttws31.min.js"></script>
67
</head>
78
<body>
89
<script src="./src/main.js?v=556f89f94" type="module"></script>

libs/mqttws31.min.js

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/graph_app.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ function onDragEvents(event){
8080
};
8181
}
8282

83+
function onMqttMessage(e){
84+
const topic = e.detail.topic
85+
const data = e.detail.payload
86+
//console.log(`graph_app> ${topic} => ${JSON.stringify(data)}`);
87+
//TODO select sub topic of new graph vs update
88+
const action = topic.split('/')[1]
89+
if(action == "reload"){
90+
physics.pause()
91+
render.pause()
92+
gio.import_json(data)
93+
render.fitLabels()
94+
physics.create_bodies(parent_div)
95+
render.create_graph()
96+
mutate.all_groups(graph)
97+
physics.resume()
98+
render.resume()
99+
}else if(action == "update"){
100+
for(const vertex of data.vertices){
101+
let v = graph.vertices[vertex.id]
102+
v.viewBox.x = vertex.viewBox.x
103+
v.viewBox.y = vertex.viewBox.y
104+
v.viewBox.placed = true
105+
}
106+
}
107+
}
108+
109+
83110
async function common_load(file,reload,config=null){
84111
if(reload){
85112
console.log(`graph_app> reloading file : ${file.name}`)
@@ -134,6 +161,8 @@ class GraphApp{
134161
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
135162
document.addEventListener(eventName, onDragEvents, false)
136163
});
164+
window.addEventListener( 'mqtt_message', onMqttMessage, false);
165+
137166
}
138167

139168
async load(config,p_div){

src/graph_io.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,19 @@ class GraphIo{
320320
g = graph_data
321321
}
322322

323+
async common_import(res){
324+
rename_properties(res);
325+
add_references_from_ids(res);
326+
add_multi_edges_info(res);
327+
init_forces(res);
328+
init_groups_ifndef(res);
329+
g.vertices = res.vertices
330+
g.edges = res.edges
331+
g.layout_done = false//init to false, override by res.properties
332+
Object.assign(g,res.properties)
333+
return
334+
}
335+
323336
async import_file(file,cfg){
324337
if(cfg != null){
325338
config=cfg
@@ -367,7 +380,7 @@ class GraphIo{
367380
if(res == null){
368381
return
369382
}
370-
console.log(JSON.stringify(res))
383+
//console.log(JSON.stringify(res))
371384
rename_properties(res);
372385
add_references_from_ids(res);
373386
add_multi_edges_info(res);
@@ -380,6 +393,12 @@ class GraphIo{
380393
return
381394
}
382395

396+
import_json(data){
397+
let res = import_json_graph(data);
398+
//console.log(JSON.stringify(res))
399+
this.common_import(res)
400+
return
401+
}
383402
}
384403

385404
export {GraphIo};

src/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {GraphApp} from "./graph_app.js"
22
import {fetch_json} from "./utils.js"
3+
import * as mqtt from './mqtt_app.js';
4+
35

46
async function main(){
57
console.log("main() start")
@@ -14,6 +16,11 @@ async function main(){
1416

1517
animate();
1618
console.log("main() done")
19+
20+
if(config.mqtt.enabled){
21+
mqtt.init(config.mqtt);
22+
}
23+
1724
}
1825

1926
main()

src/mqtt_app.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/** http://w3c.github.io/html-reference/input.color.html
2+
*
3+
* sent events:
4+
* - mqtt_message
5+
*/
6+
7+
import {event} from "./utils.js"
8+
9+
let client;
10+
let mqtt_connected = false;
11+
let subscribe_topic = ""
12+
13+
function init(mqtt_config){
14+
subscribe_topic = mqtt_config.subscribe
15+
mqtt_connect(mqtt_config)
16+
}
17+
18+
// called when the client connects
19+
function onConnect() {
20+
mqtt_connected = true;
21+
// Once a connection has been made, make a subscription and send a message.
22+
console.log("mqtt_app> onConnect() mqtt running");
23+
24+
client.subscribe(subscribe_topic);
25+
console.log(`mqtt_app> - subscribed to ${subscribe_topic}`);
26+
}
27+
28+
// called when the client loses its connection
29+
function onConnectionLost(responseObject) {
30+
if (responseObject.errorCode !== 0) {
31+
console.log("onConnectionLost:"+responseObject.errorMessage);
32+
}
33+
mqtt_connected = false;
34+
}
35+
36+
// called when a message arrives
37+
function onMessageArrived(message) {
38+
//console.log(`mqtt_app> ${message.destinationName} => ${message.payloadString}`);
39+
event("mqtt_message",{topic:message.destinationName,payload:JSON.parse(message.payloadString)});
40+
}
41+
42+
function mqtt_connect(config){
43+
// Create a client instance
44+
const client_name = window.location.hostname + '#' + config.client;
45+
client = new Paho.MQTT.Client(config.host, Number(config.port), client_name);
46+
// set callback handlers
47+
client.onConnectionLost = onConnectionLost;
48+
client.onMessageArrived = onMessageArrived;
49+
50+
// connect the client
51+
client.connect({onSuccess:onConnect});
52+
}
53+
54+
export{init}

0 commit comments

Comments
 (0)