V2: Remap many stuff

Took 3 hours 0 minutes
This commit is contained in:
2023-01-23 23:56:39 +01:00
parent 58a3df8111
commit c9a0ac68c4
32 changed files with 559 additions and 653 deletions

View File

@ -2,44 +2,97 @@ import {IContainer} from "./database/IContainer";
import {SensorType} from "./SensorType";
import {HX711} from "./HX711";
import debug from "debug";
import {ArduinoProxyPayload} from "./ArduinoProxyPayload";
import {ArduinoProxyPayloadType} from "./ArduinoProxyPayloadType";
import {ArduinoProxy} from "./ArduinoProxy";
import Container from "./database/Container";
const log = debug("itender:sensor");
export class SensorHelper {
/**
* Returns the current container weight
* Returns the current raw container weight
* @param container
*/
static measure(container: IContainer): number | null {
if (container.sensorType == SensorType.LOADCELL) {
try {
// V2: Measure weight
let sensor = new HX711(container.sensorPin1, container.sensorPin2);
static measureRaw(container: IContainer): Promise<number> {
return new Promise(async (resolve, reject) => {
if (container.sensorType == SensorType.LOADCELL) {
try {
if (container.sensorProxy) {
let payload = new ArduinoProxyPayload(ArduinoProxyPayloadType.GET_SENSOR, {
pin_data: container.sensorPin1,
pin_clock: container.sensorPin2
});
let val = await ArduinoProxy.sendRequest(payload);
if (!val.data.value)
return reject("");
container.rawData = sensor.measure();
} catch (e) {
log("Sensor (Weight cell) of container " + container._id + " is broken or has malfunction - Removing it!");
container.sensorType = SensorType.NONE;
container.save();
return null;
container.rawData = val.data.value;
} else {
let sensor = new HX711(container.sensorPin1, container.sensorPin2);
container.rawData = sensor.measure();
}
} catch (e) {
log("Sensor (Weight cell) of container " + container._id + " is broken or has malfunction - Removing it!");
container.sensorType = SensorType.NONE;
await container.save();
return reject();
}
} else if (container.sensorType == SensorType.ULTRASOUND) {
try {
// V2: Measure weight
let sensor = new HX711(container.sensorPin1, container.sensorPin2);
container.rawData = sensor.measure();
} catch (e) {
log("Sensor (Ultrasound) of container " + container._id + " is broken or has malfunction - Removing it!");
container.sensorType = SensorType.NONE;
await container.save();
return reject();
}
}
} else if (container.sensorType == SensorType.ULTRASOUND) {
try {
// V2: Measure weight
let sensor = new HX711(container.sensorPin1, container.sensorPin2);
container.rawData = sensor.measure();
} catch (e) {
log("Sensor (Ultrasound) of container " + container._id + " is broken or has malfunction - Removing it!");
container.sensorType = SensorType.NONE;
container.save();
return null;
}
}
// todo Überprüfen ob hier Umrechnungen nötig sind. Soll in Gramm zurück gegeben werden
return container.rawData;
resolve(container.rawData);
});
}
}
/**
* All raw values from the measurements get cleared
*/
public static clearAllRawMeasurements() {
return new Promise<void>(async (resolve, reject) => {
for (let c of (await Container.find({}))) {
if (c.sensorType != SensorType.NONE) {
c.rawData = -1;
await c.save();
}
}
resolve();
})
}
/**
* All containers will be measured
*/
public static measureAllRaw() {
return new Promise<void>(async (resolve, reject) => {
for (let c of (await Container.find({}))) {
if (c.sensorType != SensorType.NONE) {
let weight: number | null = c.rawData;
try {
weight = await SensorHelper.measureRaw(c);
} catch (e) {
return reject("Fehler Sensor (" + c.sensorPin1 + ", " + c.sensorPin2 + ") - Container " + c.slot + 1);
}
}
}
resolve();
})
}
}