node-raspberry-server/index.js

150 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-01-01 22:59:16 +01:00
const express = require("express");
const cors = require("cors");
2019-01-12 10:32:24 +01:00
const mongoose = require("mongoose");
const mongoDB = "mongodb://127.0.0.1/chillroom-database";
2019-01-12 15:03:08 +01:00
const deviceSchema = require("./deviceSchema");
const bodyParser = require("body-parser");
const statusSchema = require("./statusSchema");
2019-01-12 10:32:24 +01:00
mongoose.connect(
mongoDB,
{ useNewUrlParser: true }
);
2019-01-01 22:59:16 +01:00
const app = express();
app.use(cors());
app.options("*", cors());
2019-01-12 15:03:08 +01:00
app.use(bodyParser.json());
2019-01-01 22:59:16 +01:00
const port = 3000;
2019-01-12 15:03:08 +01:00
const save_data = async time => {
2019-01-12 11:44:49 +01:00
try {
2019-01-19 13:26:09 +01:00
const devices = await deviceSchema.find({});
//console.log("DEVICES => " + devices);
for (const { ip, _id, lastStatus } of devices) {
2019-01-12 15:03:08 +01:00
const value_data = await getScript(ip);
const data = JSON.parse(value_data);
2019-01-19 13:26:09 +01:00
//console.log(lastStatus, data.Sensors[0].Switch);
if (lastStatus != data.Sensors[0].Switch) {
await statusSchema.findOneAndUpdate(
{ device: _id },
{
$push: { status: { value: data.Sensors[0].Switch, time } }
}
);
await deviceSchema.findByIdAndUpdate(_id, {
lastStatus: data.Sensors[0].Switch
});
}
2019-01-12 15:03:08 +01:00
}
2019-01-12 11:44:49 +01:00
} catch (error) {
2019-01-19 13:26:09 +01:00
//console.log("ERROR z CATCHA => " + error);
2019-01-12 11:44:49 +01:00
}
2019-01-12 11:36:07 +01:00
};
2019-01-12 10:32:24 +01:00
let interval = setInterval(() => {
2019-01-12 15:03:08 +01:00
save_data(Date.now());
2019-01-19 17:47:37 +01:00
}, 1000);
2019-01-12 10:32:24 +01:00
2019-01-01 22:59:16 +01:00
const getScript = url => {
return new Promise((resolve, reject) => {
const http = require("http");
let client = http;
2019-01-12 10:32:24 +01:00
url = "http://" + url + "/json";
2019-01-01 22:59:16 +01:00
2019-01-19 13:26:09 +01:00
//console.log("DATA URL => " + url);
2019-01-12 11:36:07 +01:00
2019-01-01 22:59:16 +01:00
client
.get(url, resp => {
let data = "";
2019-01-12 10:32:24 +01:00
// Dane zostały pobrane
2019-01-01 22:59:16 +01:00
resp.on("data", chunk => {
data += chunk;
});
2019-01-12 10:32:24 +01:00
// Cały response pobrany i print
2019-01-01 22:59:16 +01:00
resp.on("end", () => {
resolve(data);
2019-01-19 13:26:09 +01:00
//console.log(data);
2019-01-01 22:59:16 +01:00
});
})
.on("error", err => {
2019-01-13 18:31:17 +01:00
resolve('{"Sensors": [{ "Switch": 2 }]}');
2019-01-01 22:59:16 +01:00
});
});
};
app.get("/chillroom-server", (req, res) => {
(async url => {
res.json(await getScript(url));
})(req.query.ip);
});
2019-01-12 10:32:24 +01:00
app.get("/stop", (req, res) => {
clearInterval(interval);
interval = null;
res.json({ message: "Przestalem dzialac :<" });
});
2019-01-12 21:07:32 +01:00
app.get("/status/:id", async (req, res) => {
const data = await statusSchema
.findOne({
device: mongoose.Types.ObjectId(req.params.id)
})
.select("status");
const status = data.status.sort((a, b) => {
return b.time - a.time;
});
res.json(status);
});
2019-01-12 10:32:24 +01:00
app.get("/start", (req, res) => {
if (interval === null) {
interval = setInterval(() => {
2019-01-12 15:03:08 +01:00
save_data(Date.now());
2019-01-19 00:44:21 +01:00
}, 15000);
2019-01-12 10:32:24 +01:00
res.json({ message: "Zaczalem dzialac :>" });
} else {
res.json({ message: "Dzialam i nie mozna mnie rozpoczac raz jeszcze :p" });
}
});
app.get("/all", async (req, res) => {
2019-01-12 15:03:08 +01:00
const items = await deviceSchema.find({});
2019-01-12 10:32:24 +01:00
res.json(items);
});
2019-01-12 15:03:08 +01:00
app.post("/device", async (req, res) => {
2019-01-19 13:26:09 +01:00
//console.log(req.body);
2019-01-12 15:03:08 +01:00
const newDevice = await deviceSchema.create({
name: req.body.name,
ip: req.body.ip
});
await statusSchema.create({ device: newDevice });
res.json({ Sukces: "Dodaje OK" });
});
2019-01-13 18:31:17 +01:00
app.delete("/device/:ip", async (req, res) => {
2019-01-19 13:26:09 +01:00
//console.log(req.body);
2019-01-13 18:31:17 +01:00
const newDevice = await deviceSchema.findOneAndRemove({ ip: req.params.ip });
await statusSchema.findOneAndRemove({ device: newDevice });
2019-01-12 15:03:08 +01:00
res.json({ Sukces: "Usuwa OK" });
});
2019-01-19 00:44:21 +01:00
app.put("/device/:id", async (req, res) => {
const newDevice = await deviceSchema.findByIdAndUpdate(
req.params.id,
req.body
);
res.json(newDevice);
});
2019-01-01 22:59:16 +01:00
app.listen(port, () =>
console.log(`Chillroom server nasluchuje na porcie ${port}!`)
);