import {CO2Sensor} from './component/CO2Sensor' import {ThresholdRelay} from './component/relay/ThresholdRelay' import {Alerts, Component, componentMap, Daytime, Ebbflow, Exaust, Humidity, IntervalIrrigation, Light, PHMeter, Relay, Reservoir, Temperature} from "./components" import {Device} from './Device' import {FloraFi} from './Florafi' import {LogLine} from './LogLine' // interface RoomStats // { // devicesTotal: number; // devicesOnline: number; // devicesOffline: number; // componentsTotal: number; // alertsTotal: number; // alertsInfo: number; // alertsWarning: number; // alertsError: number; // } export class Room { id: string name = "" device: {[componentName: string]: Device} = {} farm: FloraFi lastLog: LogLine | null = null alerts: Alerts | null = null ebbflow: Ebbflow | null = null daytime: Daytime | null = null light: Light | null = null temperature: Temperature | null = null humidity: Humidity | null = null reservoir: Reservoir | null = null humidifier: ThresholdRelay | null = null dehumidifier: ThresholdRelay | null = null lighting: Relay | null = null lightingA: Relay | null = null lightingB: Relay | null = null irrigation: IntervalIrrigation | null = null reservoirFill: Relay | null = null reservoirDrain: Relay | null = null ebbflowFlood: Relay | null = null ebbflowDrain: Relay | null = null airConditioner: ThresholdRelay | null = null heater: Relay | null = null solutionChiller: Relay | null = null solutionHeater: Relay | null = null co2Emitter: ThresholdRelay | null = null co2Sensor: CO2Sensor | null = null exaust: Exaust | null = null phMeter: PHMeter | null = null stats = { devicesTotal: 0, devicesOnline: 0, devicesOffline: 0, componentsTotal: 0, alertsTotal: 0, alertsInfo: 0, alertsWarning: 0, alertsError: 0, } private subscriptions: {[key: string]: boolean} = {} constructor(id: string, farm: FloraFi) { this.id = id this.farm = farm this.farm.subscribe(`florafi/room/${this.id}/+`) this.subscribe("device") } get components(): Component[] { return Object.keys(componentMap) .filter(id => this[id] != null) .map(id => this[id]) } subscribe(subtopic: string): void { if (this.subscriptions[subtopic]) return console.warn(`Already subscribed to ${this.id}/${subtopic}`) this.subscriptions[subtopic] = true this.farm.subscribe(`florafi/room/${this.id}/${subtopic}/#`) } unsubscribe(subtopic: string): void { if (!this.subscriptions[subtopic]) return console.warn(`Not subscribed to ${this.id}/${subtopic}`) delete this.subscriptions[subtopic] this.farm.unsubscribe(`florafi/room/${this.id}/${subtopic}`) } setControl(control: string, value: string | number | boolean): void { this.farm.setControl(`${this.id}.${control}`, value) } setName(name: string): void { console.log(`Renaming room '${this.id}' to ${name}`) this.farm.publish(`florafi/room/${this.id}/$name`, name, {retain: true}) } }