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 = info[0].split("-")[1];
16 node.normalized = true;
19 notify: function(node) {
20 console.log("[Notify] node " + node.id + " is " + node.boot_state);
22 createMaps: function($container, sites, nodes) {
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]);
32 Senslab.Map = function() {
36 "Suspected": 0xFF3030,
46 function Map($container, options) {
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);
60 this.renderer = new THREE.CanvasRenderer();
61 this.renderer.setSize(this.width, this.height);
63 this.camera = new THREE.PerspectiveCamera(75, this.width / this.height, 1, 10000);
65 this.scene = new THREE.Scene();
66 this.scene.add(this.camera);
68 this.updatePosition();
72 this.$nodeInputs = {};
74 $.each(archs, function(i, arch) {
75 self.$nodeInputs[arch] = $("<input type='text' placeholder='" + arch + "'>")
78 self.updateSelected(arch, expand($(this).val()));
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;
89 self.theta -= e.pageX - self.mouse2D.pageX;
90 self.phi += e.pageY - self.mouse2D.pageY;
96 self.mouse2D.pageX = e.pageX;
97 self.mouse2D.pageY = e.pageY;
99 self.updatePosition();
102 }).mousedown(function(e) {
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 particle = intersects[0].object;
110 if (particle.data.boot_state != "Suspected") {
111 setState(particle, particle.data.boot_state == "Alive" ? "Selected" : "Alive");
112 var $nodeInput = self.$nodeInputs[particle.data.arch];
113 $nodeInput.val(factorize(self.getNodesId(particle.data.arch)));
119 self.mouse2D.pageX = e.pageX;
120 self.mouse2D.pageY = e.pageY;
124 }).mouseup(function(e) {
131 }).mouseleave(function(e) {
133 }).mousewheel(function(e, delta) {
135 self.distance += delta * 5;
136 self.updatePosition();
140 $container.append($canvas);
143 Map.prototype.getNodesId = function(arch) {
144 var particles = this.scene.children;
146 for (var i = 0; i < particles.length; ++i) {
147 if (particles[i].data && particles[i].data.arch == arch && particles[i].data.boot_state == "Selected") {
148 nodes.push(particles[i].id);
154 Map.prototype.addNodes = function(nodes) {
155 var center = getCenter(nodes);
157 nodes.sort(function(a, b) {
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.data = nodes[i];
165 particle.id = parseInt(nodes[i].id);
166 particle.position.x = (nodes[i].x - center.x) * 10;
167 particle.position.y = (nodes[i].y - center.y) * 10;
168 particle.position.z = (nodes[i].z - center.z) * 10;
169 particle.scale.x = particle.scale.y = 1;
171 this.scene.add(particle);
176 Map.prototype.updateSelected = function(arch, selected) {
177 var particles = this.scene.children;
178 for (var i = 0; i < particles.length; ++i) {
179 if (particles[i].data && particles[i].data.arch == arch && particles[i].data.boot_state != "Suspected") {
180 var particle = particles[i];
181 var state = $.inArray(particle.id, selected) == -1 ? "Alive" : "Selected";
182 if (particle.data.boot_state != state) {
183 setState(particle, state);
189 Map.prototype.updatePosition = function() {
190 this.camera.position.x = this.distance
191 * Math.sin(this.theta * Math.PI / 360)
192 * 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
195 * Math.cos(this.theta * Math.PI / 360)
196 * Math.cos(this.phi * Math.PI / 360);
197 this.camera.lookAt(this.scene.position);
198 this.camera.updateMatrix();
201 Map.prototype.update = function() {
202 this.renderer.render(this.scene, this.camera);
205 function setState(particle, state) {
206 particle.data.boot_state = state;
208 Senslab.notify(particle.data);
211 function setColor(particle) {
212 var color = colors[particle.data.boot_state] || colors["Selected"];
213 particle.material.color.setHex(color);
216 function getCenter(nodes) {
217 var xmin = 0, ymin = 0, zmin = 0;
218 var xmax = 0, ymax = 0, zmax = 0;
220 for (var i = 0; i < nodes.length; ++i) {
221 if (nodes[i].x > xmax) xmax = nodes[i].x;
222 if (nodes[i].x < xmin) xmin = nodes[i].x;
223 if (nodes[i].y > ymax) ymax = nodes[i].y;
224 if (nodes[i].y < ymin) ymin = nodes[i].y;
225 if (nodes[i].z > zmax) zmax = nodes[i].z;
226 if (nodes[i].z < zmin) zmin = nodes[i].z;
228 return {x: (xmax + xmin) / 2, y: (ymax + ymin) / 2, z: (zmax + zmin) / 2};
231 function factorize(nodes) {
234 var intervalStart = 0;
236 for (var i = 0; i < nodes.length; ++i) {
238 if (nodes[i] == prev + 1) {
241 factorized.push(intervalStart + "-" + prev);
246 if (nodes[i] == prev + 1) {
247 intervalStart = prev;
250 prev && factorized.push(prev);
255 factorized.push(intervalStart ? intervalStart + "-" + prev : prev);
256 return factorized.join(",");
259 function expand(input) {
260 var factorized = input.split(",");
262 for (var i = 0; i < factorized.length; ++i) {
263 var d = factorized[i].split("-");
265 for (var j = parseInt(d[0]); j < parseInt(d[1]) + 1; j++) {
269 expanded.push(parseInt(factorized[i]));
273 expanded.sort(function(a, b) {
277 for (var i = 1; i < expanded.length; i++) {
278 if (expanded[i] == expanded[i - 1]) {
279 expanded.splice(i--, 1);
285 function circle(context) {
287 context.arc(0, 0, 1, 0, Math.PI * 2, true);