Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/i15-1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@atlas/blueapi-query": "workspace:*",
"@atlas/blueapi-ui": "workspace:*",
"@atlas/pvws-config": "workspace:*",
"@diamondlightsource/cs-web-lib": "0.10.15",
"@diamondlightsource/cs-web-lib": "0.10.19",
"@diamondlightsource/sci-react-ui": "^0.4.0",
"@mui/icons-material": "^6.5.0",
"@mui/material": "<7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/pvws-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"coverage": "vitest run --coverage"
},
"dependencies": {
"@diamondlightsource/cs-web-lib": "0.10.15",
"@diamondlightsource/cs-web-lib": "0.10.19",
"@mui/material": "<7.0.0",
"react-error-boundary": "^6.0.0"
},
Expand Down
38 changes: 26 additions & 12 deletions packages/pvws-config/src/readPv.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
import { parseNumericPV, parseStringPv, parseByteArrPV } from "./readPv";
import { type DType } from "@diamondlightsource/cs-web-lib";
import { newDType, type NumberArray } from "@diamondlightsource/cs-web-lib";

describe("Test Pv utils", () => {
it("parse numerical value returns string with correct decimals", () => {
expect(parseNumericPV(3.1415926)).toEqual("3.14");
expect(parseNumericPV("1.2", 1, 10)).toEqual("12.0");
expect(parseNumericPV(undefined)).toEqual("undefined");
expect(parseNumericPV("not connected")).toEqual("not connected");
expect(parseNumericPV("undefined")).toEqual("undefined");

const newDType = "302.345678" as unknown as DType;
expect(parseNumericPV(newDType)).toEqual("302.35");
let testNumericDType = newDType({ doubleValue: 302.345678 });
expect(parseNumericPV(testNumericDType)).toEqual("302.35");

let testRoundedDType = newDType({ doubleValue: 3.1415926 });
expect(parseNumericPV(testRoundedDType, 4, 1)).toEqual("3.1416");

let testScaledDType = newDType({ doubleValue: 1.2 });
expect(parseNumericPV(testScaledDType, 1, 10)).toEqual("12.0");
});

it("parse string value", () => {
expect(parseStringPv("not connected")).toEqual("not connected");
expect(parseStringPv(undefined)).toEqual("undefined");
expect(parseStringPv("not connected")).toEqual("not connected");

let testOpenDType = newDType({ stringValue: "Open" });
expect(parseStringPv(testOpenDType)).toEqual("Open");

const newDType = "Open" as unknown as DType;
expect(parseStringPv(newDType)).toEqual("Open");
expect(parseStringPv(10)).toEqual("10");
expect(parseStringPv("Close")).toEqual("Close");
let testCoercedStringDType = newDType({ doubleValue: 10 });
expect(parseStringPv(testCoercedStringDType)).toEqual("10");

let testCloseDType = newDType({ stringValue: "Close" });
expect(parseStringPv(testCloseDType)).toEqual("Close");
});

it("parses byte array values to a string", () => {
expect(parseByteArrPV("not connected")).toEqual("not connected");
expect(parseByteArrPV(undefined)).toEqual("undefined");
expect(parseByteArrPV("not connected")).toEqual("not connected");

let testArray: NumberArray = new Float64Array([
84, 101, 115, 116, 65, 114, 114, 97, 121,
]);
let testArrayDType = newDType({ arrayValue: testArray });
expect(parseByteArrPV(testArrayDType)).toEqual("TestArray");
});
});
59 changes: 18 additions & 41 deletions packages/pvws-config/src/readPv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { useConnection, type DType } from "@diamondlightsource/cs-web-lib";
import {
dTypeGetDoubleValue,
dTypeCoerceString,
useConnection,
dTypeByteArrToString,
dTypeGetArrayValue,
} from "@diamondlightsource/cs-web-lib";
import { type RawValue } from "./types";

export function ReadPvRawValue({
Expand All @@ -16,20 +22,11 @@ export function ReadPvRawValue({
return rawValue;
}

// TODO: Simplify PV parsing functions with newly exposed cs-web-lib utils
// https://github.com/DiamondLightSource/atlas/issues/93

export function parseStringPv(value: RawValue | string | number): string {
export function parseStringPv(value: RawValue): string {
let displayValue: string;
if (value != "not connected" && value != undefined) {
if (typeof value === "string" || typeof value === "number") {
displayValue = value.toString();
} else {
const stringVal = (value as DType).value.stringValue
? (value as DType).value.stringValue
: value;
displayValue = stringVal ? stringVal.toString() : "undefined";
}
const stringVal = dTypeCoerceString(value);
displayValue = stringVal ? stringVal.toString() : "undefined";
} else if (value === "not connected") {
displayValue = "not connected";
} else {
Expand All @@ -47,28 +44,19 @@ function scaleAndApprox(
}

export function parseNumericPV(
value: RawValue | string | number,
value: RawValue,
decimals?: number,
scaleFactor?: number,
): string {
let displayValue: string;
const decimalsToUse = decimals ? decimals : 2;
const scaleToUse = scaleFactor ? scaleFactor : 1;
if (value != "not connected" && value != undefined) {
if (typeof value === "number") {
displayValue = scaleAndApprox(Number(value), decimalsToUse, scaleToUse);
} else if (typeof value === "string") {
displayValue =
value === "undefined"
? "undefined"
: scaleAndApprox(parseFloat(value), decimalsToUse, scaleToUse);
const numValue = dTypeGetDoubleValue(value);
if (!numValue) {
displayValue = "undefined";
} else {
const numValue = (value as DType).value.doubleValue;
if (!numValue) {
displayValue = "undefined";
} else {
displayValue = scaleAndApprox(numValue, decimalsToUse, scaleToUse);
}
displayValue = scaleAndApprox(numValue, decimalsToUse, scaleToUse);
}
} else if (value === "not connected") {
displayValue = "not connected";
Expand All @@ -78,22 +66,11 @@ export function parseNumericPV(
return displayValue;
}

export function parseByteArrPV(value: RawValue | string | number): string {
export function parseByteArrPV(value: RawValue): string {
let displayValue: string;
if (value != "not connected" && value != undefined) {
const arrValue = (value as DType).value.arrayValue;
if (!arrValue) {
displayValue = "undefined";
} else {
let result = "";
for (let i = 0; i < arrValue.length; i++) {
if (Number(arrValue[i]) === 0) {
break;
}
result += String.fromCharCode(Number(arrValue[i]));
}
displayValue = result;
}
const arrValue = dTypeGetArrayValue(value);
displayValue = arrValue ? dTypeByteArrToString(arrValue) : "undefined";
} else if (value == "not connected") {
displayValue = "not connected";
} else {
Expand Down
24 changes: 17 additions & 7 deletions packages/pvws-config/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { defineConfig } from "vitest/config";
import { defineConfig, mergeConfig } from "vitest/config";
import baseConfig from "@atlas/vitest-conf/vitest.config";

export default defineConfig({
...baseConfig,
define: {
"import.meta.env.VITE_PROFILER_ENABLED": JSON.stringify("false"),
},
});
export default mergeConfig(
baseConfig,
defineConfig({
...baseConfig,
define: {
"import.meta.env.VITE_PROFILER_ENABLED": JSON.stringify("false"),
},
test: {
server: {
deps: {
inline: ["@diamondlightsource/cs-web-lib", "react-toastify"],
},
},
},
}),
);
Loading
Loading