Skip to content

Commit

Permalink
real realtime /data
Browse files Browse the repository at this point in the history
  • Loading branch information
Giona Righini committed Aug 27, 2024
1 parent 1bdb014 commit 11ee871
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
53 changes: 53 additions & 0 deletions src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
59 changes: 8 additions & 51 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion tests/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ socket.on("connect", () => {
});

socket.on("data", (msg) => {
console.log("Received:", msg);
console.log("Received:", msg.system.uptime);
});

0 comments on commit 11ee871

Please sign in to comment.