2 normalize: function(node) {
5 if (node.component_name) { // wsn430-11.devlille.iot-lab.info
6 info = node.component_name.split(".");
7 } /*else if (node.hrn) { // iotlab.a8-11\.devgrenoble\.iot-lab\.info
8 var info = node.hrn.split("\\.");
9 info[0] = info[0].split(".")[1];
12 if (info && info[2] == "iot-lab" && info[3] == "info") {
13 node.arch = info[0].split("-")[0];
14 node.id = parseInt(info[0].split("-")[1]);
16 node.x = parseFloat(node.x);
17 node.y = parseFloat(node.y);
18 node.z = parseFloat(node.z);
19 node.normalized = true;
22 notify: function(node) {
23 console.log("[Notify] node " + node.id + " is " + node.boot_state);
25 createMaps: function($container, sites, nodes) {
27 var $menu = $("<ul id='sites-tabs' class='nav nav-tabs' data-tabs='sites-tabs'/>").appendTo($container);
28 var $maps = $("<div id='maps' class='tab-content' />").appendTo($container);
30 $.each(sites, function(i, site) {
31 var entry = $("<li><a href='#" + site + "' data-toggle='tab'>" + site + "</a></li>").appendTo($menu);
32 var $tab = $("<div class='tab-pane' id='" + site + "' />").appendTo($maps);
33 maps[site] = new Senslab.Map($tab);
34 maps[site].addNodes(nodes[site]);
37 $menu.find("li").eq(0).addClass("active");
38 $maps.find("div").eq(0).addClass("active");
41 $container.text("No nodes to display.");
46 Senslab.Map = function() {
50 "Suspected": 0xFF3030,
60 function Map($container, options) {
69 this.pointerDetectRay = new THREE.Raycaster();
70 this.pointerDetectRay.ray.direction.set(0, -1, 0);
71 this.projector = new THREE.Projector();
72 this.mouse2D = new THREE.Vector3(0, 0, 0);
74 this.renderer = new THREE.CanvasRenderer();
75 this.renderer.setSize(this.width, this.height);
77 this.camera = new THREE.PerspectiveCamera(75, this.width / this.height, 1, 10000);
79 this.scene = new THREE.Scene();
80 this.scene.add(this.camera);
82 this.updatePosition();
86 this.$nodeInputs = {};
88 $.each(archs, function(i, arch) {
89 self.$nodeInputs[arch] = $("<input type='text' placeholder='" + arch + "'>")
92 self.updateSelected(arch, expand($(this).val()));
97 var $canvas = $(this.renderer.domElement)
98 .mousemove(function(e) {
99 self.mouse2D.x = ((e.pageX - $canvas.offset().left) / $canvas.width()) * 2 - 1;
100 self.mouse2D.y = -((e.pageY - $canvas.offset().top) / $canvas.height()) * 2 + 1;
103 self.theta -= e.pageX - self.mouse2D.pageX;
104 self.phi += e.pageY - self.mouse2D.pageY;
110 self.mouse2D.pageX = e.pageX;
111 self.mouse2D.pageY = e.pageY;
113 self.updatePosition();
116 }).mousedown(function(e) {
120 self.pointerDetectRay = self.projector.pickingRay(self.mouse2D.clone(), self.camera);
121 var intersects = self.pointerDetectRay.intersectObjects(self.scene.children);
122 if (intersects.length > 0) {
123 var particle = intersects[0].object;
124 if (particle.data.boot_state != "Suspected") {
125 setState(particle, particle.data.boot_state == "Alive" ? "Selected" : "Alive");
126 var $nodeInput = self.$nodeInputs[particle.data.arch];
127 $nodeInput.val(factorize(self.getNodesId(particle.data.arch)));
133 self.mouse2D.pageX = e.pageX;
134 self.mouse2D.pageY = e.pageY;
138 }).mouseup(function(e) {
145 }).mouseleave(function(e) {
147 }).mousewheel(function(e, delta) {
149 self.distance += delta * 5;
150 self.updatePosition();
154 $container.append($canvas);
157 Map.prototype.getNodesId = function(arch) {
158 var particles = this.scene.children;
160 for (var i = 0; i < particles.length; ++i) {
161 if (particles[i].data && particles[i].data.arch == arch && particles[i].data.boot_state == "Selected") {
162 nodes.push(particles[i].id);
168 Map.prototype.addNodes = function(nodes) {
169 var center = getCenter(nodes);
171 nodes.sort(function(a, b) {
175 for(var i = 0; i < nodes.length; ++i) {
176 var material = new THREE.ParticleCanvasMaterial({program: circle});
177 var particle = new THREE.Particle(material);
178 particle.data = nodes[i];
179 particle.id = parseInt(nodes[i].id);
180 particle.position.x = (nodes[i].x - center.x) * 10;
181 particle.position.y = (nodes[i].y - center.y) * 10;
182 particle.position.z = (nodes[i].z - center.z) * 10;
183 particle.scale.x = particle.scale.y = 1;
185 this.scene.add(particle);
190 Map.prototype.updateSelected = function(arch, selected) {
191 var particles = this.scene.children;
192 for (var i = 0; i < particles.length; ++i) {
193 if (particles[i].data && particles[i].data.arch == arch && particles[i].data.boot_state != "Suspected") {
194 var particle = particles[i];
195 var state = $.inArray(particle.id, selected) == -1 ? "Alive" : "Selected";
196 if (particle.data.boot_state != state) {
197 setState(particle, state);
203 Map.prototype.updatePosition = function() {
204 this.camera.position.x = this.distance
205 * Math.sin(this.theta * Math.PI / 360)
206 * Math.cos(this.phi * Math.PI / 360);
207 this.camera.position.y = this.distance * Math.sin(this.phi * Math.PI / 360);
208 this.camera.position.z = this.distance
209 * Math.cos(this.theta * Math.PI / 360)
210 * Math.cos(this.phi * Math.PI / 360);
211 this.camera.lookAt(this.scene.position);
212 this.camera.updateMatrix();
215 Map.prototype.update = function() {
216 this.renderer.render(this.scene, this.camera);
219 function setState(particle, state) {
220 particle.data.boot_state = state;
222 Senslab.notify(particle.data);
225 function setColor(particle) {
226 var color = colors[particle.data.boot_state] || colors["Selected"];
227 particle.material.color.setHex(color);
230 function getCenter(nodes) {
231 var xmin = 0, ymin = 0, zmin = 0;
232 var xmax = 0, ymax = 0, zmax = 0;
234 for (var i = 0; i < nodes.length; ++i) {
235 if (nodes[i].x > xmax) xmax = nodes[i].x;
236 if (nodes[i].x < xmin) xmin = nodes[i].x;
237 if (nodes[i].y > ymax) ymax = nodes[i].y;
238 if (nodes[i].y < ymin) ymin = nodes[i].y;
239 if (nodes[i].z > zmax) zmax = nodes[i].z;
240 if (nodes[i].z < zmin) zmin = nodes[i].z;
242 return {x: (xmax + xmin) / 2, y: (ymax + ymin) / 2, z: (zmax + zmin) / 2};
245 function factorize(nodes) {
248 var intervalStart = 0;
250 for (var i = 0; i < nodes.length; ++i) {
252 if (nodes[i] == prev + 1) {
255 factorized.push(intervalStart + "-" + prev);
260 if (nodes[i] == prev + 1) {
261 intervalStart = prev;
264 prev && factorized.push(prev);
269 factorized.push(intervalStart ? intervalStart + "-" + prev : prev);
270 return factorized.join(",");
273 function expand(input) {
274 var factorized = input.split(",");
276 for (var i = 0; i < factorized.length; ++i) {
277 var d = factorized[i].split("-");
279 for (var j = parseInt(d[0]); j < parseInt(d[1]) + 1; j++) {
283 expanded.push(parseInt(factorized[i]));
287 expanded.sort(function(a, b) {
291 for (var i = 1; i < expanded.length; i++) {
292 if (expanded[i] == expanded[i - 1]) {
293 expanded.splice(i--, 1);
299 function circle(context) {
301 context.arc(0, 0, 1, 0, Math.PI * 2, true);