Skip to content

Commit 1ed10b5

Browse files
committed
tidy up some variables and refactor out some instlist parsing logic to try and keep the instruments page as small as possible
1 parent d2df494 commit 1ed10b5

File tree

12 files changed

+676
-137
lines changed

12 files changed

+676
-137
lines changed

app/commonVars.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { IfcPVWSRequest, PVWSRequestType } from "@/app/types";
2+
3+
export const instListPV = "CS:INSTLIST";
4+
export const socketURL =
5+
process.env.NEXT_PUBLIC_WS_URL || "ws://localhost:8080/pvws/pv";
6+
7+
export const instListSubscription: IfcPVWSRequest = {
8+
type: PVWSRequestType.subscribe,
9+
pvs: [instListPV],
10+
};

app/components/InstList.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

app/components/InstrumentPage.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { ConfigOutput, IfcBlock, IfcPVWSRequest } from "@/app/types";
1+
import {
2+
ConfigOutput,
3+
IfcBlock,
4+
IfcPVWSRequest,
5+
PVWSRequestType,
6+
} from "@/app/types";
27
import {
38
getGroupsWithBlocksFromConfigOutput,
49
RC_ENABLE,
@@ -14,7 +19,7 @@ test("subscribeToBlockPVs subscribes to all run control PVs", () => {
1419
subscribeToBlockPVs(mockSendJsonMessage, aBlock);
1520
expect(mockSendJsonMessage.mock.calls.length).toBe(1);
1621
const expectedCall: IfcPVWSRequest = {
17-
type: "subscribe",
22+
type: PVWSRequestType.subscribe,
1823
pvs: [aBlock, aBlock + RC_ENABLE, aBlock + RC_INRANGE, aBlock + SP_RBV],
1924
};
2025
expect(JSON.stringify(mockSendJsonMessage.mock.calls[0][0])).toBe(

app/components/InstrumentPage.tsx

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@ import React, { useEffect, useState } from "react";
33
import TopBar from "./TopBar";
44
import Groups from "./Groups";
55
import useWebSocket from "react-use-websocket";
6-
import { dehex_and_decompress } from "./dehex_and_decompress";
6+
import {
7+
dehex_and_decompress,
8+
instListFromBytes,
9+
} from "./dehex_and_decompress";
710
import { findPVInDashboard, Instrument } from "./Instrument";
811
import { useSearchParams } from "next/navigation";
912
import {
1013
ConfigOutput,
1114
ConfigOutputBlock,
1215
IfcBlock,
1316
IfcGroup,
14-
IfcPV,
1517
IfcPVWSMessage,
1618
IfcPVWSRequest,
19+
PVWSRequestType,
1720
} from "@/app/types";
1821
import {
19-
findPVByAddress,
2022
ExponentialOnThresholdFormat,
23+
findPVByAddress,
2124
} from "@/app/components/PVutils";
2225
import CheckToggle from "@/app/components/CheckToggle";
26+
import { instListPV, instListSubscription, socketURL } from "@/app/commonVars";
2327

2428
let lastUpdate: string = "";
2529

@@ -46,7 +50,7 @@ export function subscribeToBlockPVs(
4650
* Subscribes to a block and its associated run control PVs
4751
*/
4852
sendJsonMessage({
49-
type: "subscribe",
53+
type: PVWSRequestType.subscribe,
5054
pvs: [
5155
block_address,
5256
block_address + RC_ENABLE,
@@ -101,15 +105,12 @@ export function toPrecision(
101105

102106
function InstrumentData({ instrumentName }: { instrumentName: string }) {
103107
const [showHiddenBlocks, setShowHiddenBlocks] = useState(false);
104-
const [showSetpoints, setShowSetpoints] = useState(false);
105-
const [showTimestamps, setShowTimestamps] = useState(false);
106108
const CONFIG_DETAILS = "CS:BLOCKSERVER:GET_CURR_CONFIG_DETAILS";
107109
const [instlist, setInstlist] = useState<Array<any> | null>(null);
108110
const [currentInstrument, setCurrentInstrument] = useState<Instrument | null>(
109111
null,
110112
);
111-
const socketURL =
112-
process.env.NEXT_PUBLIC_WS_URL || "ws://localhost:8080/pvws/pv";
113+
113114
const instName = instrumentName;
114115

115116
useEffect(() => {
@@ -130,10 +131,7 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
130131

131132
useEffect(() => {
132133
// This is an initial useEffect to subscribe to lots of PVs including the instlist.
133-
sendJsonMessage({
134-
type: "subscribe",
135-
pvs: ["CS:INSTLIST"],
136-
});
134+
sendJsonMessage(instListSubscription);
137135

138136
if (instName == "" || instName == null || instlist == null) {
139137
return;
@@ -156,15 +154,18 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
156154
setCurrentInstrument(instrument);
157155

158156
sendJsonMessage({
159-
type: "subscribe",
157+
type: PVWSRequestType.subscribe,
160158
pvs: [`${prefix}${CONFIG_DETAILS}`],
161159
});
162160

163161
// subscribe to dashboard and run info PVs
164162
for (const pv of instrument.runInfoPVs.concat(
165163
instrument.dashboard.flat(3),
166164
)) {
167-
sendJsonMessage({ type: "subscribe", pvs: [pv.pvaddress] });
165+
sendJsonMessage({
166+
type: PVWSRequestType.subscribe,
167+
pvs: [pv.pvaddress],
168+
});
168169
}
169170
}
170171
}, [instlist, instName, sendJsonMessage, currentInstrument]);
@@ -178,11 +179,8 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
178179
const updatedPVName: string = updatedPV.pv;
179180
const updatedPVbytes: string | null | undefined = updatedPV.b64byt;
180181

181-
if (updatedPVName == "CS:INSTLIST" && updatedPVbytes != null) {
182-
const dehexedInstList = dehex_and_decompress(atob(updatedPVbytes));
183-
if (dehexedInstList != null && typeof dehexedInstList == "string") {
184-
setInstlist(JSON.parse(dehexedInstList));
185-
}
182+
if (updatedPVName == instListPV && updatedPVbytes != null) {
183+
setInstlist(instListFromBytes(updatedPVbytes));
186184
}
187185

188186
if (!currentInstrument) {
@@ -200,9 +198,6 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
200198
}
201199
lastUpdate = updatedPVbytes;
202200
const res = dehex_and_decompress(atob(updatedPVbytes));
203-
if (res == null || typeof res != "string") {
204-
return;
205-
}
206201
currentInstrument.groups = getGroupsWithBlocksFromConfigOutput(
207202
JSON.parse(res),
208203
sendJsonMessage,

0 commit comments

Comments
 (0)