/** * Important: * --------------- * Model files should not use external imports except for other model files. * Other model files should also be imported directly not via their module * this can otherwise cause issue with ts-to-json-schema conversions in certain scenarios * */ import { RXObjectAttributes, ToRXObjectAttributes } from "./RXObject.model"; export type ObjectMap = { [id in KEY]: VALUE; }; const zeroValue = Buffer.from([0]); export const ZERO_STRING = zeroValue.toString() /** * Default root topic for homie */ export const DEFAULT_TOPIC = 'homie'; export const BASE_HOMIE_VERSION = '5'; export const HOMIE_VERSION: HomieVersion = `${BASE_HOMIE_VERSION}.0`; /** * @pattern ^(?!\\-)[a-z0-9\\-]+(? 0 && !!id.match(HomieIDRegex); } /** * @title HomieVersion * @description * The implemented Homie convention version, without the "patch" level. So the format is "5.x", where the 'x' is the minor version. * @pattern ^5.([0-9]+)$ */ export type HomieVersion = string; export const HomieVersionRegex = "^5.([0-9]+)$"; export function isHomieVersion(input: any): input is HomieVersion { return input !== undefined && input !== null && typeof input === 'string' && input.length > 0 && !!input.match(HomieVersionRegex); } export interface IDAttributeImpl { /** @description ID of element*/ id: HomieID; } export type IDAttribute = ToRXObjectAttributes; export interface BaseAttributesImpl { /** @description Friendly name */ name?: string, } // export interface BaseAttributes extends IDAttributeImpl { // /** @description Friendly name */ // name?: string, // } export type BaseAttributes = ToRXObjectAttributes; export const HOMIE_DEVICE_STATE_INIT = 'init'; export const HOMIE_DEVICE_STATE_READY = 'ready'; export const HOMIE_DEVICE_STATE_DISCONNECTED = 'disconnected'; export const HOMIE_DEVICE_STATE_SLEEPING = 'sleeping'; export const HOMIE_DEVICE_STATE_LOST = 'lost'; export const HOMIE_DEVICE_STATES = [ HOMIE_DEVICE_STATE_INIT, HOMIE_DEVICE_STATE_READY, HOMIE_DEVICE_STATE_DISCONNECTED, HOMIE_DEVICE_STATE_SLEEPING, HOMIE_DEVICE_STATE_LOST, ]; /** * @title DeviceState * @description * Represenjson the current state of the device. Important: for child devices also the root-device state should be taken into account. * There are 6 different states: * - `init`: this is the state the device is in when it is connected to the MQTT broker, but has not yet sent all Homie messages and is not yet ready to operate. This state is optional, and may be sent if the device takes a long time to initialize, but wishes to announce to consumers that it is coming online. A device may fall back into this state to do some reconfiguration. * - `ready`: this is the state the device is in when it is connected to the MQTT broker and has sent all Homie messages for describing the device attributes, nodes, properties and their values. The device has subscribed to all appropriate /set topics and is ready to receive messages. * - `disconnected`: this is the state the device is in when it is cleanly disconnected from the MQTT broker. You must send this message before cleanly disconnecting. * - `sleeping`: this is the state the device is in when the device is sleeping. You have to send this message before sleeping. * - `lost`: this is the state the device is in when the device has been "badly" disconnected. Important: If a root-device $state is "lost" then the state of every child device in ijson tree is also "lost". You must define this message as last will (LWT) for root devices. */ export type DeviceState = typeof HOMIE_DEVICE_STATE_INIT | typeof HOMIE_DEVICE_STATE_READY | typeof HOMIE_DEVICE_STATE_DISCONNECTED | typeof HOMIE_DEVICE_STATE_SLEEPING | typeof HOMIE_DEVICE_STATE_LOST; export function isDeviceState(input: any): input is DeviceState { return input !== undefined && input !== null && typeof input === 'string' && input.length > 0 && HOMIE_DEVICE_STATES.includes(input); } export interface PropertyAttributesImpl extends BaseAttributesImpl { /** @description The data type. Any of the following values: "integer", "float", "boolean", "string", "enum", "color", "datetime", "duration", "json". */ datatype: HomieDatatype; /** @description Specifies restrictions or options for the given data type. */ format?: string; /** * @description Specifies if the property value can be set (true) or if it is read-only(false). * @default false */ settable?: boolean; /** * @description Whether the Property is retained. * @default true */ retained?: boolean; /** * @description Unit of this property. See @see Base.model.ts HOMIE_UNIT_* consts. */ unit?: string; } /** * @title PropertyAttributes * @description Data attributes of property * @examples * ```json * { * "datatype": "integer", * "name": "First Property", * "format": "1:8", * "settable": true, * "retained": true, * "unit": "#" * } * ``` * */ export type PropertyAttributes = ToRXObjectAttributes; export interface HomiePropertyOptions { readValueFromMqtt: boolean; readTimeout?: number; } export interface NodeAttributesImpl extends BaseAttributesImpl { type?: string | null; } /** * @title NodeAttributes * @description Data attributes of node * @examples * ```json * { * "name": "First Node", * ``` * */ export type NodeAttributes = ToRXObjectAttributes; // export interface NodeAttributes extends NodeAttributesImpl { // id: HomieID // } export interface DeviceDescriptionBase extends BaseAttributesImpl { /** version of the devicedescription */ version: number; /** Homie version of the device */ homie: HomieVersion; /** @description Type of the device */ type?: string | null; /** * @description Array of IDs of child devices */ children?: HomieID[], /** * @description ID of the root parent device. Required if the device is not the root device, must be omitted otherwise. */ root?: HomieID, /** * @description ID of the parent device. Defaults to the root ID. Required if the parent is NOT the root device. */ parent?: HomieID, /** * @title Array of supported extensions. */ extensions?: string[], } export interface DeviceAttributesImpl extends DeviceDescriptionBase { /** @description: State of the device */ state: DeviceState; } /** * @title DeviceAttributes * @name DeviceAttributes * @alias DeviceAttributes * @description Data attributes of node * @examples * ```json * { * "name": "device name", * "root": "id of root device", * "parent": "id of parent device", * "children": ["ids of child devices", "ids of child devices"], * "extensions": ["extention-identifier", "extention-identifier2"], * ``` * */ export type DeviceAttributes = ToRXObjectAttributes; export type TypeNullOrUndef = T | null | undefined; export type TypeOrNull = T | null; /** * Recommended units for HOMIE (+some extra for good measure) */ export const HOMIE_UNIT_DEGREE_CELCIUS = '°C'; export const HOMIE_UNIT_DEGREE_FAHRENHEIT = '°F'; export const HOMIE_UNIT_DEGREE = '°'; export const HOMIE_UNIT_LITER = 'L'; export const HOMIE_UNIT_GALLON = 'gal'; export const HOMIE_UNIT_VOLT = 'V'; export const HOMIE_UNIT_WATT = 'W'; export const HOMIE_UNIT_KILOWATT = 'kW'; export const HOMIE_UNIT_KILOWATTHOUR = 'kWh'; export const HOMIE_UNIT_AMPERE = 'A'; export const HOMIE_UNIT_HERTZ = 'Hz'; export const HOMIE_UNIT_MILI_AMPERE = 'mA'; export const HOMIE_UNIT_PERCENT = '%'; export const HOMIE_UNIT_METER = 'm'; export const HOMIE_UNIT_CUBIC_METER = 'm³'; export const HOMIE_UNIT_FEET = 'ft'; export const HOMIE_UNIT_PASCAL = 'Pa'; export const HOMIE_UNIT_PSI = 'psi'; export const HOMIE_UNIT_SECONDS = 's'; export const HOMIE_UNIT_MINUTES = 'min'; export const HOMIE_UNIT_HOURS = 'h'; export const HOMIE_UNIT_LUX = 'lx'; export const HOMIE_UNIT_KELVIN = 'K'; export const HOMIE_UNIT_MIRED = 'MK⁻¹'; export const HOMIE_UNIT_COUNT_AMOUNT = '#';