Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migliorato segnalazione grandi eventi #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,32 @@ const geocoding = require('./maps').geocoding;
const Card = require('./maps').Card;
const social = require('./social');

const BROADCAST_ALWAYS = config('ingv').broadcastAlways;
const BROADCAST_THRESHOLD = config('ingv').broadcastThreshold;
const BROADCAST_RADIUS = config('ingv').broadcastRadius;
const SOCIAL_THRESHOLD = config('social').threshold;
const SOCIAL_ENABLED = config('social').enabled;

function _distanceFromCenter(lat, lon) {
let lat1 = 41.2909725; // "Center" of
let lon1 = 12.572917; // Italy
let lat2 = lat;
let lon2 = lon;

let R = 6371e3;
let p1 = lat1 * Math.PI/180; // φ, λ in radians
let p2 = lat2 * Math.PI/180;
let dp = (lat2-lat1) * Math.PI/180;
let dl = (lon2-lon1) * Math.PI/180;

let a = Math.sin(dp/2) * Math.sin(dp/2) +
Math.cos(p1) * Math.cos(p2) *
Math.sin(dl/2) * Math.sin(dl/2);
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

return R * c / 1000; // in km
}

// Create the HTTP server for handling tg messages
let serverPort = config('telegram').serverPort;
let server = new TelegramServer({ port: serverPort });
Expand Down Expand Up @@ -43,48 +65,48 @@ let poller = new IngvPoller(options);
// That must be notified
poller.on('earthquakes', (earthquakes) => {
logger.info(`Notifying ${earthquakes.length} events`);

// Loop through them
async.eachSeries(earthquakes, (ev, callback) => {
logger.info('New event', ev);

let { lat, lon } = ev['origin'];
let magnitude = ev['magnitude']['value'];

logger.info(`Reverse geocoding for event <${ev.id}>`);

// Convert the geographical coordinates to a city name (reverse geocoding)
geocoding.reverse(lat, lon, (err, result) => {
if (err) ; // TODO: do something, please

let city;
if (result) {
city = result['name'];
}
else {
city = ev['zone'];
}

ev['city'] = city;
// Update the db representation of the event
db.history.setCity(ev['id'], city);

// Generate the image card
let card = new Card(ev);
card.generate((err, filePath) => {
if (!err && filePath) {
ev['cardPath'] = filePath;
}

if (magnitude >= BROADCAST_THRESHOLD &&
// broadcast only if we're sure that the country is Italy
result && result['country'] == 'IT') {
if (magnitude >= BROADCAST_ALWAYS ||
// broadcast only if the earthquake is >= BROADCAST_THRESHOLD and it is in a radius with Italy in the center
(magnitude >= BROADCAST_THRESHOLD && _distanceFromCenter(lat, lon) <= BROADCAST_RADIUS) ) {
notifications.broadcast(ev, callback);
}
else {
else { // FIXME: this "else" means no notifications will be sent if someone is not interested in broadcast but that earthquake is near in his radius. Eg: There's an earthquake of magnitude 9 in Milan, if I'm interested in earthquakes near Milan but not in "big earthquake events" I will not be signaled
notifications.send(ev, callback);
}

// Post to social networks
if (SOCIAL_ENABLED && magnitude >= SOCIAL_THRESHOLD) {
social.publish(ev);
Expand All @@ -95,3 +117,4 @@ poller.on('earthquakes', (earthquakes) => {
logger.info('Done');
});
});

4 changes: 3 additions & 1 deletion lib/config/example.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
},
"ingv": {
"pollingInterval": 60000,
"broadcastThreshold": 5
"broadcastAlways": 9,
"broadcastThreshold": 5,
"broadcastRadius" : 1000
},
"social": {
"enabled": false,
Expand Down