Skip to content

Commit

Permalink
fix(miio): Don't get confused by the system clock doing weird things
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypfer committed Feb 15, 2021
1 parent 58515d9 commit 5e6d8df
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/miio/Codec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ class Codec {
*/
encode(msg, stamp, deviceId) {
const cipher = crypto.createCipheriv("aes-128-cbc", this.tokenKey, this.tokenIV);
const encrypted =
msg === null ? Buffer.alloc(0) : Buffer.concat([cipher.update(msg), cipher.final()]);
const secondsPassed = Math.floor(Date.now() - stamp.time) / 1000;
const encrypted = msg === null ? Buffer.alloc(0) : Buffer.concat([cipher.update(msg), cipher.final()]);
const secondsPassed = Math.max(0, Math.floor((Date.now() - stamp.time) / 1000));

const header = createMiioHeader({
timestamp: stamp.val + secondsPassed,
deviceId: deviceId,
Expand All @@ -113,4 +113,4 @@ class Codec {
}
}

module.exports = Codec;
module.exports = Codec;

0 comments on commit 5e6d8df

Please sign in to comment.