This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ffmapng.js
196 lines (178 loc) · 4.98 KB
/
ffmapng.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(function(d3, oboe, document, JSON, window) {
"use strict";
function Batadv(urls) {
this.urls = urls;
this.reset();
}
Batadv.prototype = {
// events
onUpdate: function() {
throw new Error("Not listening to onUpdate event!");
},
reset: function() {
this.nodes = [];
this.nodesObj = {};
this.meshNodes = {};
this.links = [];
},
update: function() {
this.reset();
this.updateNodes();
},
updateNodes: function() {
var self = this;
oboe(this.urls.nodeinfo)
.node("!.*", function(src) {
self.nodes.push(src);
self.nodesObj[src.node_id] = src;
src.network.mesh_interfaces.forEach(function(e) {
self.meshNodes[e] = src;
});
})
.done(function() {
console.log(self.nodes);
self.updateLinks();
})
.fail(function(e) {
throw e;
});
},
updateLinks: function() {
var self = this;
oboe(this.urls.batadv_vis)
.node("!.vis.*.neighbors.*", function(src) {
self.links.push({
from: self.meshNodes[src.router],
to: self.meshNodes[src.neighbor],
metric: src.metric
});
})
.done(function() {
self.updateStatistics();
})
.fail(function() {
});
},
updateStatistics: function() {
var self = this;
oboe(this.urls.statistic)
.node("!.*", function(src) {
self.nodesObj[src.node_id].statistic = src;
})
.done(function() {
self.onUpdate(self.nodes, self.links);
})
.fail(function(e) {
throw e.thrown;
});
}
};
function FFMapNG(target, config) {
this.target = target;
this.map = new OpenLayers.Map(target.id);
var mapnik = new OpenLayers.Layer.OSM("Umgebung");
this.linkLayer = new OpenLayers.Layer.Vector("Links");
this.nodeLayer = new OpenLayers.Layer.Markers("Nodes");
this.map.addLayers([ mapnik, this.linkLayer, this.nodeLayer ]);
this.map.setCenter(new OpenLayers.LonLat(config.map.lon, config.map.lat) // Center of the map
.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
), config.map.zoom
);
}
FFMapNG.init = function(target, config) {
var map = new FFMapNG(target, config);
// poor mans deep copy
config = JSON.parse(JSON.stringify(config));
var modelLoader;
if(config.api.statistic && config.api.nodeinfo, config.api.batadv_vis) {
modelLoader = new Batadv(config.api);
} else {
throw new Error("No valid datasource in config!\n"+
"Either set config.api.statistic and config.api.nodeinfo or "+
"config.api.nodes.");
}
modelLoader.onUpdate = map.redraw.bind(map);
modelLoader.update();
OpenLayers.Lang.setCode(config.lang);
};
FFMapNG.prototype = {
redraw: function(nodes, links) {
this.drawNodes(nodes);
this.drawLinks(links);
},
drawLinks: function(links) {
var self = this;
this.linkLayer.destroyFeatures();
links.forEach(function(link) {
if(!link.from || !link.from.location || !link.to || !link.to.location)
return;
var points = new Array(
self.posTransform(new OpenLayers.Geometry.Point(link.from.location.longitude, link.from.location.latitude)),
self.posTransform(new OpenLayers.Geometry.Point(link.to.location.longitude, link.to.location.latitude))
);
var style = {
strokeColor: '#0000ff',
strokeOpacity: 0.5,
strokeWidth: 5
};
var line = new OpenLayers.Geometry.LineString(points);
var lineFeature = new OpenLayers.Feature.Vector(line, null, style);
self.linkLayer.addFeatures([lineFeature]);
});
},
drawNodes: function(nodes) {
var self = this;
this.nodeLayer.clearMarkers();
var icon = new OpenLayers.Icon('img/wifi.png', {w:48,h:48}, {x:-24,y:-24});
nodes.map(function(node) {
if(!node.location) return;
var position = self.posTransform(new OpenLayers.LonLat(
node.location.longitude,
node.location.latitude
));
var i = icon.clone();
var marker = new OpenLayers.Marker(position, i);
var binder = {
icon: i,
node: node
};
marker.events.register('mousedown', binder, function() {
self.map.popups.forEach(function(p) {
self.map.removePopup(p);
});
self.map.addPopup(self.createPopup(this));
});
self.nodeLayer.addMarker(marker);
});
},
createPopup: function(binder) {
console.log(binder);
var span = document.createElement('span');
span.appendChild(document.createTextNode(binder.node.hostname));
var popup = new OpenLayers.Popup.FramedCloud(
binder.node.node_id,
this.posTransform(new OpenLayers.LonLat(
binder.node.location.longitude,
binder.node.location.latitude
)),
null,
span.innerHTML,
null,
true
);
return popup;
},
posTransform: function(pos) {
return pos.transform(
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
);
},
update: function() {
this.modelFactory.update();
}
};
window.FFMapNG = FFMapNG;
})(d3, oboe, document, JSON, window);