a75254a43c8a1aeddcc5fd7de2b916a423d01616
[myslice.git] / plugins / senslabmap / static / js / map.js
1 var Senslab = {
2   normalize: function(node) {
3     var info;
4
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];
10     }*/
11
12     if (info && info[2] == "iot-lab" && info[3] == "info") {
13       node.arch = info[0].split("-")[0];
14       node.id = info[0].split("-")[1];
15       node.site = info[1];
16       node.normalized = true;
17     }
18   },
19   notify: function(node) {
20     console.log("[Notify] node " + node.id + " is " + node.boot_state);
21   },
22   createMaps: function($container, sites, nodes) {
23     var maps = {};
24     $.each(sites, function(i, site) {
25       var $div = $("<div />").appendTo($container);
26       maps[site] = new Senslab.Map($div);
27       maps[site].addNodes(nodes[site]);
28     });
29   }
30 };
31
32 Senslab.Map = function() {
33   var colors = {
34     "Alive": 0x7FFF00,
35     "Busy": 0x9943BE,
36     "Suspected": 0xFF3030,
37     "Selected": 0x0099CC
38   };
39
40   var archs = [
41     "wsn430",
42     "m3",
43     "a8"
44   ];
45   
46   function Map($container, options) {
47     this.width  = 600;
48     this.height = 400;
49     
50     this.distance = 50;
51     this.phi = -100;
52     this.theta = 0;
53     this.onRot = false;
54     
55     this.pointerDetectRay = new THREE.Raycaster();
56     this.pointerDetectRay.ray.direction.set(0, -1, 0);
57     this.projector = new THREE.Projector();
58     this.mouse2D = new THREE.Vector3(0, 0, 0);
59     
60     this.renderer = new THREE.CanvasRenderer();
61     this.renderer.setSize(this.width, this.height);
62     
63     this.camera = new THREE.PerspectiveCamera(75, this.width / this.height, 1, 10000);
64     
65     this.scene = new THREE.Scene();
66     this.scene.add(this.camera);
67     
68     this.updatePosition();
69     
70     var self = this;
71     
72     this.$nodeInputs = {};
73     
74     $.each(archs, function(i, arch) {
75       self.$nodeInputs[arch] = $("<input type='text' placeholder='" + arch + "'>")
76       .appendTo($container)
77       .change(function() {
78         self.updateSelected(arch, expand($(this).val()));
79         self.update();
80       });
81     });
82     
83     var $canvas = $(this.renderer.domElement)
84     .mousemove(function(e) {
85       self.mouse2D.x =  ((e.pageX - $canvas.offset().left) / $canvas.width()) * 2 - 1;
86       self.mouse2D.y = -((e.pageY - $canvas.offset().top) / $canvas.height()) * 2 + 1;
87       
88       if (self.onRot) {
89         self.theta -= e.pageX - self.mouse2D.pageX;
90         self.phi += e.pageY - self.mouse2D.pageY;
91         if (self.phi > 180)
92           self.phi = 180;
93         if (self.phi < -180)
94           self.phi = -180;
95         
96         self.mouse2D.pageX = e.pageX;
97         self.mouse2D.pageY = e.pageY;
98         
99         self.updatePosition();
100         self.update();
101       }
102     }).mousedown(function(e) {
103       e.preventDefault();
104       switch (e.which) {
105         case 1:
106           self.pointerDetectRay = self.projector.pickingRay(self.mouse2D.clone(), self.camera);
107           var intersects = self.pointerDetectRay.intersectObjects(self.scene.children);
108           if (intersects.length > 0) {
109             var node = intersects[0].object;
110             if (node.boot_state != "Suspected") {
111               setState(node, node.boot_state == "Alive" ? "Selected" : "Alive");
112               var $nodeInput = self.$nodeInputs[node.arch];
113               $nodeInput.val(factorize(self.getNodesId(node.arch)));
114               self.update();
115             }
116           }
117           break;
118         case 3:
119           self.mouse2D.pageX = e.pageX;
120           self.mouse2D.pageY = e.pageY;
121           self.onRot = true;
122           break;
123       }
124     }).mouseup(function(e) {
125       e.preventDefault();
126       switch (e.which) {
127         case 3:
128           self.onRot = false;
129           break;
130       }
131     }).mouseleave(function(e) {
132       self.onRot = false;
133     }).mousewheel(function(e, delta) {
134       e.preventDefault();
135       self.distance += delta * 5;
136       self.updatePosition();
137       self.update();
138     });
139     
140     $container.append($canvas);
141   }
142   
143   Map.prototype.getNodesId = function(arch) {
144     var allNodes = this.scene.children;
145     var nodes = [];
146     for (var i = 0; i < allNodes.length; ++i) {
147       if (allNodes[i].arch == arch && allNodes[i].boot_state == "Selected") {
148         nodes.push(allNodes[i].id);
149       }
150     }
151     return nodes;
152   }
153   
154   Map.prototype.addNodes = function(nodes) {
155     var center = getCenter(nodes);
156
157     nodes.sort(function(a, b) {
158       return a.id - b.id;
159     });
160     
161     for(var i = 0; i < nodes.length; ++i) {
162       var material = new THREE.ParticleCanvasMaterial({program: circle});
163       var particle = new THREE.Particle(material);
164       particle.id = parseInt(nodes[i].id);
165       particle.arch = nodes[i].arch;
166       particle.boot_state = nodes[i].boot_state;
167       particle.component_id = nodes[i].component_id;
168       particle.position.x = (nodes[i].x - center.x) * 10;
169       particle.position.y = (nodes[i].y - center.y) * 10;
170       particle.position.z = (nodes[i].z - center.z) * 10;
171       particle.scale.x = particle.scale.y = 1;
172       setColor(particle)
173       this.scene.add(particle);
174     }
175     this.update();
176   };
177   
178   Map.prototype.updateSelected = function(arch, selected) {
179     var nodes = this.scene.children;
180     for (var i = 0; i < nodes.length; ++i) {
181       if (nodes[i].arch == arch && nodes[i].boot_state != "Suspected") {
182         var node = nodes[i];
183         var state = $.inArray(node.id, selected) == -1 ? "Alive" : "Selected";
184         if (node.boot_state != state) {
185           setState(node, state);
186         }
187       }
188     }
189   }
190   
191   Map.prototype.updatePosition = function() {
192     this.camera.position.x = this.distance * Math.sin(this.theta * Math.PI / 360) * Math.cos(this.phi * Math.PI / 360);
193     this.camera.position.y = this.distance * Math.sin(this.phi * Math.PI / 360);
194     this.camera.position.z = this.distance * Math.cos(this.theta * Math.PI / 360) * Math.cos(this.phi * Math.PI / 360);
195     this.camera.lookAt(this.scene.position);
196     this.camera.updateMatrix();
197   };
198   
199   Map.prototype.update = function() {
200     this.renderer.render(this.scene, this.camera);
201   };
202
203   function setState(node, state) {
204     node.boot_state = state;
205     setColor(node);
206     Senslab.notify(node);
207   }
208
209   function setColor(node) {
210     node.material.color.setHex(colors[node.boot_state] || colors["Selected"]);
211   }
212
213   function getCenter(nodes) {
214     var xmin = 0, ymin = 0, zmin = 0;
215     var xmax = 0, ymax = 0, zmax = 0;
216     
217     for (var i = 0; i < nodes.length; ++i) {
218       if (nodes[i].x > xmax) xmax = nodes[i].x;
219       if (nodes[i].x < xmin) xmin = nodes[i].x;
220       if (nodes[i].y > ymax) ymax = nodes[i].y;
221       if (nodes[i].y < ymin) ymin = nodes[i].y;
222       if (nodes[i].z > zmax) zmax = nodes[i].z;
223       if (nodes[i].z < zmin) zmin = nodes[i].z;
224     }
225     return {x: (xmax + xmin) / 2, y: (ymax + ymin) / 2, z: (zmax + zmin) / 2};
226   }
227
228   function factorize(nodes) {
229     var factorized = [];
230     var prev = 0;
231     var intervalStart = 0;
232     
233     for (var i = 0; i < nodes.length; ++i) {
234       if (intervalStart) {
235         if (nodes[i] == prev + 1) {
236           prev++;
237         } else {
238           factorized.push(intervalStart + "-" + prev);
239           intervalStart = 0;
240           prev = nodes[i];
241         }
242       } else {
243         if (nodes[i] == prev + 1) {
244           intervalStart = prev;
245           prev++;
246         } else {
247           prev && factorized.push(prev);
248           prev = nodes[i];
249         }
250       }
251     }
252     factorized.push(intervalStart ? intervalStart + "-" + prev : prev);
253     return factorized.join(",");
254   }
255   
256   function expand(input) {
257     var factorized = input.split(",");
258     var expanded = [];
259     for (var i = 0; i < factorized.length; ++i) {
260       var d = factorized[i].split("-");
261       if (d.length == 2) {
262         for (var j = parseInt(d[0]); j < parseInt(d[1]) + 1; j++) {
263           expanded.push(j);
264         }
265       } else {
266         expanded.push(parseInt(factorized[i]));
267       }
268     }
269     
270     expanded.sort(function(a, b) {
271       return a - b;
272     });
273     
274     for (var i = 1; i < expanded.length; i++) {
275       if (expanded[i] == expanded[i - 1]) {
276         expanded.splice(i--, 1);
277       }
278     }
279     return expanded;
280   }
281
282   function circle(context) {
283     context.beginPath();
284     context.arc(0, 0, 1, 0, Math.PI * 2, true);
285     context.closePath();
286     context.fill();
287   };
288   
289   return Map;
290 }();