From da5ef52bcd378759fdfa943fa6021574e603b9fb Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 10:40:32 +0100 Subject: [PATCH 1/9] dbScheme added --- dbScheme.js | 9 +++++++++ index.js | 10 +++++----- 2 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 dbScheme.js diff --git a/dbScheme.js b/dbScheme.js new file mode 100644 index 0000000..15305a3 --- /dev/null +++ b/dbScheme.js @@ -0,0 +1,9 @@ +const mongoose = require("mongoose"); +const dbSchema = new mongoose.Schema( + { + name: String, + status: String + }, + { timestamps: true } +); +module.exports = mongoose.model("dbSchema", dbSchema); diff --git a/index.js b/index.js index b6a70de..aadbcff 100644 --- a/index.js +++ b/index.js @@ -16,11 +16,11 @@ let interval = setInterval(() => { (async url => { const value_data = await getScript(url); console.log(value_data); - // const item = new dbSchema({ - // name: value_data.split(",")[0], - // value: value_data.split(",")[1] - // }); - // await item.save(); + const item = new dbSchema({ + name: value_data.Sensors[0].TaskName, + status: value_data.Sensors[0].Switch + }); + await item.save(); })("http://localhost:3000/chillroom-server?ip=192.168.8.107"); }, 5000); From 3a82387578b911e2038fadd9f17920ef078c7ac3 Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 11:36:07 +0100 Subject: [PATCH 2/9] added delay push data to mongo --- index.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index aadbcff..eaaf63b 100644 --- a/index.js +++ b/index.js @@ -12,16 +12,18 @@ app.use(cors()); app.options("*", cors()); const port = 3000; +const save_data = async url => { + const value_data = await getScript(url); + const data = JSON.parse(value_data); + const item = new dbSchema({ + name: data.Sensors[0].TaskName, + status: data.Sensors[0].Switch + }); + await item.save(); +}; + let interval = setInterval(() => { - (async url => { - const value_data = await getScript(url); - console.log(value_data); - const item = new dbSchema({ - name: value_data.Sensors[0].TaskName, - status: value_data.Sensors[0].Switch - }); - await item.save(); - })("http://localhost:3000/chillroom-server?ip=192.168.8.107"); + save_data("192.168.8.107"); }, 5000); const getScript = url => { @@ -32,6 +34,8 @@ const getScript = url => { url = "http://" + url + "/json"; + console.log("DATA URL => " + url); + client .get(url, resp => { let data = ""; @@ -68,15 +72,7 @@ app.get("/stop", (req, res) => { app.get("/start", (req, res) => { if (interval === null) { interval = setInterval(() => { - (async url => { - const value_data = await getScript(url); - console.log(value); - const item = new dbSchema({ - name: value_data.split(",")[0], - value: value_data.split(",")[1] - }); - await item.save(); - })("http://localhost:3000/chillroom-server?ip=192.168.8.107"); + save_data("192.168.8.107"); }, 5000); res.json({ message: "Zaczalem dzialac :>" }); From ffb64f77e6a5857c6ab648b5fc046f6e420294ae Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 11:44:49 +0100 Subject: [PATCH 3/9] catch error added --- dbScheme.js | 1 + index.js | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/dbScheme.js b/dbScheme.js index 15305a3..dbcc024 100644 --- a/dbScheme.js +++ b/dbScheme.js @@ -1,6 +1,7 @@ const mongoose = require("mongoose"); const dbSchema = new mongoose.Schema( { + ip: String, name: String, status: String }, diff --git a/index.js b/index.js index eaaf63b..85fd920 100644 --- a/index.js +++ b/index.js @@ -12,14 +12,23 @@ app.use(cors()); app.options("*", cors()); const port = 3000; -const save_data = async url => { - const value_data = await getScript(url); - const data = JSON.parse(value_data); - const item = new dbSchema({ - name: data.Sensors[0].TaskName, - status: data.Sensors[0].Switch - }); - await item.save(); +const save_data = async ip => { + 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(); + } catch (error) { + const item = new dbSchema({ + ip, + status: "Urządzenie przestało działać - prawdopodobnie brak zasilania" + }); + await item.save(); + } }; let interval = setInterval(() => { From 4151a833a034f298287e98c3fcb285b4dbaf6cf3 Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 12:41:00 +0100 Subject: [PATCH 4/9] error handling better added --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 85fd920..5443f95 100644 --- a/index.js +++ b/index.js @@ -23,11 +23,15 @@ const save_data = async ip => { }); await item.save(); } catch (error) { + console.log("ERROR z CATCHA => " + error); const item = new dbSchema({ ip, status: "Urządzenie przestało działać - prawdopodobnie brak zasilania" }); - await item.save(); + item + .save() + .then() + .catch(); } }; From 10c956eaebef2f98ae02428631a50d3aa8d02c3b Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 13:17:52 +0100 Subject: [PATCH 5/9] db admin changes first --- dbScheme.js | 10 ---------- deviceSchema.js | 10 ++++++++++ index.js | 2 +- statusSchema.js | 8 ++++++++ 4 files changed, 19 insertions(+), 11 deletions(-) delete mode 100644 dbScheme.js create mode 100644 deviceSchema.js create mode 100644 statusSchema.js diff --git a/dbScheme.js b/dbScheme.js deleted file mode 100644 index dbcc024..0000000 --- a/dbScheme.js +++ /dev/null @@ -1,10 +0,0 @@ -const mongoose = require("mongoose"); -const dbSchema = new mongoose.Schema( - { - ip: String, - name: String, - status: String - }, - { timestamps: true } -); -module.exports = mongoose.model("dbSchema", dbSchema); diff --git a/deviceSchema.js b/deviceSchema.js new file mode 100644 index 0000000..971888f --- /dev/null +++ b/deviceSchema.js @@ -0,0 +1,10 @@ +const mongoose = require("mongoose"); +const deviceSchema = new mongoose.Schema( + { + ip: String, + name: String, + updateStatuses: [String] + }, + { timestamps: true } +); +module.exports = mongoose.model("deviceSchema", deviceSchema); diff --git a/index.js b/index.js index 5443f95..83c5e10 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,7 @@ const express = require("express"); const cors = require("cors"); const mongoose = require("mongoose"); const mongoDB = "mongodb://127.0.0.1/chillroom-database"; -const dbSchema = require("./dbScheme"); +const dbSchema = require("./statusSchema"); mongoose.connect( mongoDB, { useNewUrlParser: true } diff --git a/statusSchema.js b/statusSchema.js new file mode 100644 index 0000000..f7146d6 --- /dev/null +++ b/statusSchema.js @@ -0,0 +1,8 @@ +const mongoose = require("mongoose"); +const statusSchema = new mongoose.Schema( + { + status: [String] + }, + { timestamps: true } +); +module.exports = mongoose.model("statusSchema", statusSchema); From 37fcbd537ec1fd941e3266a8967d9290fd239485 Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 15:03:08 +0100 Subject: [PATCH 6/9] endpoints post, delete and more --- deviceSchema.js | 2 +- index.js | 55 +++++++++++++++++++++++++++++++++++++------------ package.json | 1 + statusSchema.js | 11 +++++++++- 4 files changed, 54 insertions(+), 15 deletions(-) diff --git a/deviceSchema.js b/deviceSchema.js index 971888f..267080b 100644 --- a/deviceSchema.js +++ b/deviceSchema.js @@ -3,7 +3,7 @@ const deviceSchema = new mongoose.Schema( { ip: String, name: String, - updateStatuses: [String] + lastStatus: String }, { timestamps: true } ); diff --git a/index.js b/index.js index 83c5e10..e82dafd 100644 --- a/index.js +++ b/index.js @@ -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}!`) ); diff --git a/package.json b/package.json index 8c4f41f..f830af4 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/statusSchema.js b/statusSchema.js index f7146d6..c6cc469 100644 --- a/statusSchema.js +++ b/statusSchema.js @@ -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 } ); From cb482bce13da53fb1171e7611ca435c2f728eda3 Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 18:54:06 +0100 Subject: [PATCH 7/9] mongoose downgrade --- index.js | 16 ++-- package-lock.json | 205 +++++++++++++++++++++++++++------------------- package.json | 2 +- 3 files changed, 131 insertions(+), 92 deletions(-) diff --git a/index.js b/index.js index e82dafd..decdb7d 100644 --- a/index.js +++ b/index.js @@ -36,14 +36,14 @@ const save_data = async time => { } } catch (error) { console.log("ERROR z CATCHA => " + error); - const item = new dbSchema({ - ip, - status: "Urządzenie przestało działać - prawdopodobnie brak zasilania" - }); - item - .save() - .then() - .catch(); + // const item = new dbSchema({ + // ip, + // status: "Urządzenie przestało działać - prawdopodobnie brak zasilania" + // }); + // item + // .save() + // .then() + // .catch(); } }; diff --git a/package-lock.json b/package-lock.json index 25d4f62..dc6d9be 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,17 +19,17 @@ "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, "async": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/async/-/async-2.6.1.tgz", - "integrity": "sha512-fNEiL2+AZt6AlAw/29Cr0UDe4sRAHCpEHh54WMz+Bb7QfNcFw4h3loofyJpLeQs4Yx7yuqu/2dLgM5hKOs6HlQ==", + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.0.tgz", + "integrity": "sha512-xAfGg1/NTLBBKlHFmnd7PlmUW9KhVQIUuSrYem9xzFUZy13ScvtyGGejaae9iAVRiRq9+Cx7DPFaAAhCpyxyPw==", "requires": { - "lodash": "^4.17.10" + "lodash": "^4.14.0" } }, "bluebird": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", - "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==" + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.0.tgz", + "integrity": "sha1-eRQg1/VR7qKJdFOop3ZT+WYG1nw=" }, "body-parser": { "version": "1.18.3", @@ -49,9 +49,14 @@ } }, "bson": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.0.tgz", - "integrity": "sha512-9Aeai9TacfNtWXOYarkFJRW2CWo+dRon+fuLZYJmvLV3+MiUp0bEI6IAZfXEIg7/Pl/7IWlLaDnhzTsD81etQA==" + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.0.9.tgz", + "integrity": "sha512-IQX9/h7WdMBIW/q/++tGd+emQr0XMdeZ6icnT/74Xk9fnabWn+gZgpE+9V+gujL3hhJOoNrnDVY7tWdzc7NUTg==" + }, + "buffer-shims": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/buffer-shims/-/buffer-shims-1.0.0.tgz", + "integrity": "sha1-mXjOMXOIxkmth5MCjDR37wRKi1E=" }, "bytes": { "version": "3.0.0", @@ -78,6 +83,11 @@ "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, "cors": { "version": "2.8.5", "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz", @@ -115,6 +125,11 @@ "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" }, + "es6-promise": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.2.1.tgz", + "integrity": "sha1-7FYjOGgDKQkgcXDDlEjiREndH8Q=" + }, "escape-html": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", @@ -186,6 +201,11 @@ "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" }, + "hooks-fixed": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hooks-fixed/-/hooks-fixed-2.0.2.tgz", + "integrity": "sha512-YurCM4gQSetcrhwEtpQHhQ4M7Zo7poNGqY4kQGeBS6eZtOcT3tnNs01ThFa0jYBByAiYt1MjMjP/YApG0EnAvQ==" + }, "http-errors": { "version": "1.6.3", "resolved": "http://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz", @@ -215,27 +235,31 @@ "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.8.0.tgz", "integrity": "sha1-6qM9bd16zo9/b+DJygRA5wZzix4=" }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, "kareem": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.0.tgz", - "integrity": "sha512-6hHxsp9e6zQU8nXsP+02HGWXwTkOEw6IROhF2ZA28cYbUk4eJ6QbtZvdqZOdD9YPKghG3apk5eOCvs+tLl3lRg==" + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-1.5.0.tgz", + "integrity": "sha1-4+QQHZ3P3imXadr0tNtk2JXRdEg=" }, "lodash": { "version": "4.17.11", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" }, + "lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk=" + }, "media-typer": { "version": "0.3.0", "resolved": "http://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, - "memory-pager": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", - "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", - "optional": true - }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -265,73 +289,69 @@ } }, "mongodb": { - "version": "3.1.10", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.10.tgz", - "integrity": "sha512-Uml42GeFxhTGQVml1XQ4cD0o/rp7J2ROy0fdYUcVitoE7vFqEhKH4TYVqRDpQr/bXtCJVxJdNQC1ntRxNREkPQ==", + "version": "2.2.34", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-2.2.34.tgz", + "integrity": "sha1-o09Zu+thdUrsQy3nLD/iFSakTBo=", "requires": { - "mongodb-core": "3.1.9", - "safe-buffer": "^5.1.2" + "es6-promise": "3.2.1", + "mongodb-core": "2.1.18", + "readable-stream": "2.2.7" } }, "mongodb-core": { - "version": "3.1.9", - "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.9.tgz", - "integrity": "sha512-MJpciDABXMchrZphh3vMcqu8hkNf/Mi+Gk6btOimVg1XMxLXh87j6FAvRm+KmwD1A9fpu3qRQYcbQe4egj23og==", + "version": "2.1.18", + "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-2.1.18.tgz", + "integrity": "sha1-TEYTm986HwMt7ZHbSfOO7AFlkFA=", "requires": { - "bson": "^1.1.0", - "require_optional": "^1.0.1", - "safe-buffer": "^5.1.2", - "saslprep": "^1.0.0" + "bson": "~1.0.4", + "require_optional": "~1.0.0" } }, "mongoose": { - "version": "5.4.3", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.4.3.tgz", - "integrity": "sha512-zhfypQm63CPYHi43O/3lTcEPVucY3h6CqONbzreMKZIEXtwgkB9HCm4wn441Z9/CpM94G6/NA3wcaRb0N2kPYQ==", + "version": "4.13.15", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-4.13.15.tgz", + "integrity": "sha512-rcbVI915IzeLFzayATSUQRZkcEkqUoUcvuGTMWcVgQwYibWq1rSvNBTGhCJvmAEGA8e9ygAJS3y9gZFQPjRxBA==", "requires": { - "async": "2.6.1", - "bson": "~1.1.0", - "kareem": "2.3.0", - "mongodb": "3.1.10", - "mongodb-core": "3.1.9", - "mongoose-legacy-pluralize": "1.0.2", - "mpath": "0.5.1", - "mquery": "3.2.0", + "async": "2.6.0", + "bson": "~1.0.4", + "hooks-fixed": "2.0.2", + "kareem": "1.5.0", + "lodash.get": "4.4.2", + "mongodb": "2.2.34", + "mpath": "0.3.0", + "mpromise": "0.5.5", + "mquery": "2.3.3", "ms": "2.0.0", + "muri": "1.3.0", "regexp-clone": "0.0.1", - "safe-buffer": "5.1.2", "sliced": "1.0.1" } }, - "mongoose-legacy-pluralize": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", - "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==" - }, "mpath": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.5.1.tgz", - "integrity": "sha512-H8OVQ+QEz82sch4wbODFOz+3YQ61FYz/z3eJ5pIdbMEaUzDqA268Wd+Vt4Paw9TJfvDgVKaayC0gBzMIw2jhsg==" + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.3.0.tgz", + "integrity": "sha1-elj3iem1/TyUUgY0FXlg8mvV70Q=" + }, + "mpromise": { + "version": "0.5.5", + "resolved": "https://registry.npmjs.org/mpromise/-/mpromise-0.5.5.tgz", + "integrity": "sha1-9bJCWddjrMIlewoMjG2Gb9UXMuY=" }, "mquery": { - "version": "3.2.0", - "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.0.tgz", - "integrity": "sha512-qPJcdK/yqcbQiKoemAt62Y0BAc0fTEKo1IThodBD+O5meQRJT/2HSe5QpBNwaa4CjskoGrYWsEyjkqgiE0qjhg==", + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-2.3.3.tgz", + "integrity": "sha512-NC8L14kn+qxJbbJ1gbcEMDxF0sC3sv+1cbRReXXwVvowcwY1y9KoVZFq0ebwARibsadu8lx8nWGvm3V0Pf0ZWQ==", "requires": { - "bluebird": "3.5.1", - "debug": "3.1.0", + "bluebird": "3.5.0", + "debug": "2.6.9", "regexp-clone": "0.0.1", - "safe-buffer": "5.1.2", - "sliced": "1.0.1" + "sliced": "0.0.5" }, "dependencies": { - "debug": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", - "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", - "requires": { - "ms": "2.0.0" - } + "sliced": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/sliced/-/sliced-0.0.5.tgz", + "integrity": "sha1-XtwETKTrb3gW1Qui/GPiXY/kcH8=" } } }, @@ -340,6 +360,11 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "muri": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/muri/-/muri-1.3.0.tgz", + "integrity": "sha512-FiaFwKl864onHFFUV/a2szAl7X0fxVlSKNdhTf+BM8i8goEgYut8u5P9MqQqIYwvaMxjzVESsoEm/2kfkFH1rg==" + }, "negotiator": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.1.tgz", @@ -368,6 +393,11 @@ "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, + "process-nextick-args": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz", + "integrity": "sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M=" + }, "proxy-addr": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.4.tgz", @@ -398,6 +428,20 @@ "unpipe": "1.0.0" } }, + "readable-stream": { + "version": "2.2.7", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.2.7.tgz", + "integrity": "sha1-BwV6y+JGeyIELTb5jFrVBwVOlbE=", + "requires": { + "buffer-shims": "~1.0.0", + "core-util-is": "~1.0.0", + "inherits": "~2.0.1", + "isarray": "~1.0.0", + "process-nextick-args": "~1.0.6", + "string_decoder": "~1.0.0", + "util-deprecate": "~1.0.1" + } + }, "regexp-clone": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-0.0.1.tgz", @@ -427,15 +471,6 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, - "saslprep": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.2.tgz", - "integrity": "sha512-4cDsYuAjXssUSjxHKRe4DTZC0agDwsCqcMqtJAQPzC74nJ7LfAJflAtC1Zed5hMzEQKj82d3tuzqdGNRsLJ4Gw==", - "optional": true, - "requires": { - "sparse-bitfield": "^3.0.3" - } - }, "semver": { "version": "5.6.0", "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", @@ -482,20 +517,19 @@ "resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz", "integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E=" }, - "sparse-bitfield": { - "version": "3.0.3", - "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", - "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", - "optional": true, - "requires": { - "memory-pager": "^1.0.2" - } - }, "statuses": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "requires": { + "safe-buffer": "~5.1.0" + } + }, "type-is": { "version": "1.6.16", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.16.tgz", @@ -510,6 +544,11 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", diff --git a/package.json b/package.json index f830af4..bc68d44 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,6 @@ "body-parser": "^1.18.3", "cors": "^2.8.5", "express": "^4.16.4", - "mongoose": "^5.4.1" + "mongoose": "4.13.15" } } From 554b17a5899064f5de001dba3f39785a5c503df5 Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 21:07:32 +0100 Subject: [PATCH 8/9] time calculation logic added --- index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/index.js b/index.js index decdb7d..e2537fa 100644 --- a/index.js +++ b/index.js @@ -94,6 +94,18 @@ app.get("/stop", (req, res) => { res.json({ message: "Przestalem dzialac :<" }); }); +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); +}); + app.get("/start", (req, res) => { if (interval === null) { interval = setInterval(() => { From 1c44bca071bf319a22aafa4f9154b23d7fdb06f4 Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sun, 13 Jan 2019 18:31:17 +0100 Subject: [PATCH 9/9] delete endpoint modified --- index.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index e2537fa..6cbfd41 100644 --- a/index.js +++ b/index.js @@ -36,14 +36,6 @@ const save_data = async time => { } } catch (error) { console.log("ERROR z CATCHA => " + error); - // const item = new dbSchema({ - // ip, - // status: "Urządzenie przestało działać - prawdopodobnie brak zasilania" - // }); - // item - // .save() - // .then() - // .catch(); } }; @@ -77,7 +69,7 @@ const getScript = url => { }); }) .on("error", err => { - reject(err); + resolve('{"Sensors": [{ "Switch": 2 }]}'); }); }); }; @@ -133,10 +125,10 @@ app.post("/device", async (req, res) => { res.json({ Sukces: "Dodaje OK" }); }); -app.delete("/device/:id", async (req, res) => { +app.delete("/device/:ip", async (req, res) => { console.log(req.body); - const newDevice = await deviceSchema.findByIdAndRemove(req.params.id); - await statusSchema.findOneAndRemove({ device: req.params.id }); + const newDevice = await deviceSchema.findOneAndRemove({ ip: req.params.ip }); + await statusSchema.findOneAndRemove({ device: newDevice }); res.json({ Sukces: "Usuwa OK" }); });