diff --git a/src/api/MysaApiClient.ts b/src/api/MysaApiClient.ts index a6bede8..cd11631 100644 --- a/src/api/MysaApiClient.ts +++ b/src/api/MysaApiClient.ts @@ -6,7 +6,7 @@ import { ChangeDeviceState } from '@/types/mqtt/in/ChangeDeviceState'; import { InMessageType } from '@/types/mqtt/in/InMessageType'; import { StartPublishingDeviceStatus } from '@/types/mqtt/in/StartPublishingDeviceStatus'; import { OutMessageType } from '@/types/mqtt/out/OutMessageType'; -import { Devices, Firmwares } from '@/types/rest'; +import { Devices, DeviceStates, Firmwares } from '@/types/rest'; import { fromCognitoIdentityPool } from '@aws-sdk/credential-providers'; import { AuthenticationDetails, @@ -213,6 +213,24 @@ export class MysaApiClient { return response.json(); } + async getDeviceStates(): Promise { + this._logger.debug(`Fetching device states...`); + + const session = await this.getFreshSession(); + + const response = await this._fetcher(`${MysaApiBaseUrl}/devices/state`, { + headers: { + Authorization: `${session.getIdToken().getJwtToken()}` + } + }); + + if (!response.ok) { + throw new MysaApiError(response); + } + + return response.json(); + } + async setDeviceState(deviceId: string, setPoint?: number, mode?: MysaDeviceMode) { this._logger.debug(`Setting device state for '${deviceId}'`); diff --git a/src/types/rest/Firmwares.ts b/src/types/rest/Firmwares.ts index 7e649ac..c5dda13 100644 --- a/src/types/rest/Firmwares.ts +++ b/src/types/rest/Firmwares.ts @@ -1,8 +1,17 @@ +/** Device firmware information */ export interface FirmwareDevice { + /** Device ID */ Device: string; + /** Device firmware version */ InstalledVersion: string; } +/** + * Collection of firmware devices indexed by device ID + * + * Maps device ID strings to their corresponding firmware device objects, providing a lookup table for all devices + * associated with a user account. + */ export interface Firmwares { Firmware: Record; } diff --git a/src/types/rest/States.ts b/src/types/rest/States.ts new file mode 100644 index 0000000..337d5b4 --- /dev/null +++ b/src/types/rest/States.ts @@ -0,0 +1,69 @@ +/** Represents a timestamped value with metadata */ +export interface TimestampedValue { + /** Timestamp when the value was recorded */ + t: number; + /** The actual value */ + v: T; +} + +/** Represents the state of a single device */ +export interface DeviceState { + /** Device identifier */ + Device: string; + /** Overall timestamp for the device state */ + Timestamp: number; + /** Time the device has been on */ + OnTime: TimestampedValue; + /** Temperature set point */ + SetPoint: TimestampedValue; + /** Display brightness level */ + Brightness: TimestampedValue; + /** Schedule mode setting */ + ScheduleMode: TimestampedValue; + /** Hold time setting */ + HoldTime: TimestampedValue; + /** Wi-Fi signal strength */ + Rssi: TimestampedValue; + /** Thermostat mode */ + TstatMode: TimestampedValue; + /** Available heap memory */ + FreeHeap: TimestampedValue; + /** Sensor temperature reading */ + SensorTemp: TimestampedValue; + /** Current mode */ + Mode: TimestampedValue; + /** Voltage measurement */ + Voltage: TimestampedValue; + /** Temperature corrected for calibration */ + CorrectedTemp: TimestampedValue; + /** Duty cycle percentage */ + Duty: TimestampedValue; + /** Heat sink temperature */ + HeatSink: TimestampedValue; + /** Time the device has been off */ + OffTime: TimestampedValue; + /** Connection status */ + Connected: TimestampedValue; + /** Current consumption */ + Current: TimestampedValue; + /** Humidity reading */ + Humidity: TimestampedValue; + /** Lock status */ + Lock: TimestampedValue; +} + +/** + * Collection of device states indexed by device ID + * + * Maps device ID strings to their corresponding device state objects, providing a lookup table for all devices + * associated with a user account. + */ +export interface DeviceStatesObj { + /** Device state objects indexed by their unique device ID strings */ + [deviceId: string]: DeviceState; +} + +/** Top-level interface for the device states REST API response. */ +export interface DeviceStates { + DeviceStatesObj: DeviceStatesObj; +} diff --git a/src/types/rest/index.ts b/src/types/rest/index.ts index efa93be..23a4e1d 100644 --- a/src/types/rest/index.ts +++ b/src/types/rest/index.ts @@ -1,2 +1,3 @@ export * from './Devices'; export * from './Firmwares'; +export * from './States';