Compare commits
2 Commits
4a3a994359
...
0e7abb9113
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e7abb9113 | ||
|
|
ba74eb0deb |
@@ -9,6 +9,8 @@ export const Config = {
|
|||||||
gadgetbridge: {
|
gadgetbridge: {
|
||||||
db_path:
|
db_path:
|
||||||
process.env.GADGETBRIDGE_DB_PATH || "src/gadgetbridge/db/Gadgetbridge.db",
|
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: {
|
grist: {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { watchFile } from "node:fs";
|
||||||
import { DatabaseSync } from "node:sqlite";
|
import { DatabaseSync } from "node:sqlite";
|
||||||
|
|
||||||
export type StepRow = {
|
export type StepRow = {
|
||||||
@@ -7,21 +8,43 @@ export type StepRow = {
|
|||||||
|
|
||||||
export class GadgetbridgeClient {
|
export class GadgetbridgeClient {
|
||||||
private db: DatabaseSync;
|
private db: DatabaseSync;
|
||||||
|
private readonly dbPath: string;
|
||||||
|
private readonly oldDbPath: string;
|
||||||
|
|
||||||
constructor(dbPath: string) {
|
constructor(dbPath: string, oldDbPath: string) {
|
||||||
this.db = new DatabaseSync(dbPath, { open: true });
|
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[] {
|
getStepsPerDay(fromTimestamp: number, toTimestamp: number): StepRow[] {
|
||||||
const stmt = this.db.prepare(`
|
const stmt = this.db.prepare(`
|
||||||
SELECT
|
SELECT date, SUM(steps) AS steps
|
||||||
DATE(TIMESTAMP, 'unixepoch', 'localtime') AS date,
|
FROM (
|
||||||
SUM(STEPS) AS steps
|
SELECT
|
||||||
FROM HUAMI_EXTENDED_ACTIVITY_SAMPLE
|
DATE(TIMESTAMP, 'unixepoch', 'localtime') AS date,
|
||||||
WHERE TIMESTAMP >= ? AND TIMESTAMP < ?
|
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
|
GROUP BY date
|
||||||
ORDER BY date
|
ORDER BY date
|
||||||
`);
|
`);
|
||||||
return stmt.all(fromTimestamp, toTimestamp) as StepRow[];
|
return stmt.all(fromTimestamp, toTimestamp, fromTimestamp, toTimestamp) as StepRow[];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ await privateServer.register(fastifyAxios, getAxiosConfig());
|
|||||||
await publicServer.register(fastifyWebsocket);
|
await publicServer.register(fastifyWebsocket);
|
||||||
|
|
||||||
// Clients and Services
|
// 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 gadgetbridgeService = new GadgetbridgeService(gadgetbridgeClient);
|
||||||
|
|
||||||
const gristClient = new GristClient(privateServer.axios.grist);
|
const gristClient = new GristClient(privateServer.axios.grist);
|
||||||
|
|||||||
Reference in New Issue
Block a user