import {IContainer} from "./database/IContainer";
import {SensorType} from "./SensorType";

import debug from "debug";
import {ArduinoProxyPayload} from "./ArduinoProxyPayload";
import {ArduinoProxyPayloadType} from "./ArduinoProxyPayloadType";
import {ArduinoProxy} from "./ArduinoProxy";
import Container from "./database/Container";
import {HCSR04} from "hc-sr04";
const HX711 = require("pi-hx711");

const log = debug("itender:sensor");

export class SensorHelper {

    /**
     * Returns the current raw container weight
     * @param container
     */
    static measureRaw(container: IContainer): Promise<number> {
        return new Promise(async (resolve, reject) => {
            if (container.sensorType == SensorType.LOADCELL) {
                try {
                    if (container.useProxy) {
                        let payload = new ArduinoProxyPayload(ArduinoProxyPayloadType.GET_SENSOR, {
                            pin_data: container.sensorPin1,
                            pin_clock: container.sensorPin2
                        });
                        let val = await payload.send();
                        if (!val.data.value)
                            return reject("");

                        container.rawData = val.data.value;
                    } else {
                        let sensor = new HX711(container.sensorPin1, container.sensorPin2);
                        container.rawData = await sensor.readRaw();
                    }
                } 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 HCSR04(container.sensorPin1, container.sensorPin2);

                    // container.rawData = sensor.measure(); ToDo

                } 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();
                }
            }


            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();
        })
    }
}



