diff --git a/src/commands/impl/position.ts b/src/commands/impl/position.ts index 3495bd3..44ddc6c 100644 --- a/src/commands/impl/position.ts +++ b/src/commands/impl/position.ts @@ -1,5 +1,6 @@ import type { ChatMessage } from "@twurple/chat"; -import { getDeskHeight } from "../../util/api-homeassistant.ts"; +import { getDeskPositionText } from "../../util/api-homeassistant.ts"; +import { calculateSecondsBetween } from "../../util/general.ts"; import { logSuccess } from "../../util/logger.ts"; import { BaseCommand } from "../base-command.ts"; import type { ICommandRequirements } from "../interface.ts"; @@ -21,28 +22,23 @@ export class PositionCommand extends BaseCommand { msg: ChatMessage, ) => { logSuccess(`${channel} ${user} position command triggered`); - const position = await getPosition(); - this.chatClient.say(channel, `darius is ${position} right now`, { + const position = await getDeskPositionText(); + if (position) { + const time = calculateSecondsBetween( + new Date(position.last_changed).getTime(), + Date.now(), + ).toReadable(true); + this.chatClient.say( + channel, + `darius has been ${position.state} for ${time}`, + { + replyTo: msg, + }, + ); + return; + } + this.chatClient.say(channel, `darius' position is unkown right now`, { replyTo: msg, }); }; } - -async function getPosition(): Promise { - const heightFromHA = await getDeskHeight(); - if (!heightFromHA) { - return "unkown"; - } - const height = convertHeightToCentimeters(heightFromHA.state); - if (height > 95) { - return "standing"; - } else { - return "sitting"; - } -} - -function convertHeightToCentimeters(height: string): number { - const meters = parseFloat(height); - const centimeters = Math.round(meters * 100); - return centimeters; -} diff --git a/src/config/config.ts b/src/config/config.ts index 9cb62a1..2efce82 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -20,6 +20,7 @@ export const Config = { api_token: process.env.HA_API_TOKEN || "", id_desk_sensor: process.env.HA_DESK_SENSOR_ID || "", + id_desk_sensor_text: process.env.HA_DESK_SENSOR_TEXT || "", id_room_sensors: process.env.HA_ROOMTEMP_SENSOR_IDS?.split(",") || [], }, } as const; diff --git a/src/util/api-functions.ts b/src/util/api-functions.ts index 2960860..3c1d8c5 100644 --- a/src/util/api-functions.ts +++ b/src/util/api-functions.ts @@ -1,4 +1,4 @@ -import { HellFreezesOverError, type UserIdResolvable } from "@twurple/api"; +import type { UserIdResolvable } from "@twurple/api"; import { Config } from "../config/config.ts"; import { apiClient } from "../core/client.ts"; import { getUserId } from "./general.ts"; diff --git a/src/util/api-homeassistant.ts b/src/util/api-homeassistant.ts index dd493eb..fad6b70 100644 --- a/src/util/api-homeassistant.ts +++ b/src/util/api-homeassistant.ts @@ -1,6 +1,6 @@ import axios from "axios"; import { Config } from "../config/config.ts"; -import { logWarning } from "./logger.ts"; +import { logInfo, logWarning } from "./logger.ts"; type HomeAssistantEntity = { entity_id: string; @@ -22,6 +22,18 @@ type HomeAssistantEntity = { }; }; +export async function getDeskPositionText(): Promise { + try { + const position = await sendRequestToHomeAssistant( + Config.homeassistant.id_desk_sensor_text, + ); + return position; + } catch { + logWarning("error getting hoehe text from homeassistant"); + return null; + } +} + export async function getDeskHeight(): Promise { try { return await sendRequestToHomeAssistant( diff --git a/src/util/general.ts b/src/util/general.ts index 5cba28d..33b7abb 100644 --- a/src/util/general.ts +++ b/src/util/general.ts @@ -30,19 +30,25 @@ export async function promptForInput(prompt: string): Promise { export function calculateSecondsBetween( start: number, end: number, -): { seconds: number; toReadable: () => string } { +): { seconds: number; toReadable: (roundToMinutes?: boolean) => string } { const seconds = (end - start) / 1000; return { seconds, - toReadable: () => secondsToReadable(seconds), + toReadable: (roundToMinutes?: boolean) => + secondsToReadable(seconds, roundToMinutes), }; } -export function secondsToReadable(secs: number): string { - var days = Math.floor(secs / (3600 * 24)); - var hours = Math.floor((secs % (3600 * 24)) / 3600); - var minutes = Math.floor((secs % 3600) / 60); - var seconds = Math.floor(secs % 60); +export function secondsToReadable( + secs: number, + roundToMinutes: boolean = false, +): string { + 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 dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : ""; var hourDisplay = @@ -56,3 +62,9 @@ export function secondsToReadable(secs: number): string { "", ); } + +function meterToCentimeters(length: string): number { + const meters = parseFloat(length); + const centimeters = Math.round(meters * 100); + return centimeters; +}