Skip to content

Commit 68e1a95

Browse files
committed
#1 many updates.
1 parent b20e22b commit 68e1a95

File tree

11 files changed

+1368
-12
lines changed

11 files changed

+1368
-12
lines changed

generator/config/default.yml

+524
Large diffs are not rendered by default.

generator/config/local.json

+585
Large diffs are not rendered by default.

generator/index.js

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
var
2+
path = require('path'),
3+
fs = require('fs'),
4+
ce = require('cloneextend'),
5+
joola = require('joola');
6+
7+
process.env.JOOLA_CONFIG_STORE_LOGGER_CONSOLE_LEVEL = 'trace';
8+
9+
joola.init({}, function (err) {
10+
if (err)
11+
throw err;
12+
13+
joola.users.verifyAPIToken({user: joola.SYSTEM_USER}, 'apitoken-demo', function (err, user) {
14+
if (err)
15+
throw err;
16+
17+
global.user = user;
18+
joola.events.emit('goahead');
19+
});
20+
});
21+
22+
joola.events.on('goahead', function () {
23+
var sources = [];
24+
var dataPoints = [];
25+
var files = fs.readdirSync(path.join(__dirname, './sources'));
26+
files.forEach(function (file) {
27+
joola.logger.info('Require event source from: ./sources/' + file);
28+
sources.push(require(path.join(__dirname, './sources', file)));
29+
});
30+
31+
setInterval(function () {
32+
var points = generateRandomPoints({
33+
'lat': 32.476664,
34+
'lon': 34.974388
35+
}, 5000, 1);
36+
37+
points.forEach(function (point, i) {
38+
point.location = {
39+
lat: point.lat,
40+
lon: point.lon
41+
};
42+
delete point.lat;
43+
delete point.lon;
44+
sources.forEach(function (s) {
45+
var _point = ce.extend(point, s);
46+
Object.keys(_point).forEach(function (key) {
47+
var elem = _point[key].value;
48+
if (typeof elem === 'function') {
49+
_point[key].value = elem.call(this);
50+
}
51+
});
52+
if (_point._save.value) {
53+
delete _point._save;
54+
dataPoints.push(ce.clone(_point));
55+
}
56+
});
57+
});
58+
}, 100);
59+
60+
setInterval(function () {
61+
//var transmission = {};
62+
var slicedPoints = dataPoints.splice(0, 1000);
63+
if (slicedPoints && slicedPoints.length > 0) {
64+
joola.beacon.insert({user: user}, 'geo', slicedPoints, function (err) {
65+
if (err)
66+
throw err;
67+
});
68+
}
69+
}, 800);
70+
});
71+
72+
//https://gist.github.com/mkhatib/5641004
73+
/**
74+
* Generates number of random geolocation points given a center and a radius.
75+
* @param {Object} center A JS object with lat and lng attributes.
76+
* @param {number} radius Radius in meters.
77+
* @param {number} count Number of points to generate.
78+
* @return {array} Array of Objects with lat and lng attributes.
79+
*/
80+
function generateRandomPoints(center, radius, count) {
81+
var points = [];
82+
for (var i = 0; i < count; i++) {
83+
points.push(generateRandomPoint(center, radius));
84+
}
85+
return points;
86+
}
87+
88+
89+
/**
90+
* Generates number of random geolocation points given a center and a radius.
91+
* Reference URL: http://goo.gl/KWcPE.
92+
* @param {Object} center A JS object with lat and lng attributes.
93+
* @param {number} radius Radius in meters.
94+
* @return {Object} The generated random points as JS object with lat and lng attributes.
95+
*/
96+
function generateRandomPoint(center, radius) {
97+
var x0 = center.lon;
98+
var y0 = center.lat;
99+
// Convert Radius from meters to degrees.
100+
var rd = radius / 111300;
101+
102+
var u = Math.random();
103+
var v = Math.random();
104+
105+
var w = rd * Math.sqrt(u);
106+
var t = 2 * Math.PI * v;
107+
var x = w * Math.cos(t);
108+
var y = w * Math.sin(t);
109+
110+
var xp = x / Math.cos(y0);
111+
112+
// Resulting point.
113+
return {'lat': y + y0, 'lon': xp + x0};
114+
}

