import { pond } from "protobuf-ts/pond"; import { quack } from "protobuf-ts/pond"; import { or } from "utils/types"; import { clone, cloneDeep } from "lodash"; import { Component } from "models"; export class CO2 { public settings: pond.ComponentSettings = pond.ComponentSettings.create(); public status: pond.ComponentStatus = pond.ComponentStatus.create(); public lastReading: string = ""; public ppm: number = 0; public static create(comp: Component): CO2 { let my = new CO2(); my.settings = comp.settings; my.status = comp.status; if (comp.status.lastGoodMeasurement.length > 0) { comp.status.lastGoodMeasurement.forEach(um => { if (um.timestamps[0]) { my.lastReading = um.timestamps[0]; } if (um.values[0] && um.values[0].values.length > 0) { if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_PPM) { my.ppm = um.values[0].values[0]; } } }); } return my; } public static createPond(comp: pond.Component): CO2 { let my = new CO2(); my.settings = comp.settings ? comp.settings : pond.ComponentSettings.create(); my.status = comp.status ? comp.status : pond.ComponentStatus.create(); if (comp.status && comp.status.lastGoodMeasurement.length > 0) { comp.status.lastGoodMeasurement.forEach(um => { if (um.timestamps[0]) { my.lastReading = um.timestamps[0]; } if (um.values[0] && um.values[0].values.length > 0) { if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_PPM) { my.ppm = um.values[0].values[0]; } } }); } return my; } public static any(data: any): CO2 { let comp = pond.Component.fromObject(cloneDeep(data)); let my = CO2.createPond(comp); if (data && data.status && data.status.lastMeasurement) { my.status.lastMeasurement = data.status.lastMeasurement; } return my; } public update(other: CO2) { this.settings = other.settings; this.status = other.status; } public name(): string { return this.settings.name !== "" ? this.settings.name : "Component " + this.key(); } public key(): string { return this.settings.key; } public location(): quack.ComponentID { return quack.ComponentID.fromObject({ type: this.settings.type, addressType: this.settings.addressType, address: this.settings.address }); } public locationString(): string { return ( or(this.settings.type, 0).toString() + "-" + or(this.settings.addressType, 0).toString() + "-" + or(this.settings.address, 0).toString() ); } public type(): quack.ComponentType { return this.settings.type; } public subType(): number { return this.settings.subtype; } //return the CO2 as a component public asComponent(): Component { let component = Component.create(); component.settings = this.settings; component.status = this.status; return component; } }