@@ -5,10 +5,11 @@ import {
55 Button ,
66 Snackbar ,
77 SnackbarCloseReason ,
8- styled ,
98 Tooltip ,
109 Typography ,
1110} from "@mui/material" ;
11+ import { forceString , ReadPvRawValue } from "../pv/util" ;
12+ import { RawValue } from "../pv/types" ;
1213
1314type SeverityLevel = "success" | "info" | "warning" | "error" ;
1415type VariantChoice = "outlined" | "contained" ;
@@ -36,27 +37,77 @@ type RunPlanButtonProps = {
3637// This will be another PR
3738// See https://github.com/DiamondLightSource/mx-daq-ui/issues/71
3839
40+ /**
41+ * Read the full visit path from the visit PV set by the beamline staff.
42+ * @returns {string } the full visit pV /dls/i24/data/{year}/{visit}
43+ */
44+ export function readVisitFromPv ( ) : string {
45+ const fullVisitPath : RawValue = ReadPvRawValue ( {
46+ label : "visit" ,
47+ pv : "ca://BL24I-MO-IOC-13:GP100" ,
48+ } ) ;
49+ const visitString : string = forceString ( fullVisitPath ) ;
50+ return visitString ;
51+ }
52+
53+ /**
54+ * Parse the full visit path and return only the instrument session.
55+ * An error will be raised if the instrument session value is undefined or
56+ * if the PV is not connected.
57+ * @param {string } visit The full visit path
58+ * @returns {string } Only the instrument session part of the visit path
59+ */
60+ export function parseInstrumentSession ( visit : string ) : string {
61+ let instrumentSession : string | undefined ;
62+ if ( visit === "not connected" || visit === "undefined" ) {
63+ const msg =
64+ "Unable to run plan as instrument session not set. Please check visit PV." ;
65+ throw new Error ( msg ) ;
66+ } else {
67+ instrumentSession = visit . split ( "/" ) . filter ( Boolean ) . at ( - 1 ) ;
68+ if ( ! instrumentSession ) {
69+ throw new Error (
70+ "Unable to run plan as something appears to be wrong with visit path"
71+ ) ;
72+ }
73+ }
74+ return instrumentSession ;
75+ }
76+
3977export function RunPlanButton ( props : RunPlanButtonProps ) {
4078 const [ openSnackbar , setOpenSnackbar ] = React . useState < boolean > ( false ) ;
4179 const [ msg , setMsg ] = React . useState < string > ( "Running plan..." ) ;
4280 const [ severity , setSeverity ] = React . useState < SeverityLevel > ( "info" ) ;
4381
82+ const fullVisit = readVisitFromPv ( ) ;
83+ let instrumentSession : string ;
84+
4485 const params = props . planParams ? props . planParams : { } ;
4586 const variant = props . btnVariant ? props . btnVariant : "outlined" ;
4687 const size = props . btnSize ? props . btnSize : "medium" ;
4788
4889 const handleClick = ( ) => {
4990 setOpenSnackbar ( true ) ;
50- submitAndRunPlanImmediately ( {
51- planName : props . planName ,
52- planParams : params ,
53- } ) . catch ( ( error ) => {
91+ try {
92+ instrumentSession = parseInstrumentSession ( fullVisit ) ;
93+ submitAndRunPlanImmediately ( {
94+ planName : props . planName ,
95+ planParams : params ,
96+ instrumentSession : instrumentSession ,
97+ } ) . catch ( ( error ) => {
98+ setSeverity ( "error" ) ;
99+ setMsg (
100+ `Failed to run plan ${ props . planName } , see console and logs for full error`
101+ ) ;
102+ console . log ( `${ msg } . Reason: ${ error } ` ) ;
103+ } ) ;
104+ } catch ( error ) {
54105 setSeverity ( "error" ) ;
55106 setMsg (
56- `Failed to run plan ${ props . planName } , see console and logs for full error `
107+ `Failed to run plan ${ props . planName } , please check visit PV is set. `
57108 ) ;
58- console . log ( `${ msg } . Reason: ${ error } ` ) ;
59- } ) ;
109+ console . log ( `An error occurred ${ error } ` ) ;
110+ }
60111 } ;
61112
62113 const handleSnackbarClose = (
0 commit comments