generator/sources/arial.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module.exports = {
2+
_save: {key: '_save', value: function () {
3+
return ((Math.floor(Math.random() * 100) + 1) / 100) < 0.7;
4+
}},
5+
source_id: {key: 'source_id', type: 'dimension', datatype: 'string', value: 'arial'},
6+
sensor_type: {key: 'sensor_type', type: 'dimension', datatype: 'string', value: 'movement'},
7+
sensor_uid: {key: 'sensor_uid', type: 'dimension', datatype: 'string', value: function () {
8+
return joola.common.uuid(4)
9+
}},
10+
timestamp: {key: 'timestamp', type: 'dimension', datatype: 'date', value: function () {
11+
return new Date();
12+
}},
13+
grid: {key: 'grid', type: 'dimension', datatype: 'string', value: function () {
14+
return 'grid-' + Math.floor(Math.random() * 10) + 1
15+
}},
16+
detection_type: {key: 'detection_type', type: 'dimension', datatype: 'string', value: function () {
17+
return ((Math.floor(Math.random() * 100) + 1) / 100) < 0.7 ? 'person' : 'vehicle'
18+
}},
19+
speed: {key: 'speed', type: 'metric', datatype: 'number', aggregation: 'avg', value: function () {
20+
return (Math.floor(Math.random() * 180) + 0)
21+
}}
22+
};

generator/sources/sensor-a.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
_save: {key: '_save', value: function () {
3+
return ((Math.floor(Math.random() * 100) + 1) / 100) < 0.5;
4+
}},
5+
source_id: {key: 'source_id', type: 'dimension', datatype: 'string', value: 'sensor-a'},
6+
sensor_type: {key: 'sensor_type', type: 'dimension', datatype: 'string', value: 'acoustic'},
7+
sensor_uid: {key: 'sensor_uid', type: 'dimension', datatype: 'string', value: function () {
8+
return joola.common.uuid(4)
9+
}},
10+
timestamp: {key: 'timestamp', type: 'dimension', datatype: 'date', value: function () {
11+
return new Date();
12+
}},
13+
grid: {key: 'grid', type: 'dimension', datatype: 'string', value: function () {
14+
return 'grid-' + Math.floor(Math.random() * 10) + 1
15+
}},
16+
level: {key: 'level', type: 'metric', datatype: 'number', aggregation: 'avg', value: function () {
17+
return (Math.floor(Math.random() * 100) + 0) / 100
18+
}}
19+
};

