Skip to content

refactor: Date now handling #142

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

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ if (srv_config.ROOKOUT_TOKEN) rookout.start({token: srv_config.ROOKOUT_TOKEN});

// last activity track
app.use((req, res, next) => {
if (req.body.akey) db.query('UPDATE accounts SET lastactivity=? WHERE akey=?', [parseInt(new Date() / 1000), req.body.akey], () => next());
if (req.body.akey) db.query('UPDATE accounts SET lastactivity=? WHERE akey=?', [Math.floor(Date.now() / 1000), req.body.akey], () => next());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

else next();
});

Expand Down Expand Up @@ -155,7 +155,7 @@ app.post('/cat', cats.buyCat);
app.post('/debug', (req, res) => {
if (typeof req.body.data === 'string') {
db.query('INSERT INTO debug (data, akey, timestamp) VALUES (?, ?, ?)', [
req.body.data, req.body.akey, ((parseInt(req.body.timestamp)) ? req.body.timestamp : parseInt(new Date() / 1000))
req.body.data, req.body.akey, ((parseInt(req.body.timestamp)) ? req.body.timestamp : Math.floor(Date.now() / 1000))
], (err, dbRes) => {
if (!err && dbRes) {
res.json({status: true});
Expand Down
4 changes: 2 additions & 2 deletions modules/integrations/abrp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ const submitData = (akey) => {
akey
], (err, dbRes) => {
let data;
const now = parseInt(new Date() / 1000);
const now = Math.floor(Date.now() / 1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


if (!err && dbRes && (data = dbRes[0])) {
const socUpToDate = now < data.last_soc + 300;
const locationUpToDate = now < data.last_location + 300;

if (data.abrp && cars[data.car] && (data.soc_display || data.soc_bms) && socUpToDate) {
const abrpData = {
utc: new Date() / 1000,
utc: now,
soc: data.soc_display || data.soc_bms,
speed: locationUpToDate ? data.gps_speed * 3.6 || 0 : 0,
lat: locationUpToDate ? data.latitude : 0,
Expand Down
4 changes: 2 additions & 2 deletions modules/notification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const send = (req, res) => {
if (!err && dbRes && (userObj = dbRes[0]) != null) {
// validate token
if (userObj.token === req.body.token) {
const now = parseInt(new Date() / 1000);
const now = Math.floor(Date.now() / 1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


// valid, compare last_notification timestamp to determine if limit reached
if ((userObj.last_notification || 0) + 60 < now) {
Expand Down Expand Up @@ -80,4 +80,4 @@ const send = (req, res) => {

module.exports = {
send
};
};
6 changes: 3 additions & 3 deletions modules/qr/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ module.exports = {
qrStatus(req.query.code, (err, codeObj) => {
if (!err && codeObj) {
// prevent access if not charging or data is to old (older than 10 minutes)
if (!codeObj.charging || (new Date() / 1000 - 600) > codeObj.last_soc) {
if (!codeObj.charging || (Date.now() / 1000 - 600) > codeObj.last_soc) {
res.status(401).json({
error: srv_errors.ACCESS_DENIED,
debug: ((srv_config.DEBUG) ? codeObj : null)
Expand Down Expand Up @@ -274,7 +274,7 @@ module.exports = {
let dbObj;

if (!err && dbRes && (dbObj = dbRes[0])) {
const now = parseInt(new Date() / 1000);
const now = Math.floor(Date.now() / 1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


// valid, compare last_qr timestamp to determine if limit reached
if ((dbObj.last_qr || 0) + 600 < now) {
Expand Down Expand Up @@ -303,4 +303,4 @@ module.exports = {
}
});
}
};
};
8 changes: 4 additions & 4 deletions modules/sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const srv_config = require('./../../srv_config.json'),
* @param {Function} callback callback function
*/
const postSoC = (akey, socObj, callback) => {
const now = parseInt(new Date() / 1000);
const now = Math.floor(Date.now() / 1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


db.query('UPDATE sync SET soc_display=?, soc_bms=?, last_soc=? WHERE akey=?', [
socObj.display, socObj.bms, now, akey
Expand Down Expand Up @@ -48,7 +48,7 @@ const getSoC = (akey, callback) => {
* @param {Function} callback callback function
*/
const postExtended = (akey, extendedObj, callback) => {
const now = parseInt(new Date() / 1000);
const now = Math.floor(Date.now() / 1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).



db.query('UPDATE sync SET soh=?, charging=?, rapid_charge_port=?, normal_charge_port=?, slow_charge_port=?, aux_battery_voltage=?, dc_battery_voltage=?, dc_battery_current=?, dc_battery_power=?,\
Expand Down Expand Up @@ -85,7 +85,7 @@ const getExtended = (akey, callback) => {
* @param {Function} callback callback function
*/
const postLocation = (akey, locationObj, callback) => {
const now = parseInt(new Date() / 1000);
const now = Math.floor(Date.now() / 1000);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


db.query('UPDATE sync SET latitude=?, longitude=?, gps_speed=?, accuracy=?, location_timestamp=?, last_location=? WHERE akey=?', [
locationObj.latitude, locationObj.longitude, locationObj.speed, locationObj.accuracy, locationObj.timestamp, now, akey
Expand Down Expand Up @@ -349,4 +349,4 @@ module.exports = {
}
});
}
};
};
4 changes: 2 additions & 2 deletions modules/web/account/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const login = (mail, password, callback) => {
if (!err && valid) {
// update last login timestamp
db.query('UPDATE login SET last_login=? WHERE id=?', [
parseInt(new Date() / 1000), userObj.id
Math.floor(Date.now() / 1000), userObj.id
], (err, dbRes) => callback(err, ((!err && dbRes) ? userObj.id : null)));
} else callback(((err) ? err : srv_errors.INVALID_CREDENTIALS));
});
Expand Down Expand Up @@ -134,4 +134,4 @@ module.exports = {
});
}
}
};
};