component cards imported
This commit is contained in:
parent
af4cc08d87
commit
e5bc90e00e
3 changed files with 533 additions and 0 deletions
242
src/models/GrainCable.ts
Normal file
242
src/models/GrainCable.ts
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
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";
|
||||
import { describeMeasurement } from "pbHelpers/MeasurementDescriber";
|
||||
import { extractGrainCable } from "pbHelpers/ComponentTypes";
|
||||
|
||||
export class GrainCable {
|
||||
public settings: pond.ComponentSettings = pond.ComponentSettings.create();
|
||||
public status: pond.ComponentStatus = pond.ComponentStatus.create();
|
||||
public lastReading: string = "";
|
||||
public temperatures: number[] = [];
|
||||
public humidities: number[] = [];
|
||||
public grainMoistures: number[] = [];
|
||||
public topNode: number = 0;
|
||||
|
||||
public static create(comp: Component): GrainCable {
|
||||
let my = new GrainCable();
|
||||
my.settings = comp.settings;
|
||||
my.status = comp.status;
|
||||
|
||||
let temps: number[] = [];
|
||||
let hums: number[] = [];
|
||||
let emc: number[] = [];
|
||||
let lastReading = "";
|
||||
if (comp.status.measurement.length > 0) {
|
||||
comp.status.measurement.forEach(um => {
|
||||
if (um.timestamps[0]) {
|
||||
lastReading = um.timestamps[0];
|
||||
}
|
||||
if (um.values[0] && um.values[0].values.length > 0) {
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE) {
|
||||
let revTemps = clone(um.values[0].values);
|
||||
temps = revTemps.reverse();
|
||||
}
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_PERCENT) {
|
||||
let revHums = clone(um.values[0].values);
|
||||
hums = revHums.reverse();
|
||||
}
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_GRAIN_EMC) {
|
||||
let revMoist = clone(um.values[0].values);
|
||||
emc = revMoist.reverse();
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
let nodes = extractGrainCable(comp.status.lastMeasurement?.measurement?.grainCable).reverse();
|
||||
if (nodes.length > 0) {
|
||||
nodes.forEach(node => {
|
||||
hums.push(node.humidity);
|
||||
temps.push(node.temperature);
|
||||
});
|
||||
}
|
||||
}
|
||||
my.temperatures = temps;
|
||||
my.humidities = hums;
|
||||
my.grainMoistures = emc;
|
||||
my.lastReading = lastReading;
|
||||
my.topNode = comp.settings.grainFilledTo;
|
||||
return my;
|
||||
}
|
||||
|
||||
public static createPond(comp: pond.Component): GrainCable {
|
||||
let my = new GrainCable();
|
||||
my.settings = comp.settings ? comp.settings : pond.ComponentSettings.create();
|
||||
my.status = comp.status ? comp.status : pond.ComponentStatus.create();
|
||||
|
||||
let temps: number[] = [];
|
||||
let hums: number[] = [];
|
||||
if (comp.lastMeasurement.length > 0) {
|
||||
comp.lastMeasurement.forEach(um => {
|
||||
if (um.values[0]) {
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE) {
|
||||
temps = um.values[0].values.reverse();
|
||||
}
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_PERCENT) {
|
||||
hums = um.values[0].values.reverse();
|
||||
}
|
||||
//TODO-CS: could expand this to have the emc in the cable model as well
|
||||
// if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_EMC){
|
||||
// emc = um.values[0].values
|
||||
// }
|
||||
}
|
||||
});
|
||||
} else {
|
||||
let nodes = extractGrainCable(
|
||||
comp?.status?.lastMeasurement?.measurement?.grainCable
|
||||
).reverse();
|
||||
if (nodes.length > 0) {
|
||||
nodes.forEach(node => {
|
||||
hums.push(node.humidity);
|
||||
temps.push(node.temperature);
|
||||
});
|
||||
}
|
||||
}
|
||||
my.temperatures = temps;
|
||||
my.humidities = hums;
|
||||
|
||||
return my;
|
||||
}
|
||||
|
||||
public static any(data: any): GrainCable {
|
||||
let comp = pond.Component.fromObject(cloneDeep(data));
|
||||
let my = GrainCable.createPond(comp);
|
||||
if (data && data.status && data.status.lastMeasurement) {
|
||||
my.status.lastMeasurement = data.status.lastMeasurement;
|
||||
}
|
||||
return my;
|
||||
}
|
||||
|
||||
public update(other: GrainCable) {
|
||||
this.settings = other.settings;
|
||||
this.status = other.status;
|
||||
}
|
||||
|
||||
public humidColour() {
|
||||
return describeMeasurement(
|
||||
quack.MeasurementType.MEASUREMENT_TYPE_PERCENT,
|
||||
quack.ComponentType.COMPONENT_TYPE_DHT
|
||||
).colour();
|
||||
}
|
||||
|
||||
private farenheit(temp: number) {
|
||||
return temp * (9 / 5) + 32;
|
||||
}
|
||||
|
||||
public tempColour() {
|
||||
return describeMeasurement(quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE).colour();
|
||||
}
|
||||
|
||||
public minTemp(unit = pond.TemperatureUnit.TEMPERATURE_UNIT_CELSIUS) {
|
||||
if (unit === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT)
|
||||
return this.farenheit(Math.min(...this.temperatures));
|
||||
return Math.min(...this.temperatures);
|
||||
}
|
||||
|
||||
public minHumidity() {
|
||||
return Math.min(...this.humidities);
|
||||
}
|
||||
|
||||
public minMoisture() {
|
||||
return Math.min(...this.grainMoistures);
|
||||
}
|
||||
|
||||
public aveTemp(unit = pond.TemperatureUnit.TEMPERATURE_UNIT_CELSIUS) {
|
||||
if (unit === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT)
|
||||
return this.farenheit(
|
||||
this.temperatures.reduce((p: any, c: any) => p + c, 0) / this.temperatures.length
|
||||
);
|
||||
return this.temperatures.reduce((p: any, c: any) => p + c, 0) / this.temperatures.length;
|
||||
}
|
||||
|
||||
public aveHumidity() {
|
||||
return this.humidities.reduce((p: any, c: any) => p + c, 0) / this.humidities.length;
|
||||
}
|
||||
|
||||
public maxTemp(unit = pond.TemperatureUnit.TEMPERATURE_UNIT_CELSIUS) {
|
||||
if (unit === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT)
|
||||
return this.farenheit(Math.max(...this.temperatures));
|
||||
return Math.max(...this.temperatures);
|
||||
}
|
||||
|
||||
public maxHumidity() {
|
||||
return Math.max(...this.humidities);
|
||||
}
|
||||
|
||||
public maxMoisture() {
|
||||
return Math.min(...this.grainMoistures);
|
||||
}
|
||||
|
||||
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 grain cable as a component
|
||||
public asComponent(): Component {
|
||||
let component = Component.create();
|
||||
component.settings = this.settings;
|
||||
component.status = this.status;
|
||||
|
||||
//this section of the function will be deprecated as the last measurement in the new structure is in the status again
|
||||
let lastMeasurements = [];
|
||||
let lastTemp = pond.UnitMeasurementsForComponent.create();
|
||||
lastTemp.componentId = this.settings.key;
|
||||
lastTemp.timestamps = [this.lastReading];
|
||||
lastTemp.type = quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE;
|
||||
lastTemp.values = [pond.ValueArray.create()];
|
||||
lastTemp.values[0].values = this.temperatures;
|
||||
lastMeasurements.push(lastTemp);
|
||||
|
||||
let lastHum = pond.UnitMeasurementsForComponent.create();
|
||||
lastHum.componentId = this.settings.key;
|
||||
lastHum.timestamps = [this.lastReading];
|
||||
lastHum.type = quack.MeasurementType.MEASUREMENT_TYPE_PERCENT;
|
||||
lastHum.values = [pond.ValueArray.create()];
|
||||
lastHum.values[0].values = this.humidities;
|
||||
lastMeasurements.push(lastHum);
|
||||
|
||||
if (this.grainMoistures.length > 0) {
|
||||
let lastEMC = pond.UnitMeasurementsForComponent.create();
|
||||
lastEMC.componentId = this.settings.key;
|
||||
lastEMC.timestamps = [this.lastReading];
|
||||
lastEMC.type = quack.MeasurementType.MEASUREMENT_TYPE_GRAIN_EMC;
|
||||
lastEMC.values = [pond.ValueArray.create()];
|
||||
lastEMC.values[0].values = this.grainMoistures;
|
||||
lastMeasurements.push(lastEMC);
|
||||
}
|
||||
component.lastMeasurement = lastMeasurements;
|
||||
return component;
|
||||
}
|
||||
}
|
||||
146
src/models/Plenum.ts
Normal file
146
src/models/Plenum.ts
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
import { pond } from "protobuf-ts/pond";
|
||||
import { quack } from "protobuf-ts/pond";
|
||||
import { or } from "utils/types";
|
||||
import { cloneDeep } from "lodash";
|
||||
import { Component } from "models";
|
||||
import { describeMeasurement } from "pbHelpers/MeasurementDescriber";
|
||||
|
||||
export class Plenum {
|
||||
public settings: pond.ComponentSettings = pond.ComponentSettings.create();
|
||||
public status: pond.ComponentStatus = pond.ComponentStatus.create();
|
||||
public temperature: number = -127;
|
||||
public humidity: number = 200;
|
||||
|
||||
public static create(comp: Component): Plenum {
|
||||
let my = new Plenum();
|
||||
my.settings = comp.settings;
|
||||
my.status = comp.status;
|
||||
|
||||
if (comp.status.measurement.length > 0) {
|
||||
comp.status.measurement.forEach(um => {
|
||||
if (um.values[0]) {
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE) {
|
||||
my.temperature = or(um.values[0].values[0], -127);
|
||||
}
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_PERCENT) {
|
||||
my.humidity = or(um.values[0].values[0], 200);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
let temp = comp.status?.lastMeasurement?.measurement?.humidityAndTemperature?.celciusTimes_10;
|
||||
let hum =
|
||||
comp.status?.lastMeasurement?.measurement?.humidityAndTemperature
|
||||
?.relativeHumidityTimes_100;
|
||||
if (temp === undefined) {
|
||||
if (comp.status?.lastMeasurement?.measurement?.humidityAndTemperature) {
|
||||
let humAndTemp = comp.status?.lastMeasurement?.measurement?.humidityAndTemperature as any;
|
||||
temp = humAndTemp["celciusTimes10"];
|
||||
hum = humAndTemp["relativeHumidityTimes100"];
|
||||
}
|
||||
}
|
||||
my.temperature = or(temp, -1270) / 10;
|
||||
my.humidity = or(hum, 20000) / 100;
|
||||
}
|
||||
|
||||
return my;
|
||||
}
|
||||
|
||||
public static createPond(comp: pond.Component): Plenum {
|
||||
let my = new Plenum();
|
||||
my.settings = comp.settings ? comp.settings : pond.ComponentSettings.create();
|
||||
my.status = comp.status ? comp.status : pond.ComponentStatus.create();
|
||||
let temp = comp.status?.lastMeasurement?.measurement?.humidityAndTemperature?.celciusTimes_10;
|
||||
let hum =
|
||||
comp.status?.lastMeasurement?.measurement?.humidityAndTemperature?.relativeHumidityTimes_100;
|
||||
if (temp === undefined) {
|
||||
if (comp.status?.lastMeasurement?.measurement?.humidityAndTemperature) {
|
||||
let humAndTemp = comp.status?.lastMeasurement?.measurement?.humidityAndTemperature as any;
|
||||
temp = humAndTemp["celciusTimes10"];
|
||||
hum = humAndTemp["relativeHumidityTimes100"];
|
||||
}
|
||||
}
|
||||
my.temperature = or(temp, -1270) / 10;
|
||||
my.humidity = or(hum, 20000) / 100;
|
||||
|
||||
return my;
|
||||
}
|
||||
|
||||
public static any(data: any): Plenum {
|
||||
let comp = pond.Component.fromObject(cloneDeep(data));
|
||||
let my = Plenum.createPond(comp);
|
||||
if (data && data.status && data.status.lastMeasurement) {
|
||||
my.status.lastMeasurement = data.status.lastMeasurement;
|
||||
}
|
||||
return my;
|
||||
}
|
||||
|
||||
public update(other: Plenum) {
|
||||
this.settings = other.settings;
|
||||
this.status = other.status;
|
||||
}
|
||||
|
||||
public static humidColour() {
|
||||
return describeMeasurement(
|
||||
quack.MeasurementType.MEASUREMENT_TYPE_PERCENT,
|
||||
quack.ComponentType.COMPONENT_TYPE_DHT
|
||||
).colour();
|
||||
}
|
||||
|
||||
public static tempColour() {
|
||||
return describeMeasurement(quack.MeasurementType.MEASUREMENT_TYPE_TEMPERATURE).colour();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public getTempString(unit = pond.TemperatureUnit.TEMPERATURE_UNIT_CELSIUS): string {
|
||||
if (this.temperature < -126) return "--";
|
||||
if (unit === pond.TemperatureUnit.TEMPERATURE_UNIT_FAHRENHEIT)
|
||||
return (this.temperature * (9 / 5) + 32).toFixed(2) + "°F";
|
||||
return this.temperature.toFixed(2) + "°C";
|
||||
}
|
||||
|
||||
public getHumidityString(): string {
|
||||
if (this.humidity > 199) return "--";
|
||||
return this.humidity.toFixed(2) + "%";
|
||||
}
|
||||
|
||||
public asComponent(): Component {
|
||||
let c = Component.create();
|
||||
c.settings = this.settings;
|
||||
c.status = this.status;
|
||||
return c;
|
||||
}
|
||||
}
|
||||
145
src/models/Pressure.ts
Normal file
145
src/models/Pressure.ts
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
import { pond } from "protobuf-ts/pond";
|
||||
import { quack } from "protobuf-ts/pond";
|
||||
import { or } from "utils/types";
|
||||
import { cloneDeep } from "lodash";
|
||||
import { Component } from "models";
|
||||
import { describeMeasurement } from "pbHelpers/MeasurementDescriber";
|
||||
|
||||
export class Pressure {
|
||||
public settings: pond.ComponentSettings = pond.ComponentSettings.create();
|
||||
public status: pond.ComponentStatus = pond.ComponentStatus.create();
|
||||
public pascals: number = 0;
|
||||
public fanId: number = 0;
|
||||
|
||||
public static create(comp: Component): Pressure {
|
||||
let my = new Pressure();
|
||||
my.settings = comp.settings;
|
||||
my.status = comp.status ? comp.status : pond.ComponentStatus.create();
|
||||
my.fanId = comp.settings.fanId;
|
||||
//getting the value from the unitmeasurements in status instead of the old style measurements in the status
|
||||
if (comp.status.measurement.length > 0) {
|
||||
comp.status.measurement.forEach(um => {
|
||||
if (um.values[0] && um.values[0].values.length > 0) {
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_PRESSURE) {
|
||||
my.pascals = um.values[0].values[0];
|
||||
}
|
||||
//TODO-CS: could expand this to have the fan cfm in the pressure model as well
|
||||
// if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_CFM){
|
||||
// my.fanCFM = um.values[0].values[0]
|
||||
// }
|
||||
}
|
||||
});
|
||||
} else {
|
||||
//if no unit measurements in status use the old measurements in status
|
||||
let pre = comp.status?.lastMeasurement?.measurement?.pressure?.pascals;
|
||||
if (pre) {
|
||||
my.pascals = pre;
|
||||
}
|
||||
}
|
||||
return my;
|
||||
}
|
||||
|
||||
public static createPond(comp: pond.Component): Pressure {
|
||||
let my = new Pressure();
|
||||
my.settings = comp.settings ? comp.settings : pond.ComponentSettings.create();
|
||||
my.status = comp.status ? comp.status : pond.ComponentStatus.create();
|
||||
//getting the value from the unitmeasurement part of the component instead of the old style measurements in the status
|
||||
if (comp.lastMeasurement.length > 0) {
|
||||
comp.lastMeasurement.forEach(um => {
|
||||
if (um.values[0] && um.values[0].values[0]) {
|
||||
if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_PRESSURE) {
|
||||
my.pascals = um.values[0].values[0];
|
||||
}
|
||||
//TODO-CS: could expand this to have the fan cfm in the pressure model as well
|
||||
// if (um.type === quack.MeasurementType.MEASUREMENT_TYPE_CFM){
|
||||
// my.fanCFM = um.values[0].values[0]
|
||||
// }
|
||||
}
|
||||
});
|
||||
} else {
|
||||
//if last measurements is not in the component from the pond use the status measurement
|
||||
let pre = comp.status?.lastMeasurement?.measurement?.pressure?.pascals;
|
||||
if (pre) {
|
||||
my.pascals = pre;
|
||||
}
|
||||
}
|
||||
return my;
|
||||
}
|
||||
|
||||
public static any(data: any): Pressure {
|
||||
let comp = pond.Component.fromObject(cloneDeep(data));
|
||||
let my = Pressure.createPond(comp);
|
||||
let pre = comp.status?.lastMeasurement?.measurement?.pressure?.pascals;
|
||||
if (pre) {
|
||||
my.pascals = pre;
|
||||
}
|
||||
return my;
|
||||
}
|
||||
|
||||
public update(other: Pressure) {
|
||||
this.settings = other.settings;
|
||||
this.status = other.status;
|
||||
}
|
||||
|
||||
public static colour() {
|
||||
return describeMeasurement(
|
||||
quack.MeasurementType.MEASUREMENT_TYPE_PRESSURE,
|
||||
quack.ComponentType.COMPONENT_TYPE_PRESSURE
|
||||
).colour();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public getPressureString(unit = pond.PressureUnit.PRESSURE_UNIT_INCHES_OF_WATER): string {
|
||||
let p: number = this.pascals;
|
||||
let unitString: string = "Pa";
|
||||
if (unit === pond.PressureUnit.PRESSURE_UNIT_INCHES_OF_WATER) {
|
||||
unitString = "iwg";
|
||||
p = p / 249;
|
||||
}
|
||||
if (unit === pond.PressureUnit.PRESSURE_UNIT_KILOPASCALS) {
|
||||
unitString = "kPa";
|
||||
p = p / 1000;
|
||||
}
|
||||
return p.toFixed(2) + unitString;
|
||||
}
|
||||
|
||||
public asComponent(): Component {
|
||||
let c = Component.create();
|
||||
c.settings = this.settings;
|
||||
c.status = this.status;
|
||||
return c;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue