feat: stop experiment button
This commit is contained in:
@@ -98,6 +98,11 @@ University within the Software Project course.
|
|||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.stop {
|
||||||
|
background-color: red;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
.restartExperiment{
|
.restartExperiment{
|
||||||
background-color: red;
|
background-color: red;
|
||||||
color: white;
|
color: white;
|
||||||
|
|||||||
@@ -6,17 +6,18 @@ import styles from './MonitoringPage.module.css';
|
|||||||
|
|
||||||
// Store & API
|
// Store & API
|
||||||
import useProgramStore from "../../utils/programStore";
|
import useProgramStore from "../../utils/programStore";
|
||||||
import {
|
import {
|
||||||
nextPhase,
|
nextPhase,
|
||||||
useExperimentLogger,
|
stopExperiment,
|
||||||
useStatusLogger,
|
useExperimentLogger,
|
||||||
pauseExperiment,
|
useStatusLogger,
|
||||||
playExperiment,
|
pauseExperiment,
|
||||||
type ExperimentStreamData,
|
playExperiment,
|
||||||
type GoalUpdate,
|
type ExperimentStreamData,
|
||||||
type TriggerUpdate,
|
type GoalUpdate,
|
||||||
type CondNormsStateUpdate,
|
type TriggerUpdate,
|
||||||
type PhaseUpdate
|
type CondNormsStateUpdate,
|
||||||
|
type PhaseUpdate
|
||||||
} from "./MonitoringPageAPI";
|
} from "./MonitoringPageAPI";
|
||||||
import { graphReducer, runProgram } from '../VisProgPage/VisProgLogic.ts';
|
import { graphReducer, runProgram } from '../VisProgPage/VisProgLogic.ts';
|
||||||
|
|
||||||
@@ -26,12 +27,12 @@ import type { GoalNode } from '../VisProgPage/visualProgrammingUI/nodes/GoalNode
|
|||||||
import type { TriggerNode } from '../VisProgPage/visualProgrammingUI/nodes/TriggerNode';
|
import type { TriggerNode } from '../VisProgPage/visualProgrammingUI/nodes/TriggerNode';
|
||||||
|
|
||||||
// Sub-components
|
// Sub-components
|
||||||
import {
|
import {
|
||||||
GestureControls,
|
GestureControls,
|
||||||
SpeechPresets,
|
SpeechPresets,
|
||||||
DirectSpeechInput,
|
DirectSpeechInput,
|
||||||
StatusList,
|
StatusList,
|
||||||
RobotConnected
|
RobotConnected
|
||||||
} from './MonitoringPageComponents';
|
} from './MonitoringPageComponents';
|
||||||
import ExperimentLogs from "./components/ExperimentLogs.tsx";
|
import ExperimentLogs from "./components/ExperimentLogs.tsx";
|
||||||
|
|
||||||
@@ -76,7 +77,7 @@ function useExperimentLogic() {
|
|||||||
setGoalIndex(0);
|
setGoalIndex(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (data.type === 'goal_update') {
|
else if (data.type === 'goal_update') {
|
||||||
const payload = data as GoalUpdate;
|
const payload = data as GoalUpdate;
|
||||||
const currentPhaseGoals = getGoalsInPhase(phaseIds[phaseIndex]) as GoalNode[];
|
const currentPhaseGoals = getGoalsInPhase(phaseIds[phaseIndex]) as GoalNode[];
|
||||||
@@ -97,7 +98,7 @@ function useExperimentLogic() {
|
|||||||
return nextState;
|
return nextState;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (data.type === 'trigger_update') {
|
else if (data.type === 'trigger_update') {
|
||||||
const payload = data as TriggerUpdate;
|
const payload = data as TriggerUpdate;
|
||||||
setActiveIds((prev) => ({ ...prev, [payload.id]: payload.achieved }));
|
setActiveIds((prev) => ({ ...prev, [payload.id]: payload.achieved }));
|
||||||
@@ -111,7 +112,7 @@ function useExperimentLogic() {
|
|||||||
setActiveIds((prev) => {
|
setActiveIds((prev) => {
|
||||||
const hasChanges = payload.norms.some((u) => prev[u.id] !== u.active);
|
const hasChanges = payload.norms.some((u) => prev[u.id] !== u.active);
|
||||||
if (!hasChanges) return prev;
|
if (!hasChanges) return prev;
|
||||||
|
|
||||||
const nextState = { ...prev };
|
const nextState = { ...prev };
|
||||||
payload.norms.forEach((u) => { nextState[u.id] = u.active; });
|
payload.norms.forEach((u) => { nextState[u.id] = u.active; });
|
||||||
return nextState;
|
return nextState;
|
||||||
@@ -144,7 +145,7 @@ function useExperimentLogic() {
|
|||||||
}
|
}
|
||||||
}, [setProgramState]);
|
}, [setProgramState]);
|
||||||
|
|
||||||
const handleControlAction = async (action: "pause" | "play" | "nextPhase") => {
|
const handleControlAction = async (action: "pause" | "play" | "nextPhase" | "stop") => {
|
||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
switch (action) {
|
switch (action) {
|
||||||
@@ -159,6 +160,9 @@ function useExperimentLogic() {
|
|||||||
case "nextPhase":
|
case "nextPhase":
|
||||||
await nextPhase();
|
await nextPhase();
|
||||||
break;
|
break;
|
||||||
|
case "stop":
|
||||||
|
await stopExperiment();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
@@ -189,14 +193,14 @@ function useExperimentLogic() {
|
|||||||
/**
|
/**
|
||||||
* Visual indicator of progress through experiment phases.
|
* Visual indicator of progress through experiment phases.
|
||||||
*/
|
*/
|
||||||
function PhaseProgressBar({
|
function PhaseProgressBar({
|
||||||
phaseIds,
|
phaseIds,
|
||||||
phaseIndex,
|
phaseIndex,
|
||||||
isFinished
|
isFinished
|
||||||
}: {
|
}: {
|
||||||
phaseIds: string[],
|
phaseIds: string[],
|
||||||
phaseIndex: number,
|
phaseIndex: number,
|
||||||
isFinished: boolean
|
isFinished: boolean
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<div className={styles.phaseProgress}>
|
<div className={styles.phaseProgress}>
|
||||||
@@ -218,16 +222,16 @@ function PhaseProgressBar({
|
|||||||
/**
|
/**
|
||||||
* Main control buttons (Play, Pause, Next, Reset).
|
* Main control buttons (Play, Pause, Next, Reset).
|
||||||
*/
|
*/
|
||||||
function ControlPanel({
|
function ControlPanel({
|
||||||
loading,
|
loading,
|
||||||
isPlaying,
|
isPlaying,
|
||||||
onAction,
|
onAction,
|
||||||
onReset
|
onReset
|
||||||
}: {
|
}: {
|
||||||
loading: boolean,
|
loading: boolean,
|
||||||
isPlaying: boolean,
|
isPlaying: boolean,
|
||||||
onAction: (a: "pause" | "play" | "nextPhase") => void,
|
onAction: (a: "pause" | "play" | "nextPhase" | "stop") => void,
|
||||||
onReset: () => void
|
onReset: () => void
|
||||||
}) {
|
}) {
|
||||||
return (
|
return (
|
||||||
<div className={styles.experimentControls}>
|
<div className={styles.experimentControls}>
|
||||||
@@ -245,17 +249,23 @@ function ControlPanel({
|
|||||||
disabled={loading}
|
disabled={loading}
|
||||||
>▶</button>
|
>▶</button>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
className={styles.next}
|
className={styles.next}
|
||||||
onClick={() => onAction("nextPhase")}
|
onClick={() => onAction("nextPhase")}
|
||||||
disabled={loading}
|
disabled={loading}
|
||||||
>⏭</button>
|
>⏭</button>
|
||||||
|
|
||||||
<button
|
<button
|
||||||
className={styles.restartExperiment}
|
className={styles.restartExperiment}
|
||||||
onClick={onReset}
|
onClick={onReset}
|
||||||
disabled={loading}
|
disabled={loading}
|
||||||
>⟲</button>
|
>⟲</button>
|
||||||
|
|
||||||
|
<button
|
||||||
|
className={styles.stop}
|
||||||
|
onClick={() => onAction("stop")}
|
||||||
|
disabled={loading}
|
||||||
|
>⏹</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
@@ -353,11 +363,11 @@ const MonitoringPage: React.FC = () => {
|
|||||||
<PhaseProgressBar phaseIds={phaseIds} phaseIndex={phaseIndex} isFinished={isFinished} />
|
<PhaseProgressBar phaseIds={phaseIds} phaseIndex={phaseIndex} isFinished={isFinished} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ControlPanel
|
<ControlPanel
|
||||||
loading={loading}
|
loading={loading}
|
||||||
isPlaying={isPlaying}
|
isPlaying={isPlaying}
|
||||||
onAction={handleControlAction}
|
onAction={handleControlAction}
|
||||||
onReset={resetExperiment}
|
onReset={resetExperiment}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className={styles.connectionStatus}>
|
<div className={styles.connectionStatus}>
|
||||||
@@ -370,17 +380,17 @@ const MonitoringPage: React.FC = () => {
|
|||||||
<section className={styles.phaseOverviewText}>
|
<section className={styles.phaseOverviewText}>
|
||||||
<h3>Phase Overview</h3>
|
<h3>Phase Overview</h3>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
{isFinished ? (
|
{isFinished ? (
|
||||||
<div className={styles.finishedMessage}>
|
<div className={styles.finishedMessage}>
|
||||||
<p>All phases have been successfully completed.</p>
|
<p>All phases have been successfully completed.</p>
|
||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<PhaseDashboard
|
<PhaseDashboard
|
||||||
phaseId={phaseIds[phaseIndex]}
|
phaseId={phaseIds[phaseIndex]}
|
||||||
activeIds={activeIds}
|
activeIds={activeIds}
|
||||||
setActiveIds={setActiveIds}
|
setActiveIds={setActiveIds}
|
||||||
goalIndex={goalIndex}
|
goalIndex={goalIndex}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
</main>
|
</main>
|
||||||
@@ -398,4 +408,4 @@ const MonitoringPage: React.FC = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MonitoringPage;
|
export default MonitoringPage;
|
||||||
|
|||||||
@@ -32,6 +32,12 @@ export async function nextPhase(): Promise<void> {
|
|||||||
sendAPICall(type, context)
|
sendAPICall(type, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function stopExperiment(): Promise<void> {
|
||||||
|
const type = "stop"
|
||||||
|
const context = ""
|
||||||
|
sendAPICall(type, context)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends an API call to the CB for going to pause experiment
|
* Sends an API call to the CB for going to pause experiment
|
||||||
@@ -95,14 +101,14 @@ export function useExperimentLogger(onUpdate?: (data: ExperimentStreamData) => v
|
|||||||
console.log("Closing Experiment Stream...");
|
console.log("Closing Experiment Stream...");
|
||||||
eventSource.close();
|
eventSource.close();
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A hook that listens to the status stream that updates active conditional norms
|
* A hook that listens to the status stream that updates active conditional norms
|
||||||
* via updates sent from the backend
|
* via updates sent from the backend
|
||||||
*/
|
*/
|
||||||
export function useStatusLogger(onUpdate?: (data: ExperimentStreamData) => void) {
|
export function useStatusLogger(onUpdate?: (data: ExperimentStreamData) => void) {
|
||||||
const callbackRef = React.useRef(onUpdate);
|
const callbackRef = React.useRef(onUpdate);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user