Skip to content
Draft
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
6 changes: 6 additions & 0 deletions apps/i15-1/public/pvwsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"storeMode": "PROD",
"PVWS_SOCKET": "pvws.diamond.ac.uk",
"PVWS_SSL": true,
"THROTTLE_PERIOD": 100
}
4 changes: 4 additions & 0 deletions apps/i15-1/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import relay from "vite-plugin-relay";
export default defineConfig({
plugins: [react(), relay],
define: {
"process.env": {
VITE_PVWS_SOCKET: "pvws.diamond.ac.uk",
VITE_PVWS_SSL: "true",
},
global: {},
},
});
11 changes: 11 additions & 0 deletions packages/pvws-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `@atlas/pvws-config`

## Installation

To install it to an application `@atlas/myapp`:

```
pnpm add @atlas/pvws-config --filter myapp --workspace
```

## Usage
22 changes: 22 additions & 0 deletions packages/pvws-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@atlas/pvws-config",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
".": "./src/index.ts"
},
"scripts": {
"test": "vitest run",
"coverage": "vitest run --coverage"
},
"dependencies": {
"@diamondlightsource/cs-web-lib": "0.9.5",
"@mui/material": "<7.0.0",
"react-error-boundary": "^6.0.0"
},
"devDependencies": {
"@atlas/vitest-conf": "workspace:*",
"vitest": "*"
}
}
15 changes: 15 additions & 0 deletions packages/pvws-config/src/components/PvwsFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { TextField } from "@mui/material";
import type { ReactNode } from "react";

export function PvwsFallback(): ReactNode {
return (
<TextField
color="error"
slotProps={{
input: { readOnly: true },
}}
>
<b>Error Connecting!</b>
</TextField>
);
}
27 changes: 27 additions & 0 deletions packages/pvws-config/src/components/ReadOnlyPv.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Box, TextField } from "@mui/material";
import { ErrorBoundary } from "react-error-boundary";
import type { ParsePvProps } from "../types";
import { useParsedPvConnection } from "../useParsedPvConnection";
import { PvwsFallback } from "./PvwsFallback";

function PvComponent(props: ParsePvProps): JSX.Element {
const latestValue = useParsedPvConnection(props);
return (
<TextField
size="medium"
slotProps={{
input: { readOnly: true },
}}
>
<b>{props.label}:</b> {latestValue}
</TextField>
);
}

export function ReadOnlyPv(props: ParsePvProps) {
return (
<ErrorBoundary fallback={<PvwsFallback />}>
{PvComponent(props)}
</ErrorBoundary>
);
}
4 changes: 4 additions & 0 deletions packages/pvws-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from "./loadConfiguration";
export * from "./types";
export * from "./useParsedPvConnection";
export * from "./components/ReadOnlyPv";
27 changes: 27 additions & 0 deletions packages/pvws-config/src/loadConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { type CsWebLibConfig } from "@diamondlightsource/cs-web-lib";

const configFile = "/pvwsconfig.json";
const defaultConfig = {
storeMode: "PROD",
PVWS_SOCKET: "pvws.diamond.ac.uk",
PVWS_SSL: true,
THROTTLE_PERIOD: 100,
};

export const loadPvwsConfig = async (): Promise<CsWebLibConfig> => {
let config;
if (config) {
return config;
}
try {
// Point towards your file location
const response = await fetch(configFile);
config = await response.json();
} catch (error) {
console.warn("Configuration not found falling back to defaults", error);
// Set defaults here if necessary
config = defaultConfig;
}

return config as CsWebLibConfig;
};
60 changes: 60 additions & 0 deletions packages/pvws-config/src/readPv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { useConnection, type DType } from "@diamondlightsource/cs-web-lib";
import { type RawValue } from "./types";

export function ReadPvRawValue({
label,
pv,
}: {
label: string;
pv: string;
}): RawValue {
const [_effectivePvName, connected, _readonly, latestValue] = useConnection(
label,
pv,
);
const rawValue: RawValue = connected ? latestValue : "not connected";
return rawValue;
}

export function parseStringPv(value: RawValue | string | number): string {
let displayValue: string;
if (value != "not connected" && value != undefined) {
const stringVal =
typeof (value as DType)["getStringValue"] === "function"
? (value as DType).getStringValue()
: value;
displayValue = stringVal ? stringVal.toString() : "undefined";
} else if (value === "not connected") {
displayValue = "not connected";
} else {
displayValue = "undefined";
}
return displayValue;
}

export function parseNumericPV(
value: RawValue | string | number,
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 as DType)["getStringValue"] === "function") {
const numValue = (value as DType).getDoubleValue();
if (!numValue) {
displayValue = "undefined";
} else {
displayValue = (numValue * scaleToUse).toFixed(decimalsToUse);
}
} else {
displayValue = value.toString();
}
} else if (value === "not connected") {
displayValue = "not connected";
} else {
displayValue = "undefined";
}
return displayValue;
}
18 changes: 18 additions & 0 deletions packages/pvws-config/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DType } from "@diamondlightsource/cs-web-lib";

type NotConnected = "not connected";
export type RawValue = DType | undefined | NotConnected;
// NOTE - ACHTUNG! DTYPE not exported anymore in newer versions
// Problem is that useConnection returns latest value as Dtype so we need it
// waiting for an answer on this, in the meantime sticking to 0.9.5

export type PvDescription = {
label: string;
pv: string;
};

export type ParsePvProps = PvDescription & {
parseNumeric?: boolean;
decimals?: number;
scaleFactor?: number;
};
30 changes: 30 additions & 0 deletions packages/pvws-config/src/useParsedPvConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { parseNumericPV, parseStringPv, ReadPvRawValue } from "./readPv";
import type { ParsePvProps } from "./types";

export function useParsedPvConnection(props: ParsePvProps): number | string {
const rawValue = ReadPvRawValue({ label: props.label, pv: props.pv });
const numericPv = props.parseNumeric ? props.parseNumeric : false;
let returnValue;
if (rawValue === "not connected") {
returnValue = "not connected";
} else if (!rawValue) {
console.error("Parsed value was undefined");
returnValue = "undefined";
} else {
if (numericPv) {
returnValue = parseNumericPV(rawValue, props.decimals, props.scaleFactor);
} else {
returnValue = parseStringPv(rawValue);
}
}

// If it's a Dtype cast to string
if (typeof returnValue !== "string" || typeof returnValue !== "number") {
returnValue = returnValue.toString();
}

console.log(
`fetched parsed value ${returnValue} for PV: ${props.pv} labeled ${props.label}`,
);
return returnValue;
}
10 changes: 10 additions & 0 deletions packages/pvws-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"allowImportingTsExtensions": false,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"include": ["src"],
"exclude": ["node_modules", "dist", "**/*.test.ts", "**/*.spec.ts"]
}
8 changes: 8 additions & 0 deletions packages/pvws-config/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true,
"types": ["@atlas/vitest-conf/global-types"]
},
"include": ["src/**/*.test.ts"]
}
6 changes: 6 additions & 0 deletions packages/pvws-config/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "vitest/config";
import baseConfig from "@atlas/vitest-conf/vitest.config";

export default defineConfig({
...baseConfig,
});
Loading
Loading