diff --git a/src/db/index.js b/src/db/index.js index 9621087..4a28152 100644 --- a/src/db/index.js +++ b/src/db/index.js @@ -12,6 +12,59 @@ const getSettingsVars = async () => { }, {}); }; +conn.realTimeQuery = () => + conn.raw( + `SELECT\ + CONVERT_TZ(timestamp, 'UTC', '${process.env.DB_TIMEZONE}') as timestamp,\ + round(bm_voltage, 2) AS bmV,\ + round(b1_voltage, 2) AS b1V,\ + round(b2_voltage, 2) AS b2V,\ + round(b1_current, 1) AS b1A,\ + round(b2_current, 1) AS b2A,\ + round(temperature, 2) AS temp\ + FROM\ + \`battery-snaps\`\ + WHERE\ + timestamp> (NOW() - INTERVAL ${process.env.REALTIME_MINUTES} MINUTE)\ + and b1_voltage > 9\ + and b2_voltage > 9\ + and bm_voltage > 9\ + ORDER BY\ + id ASC;` + // LIMIT 500; + ); + +conn.dayWeekQuery = () => + conn.raw( + `SELECT\ + date(CONVERT_TZ(timestamp, 'UTC', '${process.env.DB_TIMEZONE}')) AS day,\ + round(avg(bm_voltage), 2) bmV,\ + round(min(bm_voltage), 2) bmVmin,\ + round(max(bm_voltage), 2) bmVmax,\ + round(avg(b1_voltage), 2) b1V,\ + round(min(b1_voltage), 2) b1Vmin,\ + round(max(b1_voltage), 2) b1Vmax,\ + round(avg(b2_voltage), 2) b2V,\ + round(min(b2_voltage), 2) b2Vmin,\ + round(max(b2_voltage), 2) b2Vmax,\ + round(sum(b1_current * coeff), 1) AS b1Ah,\ + round(sum(b2_current * coeff), 1) AS b2Ah,\ + round(sum(b1_current * coeff * b1_voltage), 1) AS b1Wh,\ + round(sum(b2_current * coeff * b2_voltage), 1) AS b2Wh,\ + round(avg(temperature), 2) temp,\ + round(min(temperature), 2) tempMin,\ + round(max(temperature), 2) tempMax\ + FROM\ + \`battery-snaps\`\ + WHERE\ + date(timestamp)> (NOW() - INTERVAL 14 DAY)\ + and b1_voltage > 9\ + and b2_voltage > 9\ + and bm_voltage > 9\ + GROUP BY\ + day;` + ); + module.exports = { conn, getSettingsVars, diff --git a/src/manager.js b/src/manager.js index c012766..1731383 100644 --- a/src/manager.js +++ b/src/manager.js @@ -68,62 +68,16 @@ const share = async () => { ), }; - const [dayWeek] = await db.conn.raw( - `SELECT\ - date(CONVERT_TZ(timestamp, 'UTC', '${process.env.DB_TIMEZONE}')) AS day,\ - round(avg(bm_voltage), 2) bmV,\ - round(min(bm_voltage), 2) bmVmin,\ - round(max(bm_voltage), 2) bmVmax,\ - round(avg(b1_voltage), 2) b1V,\ - round(min(b1_voltage), 2) b1Vmin,\ - round(max(b1_voltage), 2) b1Vmax,\ - round(avg(b2_voltage), 2) b2V,\ - round(min(b2_voltage), 2) b2Vmin,\ - round(max(b2_voltage), 2) b2Vmax,\ - round(sum(b1_current * coeff), 1) AS b1Ah,\ - round(sum(b2_current * coeff), 1) AS b2Ah,\ - round(sum(b1_current * coeff * b1_voltage), 1) AS b1Wh,\ - round(sum(b2_current * coeff * b2_voltage), 1) AS b2Wh,\ - round(avg(temperature), 2) temp,\ - round(min(temperature), 2) tempMin,\ - round(max(temperature), 2) tempMax\ - FROM\ - \`battery-snaps\`\ - WHERE\ - date(timestamp)> (NOW() - INTERVAL 14 DAY)\ - and b1_voltage > 9\ - and b2_voltage > 9\ - and bm_voltage > 9\ - GROUP BY\ - day;` - ); - - const [realtime] = await db.conn.raw( - `SELECT\ - CONVERT_TZ(timestamp, 'UTC', '${process.env.DB_TIMEZONE}') as timestamp,\ - round(bm_voltage, 2) AS bmV,\ - round(b1_voltage, 2) AS b1V,\ - round(b2_voltage, 2) AS b2V,\ - round(b1_current, 1) AS b1A,\ - round(b2_current, 1) AS b2A,\ - round(temperature, 2) AS temp\ - FROM\ - \`battery-snaps\`\ - WHERE\ - timestamp> (NOW() - INTERVAL ${process.env.REALTIME_MINUTES} MINUTE)\ - and b1_voltage > 9\ - and b2_voltage > 9\ - and bm_voltage > 9\ - ORDER BY\ - id ASC;` - // LIMIT 500; - ); + const [dayWeek] = await db.conn.dayWeekQuery(); + + const [realtime] = await db.conn.realTimeQuery(); app._data = { system, dayWeek, realtime, }; + await axios.post("https://api.giona.tech/domotica/battery", app._data, { headers: { "x-giona-tech": apiToken, @@ -150,7 +104,10 @@ const share = async () => { cron.schedule(shareInterval, share); share(); -app.get("/data", (req, res) => res.json(app._data)); +app.get("/data", async (req, res) => { + const [realtime] = await db.conn.realTimeQuery(); + res.json(realtime); +}); // BUTTONS if (process.env.ENABLE_BUTTONS === "true") { diff --git a/tests/socket.js b/tests/socket.js index e14a7af..3d188fa 100644 --- a/tests/socket.js +++ b/tests/socket.js @@ -7,5 +7,5 @@ socket.on("connect", () => { }); socket.on("data", (msg) => { - console.log("Received:", msg); + console.log("Received:", msg.system.uptime); });