Skip to content

Commit

Permalink
Merge pull request #14 from sacconazzo:develop
Browse files Browse the repository at this point in the history
db module
  • Loading branch information
sacconazzo authored Aug 28, 2024
2 parents 41860cc + 0b30278 commit bc146b8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/calibrate/ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const apiKey = process.env.OPENAI_API_KEY;
const openai = new OpenAIApi.OpenAI({ key: apiKey });

const aai = async () => {
const [signals] = await db.conn.raw(
const [signals] = await db.raw(
`SELECT\
ch5, ch6\
FROM\
Expand Down
4 changes: 2 additions & 2 deletions src/calibrate/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { conn, getSettingsVars } = require("../db");
const db = require("../db");
const ai = require("./ai");

const calibrate = async ({ force = true, tentative = 1, absorption } = {}) => {
Expand Down Expand Up @@ -62,7 +62,7 @@ const calibrate = async ({ force = true, tentative = 1, absorption } = {}) => {
console.log(`Current sensor recalibrate: ${JSON.stringify(data)}`);

if (data && data.OFFSET_A1 > 0.2 && data.OFFSET_A2 > 0.2) {
const vars = await getSettingsVars();
const vars = await db.getSettingsVars();

const baseA = Number(absorption) || vars["IDLE_A"];
const offsetBaseA1 = baseA / 2 / (vars["VREF"] * vars["COEFF_A1"]);
Expand Down
19 changes: 10 additions & 9 deletions src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const connectionConfig = require("../../knexfile");

const conn = knex(connectionConfig);

const getSettingsVars = async () => {
conn.getSettingsVars = async () => {
const vars = await conn("settings");

return vars.reduce((o, e) => {
Expand All @@ -12,8 +12,8 @@ const getSettingsVars = async () => {
}, {});
};

conn.realTimeQuery = () =>
conn.raw(
conn.realTime = async () => {
const [realtime] = await conn.raw(
`SELECT\
CONVERT_TZ(timestamp, 'UTC', '${process.env.DB_TIMEZONE}') as timestamp,\
round(bm_voltage, 2) AS bmV,\
Expand All @@ -33,9 +33,11 @@ conn.realTimeQuery = () =>
id ASC;`
// LIMIT 500;
);
return realtime;
};

conn.dayWeekQuery = () =>
conn.raw(
conn.dayWeek = async () => {
const [dayweek] = await conn.raw(
`SELECT\
date(CONVERT_TZ(timestamp, 'UTC', '${process.env.DB_TIMEZONE}')) AS day,\
round(avg(bm_voltage), 2) bmV,\
Expand Down Expand Up @@ -64,8 +66,7 @@ conn.dayWeekQuery = () =>
GROUP BY\
day;`
);

module.exports = {
conn,
getSettingsVars,
return dayweek;
};

module.exports = conn;
9 changes: 3 additions & 6 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ const share = async () => {
),
};

const [dayWeek] = await db.conn.dayWeekQuery();
const dayWeek = await db.dayWeek();

const [realtime] = await db.conn.realTimeQuery();
const realtime = await db.realTime();

app._data = {
system,
Expand Down Expand Up @@ -104,10 +104,7 @@ const share = async () => {
cron.schedule(shareInterval, share);
share();

app.get("/data", async (req, res) => {
const [realtime] = await db.conn.realTimeQuery();
res.json(realtime);
});
app.get("/data", async (req, res) => res.json(await db.realTime()));

// BUTTONS
if (process.env.ENABLE_BUTTONS === "true") {
Expand Down

0 comments on commit bc146b8

Please sign in to comment.