add grist
This commit is contained in:
@@ -1,48 +1,48 @@
|
||||
import type { AxiosInstance } from "axios";
|
||||
|
||||
export type ServiceResult<T = unknown> = {
|
||||
result: T;
|
||||
successful: boolean;
|
||||
result: T;
|
||||
successful: boolean;
|
||||
};
|
||||
|
||||
export class API_Error {
|
||||
constructor(public error: string) {}
|
||||
constructor(public error: string) {}
|
||||
}
|
||||
|
||||
export abstract class BaseClient {
|
||||
private axiosInstance: AxiosInstance;
|
||||
private axiosInstance: AxiosInstance;
|
||||
|
||||
constructor(axiosInstance: AxiosInstance) {
|
||||
this.axiosInstance = axiosInstance;
|
||||
}
|
||||
constructor(axiosInstance: AxiosInstance) {
|
||||
this.axiosInstance = axiosInstance;
|
||||
}
|
||||
|
||||
getAxios(): AxiosInstance {
|
||||
return this.axiosInstance;
|
||||
}
|
||||
getAxios(): AxiosInstance {
|
||||
return this.axiosInstance;
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class BaseService<T> {
|
||||
private client: T;
|
||||
private client: T;
|
||||
|
||||
constructor(client: T) {
|
||||
this.client = client;
|
||||
}
|
||||
constructor(client: T) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
getClient(): T {
|
||||
return this.client;
|
||||
}
|
||||
getClient(): T {
|
||||
return this.client;
|
||||
}
|
||||
|
||||
getSuccessfulResult<R = unknown>(result: R): ServiceResult<R> {
|
||||
return {
|
||||
result,
|
||||
successful: true,
|
||||
};
|
||||
}
|
||||
getSuccessfulResult<R = unknown>(result: R): ServiceResult<R> {
|
||||
return {
|
||||
result,
|
||||
successful: true,
|
||||
};
|
||||
}
|
||||
|
||||
getErrorResult(error: string): ServiceResult<string> {
|
||||
return {
|
||||
result: error,
|
||||
successful: false,
|
||||
};
|
||||
}
|
||||
getErrorResult(error: string): ServiceResult<string> {
|
||||
return {
|
||||
result: error,
|
||||
successful: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
14
src/grist.ts
Normal file
14
src/grist.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export type Record_PersonalGoals = {
|
||||
went_outside: boolean;
|
||||
standing: boolean;
|
||||
standing_goal: number;
|
||||
steps: boolean;
|
||||
steps_goal: number;
|
||||
pushups: boolean;
|
||||
squats: boolean;
|
||||
leg_raises: boolean;
|
||||
reps_goal: number;
|
||||
stairs: boolean;
|
||||
stairs_goal: number;
|
||||
is_workday: boolean;
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
import { type API_HA_DeskPosition } from "./homeassistant";
|
||||
import { type TidalGetCurrent } from "./tidal";
|
||||
import type { API_HA_DeskPosition } from "./homeassistant";
|
||||
import type { TidalGetCurrent } from "./tidal";
|
||||
|
||||
export type FullInformation = {
|
||||
ha_desk_position: API_HA_DeskPosition | null;
|
||||
|
||||
@@ -2,6 +2,6 @@ export * from "./fastify.js";
|
||||
export * from "./homeassistant.js";
|
||||
export * from "./homepage.js";
|
||||
export * from "./logger.js";
|
||||
export * from "./ws.js";
|
||||
export * from "./tidal.js";
|
||||
export * from "./timehelper.js";
|
||||
export * from "./ws.js";
|
||||
|
||||
@@ -2,31 +2,31 @@ import axios from "axios";
|
||||
import chalk from "chalk";
|
||||
|
||||
export function logError(...args: unknown[]) {
|
||||
console.error(chalk.red("ERROR:"), ...args);
|
||||
console.error(chalk.red("ERROR:"), ...args);
|
||||
}
|
||||
|
||||
export function logWarning(...args: unknown[]) {
|
||||
console.warn(chalk.yellow("WARNING:"), ...args);
|
||||
console.warn(chalk.yellow("WARNING:"), ...args);
|
||||
}
|
||||
|
||||
export function logSuccess(...args: unknown[]) {
|
||||
console.info(chalk.green("SUCCESS:"), ...args);
|
||||
console.info(chalk.green("SUCCESS:"), ...args);
|
||||
}
|
||||
|
||||
export function logInfo(...args: unknown[]) {
|
||||
console.info(chalk.cyan("INFO:"), ...args);
|
||||
console.info(chalk.cyan("INFO:"), ...args);
|
||||
}
|
||||
|
||||
export function printNetworkError(error: unknown) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
logError("Axios error details:", {
|
||||
message: error.message,
|
||||
status: error.response?.status,
|
||||
statusText: error.response?.statusText,
|
||||
data: error.response?.data,
|
||||
url: error.config?.url,
|
||||
});
|
||||
} else {
|
||||
logError("Unexpected error:", error);
|
||||
}
|
||||
if (axios.isAxiosError(error)) {
|
||||
logError("Axios error details:", {
|
||||
message: error.message,
|
||||
status: error.response?.status,
|
||||
statusText: error.response?.statusText,
|
||||
data: error.response?.data,
|
||||
url: error.config?.url,
|
||||
});
|
||||
} else {
|
||||
logError("Unexpected error:", error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,80 +1,80 @@
|
||||
export class TimeSpan {
|
||||
private start: { hours: number; minutes: number };
|
||||
private end: { hours: number; minutes: number };
|
||||
private timeZone;
|
||||
private start: { hours: number; minutes: number };
|
||||
private end: { hours: number; minutes: number };
|
||||
private timeZone;
|
||||
|
||||
constructor(timeSpanStr: string, timeZone: string) {
|
||||
const [startStr, endStr] = timeSpanStr.split("-");
|
||||
this.start = this.parseTime(startStr);
|
||||
this.end = this.parseTime(endStr);
|
||||
this.timeZone = timeZone;
|
||||
}
|
||||
constructor(timeSpanStr: string, timeZone: string) {
|
||||
const [startStr, endStr] = timeSpanStr.split("-");
|
||||
this.start = this.parseTime(startStr);
|
||||
this.end = this.parseTime(endStr);
|
||||
this.timeZone = timeZone;
|
||||
}
|
||||
|
||||
private parseTime(timeStr: string) {
|
||||
const [hours, minutes] = timeStr.split(":").map(Number);
|
||||
return { hours, minutes };
|
||||
}
|
||||
private parseTime(timeStr: string) {
|
||||
const [hours, minutes] = timeStr.split(":").map(Number);
|
||||
return { hours, minutes };
|
||||
}
|
||||
|
||||
contains(timestamp: number = Date.now()): boolean {
|
||||
const date = new Date(timestamp);
|
||||
contains(timestamp: number = Date.now()): boolean {
|
||||
const date = new Date(timestamp);
|
||||
|
||||
const berlinTimeStr = date.toLocaleString("de-DE", {
|
||||
timeZone: this.timeZone, // "Europe/Berlin"
|
||||
hour12: false,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
const berlinTimeStr = date.toLocaleString("de-DE", {
|
||||
timeZone: this.timeZone, // "Europe/Berlin"
|
||||
hour12: false,
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
|
||||
const [hours, minutes] = berlinTimeStr.split(":").map(Number);
|
||||
const currentMinutes = hours * 60 + minutes;
|
||||
const startMinutes = this.start.hours * 60 + this.start.minutes;
|
||||
const endMinutes = this.end.hours * 60 + this.end.minutes;
|
||||
const [hours, minutes] = berlinTimeStr.split(":").map(Number);
|
||||
const currentMinutes = hours * 60 + minutes;
|
||||
const startMinutes = this.start.hours * 60 + this.start.minutes;
|
||||
const endMinutes = this.end.hours * 60 + this.end.minutes;
|
||||
|
||||
if (startMinutes > endMinutes) {
|
||||
return currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
||||
} else {
|
||||
return currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
||||
}
|
||||
}
|
||||
if (startMinutes > endMinutes) {
|
||||
return currentMinutes >= startMinutes || currentMinutes < endMinutes;
|
||||
} else {
|
||||
return currentMinutes >= startMinutes && currentMinutes < endMinutes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface TimeBetween {
|
||||
seconds: number;
|
||||
toReadable: (roundToMinutes?: boolean) => string;
|
||||
seconds: number;
|
||||
toReadable: (roundToMinutes?: boolean) => string;
|
||||
}
|
||||
|
||||
export function calculateSecondsBetween(
|
||||
start: number,
|
||||
end: number,
|
||||
start: number,
|
||||
end: number,
|
||||
): TimeBetween {
|
||||
const seconds = Math.max(60, (end - start) / 1000);
|
||||
return {
|
||||
seconds,
|
||||
toReadable: (roundToMinutes?: boolean) =>
|
||||
secondsToReadable(seconds, roundToMinutes),
|
||||
};
|
||||
const seconds = Math.max(60, (end - start) / 1000);
|
||||
return {
|
||||
seconds,
|
||||
toReadable: (roundToMinutes?: boolean) =>
|
||||
secondsToReadable(seconds, roundToMinutes),
|
||||
};
|
||||
}
|
||||
|
||||
export function secondsToReadable(
|
||||
secs: number,
|
||||
roundToMinutes: boolean = false,
|
||||
secs: number,
|
||||
roundToMinutes: boolean = false,
|
||||
): string {
|
||||
const totalSeconds = roundToMinutes ? Math.round(secs / 60) * 60 : secs;
|
||||
const totalSeconds = roundToMinutes ? Math.round(secs / 60) * 60 : secs;
|
||||
|
||||
var days = Math.floor(totalSeconds / (3600 * 24));
|
||||
var hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
|
||||
var minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
var seconds = Math.floor(totalSeconds % 60);
|
||||
var days = Math.floor(totalSeconds / (3600 * 24));
|
||||
var hours = Math.floor((totalSeconds % (3600 * 24)) / 3600);
|
||||
var minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
var seconds = Math.floor(totalSeconds % 60);
|
||||
|
||||
var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
|
||||
var hourDisplay =
|
||||
hours > 0 ? hours + (hours === 1 ? " hour, " : " hours, ") : "";
|
||||
var minuteDisplay =
|
||||
minutes > 0 ? minutes + (minutes === 1 ? " minute, " : " minutes, ") : "";
|
||||
var secondDisplay =
|
||||
seconds > 0 ? seconds + (seconds === 1 ? " second" : " seconds") : "";
|
||||
return (dayDisplay + hourDisplay + minuteDisplay + secondDisplay).replace(
|
||||
/,\s*$/,
|
||||
"",
|
||||
);
|
||||
var dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
|
||||
var hourDisplay =
|
||||
hours > 0 ? hours + (hours === 1 ? " hour, " : " hours, ") : "";
|
||||
var minuteDisplay =
|
||||
minutes > 0 ? minutes + (minutes === 1 ? " minute, " : " minutes, ") : "";
|
||||
var secondDisplay =
|
||||
seconds > 0 ? seconds + (seconds === 1 ? " second" : " seconds") : "";
|
||||
return (dayDisplay + hourDisplay + minuteDisplay + secondDisplay).replace(
|
||||
/,\s*$/,
|
||||
"",
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user