import {Component} from './components' import {FloraFi} from './Florafi' import {Room} from './Room' interface IHomieConfigWifi { ssid?: string; password?: string; bssid?: string; channel?: number; ip?: string; mask?: string; gw?: string; dns1?: string; dns2?: string; } interface IHomieConfigMqtt { host?: string; port?: number; base_topic?: string; auth?: boolean; username?: string; password?: string; ssl?: boolean; ssl_fingerprint?: string; } export interface IHomieConfig { name?: string; device_id?: string; device_stats_interval?: number; wifi?: IHomieConfigWifi; mqtt?: IHomieConfigMqtt; ota?: { enabled: boolean; }; settings?: { [key: string]: string | number | boolean }; } interface IDeviceStats { uptime: number | null; signal: number | null; freeHeap?: number; interval?: number; } interface IDeviceNetwork { ip?: string; mac?: string; wifi: IHomieConfigWifi; mqtt: IHomieConfigMqtt; } export interface IDeviceFirmware { name: string | null; version?: string; checksum?: string; data?: Promise; binary_size?: number; binary_path?: string; binary_checksum?: string; } export interface IFirmwareRepositoryManifest { [key: string]: IDeviceFirmware; } interface IDeviceOta { enabled: boolean; status: { progress: number; code: number; message: string; isError: boolean; }; } interface IDeviceSettings { garden_room?: string; ntp_server?: string; update_channel?: string; deactivated?: boolean; } export class Device { id: string name = "" roomId?: string = undefined farm: FloraFi room: Room | null = null components: Component[] = [] isOnline = false isLoading = true isUnknown = true isDeactivated = false stats: IDeviceStats = { uptime: null, signal: null, } network: IDeviceNetwork = { ip: undefined, mac: undefined, wifi: {}, mqtt: {} } firmware: IDeviceFirmware = { name: null } firmwareUpdate?: IDeviceFirmware settings: IDeviceSettings = {} ota: IDeviceOta = { enabled: false, status: { code: NaN, message: "", progress: NaN, isError: false } } constructor(id: string, farm: FloraFi) { this.id = id this.farm = farm this.farm.subscribe(`homie/${this.id}/$implementation/config`) this.farm.subscribe(`homie/${this.id}/$state`) this.farm.subscribe(`homie/${this.id}/$stats/+`) this.farm.subscribe(`homie/${this.id}/$fw/+`) } loadMore(): void { // this.farm.subscribe(`homie/${this.id}/$fw/+`) // this.farm.subscribe(`homie/${this.id}/$implementation/ota/status`) this.farm.subscribe(`homie/${this.id}/$localip`) this.farm.subscribe(`homie/${this.id}/$mac`) } isUpdated(): boolean { return !!this.firmwareUpdate && this.firmwareUpdate.checksum == this.firmware.checksum } isOutdated(): boolean { return !!this.firmwareUpdate && this.firmwareUpdate.checksum != this.firmware.checksum } isUpdateable(): boolean { return this.isOutdated() && this.isOnline } onReboot(): void { this.ota.status.code = NaN this.components = [] } isRelay(): boolean { return this.firmware.name != null && this.firmware.name.match("sonoff") !== null } get relayCount(): number { const firmwareName = this.firmware.name if (!firmwareName) return 0 if (firmwareName.match("1ch")) return 1 else if (firmwareName.match("2ch")) return 2 else if (firmwareName.match("4ch")) return 4 else return 0 } // get components(): Component[] { // if (!this.room) return [] // return this.room?.components.filter(c => c.device?.id == this.id) // } }