package com.kosdev.samples.dispenser.part1.hardware;
import com.tccc.kos.commons.util.KosUtil;
import com.tccc.kos.commons.util.concurrent.future.FutureEvent;
import com.tccc.kos.commons.util.concurrent.future.FutureWork;
import com.tccc.kos.ext.dispense.Pump;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
@Getter
@Slf4j
public class Valve extends Pump<ValveConfig> {
private final int pos;
public Valve(ControlBoard controlBoard, String name, int pos) {
super(controlBoard, name, null);
this.pos = pos;
}
/**
* Starts a time-based pour operation for this valve.
*/
@Override
public FutureWork tpour(int duration, double rate) {
// Create a future to turn the pump on for the requested duration
FutureWork future = new FutureWork("tpour" + "-" + getName(), f -> {
log.info("start: {}", getName());
KosUtil.scheduleCallback(() -> f.success(), duration);
// TODO: turn on the valve
});
// Add a completion handler to turn off the valve, which is
// called when the timer fires or if the pour is cancelled
future.append("stop", FutureEvent.COMPLETE, f -> {
log.info("stop: {}", getName());
// TODO: turn off the valve
});
return future;
}
/**
* Starts a volume-based pour operation for this valve.
*/
@Override
public FutureWork vpour(int volume, double rate) {
// convert the volume pour to a timed pour
return tpour((int) (volume / rate), rate);
}
@Override
public String getType() {
return "valve";
}
}