add desk text sensor and change detection
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import type { ChatMessage } from "@twurple/chat";
|
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 { logSuccess } from "../../util/logger.ts";
|
||||||
import { BaseCommand } from "../base-command.ts";
|
import { BaseCommand } from "../base-command.ts";
|
||||||
import type { ICommandRequirements } from "../interface.ts";
|
import type { ICommandRequirements } from "../interface.ts";
|
||||||
@@ -21,28 +22,23 @@ export class PositionCommand extends BaseCommand {
|
|||||||
msg: ChatMessage,
|
msg: ChatMessage,
|
||||||
) => {
|
) => {
|
||||||
logSuccess(`${channel} ${user} position command triggered`);
|
logSuccess(`${channel} ${user} position command triggered`);
|
||||||
const position = await getPosition();
|
const position = await getDeskPositionText();
|
||||||
this.chatClient.say(channel, `darius is ${position} right now`, {
|
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,
|
replyTo: msg,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPosition(): Promise<string> {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ export const Config = {
|
|||||||
api_token: process.env.HA_API_TOKEN || "",
|
api_token: process.env.HA_API_TOKEN || "",
|
||||||
|
|
||||||
id_desk_sensor: process.env.HA_DESK_SENSOR_ID || "",
|
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(",") || [],
|
id_room_sensors: process.env.HA_ROOMTEMP_SENSOR_IDS?.split(",") || [],
|
||||||
},
|
},
|
||||||
} as const;
|
} as const;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { HellFreezesOverError, type UserIdResolvable } from "@twurple/api";
|
import type { UserIdResolvable } from "@twurple/api";
|
||||||
import { Config } from "../config/config.ts";
|
import { Config } from "../config/config.ts";
|
||||||
import { apiClient } from "../core/client.ts";
|
import { apiClient } from "../core/client.ts";
|
||||||
import { getUserId } from "./general.ts";
|
import { getUserId } from "./general.ts";
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { Config } from "../config/config.ts";
|
import { Config } from "../config/config.ts";
|
||||||
import { logWarning } from "./logger.ts";
|
import { logInfo, logWarning } from "./logger.ts";
|
||||||
|
|
||||||
type HomeAssistantEntity = {
|
type HomeAssistantEntity = {
|
||||||
entity_id: string;
|
entity_id: string;
|
||||||
@@ -22,6 +22,18 @@ type HomeAssistantEntity = {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export async function getDeskPositionText(): Promise<HomeAssistantEntity | null> {
|
||||||
|
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<HomeAssistantEntity | null> {
|
export async function getDeskHeight(): Promise<HomeAssistantEntity | null> {
|
||||||
try {
|
try {
|
||||||
return await sendRequestToHomeAssistant(
|
return await sendRequestToHomeAssistant(
|
||||||
|
|||||||
@@ -30,19 +30,25 @@ export async function promptForInput(prompt: string): Promise<string> {
|
|||||||
export function calculateSecondsBetween(
|
export function calculateSecondsBetween(
|
||||||
start: number,
|
start: number,
|
||||||
end: number,
|
end: number,
|
||||||
): { seconds: number; toReadable: () => string } {
|
): { seconds: number; toReadable: (roundToMinutes?: boolean) => string } {
|
||||||
const seconds = (end - start) / 1000;
|
const seconds = (end - start) / 1000;
|
||||||
return {
|
return {
|
||||||
seconds,
|
seconds,
|
||||||
toReadable: () => secondsToReadable(seconds),
|
toReadable: (roundToMinutes?: boolean) =>
|
||||||
|
secondsToReadable(seconds, roundToMinutes),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function secondsToReadable(secs: number): string {
|
export function secondsToReadable(
|
||||||
var days = Math.floor(secs / (3600 * 24));
|
secs: number,
|
||||||
var hours = Math.floor((secs % (3600 * 24)) / 3600);
|
roundToMinutes: boolean = false,
|
||||||
var minutes = Math.floor((secs % 3600) / 60);
|
): string {
|
||||||
var seconds = Math.floor(secs % 60);
|
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 dayDisplay = days > 0 ? days + (days === 1 ? " day, " : " days, ") : "";
|
||||||
var hourDisplay =
|
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;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user