endpoints post, delete and more
This commit is contained in:
parent
10c956eaeb
commit
37fcbd537e
@ -3,7 +3,7 @@ const deviceSchema = new mongoose.Schema(
|
|||||||
{
|
{
|
||||||
ip: String,
|
ip: String,
|
||||||
name: String,
|
name: String,
|
||||||
updateStatuses: [String]
|
lastStatus: String
|
||||||
},
|
},
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
55
index.js
55
index.js
@ -2,7 +2,10 @@ const express = require("express");
|
|||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
const mongoDB = "mongodb://127.0.0.1/chillroom-database";
|
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(
|
mongoose.connect(
|
||||||
mongoDB,
|
mongoDB,
|
||||||
{ useNewUrlParser: true }
|
{ useNewUrlParser: true }
|
||||||
@ -10,18 +13,27 @@ mongoose.connect(
|
|||||||
const app = express();
|
const app = express();
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.options("*", cors());
|
app.options("*", cors());
|
||||||
|
app.use(bodyParser.json());
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
|
||||||
const save_data = async ip => {
|
const save_data = async time => {
|
||||||
try {
|
try {
|
||||||
const value_data = await getScript(ip);
|
const devices = await deviceSchema.find({}).select("name ip");
|
||||||
const data = JSON.parse(value_data);
|
console.log("DEVICES => " + devices);
|
||||||
const item = new dbSchema({
|
for (const { ip, _id } of devices) {
|
||||||
ip,
|
const value_data = await getScript(ip);
|
||||||
name: data.Sensors[0].TaskName,
|
const data = JSON.parse(value_data);
|
||||||
status: data.Sensors[0].Switch
|
|
||||||
});
|
await statusSchema.findOneAndUpdate(
|
||||||
await item.save();
|
{ device: _id },
|
||||||
|
{
|
||||||
|
$push: { status: { value: data.Sensors[0].Switch, time } }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
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);
|
||||||
const item = new dbSchema({
|
const item = new dbSchema({
|
||||||
@ -36,7 +48,7 @@ const save_data = async ip => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let interval = setInterval(() => {
|
let interval = setInterval(() => {
|
||||||
save_data("192.168.8.107");
|
save_data(Date.now());
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
|
||||||
const getScript = url => {
|
const getScript = url => {
|
||||||
@ -85,7 +97,7 @@ app.get("/stop", (req, res) => {
|
|||||||
app.get("/start", (req, res) => {
|
app.get("/start", (req, res) => {
|
||||||
if (interval === null) {
|
if (interval === null) {
|
||||||
interval = setInterval(() => {
|
interval = setInterval(() => {
|
||||||
save_data("192.168.8.107");
|
save_data(Date.now());
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
|
||||||
res.json({ message: "Zaczalem dzialac :>" });
|
res.json({ message: "Zaczalem dzialac :>" });
|
||||||
@ -95,10 +107,27 @@ app.get("/start", (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.get("/all", async (req, res) => {
|
app.get("/all", async (req, res) => {
|
||||||
const items = await dbSchema.find({});
|
const items = await deviceSchema.find({});
|
||||||
res.json(items);
|
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, () =>
|
app.listen(port, () =>
|
||||||
console.log(`Chillroom server nasluchuje na porcie ${port}!`)
|
console.log(`Chillroom server nasluchuje na porcie ${port}!`)
|
||||||
);
|
);
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.18.3",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.16.4",
|
"express": "^4.16.4",
|
||||||
"mongoose": "^5.4.1"
|
"mongoose": "^5.4.1"
|
||||||
|
@ -1,7 +1,16 @@
|
|||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
const statusSchema = new mongoose.Schema(
|
const statusSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
status: [String]
|
status: [
|
||||||
|
{
|
||||||
|
value: String,
|
||||||
|
time: Date
|
||||||
|
}
|
||||||
|
],
|
||||||
|
device: {
|
||||||
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
|
ref: "deviceSchema"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
{ timestamps: true }
|
{ timestamps: true }
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user