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;
21 Senslab.Map = function() {
25 "Suspected": 0xFF3030,
35 function Map($container, options) {
44 this.pointerDetectRay = new THREE.Raycaster();
45 this.pointerDetectRay.ray.direction.set(0, -1, 0);
46 this.projector = new THREE.Projector();
47 this.mouse2D = new THREE.Vector3(0, 0, 0);
49 this.renderer = new THREE.CanvasRenderer();
50 this.renderer.setSize(this.width, this.height);
52 this.camera = new THREE.PerspectiveCamera(75, this.width / this.height, 1, 10000);
54 this.scene = new THREE.Scene();
55 this.scene.add(this.camera);
57 this.updatePosition();
61 this.$nodeInputs = {};
63 $.each(archs, function(i, arch) {
64 self.$nodeInputs[arch] = $("<input type='text' placeholder='" + arch + "'>")
67 self.updateSelected(arch, expand($(this).val()));
72 var $canvas = $(this.renderer.domElement)
73 .mousemove(function(e) {
74 self.mouse2D.x = ((e.pageX - $canvas.offset().left) / $canvas.width()) * 2 - 1;
75 self.mouse2D.y = -((e.pageY - $canvas.offset().top) / $canvas.height()) * 2 + 1;
78 self.theta -= e.pageX - self.mouse2D.pageX;
79 self.phi += e.pageY - self.mouse2D.pageY;
85 self.mouse2D.pageX = e.pageX;
86 self.mouse2D.pageY = e.pageY;
88 self.updatePosition();
91 }).mousedown(function(e) {
95 self.pointerDetectRay = self.projector.pickingRay(self.mouse2D.clone(), self.camera);
96 var intersects = self.pointerDetectRay.intersectObjects(self.scene.children);
97 if (intersects.length > 0) {
98 var node = intersects[0].object;
99 if (node.boot_state != "Suspected") {
100 setState(node, node.boot_state == "Alive" ? "Selected" : "Alive");
101 var $nodeInput = self.$nodeInputs[node.arch];
102 $nodeInput.val(factorize(self.getNodesId(node.arch)));
108 self.mouse2D.pageX = e.pageX;
109 self.mouse2D.pageY = e.pageY;
113 }).mouseup(function(e) {
120 }).mouseleave(function(e) {
122 }).mousewheel(function(e, delta) {
124 self.distance += delta * 5;
125 self.updatePosition();
129 $container.append($canvas);
132 Map.prototype.getNodesId = function(arch) {
133 var allNodes = this.scene.children;
135 for (var i = 0; i < allNodes.length; ++i) {
136 if (allNodes[i].arch == arch && allNodes[i].boot_state == "Selected") {
137 nodes.push(allNodes[i].id);
143 Map.prototype.addNodes = function(nodes) {
144 var center = getCenter(nodes);
146 nodes.sort(function(a, b) {
150 for(var i = 0; i < nodes.length; ++i) {
151 var material = new THREE.ParticleCanvasMaterial({program: circle});
152 var particle = new THREE.Particle(material);
153 particle.id = parseInt(nodes[i].id);
154 particle.arch = nodes[i].arch;
155 particle.boot_state = nodes[i].boot_state;
156 particle.position.x = (nodes[i].x - center.x) * 10;
157 particle.position.y = (nodes[i].y - center.y) * 10;
158 particle.position.z = (nodes[i].z - center.z) * 10;
159 particle.scale.x = particle.scale.y = 1;
161 this.scene.add(particle);
166 Map.prototype.updateSelected = function(arch, selected) {
167 var nodes = this.scene.children;
168 for (var i = 0; i < nodes.length; ++i) {
169 if (nodes[i].arch == arch && nodes[i].boot_state != "Suspected") {
171 var state = $.inArray(node.id, selected) == -1 ? "Alive" : "Selected";
172 if (node.boot_state != state) {
173 setState(node, state);
179 Map.prototype.updatePosition = function() {
180 this.camera.position.x = this.distance * Math.sin(this.theta * Math.PI / 360) * Math.cos(this.phi * Math.PI / 360);
181 this.camera.position.y = this.distance * Math.sin(this.phi * Math.PI / 360);
182 this.camera.position.z = this.distance * Math.cos(this.theta * Math.PI / 360) * Math.cos(this.phi * Math.PI / 360);
183 this.camera.lookAt(this.scene.position);
184 this.camera.updateMatrix();
187 Map.prototype.update = function() {
188 this.renderer.render(this.scene, this.camera);
191 function setState(node, state) {
192 node.boot_state = state;
197 function setColor(node) {
198 node.material.color.setHex(colors[node.boot_state] || colors["Selected"]);
201 function getCenter(nodes) {
202 var xmin = 0, ymin = 0, zmin = 0;
203 var xmax = 0, ymax = 0, zmax = 0;
205 for (var i = 0; i < nodes.length; ++i) {
206 if (nodes[i].x > xmax) xmax = nodes[i].x;
207 if (nodes[i].x < xmin) xmin = nodes[i].x;
208 if (nodes[i].y > ymax) ymax = nodes[i].y;
209 if (nodes[i].y < ymin) ymin = nodes[i].y;
210 if (nodes[i].z > zmax) zmax = nodes[i].z;
211 if (nodes[i].z < zmin) zmin = nodes[i].z;
213 return {x: (xmax + xmin) / 2, y: (ymax + ymin) / 2, z: (zmax + zmin) / 2};
216 function factorize(nodes) {
219 var intervalStart = 0;
221 for (var i = 0; i < nodes.length; ++i) {
223 if (nodes[i] == prev + 1) {
226 factorized.push(intervalStart + "-" + prev);
231 if (nodes[i] == prev + 1) {
232 intervalStart = prev;
235 prev && factorized.push(prev);
240 factorized.push(intervalStart ? intervalStart + "-" + prev : prev);
241 return factorized.join(",");
244 function expand(input) {
245 var factorized = input.split(",");
247 for (var i = 0; i < factorized.length; ++i) {
248 var d = factorized[i].split("-");
250 for (var j = parseInt(d[0]); j < parseInt(d[1]) + 1; j++) {
254 expanded.push(parseInt(factorized[i]));
258 expanded.sort(function(a, b) {
262 for (var i = 1; i < expanded.length; i++) {
263 if (expanded[i] == expanded[i - 1]) {
264 expanded.splice(i--, 1);
270 function circle(context) {
272 context.arc(0, 0, 1, 0, Math.PI * 2, true);
277 var notify = function(node) {
278 console.log("[Notify] node " + node.id + " is " + node.boot_state);