Skip to content

Commit 103cccf

Browse files
committed
truncate SPs with precision and add tests for this
1 parent bd7404c commit 103cccf

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

app/components/InstrumentPage.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { ConfigOutput, IfcPVWSRequest } from "@/app/types";
1+
import { ConfigOutput, IfcBlock, IfcPVWSRequest } from "@/app/types";
22
import {
33
getGroupsWithBlocksFromConfigOutput,
44
RC_ENABLE,
55
RC_INRANGE,
66
SP,
77
subscribeToBlockPVs,
8+
toPrecision,
89
} from "@/app/components/InstrumentPage";
910

1011
test("subscribeToBlockPVs subscribes to all run control PVs", () => {
@@ -21,6 +22,37 @@ test("subscribeToBlockPVs subscribes to all run control PVs", () => {
2122
);
2223
});
2324

25+
test("toPrecision does nothing to string value ", () => {
26+
const expectedValue = "untouched";
27+
const aBlock: IfcBlock = {
28+
pvaddress: "SOME:BLOCK:STR",
29+
value: expectedValue,
30+
};
31+
expect(toPrecision(aBlock, expectedValue)).toBe(expectedValue);
32+
});
33+
34+
test("toPrecision does nothing to block without pv ", () => {
35+
const expectedValue = 0.00123456;
36+
const aBlock: IfcBlock = {
37+
pvaddress: "SOME:BLOCK:STR",
38+
value: expectedValue,
39+
};
40+
expect(toPrecision(aBlock, expectedValue)).toBe(expectedValue);
41+
});
42+
43+
test("toPrecision truncates block if it has precision", () => {
44+
const originalValue = 0.00123456;
45+
const precision = 3;
46+
const aBlock: IfcBlock = {
47+
pvaddress: "SOME:BLOCK:STR",
48+
value: originalValue,
49+
precision: precision,
50+
};
51+
expect(toPrecision(aBlock, originalValue)).toBe(
52+
originalValue.toPrecision(precision),
53+
);
54+
});
55+
2456
test("getGroupsWithBlocksFromConfigOutput gets blocks from blockserver groups", () => {
2557
const blockNameToTest = "aBlock";
2658
const groupNameToTest = "aGroup";

app/components/InstrumentPage.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ export function getGroupsWithBlocksFromConfigOutput(
8686
return newGroups;
8787
}
8888

89+
export function toPrecision(
90+
block: IfcBlock,
91+
pvVal: number | string,
92+
): string | number {
93+
return block.precision && typeof pvVal == "number"
94+
? pvVal.toPrecision(block.precision)
95+
: pvVal;
96+
}
97+
8998
function InstrumentData({ instrumentName }: { instrumentName: string }) {
9099
// set up the different states for the instrument data
91100

@@ -232,12 +241,9 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
232241
// 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)
233242
block.precision = prec;
234243
}
235-
if (block.precision && typeof pvVal == "number") {
236-
// if a block has precision truncate it here
237-
block.value = pvVal.toPrecision(block.precision);
238-
} else {
239-
block.value = pvVal;
240-
}
244+
// if a block has precision truncate it here
245+
block.value = toPrecision(block, pvVal);
246+
241247
if (updatedPV.units) block.units = updatedPV.units;
242248
if (updatedPV.severity) block.severity = updatedPV.severity;
243249
} else if (updatedPVName == block_full_pv_name + RC_INRANGE) {
@@ -247,7 +253,7 @@ function InstrumentData({ instrumentName }: { instrumentName: string }) {
247253
block.runcontrol_enabled = updatedPV.value == 1;
248254
return;
249255
} else if (updatedPVName == block_full_pv_name + SP) {
250-
block.sp_value = pvVal;
256+
block.sp_value = toPrecision(block, pvVal);
251257
return;
252258
}
253259
}

0 commit comments

Comments
 (0)