play button works, just need to build out the controls
This commit is contained in:
parent
1dbf3a96c6
commit
d30ed99ca7
1 changed files with 40 additions and 29 deletions
|
|
@ -6,7 +6,7 @@ import moment, { Moment } from "moment";
|
||||||
import { pond } from "protobuf-ts/pond";
|
import { pond } from "protobuf-ts/pond";
|
||||||
import { quack } from "protobuf-ts/quack";
|
import { quack } from "protobuf-ts/quack";
|
||||||
import { useComponentAPI } from "providers";
|
import { useComponentAPI } from "providers";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
const CHECKPOINT_COUNT = 10;
|
const CHECKPOINT_COUNT = 10;
|
||||||
|
|
||||||
|
|
@ -270,44 +270,55 @@ function buildStatusCheckpoints(
|
||||||
* @returns a JSX Element
|
* @returns a JSX Element
|
||||||
*/
|
*/
|
||||||
export default function BinPlayer(props: Props) {
|
export default function BinPlayer(props: Props) {
|
||||||
const { bin, componentDevices } = props;
|
const { bin, componentDevices, setBin } = props;
|
||||||
const componentAPI = useComponentAPI();
|
const componentAPI = useComponentAPI();
|
||||||
const defaultDateRange = GetDefaultDateRange();
|
const defaultDateRange = GetDefaultDateRange();
|
||||||
const [startDate, setStartDate] = useState<Moment>(defaultDateRange.start);
|
const [startDate, setStartDate] = useState<Moment>(defaultDateRange.start);
|
||||||
const [endDate, setEndDate] = useState<Moment>(defaultDateRange.end);
|
const [endDate, setEndDate] = useState<Moment>(defaultDateRange.end);
|
||||||
const [statusCheckpoints, setStatusCheckpoints] = useState<StatusCheckpoint[]>([]);
|
// const [statusCheckpoints, setStatusCheckpoints] = useState<StatusCheckpoint[]>([]);
|
||||||
|
const isPlaying = useRef(false);
|
||||||
useEffect(()=>{
|
const stopPlayer = () => { isPlaying.current = false; };
|
||||||
console.log(statusCheckpoints)
|
const originalBin = useRef<Bin | null>(null);
|
||||||
},[statusCheckpoints])
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches sampled unit measurements for every grain cable in the current bin status,
|
* Fetches sampled unit measurements for every grain cable in the current bin status,
|
||||||
* then builds `statusCheckpoints` for playback over [startDate, endDate].
|
* then builds `statusCheckpoints` for playback over [startDate, endDate].
|
||||||
*/
|
*/
|
||||||
const startPlayer = () => {
|
const startPlayer = async () => {
|
||||||
const sampleRequests = bin.status.grainCables.map(cable => {
|
console.log(startDate.toISOString())
|
||||||
const deviceID = componentDevices.get(cable.key) ?? 0;
|
console.log(endDate.toISOString())
|
||||||
return componentAPI.sampleUnitMeasurements(
|
originalBin.current = cloneDeep(bin); // capture before async work begins
|
||||||
deviceID,
|
isPlaying.current = true;
|
||||||
cable.key,
|
|
||||||
startDate,
|
|
||||||
endDate,
|
|
||||||
100
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
Promise.all(sampleRequests).then(responses => {
|
const sampleRequests = bin.status.grainCables.map(cable => {
|
||||||
setStatusCheckpoints(
|
const deviceID = componentDevices.get(cable.key) ?? 0;
|
||||||
buildStatusCheckpoints(
|
return componentAPI.sampleUnitMeasurements(deviceID, cable.key, startDate, endDate, 100);
|
||||||
bin.status.grainCables,
|
});
|
||||||
responses.map(r => r.data),
|
|
||||||
startDate,
|
const responses = await Promise.all(sampleRequests);
|
||||||
endDate
|
const checkpoints = buildStatusCheckpoints(
|
||||||
)
|
bin.status.grainCables,
|
||||||
);
|
responses.map(r => r.data),
|
||||||
});
|
startDate,
|
||||||
};
|
endDate
|
||||||
|
);
|
||||||
|
console.log(responses)
|
||||||
|
console.log(checkpoints)
|
||||||
|
|
||||||
|
for (const checkpoint of checkpoints) {
|
||||||
|
if (!isPlaying.current) {
|
||||||
|
setBin(originalBin.current!);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const newBin = cloneDeep(originalBin.current!);
|
||||||
|
newBin.status.grainCables = checkpoint.cables;
|
||||||
|
setBin(newBin);
|
||||||
|
await new Promise(res => setTimeout(res, 2000));
|
||||||
|
console.log("next step")
|
||||||
|
}
|
||||||
|
isPlaying.current = false;
|
||||||
|
setBin(originalBin.current!);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue