Skip to content

Instantly share code, notes, and snippets.

@doorbash
Last active November 24, 2019 12:23
Show Gist options
  • Save doorbash/1ed3f7af5b99c48b0117ec48d2106613 to your computer and use it in GitHub Desktop.
Save doorbash/1ed3f7af5b99c48b0117ec48d2106613 to your computer and use it in GitHub Desktop.
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