From 3a82387578b911e2038fadd9f17920ef078c7ac3 Mon Sep 17 00:00:00 2001 From: Dawid Kubicki Date: Sat, 12 Jan 2019 11:36:07 +0100 Subject: [PATCH] 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 :>" });