Last active
November 24, 2019 12:23
-
-
Save doorbash/1ed3f7af5b99c48b0117ec48d2106613 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SerialPort = require('serialport'); | |
var createInterface = require('readline').createInterface; | |
var moment = require('moment'); | |
const PHONE_NUMBER = "+989XXXXXXXXX"; | |
var port = new SerialPort('COM3'); | |
var lineReader = createInterface({ | |
input: port | |
}); | |
lineReader.on('line', function (line) { | |
console.log(line); | |
if (line.startsWith("+CMGR:")) { | |
var spl = line.split("\""); | |
console.log(spl); | |
if(spl[1] !== "REC UNREAD") { | |
console.log("message is not unread"); | |
return; | |
} | |
if(spl[3] !== PHONE_NUMBER) { | |
console.log("phone number is wrong"); | |
return; | |
} | |
var diff = moment(spl[5].substr(0, 17), "YY/MM/DD,HH:mm:ss").diff(moment()); | |
console.log("diff is", diff); | |
if (diff <= 0 && diff >= -10000) { | |
console.log("Let's open the door"); | |
setTimeout(() => { | |
port.write('OPEN\n'); | |
}, 1000); | |
} else { | |
console.log("message is old.. ignore it"); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment