endpoints post, delete and more

This commit is contained in:
Dawid Kubicki 2019-01-12 15:03:08 +01:00
parent 10c956eaeb
commit 37fcbd537e
4 changed files with 54 additions and 15 deletions

View File

@ -3,7 +3,7 @@ const deviceSchema = new mongoose.Schema(
{
ip: String,
name: String,
updateStatuses: [String]
lastStatus: String
},
{ timestamps: true }
);

View File

@ -2,7 +2,10 @@ const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const mongoDB = "mongodb://127.0.0.1/chillroom-database";
const dbSchema = require("./statusSchema");
const deviceSchema = require("./deviceSchema");
const bodyParser = require("body-parser");
const statusSchema = require("./statusSchema");
mongoose.connect(
mongoDB,
{ useNewUrlParser: true }
@ -10,18 +13,27 @@ mongoose.connect(
const app = express();
app.use(cors());
app.options("*", cors());
app.use(bodyParser.json());
const port = 3000;
const save_data = async ip => {
const save_data = async time => {
try {
const value_data = await getScript(ip);
const data = JSON.parse(value_data);
const item = new dbSchema({
ip,
name: data.Sensors[0].TaskName,
status: data.Sensors[0].Switch
});
await item.save();
const devices = await deviceSchema.find({}).select("name ip");
console.log("DEVICES => " + devices);
for (const { ip, _id } of devices) {
const value_data = await getScript(ip);
const data = JSON.parse(value_data);
await statusSchema.findOneAndUpdate(
{ device: _id },
{
$push: { status: { value: data.Sensors[0].Switch, time } }
}
);
await deviceSchema.findByIdAndUpdate(_id, {
lastStatus: data.Sensors[0].Switch
});
}
} catch (error) {
console.log("ERROR z CATCHA => " + error);
const item = new dbSchema({
@ -36,7 +48,7 @@ const save_data = async ip => {
};
let interval = setInterval(() => {
save_data("192.168.8.107");
save_data(Date.now());
}, 5000);
const getScript = url => {
@ -85,7 +97,7 @@ app.get("/stop", (req, res) => {
app.get("/start", (req, res) => {
if (interval === null) {
interval = setInterval(() => {
save_data("192.168.8.107");
save_data(Date.now());
}, 5000);
res.json({ message: "Zaczalem dzialac :>" });
@ -95,10 +107,27 @@ app.get("/start", (req, res) => {
});
app.get("/all", async (req, res) => {
const items = await dbSchema.find({});
const items = await deviceSchema.find({});
res.json(items);
});
app.post("/device", async (req, res) => {
console.log(req.body);
const newDevice = await deviceSchema.create({
name: req.body.name,
ip: req.body.ip
});
await statusSchema.create({ device: newDevice });
res.json({ Sukces: "Dodaje OK" });
});
app.delete("/device/:id", async (req, res) => {
console.log(req.body);
const newDevice = await deviceSchema.findByIdAndRemove(req.params.id);
await statusSchema.findOneAndRemove({ device: req.params.id });
res.json({ Sukces: "Usuwa OK" });
});
app.listen(port, () =>
console.log(`Chillroom server nasluchuje na porcie ${port}!`)
);

View File

@ -9,6 +9,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.3",
"cors": "^2.8.5",
"express": "^4.16.4",
"mongoose": "^5.4.1"

View File

@ -1,7 +1,16 @@
const mongoose = require("mongoose");
const statusSchema = new mongoose.Schema(
{
status: [String]
status: [
{
value: String,
time: Date
}
],
device: {
type: mongoose.Schema.Types.ObjectId,
ref: "deviceSchema"
}
},
{ timestamps: true }
);