generator/sources/sensor-b.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
_save: {key: '_save', value: function () {
3+
return ((Math.floor(Math.random() * 100) + 1) / 100) < 0.7;
4+
}},
5+
source_id: {key: 'source_id', type: 'dimension', datatype: 'string', value: 'sensor-b'},
6+
sensor_type: {key: 'sensor_datatype', type: 'dimension', datatype: 'string', value: 'atmosphere'},
7+
sensor_uid: {key: 'sensor_uid', type: 'dimension', datatype: 'string', value: function () {
8+
return joola.common.uuid(4)
9+
}},
10+
timestamp: {key: 'timestamp', type: 'dimension', datatype: 'date', value: function () {
11+
return new Date();
12+
}},
13+
grid: {key: 'grid', type: 'dimension', datatype: 'string', value: function () {
14+
return 'grid-' + Math.floor(Math.random() * 10) + 1
15+
}},
16+
o2: {key: 'o2', type: 'metric', datatype: 'number', aggregation: 'avg', value: function () {
17+
return (Math.floor(Math.random() * 100) + 90) / 100
18+
}}
19+
};

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ joola.init({}, function (err) {
2323
point.location = {lat: point.lat, lon: point.lon};
2424
point.tag = 'tag';
2525
point.type = 'sensor';
26+
point.source_id = ((Math.floor(Math.random() * 100) + 0) / 100) < 0.5 ? 'sensor' : 'arial';
2627
point.uuid = joola.common.uuid();
2728
point.metric = Math.floor(Math.random() * 60) + 1;
2829
point.temp = (Math.floor(Math.random() * 42) + 1);

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "joola.demo.geo-track",
33
"version": "0.0.1",
4-
"scripts":
5-
{
6-
"start":"node index.js"
4+
"scripts": {
5+
"start": "node index.js"
76
},
87
"dependencies": {
8+
"cloneextend": "0.0.3",
99
"joola": "latest",
1010
"node-static": "^0.7.6"
1111
}

public/js/globals.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
var query = {
22
timeframe: 'last_90_seconds',
33
interval: 'second',
4-
dimensions: ['location.lat', 'location.lon', 'tag', 'type'],
4+
dimensions: ['source_id', 'location.lat', 'location.lon', 'tag', 'type'],
55
metrics: ['metric', {key: 'humidity', aggregation: 'avg', decimals: 2}],
66
collection: 'geo',
77
realtime: {
88
enabled: true,
99
interval: state.get('config.refresh.duration')
1010
}
1111
};
12-
var runningQueries=[];
12+
var runningQueries = [];
1313
var map;
14+
var layers;
1415
var geojson = [];
1516
var heat;
1617
var EPSData = [];

public/js/joola.js

+64-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ joola.on('ready', function () {
3636
[bounds.getSouthEast().lat, bounds.getSouthEast().lng]
3737
];
3838

39+
var collections = {};
40+
3941
query.filters = [];
4042
query.filters.push(['location', 'geo_bounds', bounds]);
41-
console.log(query.filters);
43+
//console.log(query.filters);
4244
new joola.viz.Table({
4345
container: '#table',
4446
query: query,
@@ -64,9 +66,58 @@ joola.on('ready', function () {
6466
if (runningQueries.indexOf(uuid) === -1)
6567
runningQueries.push(uuid);
6668
buildShowingResults();
69+
70+
6771
},
6872
enter: function (data) {
6973
data.forEach(function (point) {
74+
//build the collection list
75+
var source_id = point.raw.source_id;
76+
if (source_id) {
77+
collections[source_id] = collections[source_id] || 0;
78+
collections[source_id]++;
79+
}
80+
Object.keys(layers._layers).forEach(function (key) {
81+
var layer = layers._layers[key];
82+
var name = layer.name;
83+
if (!layer.overlay)
84+
return;
85+
var found = false;
86+
Object.keys(collections).forEach(function (col) {
87+
if (name.indexOf(col) > -1)
88+
found = true;
89+
});
90+
if (!found) {
91+
//console.log('Removing layer', name);
92+
layers.removeLayer(layer.layer);
93+
}
94+
});
95+
Object.keys(collections).forEach(function (col) {
96+
var found = false;
97+
Object.keys(layers._layers).forEach(function (key) {
98+
var layer = layers._layers[key];
99+
var name = layer.name;
100+
if (name.indexOf(col) > -1) {
101+
//console.log('Updating layer', name);
102+
// layer.name = col + ' (' + collections[col] + ')';
103+
found = true;
104+
}
105+
});
106+
if (!found) {
107+
//var featureLayer = L.mapbox.featureLayer().addTo(map);
108+
//console.log('Adding layer', col);
109+
var featureLayer = new L.MarkerClusterGroup({ polygonOptions: {
110+
fillColor: '#3887be',
111+
color: '#3887be',
112+
weight: 2,
113+
opacity: 1,
114+
fillOpacity: 0.5
115+
}}).addTo(map);
116+
layers.addOverlay(featureLayer, col);
117+
}
118+
});
119+
//layers._update();
120+
70121
var key = point.key;
71122
point = point.raw;
72123

@@ -79,7 +130,18 @@ joola.on('ready', function () {
79130
data: point,
80131
uuid: key
81132
});
82-
markers.addLayer(marker);
133+
134+
Object.keys(layers._layers).forEach(function (key) {
135+
var layer = layers._layers[key];
136+
var name = layer.name;
137+
if (name.indexOf(source_id) > -1) {
138+
//console.log('adding point', marker);
139+
layer.layer.addLayer(marker);
140+
}
141+
});
142+
143+
144+
//markers.addLayer(marker);
83145
currentTableMarkers[key] = marker;
84146
markers.eachLayer(function (marker) {
85147
//heat.addLatLng(marker.getLatLng());

public/js/leaflet.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,20 @@ function initMap(next) {
1010
L.mapbox.accessToken = 'pk.eyJ1Ijoiam9vbGEiLCJhIjoiNnFpMlJEbyJ9.R3cNnoE_WpDSxtJIjhN3JQ';
1111
map = L.mapbox.map('map-canvas', null, {attributionControl: {compact: true}}).setView(L.latLng(32.476664, 34.974388), 13);
1212

13-
14-
L.control.layers({
15-
'Satellite': L.mapbox.tileLayer('joola.lhekjm4g'),
16-
'Dark': L.mapbox.tileLayer('joola.lhf4b967').addTo(map)
17-
}).addTo(map);
13+
layers = L.control.layers({
14+
Satellite: L.mapbox.tileLayer('joola.lhekjm4g'),
15+
Dark: L.mapbox.tileLayer('joola.lhf4b967').addTo(map)
16+
}, {}, {collapsed: false}
17+
).addTo(map);
18+
19+
map.on('overlayadd', function (e) {
20+
var layer = e.layer;
21+
console.log('overlay added', e);
22+
});
23+
map.on('overlayremove', function (e) {
24+
var layer = e.layer;
25+
console.log('overlay removed', e);
26+
});
1827

1928
var featureGroup = L.featureGroup().addTo(map);
2029
var drawControl = new L.Control.Draw({

0 commit comments

Comments
 (0)