Skip to content

Commit 43ad5ad

Browse files
committed
use callbacks instead of useeffects
1 parent ca33294 commit 43ad5ad

File tree

7 files changed

+170
-165
lines changed

7 files changed

+170
-165
lines changed

app/components/InstrumentData.tsx

Lines changed: 94 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ import Groups from "@/app/components/Groups";
3636
export function InstrumentData({ instrumentName }: { instrumentName: string }) {
3737
const [showHiddenBlocks, setShowHiddenBlocks] = useState(false);
3838
const CONFIG_DETAILS = "CS:BLOCKSERVER:WD_CONF_DETAILS";
39-
const [instlist, setInstlist] = useState<instList | null>(null);
4039
const [currentInstrument, setCurrentInstrument] = useState<Instrument | null>(
4140
null,
4241
);
4342
const [lastUpdate, setLastUpdate] = useState<string>("");
43+
const [webSockErr, setWebSockErr] = useState("");
4444

4545
const instName = instrumentName;
4646

@@ -58,133 +58,120 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
5858
lastJsonMessage: IfcPVWSMessage;
5959
} = useWebSocket(socketURL, {
6060
shouldReconnect: (closeEvent) => true,
61-
});
62-
63-
useEffect(() => {
64-
// This is an initial useEffect to subscribe to lots of PVs including the instlist.
65-
sendJsonMessage(instListSubscription);
66-
67-
if (instName == "" || instName == null || instlist == null) {
68-
return;
69-
}
70-
71-
let prefix = getPrefix(instlist, instName);
72-
73-
if (!currentInstrument) {
74-
let instrument = new Instrument(prefix);
75-
setCurrentInstrument(instrument);
76-
77-
sendJsonMessage({
78-
type: PVWSRequestType.subscribe,
79-
pvs: [`${prefix}${CONFIG_DETAILS}`],
80-
});
61+
onOpen: () => {
62+
sendJsonMessage(instListSubscription);
63+
},
64+
onMessage: (m) => {
65+
const updatedPV: IfcPVWSMessage = JSON.parse(m.data);
66+
const updatedPVName: string = updatedPV.pv;
67+
const updatedPVbytes: string | null | undefined = updatedPV.b64byt;
68+
69+
if (updatedPVName == instListPV && updatedPVbytes != null) {
70+
const instlist = instListFromBytes(updatedPVbytes);
71+
const prefix = getPrefix(instlist, instName);
72+
let instrument = new Instrument(prefix);
73+
setCurrentInstrument(instrument);
8174

82-
// subscribe to dashboard and run info PVs
83-
for (const pv of instrument.runInfoPVs.concat(
84-
instrument.dashboard.flat(3),
85-
)) {
8675
sendJsonMessage({
8776
type: PVWSRequestType.subscribe,
88-
pvs: [pv.pvaddress],
77+
pvs: [`${prefix}${CONFIG_DETAILS}`],
8978
});
90-
}
91-
}
92-
}, [instlist, instName, sendJsonMessage, currentInstrument]);
93-
94-
useEffect(() => {
95-
// This gets run whenever there is a PV update ie. when lastJsonMessage changes.
96-
if (!lastJsonMessage) {
97-
return;
98-
}
99-
const updatedPV: IfcPVWSMessage = lastJsonMessage;
100-
const updatedPVName: string = updatedPV.pv;
101-
const updatedPVbytes: string | null | undefined = updatedPV.b64byt;
102-
103-
if (updatedPVName == instListPV && updatedPVbytes != null) {
104-
setInstlist(instListFromBytes(updatedPVbytes));
105-
return;
106-
}
10779

108-
if (!currentInstrument) {
109-
return;
110-
}
111-
112-
if (
113-
updatedPVName == `${currentInstrument.prefix}${CONFIG_DETAILS}` &&
114-
updatedPVbytes != null
115-
) {
116-
// config change, reset instrument groups
117-
if (updatedPVbytes == lastUpdate) {
118-
// config hasnt actually changed so do nothing
80+
// subscribe to dashboard and run info PVs
81+
for (const pv of instrument.runInfoPVs.concat(
82+
instrument.dashboard.flat(3),
83+
)) {
84+
sendJsonMessage({
85+
type: PVWSRequestType.subscribe,
86+
pvs: [pv.pvaddress],
87+
});
88+
}
11989
return;
12090
}
121-
setLastUpdate(updatedPVbytes);
122-
const res = dehex_and_decompress(atob(updatedPVbytes));
123-
currentInstrument.groups = getGroupsWithBlocksFromConfigOutput(
124-
JSON.parse(res),
125-
sendJsonMessage,
126-
currentInstrument.prefix,
127-
);
128-
} else {
129-
const pvVal = getPvValue(updatedPV);
13091

131-
if (pvVal == undefined) {
132-
console.debug(`initial/blank message from ${updatedPVName}`);
92+
if (!currentInstrument) {
13393
return;
13494
}
13595

136-
// Check if this is a dashboard, run info, or block PV update.
137-
const pv =
138-
findPVInDashboard(currentInstrument.dashboard, updatedPVName) ||
139-
findPVByAddress(currentInstrument.runInfoPVs, updatedPVName) ||
140-
findPVInGroups(
141-
currentInstrument.groups,
96+
if (
97+
updatedPVName == `${currentInstrument.prefix}${CONFIG_DETAILS}` &&
98+
updatedPVbytes != null
99+
) {
100+
// config change, reset instrument groups
101+
if (updatedPVbytes == lastUpdate) {
102+
// config hasnt actually changed so do nothing
103+
return;
104+
}
105+
setLastUpdate(updatedPVbytes);
106+
const res = dehex_and_decompress(atob(updatedPVbytes));
107+
currentInstrument.groups = getGroupsWithBlocksFromConfigOutput(
108+
JSON.parse(res),
109+
sendJsonMessage,
142110
currentInstrument.prefix,
143-
updatedPVName,
144111
);
145-
if (pv) {
146-
storePrecision(updatedPV, pv);
147-
pv.value = toPrecision(pv, pvVal);
148-
if (updatedPV.seconds) pv.updateSeconds = updatedPV.seconds;
149-
if (updatedPV.units) pv.units = updatedPV.units;
150-
if (updatedPV.severity) pv.severity = updatedPV.severity;
151112
} else {
152-
// OK, we haven't found the block, but we may have an update for
153-
// its object such as its run control status or SP:RBV
154-
if (updatedPVName.endsWith(RC_INRANGE)) {
155-
const underlyingBlock = findPVInGroups(
156-
currentInstrument.groups,
157-
currentInstrument.prefix,
158-
updatedPVName.replace(RC_INRANGE, ""),
159-
);
160-
if (underlyingBlock)
161-
underlyingBlock.runcontrol_inrange = yesToBoolean(pvVal);
162-
} else if (updatedPVName.endsWith(RC_ENABLE)) {
163-
const underlyingBlock = findPVInGroups(
164-
currentInstrument.groups,
165-
currentInstrument.prefix,
166-
updatedPVName.replace(RC_ENABLE, ""),
167-
);
168-
if (underlyingBlock)
169-
underlyingBlock.runcontrol_enabled = yesToBoolean(pvVal);
170-
} else if (updatedPVName.endsWith(SP_RBV)) {
171-
const underlyingBlock = findPVInGroups(
113+
const pvVal = getPvValue(updatedPV);
114+
115+
if (pvVal == undefined) {
116+
console.debug(`initial/blank message from ${updatedPVName}`);
117+
return;
118+
}
119+
120+
// Check if this is a dashboard, run info, or block PV update.
121+
const pv =
122+
findPVInDashboard(currentInstrument.dashboard, updatedPVName) ||
123+
findPVByAddress(currentInstrument.runInfoPVs, updatedPVName) ||
124+
findPVInGroups(
172125
currentInstrument.groups,
173126
currentInstrument.prefix,
174-
updatedPVName.replace(SP_RBV, ""),
127+
updatedPVName,
175128
);
176-
if (underlyingBlock)
177-
underlyingBlock.sp_value = toPrecision(underlyingBlock, pvVal);
129+
if (pv) {
130+
storePrecision(updatedPV, pv);
131+
pv.value = toPrecision(pv, pvVal);
132+
if (updatedPV.seconds) pv.updateSeconds = updatedPV.seconds;
133+
if (updatedPV.units) pv.units = updatedPV.units;
134+
if (updatedPV.severity) pv.severity = updatedPV.severity;
178135
} else {
179-
console.warn(
180-
`update from unknown PV: ${updatedPVName} with value ${pvVal}`,
181-
);
136+
// OK, we haven't found the block, but we may have an update for
137+
// its object such as its run control status or SP:RBV
138+
if (updatedPVName.endsWith(RC_INRANGE)) {
139+
const underlyingBlock = findPVInGroups(
140+
currentInstrument.groups,
141+
currentInstrument.prefix,
142+
updatedPVName.replace(RC_INRANGE, ""),
143+
);
144+
if (underlyingBlock)
145+
underlyingBlock.runcontrol_inrange = yesToBoolean(pvVal);
146+
} else if (updatedPVName.endsWith(RC_ENABLE)) {
147+
const underlyingBlock = findPVInGroups(
148+
currentInstrument.groups,
149+
currentInstrument.prefix,
150+
updatedPVName.replace(RC_ENABLE, ""),
151+
);
152+
if (underlyingBlock)
153+
underlyingBlock.runcontrol_enabled = yesToBoolean(pvVal);
154+
} else if (updatedPVName.endsWith(SP_RBV)) {
155+
const underlyingBlock = findPVInGroups(
156+
currentInstrument.groups,
157+
currentInstrument.prefix,
158+
updatedPVName.replace(SP_RBV, ""),
159+
);
160+
if (underlyingBlock)
161+
underlyingBlock.sp_value = toPrecision(underlyingBlock, pvVal);
162+
} else {
163+
console.warn(
164+
`update from unknown PV: ${updatedPVName} with value ${pvVal}`,
165+
);
166+
}
182167
}
183168
}
184-
}
185-
}, [lastJsonMessage, currentInstrument, sendJsonMessage, lastUpdate]);
169+
},
170+
share: true,
171+
retryOnError: true,
172+
});
186173

187-
if (!instName || !currentInstrument) {
174+
if (!currentInstrument) {
188175
return <h1>Loading...</h1>;
189176
}
190177
return (

app/components/InstrumentWallCard.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import Link from "next/link";
2-
import { getForegroundColour, getStatusColour } from "./getRunstateColours";
2+
import {
3+
getForegroundColour,
4+
getStatusColour,
5+
UNREACHABLE,
6+
} from "./getRunstateColours";
37
import { instListEntryWithRunstatePVandValue } from "@/app/types";
48

59
export default function InstrumentWallCard({
@@ -13,16 +17,16 @@ export default function InstrumentWallCard({
1317
href={"/instrument?name=" + instrument.name}
1418
target="_blank"
1519
className={`flex items-center justify-center text-center py-1 w-28 max-h-12 rounded-lg shadow-sm border-2 border-gray-700 dark:border-gray-200 hover:shadow-lg hover:border-black dark:hover:border-gray-700 transition-all duration-200
16-
${getStatusColour(instrument.runStateValue || "UNKNOWN")} ${getForegroundColour(
17-
instrument.runStateValue || "UNKNOWN",
20+
${getStatusColour(instrument.runStateValue || UNREACHABLE)} ${getForegroundColour(
21+
instrument.runStateValue || UNREACHABLE,
1822
)}`}
1923
>
2024
<div className="flex flex-col">
2125
<span className="text-sm font-bold line-clamp-1 w-full">
2226
{instrument.name}
2327
</span>
2428
<span className="text-xs ">
25-
{instrument.runStateValue || "UNKNOWN"}
29+
{instrument.runStateValue || UNREACHABLE}
2630
</span>
2731
</div>
2832
</Link>

0 commit comments

Comments
 (0)