First github Commit

This commit is contained in:
canove
2020-06-18 17:25:43 -03:00
parent 48ee6385d3
commit 8a6324a98b
19 changed files with 391 additions and 210 deletions

View File

@@ -1,10 +1,6 @@
const Contact = require("../models/Contact");
const Message = require("../models/Message");
const Whatsapp = require("../models/Whatsapp");
const wbotMessageListener = require("./wbotMessageListener");
const path = require("path");
const fs = require("fs");
const { getIO } = require("../socket");
const { getWbot, init } = require("./wbot");
@@ -12,32 +8,43 @@ const wbotMonitor = () => {
const io = getIO();
const wbot = getWbot();
wbot.on("change_state", newState => {
console.log("monitor", newState);
});
try {
wbot.on("change_state", newState => {
console.log("monitor", newState);
});
wbot.on("change_battery", batteryInfo => {
// Battery percentage for attached device has changed
const { battery, plugged } = batteryInfo;
console.log(`Battery: ${battery}% - Charging? ${plugged}`);
});
wbot.on("change_battery", async batteryInfo => {
// Battery percentage for attached device has changed
const { battery, plugged } = batteryInfo;
try {
await Whatsapp.update({ battery, plugged }, { where: { id: 1 } });
} catch (err) {
console.log(err);
}
wbot.on("disconnected", reason => {
console.log("disconnected", reason);
wbot.destroy();
setTimeout(() =>
init()
.then(res => {
wbotMessageListener();
wbotMonitor();
})
.catch(err => console.log(err))
);
});
console.log(`Battery: ${battery}% - Charging? ${plugged}`); //todo> save batery state to db
});
// setInterval(() => {
// wbot.resetState();
// }, 20000);
wbot.on("disconnected", reason => {
console.log("disconnected", reason); //todo> save connection status to DB
setTimeout(
() =>
init()
.then(res => {
wbotMessageListener();
wbotMonitor();
})
.catch(err => console.log(err)),
2000
);
});
// setInterval(() => {
// wbot.resetState();
// }, 20000);
} catch (err) {
console.log(err);
}
};
module.exports = wbotMonitor;