From 0fb087fbd90dcc15ab03916ab050f737f2919e3d Mon Sep 17 00:00:00 2001 From: Darius Date: Sun, 16 Nov 2025 20:38:49 +0100 Subject: [PATCH] stand impl part 4 --- .env.example | 1 + src/commands/impl/stand.ts | 10 ++++++++-- src/config/config.ts | 1 + src/util/api-homeassistant.ts | 2 +- src/util/timespan.ts | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/util/timespan.ts diff --git a/.env.example b/.env.example index 7fa9c63..62b0063 100644 --- a/.env.example +++ b/.env.example @@ -8,6 +8,7 @@ DEVELOPERS=userIdOfDeveloper(,separated) TIDAL_HOST=http://localhost TIDAL_PORT=47836 +NIGHT_TIME=01:00-08:00 HA_API_URL=http://homeassistant.com/api/states/ HA_API_TOKEN=Nina hätte hier jetzt ihr Token ausversehen stehen hihi HA_DESK_SENSOR_ID=entityId diff --git a/src/commands/impl/stand.ts b/src/commands/impl/stand.ts index cfb6ce6..0b80fb4 100644 --- a/src/commands/impl/stand.ts +++ b/src/commands/impl/stand.ts @@ -3,6 +3,7 @@ import { getDeskHeight, startStandingAutomation } from "../../util/api-homeassis import { logInfo, logSuccess } from "../../util/logger.js"; import { BaseCommand } from "../base-command.js"; import type { ICommandRequirements } from "../interface.ts"; +import { Config } from "../../config/config.js"; export class StandCommand extends BaseCommand { name = "stand"; @@ -28,11 +29,16 @@ export class StandCommand extends BaseCommand { // } + if (Config.night_time.contains()) { + this.chatClient.say(channel, `command disabled during nighttime`, { + replyTo: msg, + }); + return + } + const response = await startStandingAutomation(); logInfo(response) - - this.chatClient.say(channel, `blabla`, { replyTo: msg, }); diff --git a/src/config/config.ts b/src/config/config.ts index 4313254..8dacb63 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -9,6 +9,7 @@ export const Config = { client_secret: process.env.CLIENT_SECRET || "", channels: process.env.CHANNELS?.split(",") || [], developers: process.env.DEVELOPERS?.split(",") || [], + night_time: new TimeSpan(process.env.NIGHT_TIME || "00:00-00:00"), tidal: { host: process.env.TIDAL_HOST || "", diff --git a/src/util/api-homeassistant.ts b/src/util/api-homeassistant.ts index 318944d..54a65c8 100644 --- a/src/util/api-homeassistant.ts +++ b/src/util/api-homeassistant.ts @@ -99,7 +99,7 @@ async function sendRequestToHomeAssistantStates( async function sendRequestToHomeAssistantWebhook( webhook_id: string, ): Promise { - const url = `${Config.homeassistant.api_url}webhook/"${webhook_id}` + const url = `${Config.homeassistant.api_url}webhook/${webhook_id}` logInfo(`sending request to ${url}`) const response = await axios.post(url); diff --git a/src/util/timespan.ts b/src/util/timespan.ts new file mode 100644 index 0000000..c81bfef --- /dev/null +++ b/src/util/timespan.ts @@ -0,0 +1,32 @@ +class TimeSpan { + private start: { hours: number; minutes: number }; + private end: { hours: number; minutes: number }; + + constructor(timeSpanStr: string) { + const [startStr, endStr] = timeSpanStr.split('-'); + this.start = this.parseTime(startStr); + this.end = this.parseTime(endStr); + } + + 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); + + const hours = date.getHours(); + const minutes = date.getMinutes(); + 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; + } + } +}