Compare commits

..

2 Commits

Author SHA1 Message Date
Darius
0e7abb9113 reconnect on new db 2026-03-05 01:21:33 +01:00
Darius
ba74eb0deb Add Old Database 2026-03-05 01:17:11 +01:00
3 changed files with 34 additions and 9 deletions

View File

@@ -9,6 +9,8 @@ export const Config = {
gadgetbridge: {
db_path:
process.env.GADGETBRIDGE_DB_PATH || "src/gadgetbridge/db/Gadgetbridge.db",
old_db_path:
process.env.GADGETBRIDGE_OLD_DB_PATH || "src/gadgetbridge/db/OldSteps.db",
},
grist: {

View File

@@ -1,3 +1,4 @@
import { watchFile } from "node:fs";
import { DatabaseSync } from "node:sqlite";
export type StepRow = {
@@ -7,21 +8,43 @@ export type StepRow = {
export class GadgetbridgeClient {
private db: DatabaseSync;
private readonly dbPath: string;
private readonly oldDbPath: string;
constructor(dbPath: string) {
this.db = new DatabaseSync(dbPath, { open: true });
constructor(dbPath: string, oldDbPath: string) {
this.dbPath = dbPath;
this.oldDbPath = oldDbPath;
this.db = this.openDb();
watchFile(dbPath, { interval: 20 * 60 * 1000 }, () => {
try { this.db.close(); } catch {}
this.db = this.openDb();
});
}
private openDb(): DatabaseSync {
const db = new DatabaseSync(this.dbPath, { open: true });
db.exec(`ATTACH DATABASE '${this.oldDbPath}' AS old`);
return db;
}
getStepsPerDay(fromTimestamp: number, toTimestamp: number): StepRow[] {
const stmt = this.db.prepare(`
SELECT date, SUM(steps) AS steps
FROM (
SELECT
DATE(TIMESTAMP, 'unixepoch', 'localtime') AS date,
SUM(STEPS) AS steps
STEPS AS steps
FROM HUAMI_EXTENDED_ACTIVITY_SAMPLE
WHERE TIMESTAMP >= ? AND TIMESTAMP < ?
UNION ALL
SELECT date, steps
FROM old.steps
WHERE date >= DATE(?, 'unixepoch', 'localtime')
AND date < DATE(?, 'unixepoch', 'localtime')
)
GROUP BY date
ORDER BY date
`);
return stmt.all(fromTimestamp, toTimestamp) as StepRow[];
return stmt.all(fromTimestamp, toTimestamp, fromTimestamp, toTimestamp) as StepRow[];
}
}

View File

@@ -140,7 +140,7 @@ await privateServer.register(fastifyAxios, getAxiosConfig());
await publicServer.register(fastifyWebsocket);
// Clients and Services
const gadgetbridgeClient = new GadgetbridgeClient(Config.gadgetbridge.db_path);
const gadgetbridgeClient = new GadgetbridgeClient(Config.gadgetbridge.db_path, Config.gadgetbridge.old_db_path);
const gadgetbridgeService = new GadgetbridgeService(gadgetbridgeClient);
const gristClient = new GristClient(privateServer.axios.grist);