Skip to content

Commit

Permalink
Restructuring cooldowns
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas committed Nov 26, 2017
1 parent 33a69cf commit 3939c64
Showing 1 changed file with 44 additions and 45 deletions.
89 changes: 44 additions & 45 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { sendEvent } = server;
const { connections, messages, errors, options } = server;

// Create history storage
const history = {cooldown: 0};
let history = {cooldown: 0};

// New connections
connections.subscribe(([ws, chan]) => {
Expand All @@ -37,50 +37,49 @@ messages
// const filename = `/tmp/${payload.id}-full.jpg`;
const filename = `${payload.id}-full`;

// Write base64 data to JPG file
saver.img('data:image/jpg;base64,'+payload.data, '/tmp', filename, (err, data) => {

// Log error
if (err) {
return console.log(err);
}

// Log new image
console.log(`* got new image ${filename}`);

// Try predict
axios
.get(`http://localhost:8080/predict/${payload.id}`)
.then(res => {

// Compute cooldown
const cooldown = (+new Date() - history.cooldown);

// Should handle
if (res.data.valid && cooldown > 5000) {
sendEvent(chan, 'MARK_RECEIVE', {
id: payload.id,
timestamp: +new Date(),
lat: payload.lat,
lng: payload.lng,
image: `http://${BASE_PATH}/static/${payload.id}.jpg`,
image_full: `http://${BASE_PATH}/static/${payload.id}-full.jpg`,
probability: 0,
valid: true
});

history.cooldown = +new Date();
console.log(`* [id:${payload.id}] valid=${res.data.valid}, cooldown=0`);
}
// Not handled
else {
console.log(`* [id:${payload.id}] valid=${res.data.valid}, cooldown=${cooldown}`);
}
})
.catch(err => {
console.log(err);
})
});
// Compute cooldown
let cooldown = (+new Date() - history.cooldown);

// Accept positive result only every 5 seconds
if (cooldown > 5000) {

// Write base64 data to JPG file
saver.img('data:image/jpg;base64,'+payload.data, '/tmp', filename, (err, data) => {

// Log error
if (err) return console.log(err);

// Log new image
console.log(`* got new image ${filename}`);

// Try predict
axios
.get(`http://localhost:8080/predict/${payload.id}`)
.then(res => {
// Should handle
if (res.data.valid) {
sendEvent(chan, 'MARK_RECEIVE', {
id: payload.id,
timestamp: +new Date(),
lat: payload.lat,
lng: payload.lng,
image: `http://${BASE_PATH}/static/${payload.id}.jpg`,
image_full: `http://${BASE_PATH}/static/${payload.id}-full.jpg`,
probability: 0,
valid: true
});
history.cooldown = +new Date();
console.log(`* [id:${payload.id}] valid=${res.data.valid}, cooldown=0`);
}
})
.catch(err => {
console.log(err.toString());
});
});
}
else {
console.log(`* [id:${payload.id}] cooldown=${cooldown}`);
}
}
});

Expand Down

0 comments on commit 3939c64

Please sign in to comment.