Skip to content

Commit 5a8dd8b

Browse files
committed
refactor PV list getting out to function, add test for it
1 parent 5fc5b51 commit 5a8dd8b

File tree

3 files changed

+77
-27
lines changed

3 files changed

+77
-27
lines changed

app/components/Instrument.test.ts

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
getExtraPVsForBlock,
1111
toPrecision,
1212
yesToBoolean,
13+
getAllBlockPVs,
14+
Instrument,
1315
} from "@/app/components/Instrument";
1416
import {
1517
ConfigOutput,
@@ -180,16 +182,14 @@ test("getGroupsWithBlocksFromConfigOutput gets blocks from blockserver groups",
180182
component_iocs: [],
181183
history: [],
182184
};
183-
const groups = getGroupsWithBlocksFromConfigOutput(
184-
configOutput,
185-
);
185+
const groups = getGroupsWithBlocksFromConfigOutput(configOutput);
186186
expect(groups[0].name).toBe(groupNameToTest);
187187
expect(groups[0].blocks[0].human_readable_name).toBe(blockNameToTest);
188188
});
189189

190190
test("subscribeToBlockPVs subscribes to blocks, their run control and their SP:RBV PVs", () => {
191191
const blockAddress = "IN:INST:CS:SB:Block1";
192-
expect(getExtraPVsForBlock( blockAddress)).toEqual(
192+
expect(getExtraPVsForBlock(blockAddress)).toEqual(
193193
expect.arrayContaining([
194194
blockAddress,
195195
blockAddress + RC_ENABLE,
@@ -210,3 +210,49 @@ test("storePrecision adds precision to a block if it is the first update", () =>
210210
storePrecision(message, blockWithoutPrecision);
211211
expect(blockWithoutPrecision.precision).toEqual(precision);
212212
});
213+
214+
test("getAllBlockPVs returns flat list of blocks, their RC and SPRBV pvs", () => {
215+
const block1Name = "blockName";
216+
const block2Name = "block2Name";
217+
const prefix = "IN:TEST:";
218+
219+
let inst = new Instrument(prefix);
220+
221+
inst.groups = [
222+
{
223+
name: "aGroup",
224+
blocks: [
225+
{
226+
human_readable_name: block1Name,
227+
pvaddress: "some:underlying:pv:name",
228+
},
229+
],
230+
},
231+
{
232+
name: "aDifferentGroup",
233+
blocks: [
234+
{
235+
human_readable_name: block2Name,
236+
pvaddress: "someother:underlying:pv:name",
237+
},
238+
],
239+
},
240+
];
241+
242+
const res = getAllBlockPVs(inst);
243+
244+
expect(res.length).toBe(2 * 4); // 2 blocks, which means 4 PVs to subscribe to
245+
246+
expect(res).toEqual(
247+
expect.arrayContaining([
248+
prefix + CSSB + block1Name,
249+
prefix + CSSB + block1Name + RC_ENABLE,
250+
prefix + CSSB + block1Name + RC_INRANGE,
251+
prefix + CSSB + block1Name + SP_RBV,
252+
prefix + CSSB + block2Name,
253+
prefix + CSSB + block2Name + RC_ENABLE,
254+
prefix + CSSB + block2Name + RC_INRANGE,
255+
prefix + CSSB + block2Name + SP_RBV,
256+
]),
257+
);
258+
});

app/components/Instrument.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {
66
IfcGroup,
77
IfcPV,
88
IfcPVWSMessage,
9-
IfcPVWSRequest,
10-
PVWSRequestType,
119
} from "@/app/types";
1210
import {
1311
ExponentialOnThresholdFormat,
@@ -226,18 +224,16 @@ export function yesToBoolean(pvVal: string | number): boolean {
226224
return pvVal == "YES";
227225
}
228226

229-
export function getExtraPVsForBlock(
230-
block_address: string,
231-
): Array<string> {
227+
export function getExtraPVsForBlock(block_address: string): Array<string> {
232228
/**
233229
* Given a block name, give the run control and sp_rbv PVs.
234230
*/
235231
return [
236-
block_address,
237-
block_address + RC_ENABLE,
238-
block_address + RC_INRANGE,
239-
block_address + SP_RBV,
240-
];
232+
block_address,
233+
block_address + RC_ENABLE,
234+
block_address + RC_INRANGE,
235+
block_address + SP_RBV,
236+
];
241237
}
242238

243239
/**
@@ -286,3 +282,15 @@ export function findPVInGroups(
286282
updatedPVName == prefix + CSSB + block.human_readable_name,
287283
);
288284
}
285+
286+
export function getAllBlockPVs(currentInstrument: Instrument): Array<string> {
287+
return currentInstrument.groups
288+
.map((g: IfcGroup) => g.blocks)
289+
.flat(1) // flatten to a big array of blocks
290+
.map((b: IfcBlock) =>
291+
getExtraPVsForBlock(
292+
currentInstrument.prefix + CSSB + b.human_readable_name,
293+
),
294+
)
295+
.flat(1); //flatten block, rc, sp_rbv pvs for every block to a 1d array
296+
}

app/components/InstrumentData.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
"use client";
22
import { useEffect, useState } from "react";
3-
import {IfcBlock, IfcGroup, IfcPV, IfcPVWSMessage, IfcPVWSRequest, PVWSRequestType} from "@/app/types";
43
import {
5-
CSSB,
4+
IfcPV,
5+
IfcPVWSMessage,
6+
IfcPVWSRequest,
7+
PVWSRequestType,
8+
} from "@/app/types";
9+
import {
610
findPVInDashboard,
711
findPVInGroups,
12+
getAllBlockPVs,
813
getGroupsWithBlocksFromConfigOutput,
914
Instrument,
1015
RC_ENABLE,
1116
RC_INRANGE,
1217
SP_RBV,
1318
storePrecision,
14-
getExtraPVsForBlock,
1519
toPrecision,
1620
yesToBoolean,
1721
} from "@/app/components/Instrument";
@@ -100,22 +104,14 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
100104
return;
101105
}
102106
setLastUpdate(updatedPVbytes);
103-
const res = dehex_and_decompress(atob(updatedPVbytes));
104107
currentInstrument.groups = getGroupsWithBlocksFromConfigOutput(
105-
JSON.parse(res),
108+
JSON.parse(dehex_and_decompress(atob(updatedPVbytes))),
106109
);
107110

108111
sendJsonMessage({
109112
type: PVWSRequestType.subscribe,
110-
pvs: currentInstrument.groups
111-
.map((g: IfcGroup) => g.blocks)
112-
.flat(1) // flatten to a big array of blocks
113-
.map((b: IfcBlock) =>
114-
getExtraPVsForBlock(
115-
currentInstrument.prefix + CSSB + b.human_readable_name
116-
)).flat(1), //flatten block, rc, sp_rbv pvs for every block to a 1d array
113+
pvs: getAllBlockPVs(currentInstrument),
117114
});
118-
119115
} else {
120116
const pvVal = getPvValue(updatedPV);
121117

0 commit comments

Comments
 (0)