Skip to content

Commit 5fc5b51

Browse files
committed
subscribe in one go
1 parent 40c9f43 commit 5fc5b51

File tree

3 files changed

+38
-62
lines changed

3 files changed

+38
-62
lines changed

app/components/Instrument.test.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RC_INRANGE,
88
SP_RBV,
99
storePrecision,
10-
subscribeToBlockPVs,
10+
getExtraPVsForBlock,
1111
toPrecision,
1212
yesToBoolean,
1313
} from "@/app/components/Instrument";
@@ -18,8 +18,6 @@ import {
1818
IfcGroup,
1919
IfcPV,
2020
IfcPVWSMessage,
21-
IfcPVWSRequest,
22-
PVWSRequestType,
2321
} from "@/app/types";
2422

2523
test("findPVinDashboard finds a pv in the dashboard and returns it", () => {
@@ -109,20 +107,6 @@ test("findPVInGroups returns a block when it finds one", () => {
109107
findPVInGroups(groups, prefix, prefix + CSSB + blockName);
110108
});
111109

112-
test("subscribeToBlockPVs subscribes to all run control PVs", () => {
113-
const mockSendJsonMessage = jest.fn();
114-
const aBlock = "INST:CS:SB:SomeBlock";
115-
subscribeToBlockPVs(mockSendJsonMessage, aBlock);
116-
expect(mockSendJsonMessage.mock.calls.length).toBe(1);
117-
const expectedCall: IfcPVWSRequest = {
118-
type: PVWSRequestType.subscribe,
119-
pvs: [aBlock, aBlock + RC_ENABLE, aBlock + RC_INRANGE, aBlock + SP_RBV],
120-
};
121-
expect(JSON.stringify(mockSendJsonMessage.mock.calls[0][0])).toBe(
122-
JSON.stringify(expectedCall),
123-
);
124-
});
125-
126110
test("toPrecision does nothing to string value ", () => {
127111
const expectedValue = "untouched";
128112
const aBlock: IfcBlock = {
@@ -165,8 +149,6 @@ test("yesToBoolean works with NO as value", () => {
165149
test("getGroupsWithBlocksFromConfigOutput gets blocks from blockserver groups", () => {
166150
const blockNameToTest = "aBlock";
167151
const groupNameToTest = "aGroup";
168-
const prefix = "TESTING";
169-
const mockSendJsonMessage = jest.fn();
170152

171153
const configOutput: ConfigOutput = {
172154
groups: [{ blocks: [blockNameToTest], name: groupNameToTest }],
@@ -200,20 +182,14 @@ test("getGroupsWithBlocksFromConfigOutput gets blocks from blockserver groups",
200182
};
201183
const groups = getGroupsWithBlocksFromConfigOutput(
202184
configOutput,
203-
mockSendJsonMessage,
204-
prefix,
205185
);
206186
expect(groups[0].name).toBe(groupNameToTest);
207187
expect(groups[0].blocks[0].human_readable_name).toBe(blockNameToTest);
208188
});
209189

210190
test("subscribeToBlockPVs subscribes to blocks, their run control and their SP:RBV PVs", () => {
211-
const mockSendJsonMessage = jest.fn();
212191
const blockAddress = "IN:INST:CS:SB:Block1";
213-
subscribeToBlockPVs(mockSendJsonMessage, blockAddress);
214-
expect(mockSendJsonMessage).toBeCalledTimes(1);
215-
const call: Array<string> = mockSendJsonMessage.mock.calls[0][0].pvs;
216-
expect(call).toEqual(
192+
expect(getExtraPVsForBlock( blockAddress)).toEqual(
217193
expect.arrayContaining([
218194
blockAddress,
219195
blockAddress + RC_ENABLE,

app/components/Instrument.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,34 +211,33 @@ export function toPrecision(
211211
: pvVal;
212212
}
213213

214-
export function storePrecision(updatedPV: IfcPVWSMessage, block: IfcBlock) {
214+
export function storePrecision(
215+
updatedPV: IfcPVWSMessage,
216+
block: IfcBlock,
217+
): void {
215218
const prec = updatedPV.precision;
216219
if (prec != null && prec > 0 && !block.precision) {
217220
// this is likely the first update, and contains precision information which is not repeated on a normal value update - store this in the block for later truncation (see below)
218221
block.precision = prec;
219222
}
220223
}
221224

222-
export function yesToBoolean(pvVal: string | number) {
225+
export function yesToBoolean(pvVal: string | number): boolean {
223226
return pvVal == "YES";
224227
}
225228

226-
export function subscribeToBlockPVs(
227-
sendJsonMessage: (a: IfcPVWSRequest) => void,
229+
export function getExtraPVsForBlock(
228230
block_address: string,
229-
) {
231+
): Array<string> {
230232
/**
231-
* Subscribes to a block and its associated run control PVs
233+
* Given a block name, give the run control and sp_rbv PVs.
232234
*/
233-
sendJsonMessage({
234-
type: PVWSRequestType.subscribe,
235-
pvs: [
235+
return [
236236
block_address,
237237
block_address + RC_ENABLE,
238238
block_address + RC_INRANGE,
239239
block_address + SP_RBV,
240-
],
241-
});
240+
];
242241
}
243242

244243
/**
@@ -247,17 +246,15 @@ export function subscribeToBlockPVs(
247246
*/
248247
export function getGroupsWithBlocksFromConfigOutput(
249248
configOutput: ConfigOutput,
250-
sendJsonMessage: (a: IfcPVWSRequest) => void,
251-
prefix: string,
252249
): Array<IfcGroup> {
253-
const groups = configOutput.groups;
250+
const configOutputGroups = configOutput.groups;
254251
let newGroups: Array<IfcGroup> = [];
255-
for (const group of groups) {
256-
const groupName = group.name;
252+
for (const configOutputGroup of configOutputGroups) {
253+
const groupName = configOutputGroup.name;
257254
let blocks: Array<IfcBlock> = [];
258-
for (const block of group.blocks) {
255+
for (const configOutputBlock of configOutputGroup.blocks) {
259256
const newBlock = configOutput.blocks.find(
260-
(b: ConfigOutputBlock) => b.name === block,
257+
(b: ConfigOutputBlock) => b.name === configOutputBlock,
261258
);
262259
if (newBlock) {
263260
blocks.push({
@@ -267,8 +264,6 @@ export function getGroupsWithBlocksFromConfigOutput(
267264
high_rc: newBlock.highlimit,
268265
visible: newBlock.visible,
269266
});
270-
const fullyQualifiedBlockPVAddress = prefix + CSSB + newBlock.name;
271-
subscribeToBlockPVs(sendJsonMessage, fullyQualifiedBlockPVAddress);
272267
}
273268
}
274269
newGroups.push({

app/components/InstrumentData.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"use client";
22
import { useEffect, useState } from "react";
3-
import { IfcPVWSMessage, IfcPVWSRequest, PVWSRequestType } from "@/app/types";
3+
import {IfcBlock, IfcGroup, IfcPV, IfcPVWSMessage, IfcPVWSRequest, PVWSRequestType} from "@/app/types";
44
import {
5+
CSSB,
56
findPVInDashboard,
67
findPVInGroups,
78
getGroupsWithBlocksFromConfigOutput,
@@ -10,6 +11,7 @@ import {
1011
RC_INRANGE,
1112
SP_RBV,
1213
storePrecision,
14+
getExtraPVsForBlock,
1315
toPrecision,
1416
yesToBoolean,
1517
} from "@/app/components/Instrument";
@@ -69,25 +71,18 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
6971
const updatedPVbytes: string | null | undefined = updatedPV.b64byt;
7072

7173
if (updatedPVName == instListPV && updatedPVbytes != null) {
72-
const instlist = instListFromBytes(updatedPVbytes);
73-
const prefix = getPrefix(instlist, instName);
74+
const prefix = getPrefix(instListFromBytes(updatedPVbytes), instName);
7475
const instrument = new Instrument(prefix);
7576
setCurrentInstrument(instrument);
7677

78+
// subscribe to dashboard and run info PVs
7779
sendJsonMessage({
7880
type: PVWSRequestType.subscribe,
79-
pvs: [`${prefix}${CONFIG_DETAILS}`],
81+
pvs: instrument.runInfoPVs
82+
.concat(instrument.dashboard.flat(3))
83+
.map((v: IfcPV) => v.pvaddress)
84+
.concat([`${prefix}${CONFIG_DETAILS}`]),
8085
});
81-
82-
// subscribe to dashboard and run info PVs
83-
for (const pv of instrument.runInfoPVs.concat(
84-
instrument.dashboard.flat(3),
85-
)) {
86-
sendJsonMessage({
87-
type: PVWSRequestType.subscribe,
88-
pvs: [pv.pvaddress],
89-
});
90-
}
9186
return;
9287
}
9388

@@ -108,9 +103,19 @@ export function InstrumentData({ instrumentName }: { instrumentName: string }) {
108103
const res = dehex_and_decompress(atob(updatedPVbytes));
109104
currentInstrument.groups = getGroupsWithBlocksFromConfigOutput(
110105
JSON.parse(res),
111-
sendJsonMessage,
112-
currentInstrument.prefix,
113106
);
107+
108+
sendJsonMessage({
109+
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
117+
});
118+
114119
} else {
115120
const pvVal = getPvValue(updatedPV);
116121

0 commit comments

Comments
 (0)