Merge branch 'develop' of s412146/node-raspberry-server into master
This commit is contained in:
commit
d6d259aab2
50
index.js
50
index.js
@ -18,30 +18,34 @@ const port = 3000;
|
|||||||
|
|
||||||
const save_data = async time => {
|
const save_data = async time => {
|
||||||
try {
|
try {
|
||||||
const devices = await deviceSchema.find({}).select("name ip");
|
const devices = await deviceSchema.find({});
|
||||||
console.log("DEVICES => " + devices);
|
//console.log("DEVICES => " + devices);
|
||||||
for (const { ip, _id } of devices) {
|
for (const { ip, _id, lastStatus } of devices) {
|
||||||
const value_data = await getScript(ip);
|
const value_data = await getScript(ip);
|
||||||
const data = JSON.parse(value_data);
|
const data = JSON.parse(value_data);
|
||||||
|
|
||||||
await statusSchema.findOneAndUpdate(
|
//console.log(lastStatus, data.Sensors[0].Switch);
|
||||||
{ device: _id },
|
|
||||||
{
|
if (lastStatus != data.Sensors[0].Switch) {
|
||||||
$push: { status: { value: data.Sensors[0].Switch, time } }
|
await statusSchema.findOneAndUpdate(
|
||||||
}
|
{ device: _id },
|
||||||
);
|
{
|
||||||
await deviceSchema.findByIdAndUpdate(_id, {
|
$push: { status: { value: data.Sensors[0].Switch, time } }
|
||||||
lastStatus: data.Sensors[0].Switch
|
}
|
||||||
});
|
);
|
||||||
|
await deviceSchema.findByIdAndUpdate(_id, {
|
||||||
|
lastStatus: data.Sensors[0].Switch
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("ERROR z CATCHA => " + error);
|
//console.log("ERROR z CATCHA => " + error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let interval = setInterval(() => {
|
let interval = setInterval(() => {
|
||||||
save_data(Date.now());
|
save_data(Date.now());
|
||||||
}, 5000);
|
}, 1000);
|
||||||
|
|
||||||
const getScript = url => {
|
const getScript = url => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -51,7 +55,7 @@ const getScript = url => {
|
|||||||
|
|
||||||
url = "http://" + url + "/json";
|
url = "http://" + url + "/json";
|
||||||
|
|
||||||
console.log("DATA URL => " + url);
|
//console.log("DATA URL => " + url);
|
||||||
|
|
||||||
client
|
client
|
||||||
.get(url, resp => {
|
.get(url, resp => {
|
||||||
@ -65,7 +69,7 @@ const getScript = url => {
|
|||||||
// Cały response pobrany i print
|
// Cały response pobrany i print
|
||||||
resp.on("end", () => {
|
resp.on("end", () => {
|
||||||
resolve(data);
|
resolve(data);
|
||||||
console.log(data);
|
//console.log(data);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.on("error", err => {
|
.on("error", err => {
|
||||||
@ -102,7 +106,7 @@ app.get("/start", (req, res) => {
|
|||||||
if (interval === null) {
|
if (interval === null) {
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
save_data(Date.now());
|
save_data(Date.now());
|
||||||
}, 5000);
|
}, 15000);
|
||||||
|
|
||||||
res.json({ message: "Zaczalem dzialac :>" });
|
res.json({ message: "Zaczalem dzialac :>" });
|
||||||
} else {
|
} else {
|
||||||
@ -116,7 +120,7 @@ app.get("/all", async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.post("/device", async (req, res) => {
|
app.post("/device", async (req, res) => {
|
||||||
console.log(req.body);
|
//console.log(req.body);
|
||||||
const newDevice = await deviceSchema.create({
|
const newDevice = await deviceSchema.create({
|
||||||
name: req.body.name,
|
name: req.body.name,
|
||||||
ip: req.body.ip
|
ip: req.body.ip
|
||||||
@ -126,12 +130,20 @@ app.post("/device", async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.delete("/device/:ip", 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 });
|
const newDevice = await deviceSchema.findOneAndRemove({ ip: req.params.ip });
|
||||||
await statusSchema.findOneAndRemove({ device: newDevice });
|
await statusSchema.findOneAndRemove({ device: newDevice });
|
||||||
res.json({ Sukces: "Usuwa OK" });
|
res.json({ Sukces: "Usuwa OK" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.put("/device/:id", async (req, res) => {
|
||||||
|
const newDevice = await deviceSchema.findByIdAndUpdate(
|
||||||
|
req.params.id,
|
||||||
|
req.body
|
||||||
|
);
|
||||||
|
res.json(newDevice);
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(port, () =>
|
app.listen(port, () =>
|
||||||
console.log(`Chillroom server nasluchuje na porcie ${port}!`)
|
console.log(`Chillroom server nasluchuje na porcie ${port}!`)
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user