waiting for change
This commit is contained in:
parent
fe001c3292
commit
a2e39ae7f5
22
index.js
22
index.js
@ -18,12 +18,15 @@ const port = 3000;
|
||||
|
||||
const save_data = async time => {
|
||||
try {
|
||||
const devices = await deviceSchema.find({}).select("name ip");
|
||||
console.log("DEVICES => " + devices);
|
||||
for (const { ip, _id } of devices) {
|
||||
const devices = await deviceSchema.find({});
|
||||
//console.log("DEVICES => " + devices);
|
||||
for (const { ip, _id, lastStatus } of devices) {
|
||||
const value_data = await getScript(ip);
|
||||
const data = JSON.parse(value_data);
|
||||
|
||||
//console.log(lastStatus, data.Sensors[0].Switch);
|
||||
|
||||
if (lastStatus != data.Sensors[0].Switch) {
|
||||
await statusSchema.findOneAndUpdate(
|
||||
{ device: _id },
|
||||
{
|
||||
@ -34,14 +37,15 @@ const save_data = async time => {
|
||||
lastStatus: data.Sensors[0].Switch
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("ERROR z CATCHA => " + error);
|
||||
//console.log("ERROR z CATCHA => " + error);
|
||||
}
|
||||
};
|
||||
|
||||
let interval = setInterval(() => {
|
||||
save_data(Date.now());
|
||||
}, 15000);
|
||||
}, 100);
|
||||
|
||||
const getScript = url => {
|
||||
return new Promise((resolve, reject) => {
|
||||
@ -51,7 +55,7 @@ const getScript = url => {
|
||||
|
||||
url = "http://" + url + "/json";
|
||||
|
||||
console.log("DATA URL => " + url);
|
||||
//console.log("DATA URL => " + url);
|
||||
|
||||
client
|
||||
.get(url, resp => {
|
||||
@ -65,7 +69,7 @@ const getScript = url => {
|
||||
// Cały response pobrany i print
|
||||
resp.on("end", () => {
|
||||
resolve(data);
|
||||
console.log(data);
|
||||
//console.log(data);
|
||||
});
|
||||
})
|
||||
.on("error", err => {
|
||||
@ -116,7 +120,7 @@ app.get("/all", async (req, res) => {
|
||||
});
|
||||
|
||||
app.post("/device", async (req, res) => {
|
||||
console.log(req.body);
|
||||
//console.log(req.body);
|
||||
const newDevice = await deviceSchema.create({
|
||||
name: req.body.name,
|
||||
ip: req.body.ip
|
||||
@ -126,7 +130,7 @@ app.post("/device", async (req, res) => {
|
||||
});
|
||||
|
||||
app.delete("/device/:ip", async (req, res) => {
|
||||
console.log(req.body);
|
||||
//console.log(req.body);
|
||||
const newDevice = await deviceSchema.findOneAndRemove({ ip: req.params.ip });
|
||||
await statusSchema.findOneAndRemove({ device: newDevice });
|
||||
res.json({ Sukces: "Usuwa OK" });
|
||||
|
Loading…
Reference in New Issue
Block a user