| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import { Database } from "bun:sqlite";
- import { nanoid } from 'nanoid'
- import generateSecureRandomString from "./generateSecureRandomString";
- const meteostaniceDB = new Database("./data/meteostanice.sqlite");
- meteostaniceDB.run(`create table if not exists list (
- id text not null primary key,
- owner text not null,
- name text not null,
- description text,
- websocketKey text not null,
- timestamp datetime default current_timestamp
- );`)
- meteostaniceDB.run(`create table if not exists data (
- id text not null primary key,
- meteostanica text not null,
- timestamp datetime default current_timestamp,
- indoorTemp text not null,
- indoorPressure text not null,
- indoorHumidity text not null,
- indoorAltitude text not null,
- outdoorConnected integer not null,
- outdoorTemp text not null,
- outdoorPressure text not null,
- outdoorHumidity text not null,
- outdoorAltitude text not null
- );`)
- meteostaniceDB.run(`create table if not exists public (
- id text not null primary key,
- name text not null,
- description text,
- timestamp datetime default current_timestamp,
- showOwner integer default 1,
-
- showIndoorTemp integer default 1,
- showIndoorPressure integer default 1,
- showIndoorHumidity integer default 1,
- showIndoorAltitude integer default 1,
- showOutdoorConnected integer default 1,
- showOutdoorTemp integer default 1,
- showOutdoorPressure integer default 1,
- showOutdoorHumidity integer default 1,
- showOutdoorAltitude integer default 1
- );`)
- export default class Meteostanice {
- static add(owner, name, description) {
- const id = nanoid()
- const websocketKey = generateSecureRandomString()
- meteostaniceDB.prepare(`
- INSERT INTO list (id, owner, name, description, websocketKey)
- VALUES (?, ?, ?, ?, ?);
- `).run(id, owner, name, description, websocketKey)
- }
- static get(owner, id) {
- const statement = meteostaniceDB.prepare(`
- SELECT *
- FROM list
- WHERE owner = $owner AND id = $id;
- `)
- const result = statement.get({
- $owner: owner,
- $id: id
- });
- return result
- }
- static getWebsocket(key) {
- const statement = meteostaniceDB.prepare(`
- SELECT *
- FROM list
- WHERE websocketKey = $key;
- `)
- const result = statement.get({
- $key: key
- });
- return result
- }
- static getOwned(owner) {
- const statement = meteostaniceDB.prepare(`
- SELECT *
- FROM list
- WHERE owner = $owner
- ORDER BY timestamp DESC;
- `)
- const result = statement.all({
- $owner: owner
- });
- return result
- }
- static edit(id, newName, newDescription, newOwner) {
- meteostaniceDB.prepare(`
- update list
- set name = ?,
- description = ?,
- owner = ?
- where id = ?;
- `).run(newName, newDescription, newOwner, id)
- }
- static editOwnerOnOwned(owner, newOwner) {
- meteostaniceDB.prepare(`
- update list
- set owner = ?
- where owner = ?;
- `).run(newOwner, owner)
- }
- static delete(id) {
- meteostaniceDB.prepare(`
- DELETE
- FROM data
- WHERE meteostanica = $id;
- `).run({
- $id: id
- });
- meteostaniceDB.prepare(`
- DELETE
- FROM public
- WHERE id = $id;
- `).run({
- $id: id
- });
- meteostaniceDB.prepare(`
- DELETE
- FROM list
- WHERE id = $id;
- `).run({
- $id: id
- });
- }
- static deleteOwned(owner) {
- const meteostanice = this.getOwned(owner)
- for (const meteostanica of meteostanice) {
- this.delete(meteostanica.owner, meteostanica.id)
- }
- }
- static postData(meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude) {
- const id = nanoid()
- meteostaniceDB.prepare(`
- INSERT INTO data (id, meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude)
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
- `).run(id, meteostanica, indoorTemp, indoorPressure, indoorHumidity, indoorAltitude, outdoorConnected, outdoorTemp, outdoorPressure, outdoorHumidity, outdoorAltitude)
- }
- static getData(meteostanica) {
- const statement = meteostaniceDB.prepare(`
- SELECT *
- FROM data
- WHERE meteostanica = $meteostanica
- ORDER BY timestamp DESC;
- `)
- const result = statement.all({
- $meteostanica: meteostanica
- });
- return result
- }
- static getDataProperty(meteostanica, property) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT ${property}
- FROM data
- WHERE meteostanica = $meteostanica
- ORDER BY timestamp DESC
- LIMIT 10;
- `)
- const result = statement.all({
- $meteostanica: meteostanica
- });
- return result
- }
- static getDataPropertyDaily(meteostanica, property, date) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT strftime('%Y-%m-%d %H:00:00', timestamp) AS timeMark,
- AVG(${property}) AS value
- FROM data
- WHERE meteostanica = ? AND date(timestamp) = ? -- Pass 'YYYY-MM-DD' here
- GROUP BY timeMark
- ORDER BY timeMark;
- `)
-
- const result = statement.all(meteostanica, date);
- return result
- }
- static getDataPropertyMonthly(meteostanica, property, yearMonth) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT date(timestamp) AS timeMark,
- AVG(${property}) AS value
- FROM data
- WHERE meteostanica = ? AND strftime('%Y-%m', timestamp) = ? -- Pass 'YYYY-MM' here
- GROUP BY timeMark
- ORDER BY timeMark;
- `)
-
- const result = statement.all(meteostanica, yearMonth);
- return result
- }
- static getDataPropertyYearly(meteostanica, property, year) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT strftime('%Y-%m', timestamp) AS timeMark,
- AVG(${property}) AS value
- FROM data
- WHERE meteostanica = ? AND strftime('%Y', timestamp) = ? -- Pass 'YYYY' here
- GROUP BY timeMark
- ORDER BY timeMark;
- `)
-
- const result = statement.all(meteostanica, year);
- return result
- }
- static getDataPropertyAllTime(meteostanica, property) {
- const tableNames = meteostanicaDB.prepare(`PRAGMA table_info('data');`).all()
- if (!tableNames.find(i => i.name === property)) return null
- const statement = meteostanicaDB.prepare(`
- SELECT strftime('%Y', timestamp) AS timeMark,
- AVG(${property}) AS value
- FROM data
- WHERE meteostanica = ?
- GROUP BY timeMark
- ORDER BY timeMark;
- `)
-
- const result = statement.all(meteostanica);
- return result
- }
- static getDateMap(meteostanica) {
- const statement = meteostanicaDB.query("SELECT DISTINCT date(timestamp) as d FROM data WHERE meteostanica = ? ORDER BY d ASC");
- const rows = statement.all(meteostanica);
- return rows.reduce((acc, row) => {
- const [year, month, day] = row.d.split("-");
- if (!acc[year]) acc[year] = {};
- if (!acc[year][month]) acc[year][month] = [];
- acc[year][month].push(day);
- return acc;
- }, {});
- }
- static resetWebsocketKey(id) {
- const websocketKey = generateSecureRandomString()
- meteostaniceDB.prepare(`
- update list
- set websocketKey = ?
- where id = ?;
- `).run(websocketKey, id)
- }
- }
|