Skip to content

Commit

Permalink
Merge pull request #169 from dls-controls/macros
Browse files Browse the repository at this point in the history
Complete macro refactor.
  • Loading branch information
willrogers committed May 12, 2020
2 parents d083707 + fdaa0b5 commit be9617b
Show file tree
Hide file tree
Showing 41 changed files with 577 additions and 234 deletions.
8 changes: 6 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
"plugins": ["react", "@typescript-eslint", "prettier", "unicorn"],
"rules": {
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-empty-function": ["error", { "allow": ["arrowFunctions"] }],
"@typescript-eslint/no-empty-function": [
"error",
{ "allow": ["arrowFunctions"] }
],
"import/no-cycle": "error",
"prettier/prettier": [
"error",
{
"singleQuote": false
"singleQuote": false,
"trailingComma": "none"
}
],
"unicorn/filename-case": [
Expand Down
2 changes: 1 addition & 1 deletion public/json/beamline.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"width": "100%",
"backgroundColor": "#cccccc",
"overflow": "auto",
"macroMap": {
"macros": {
"motor": "1"
},
"children": [
Expand Down
2 changes: 1 addition & 1 deletion public/json/digitelmpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"margin": "10px",
"file": "digitelMpc.opi",
"filetype": "opi",
"macroMap": {
"macros": {
"device": "pva://TEST",
"pv_name": "${pvName}",
"pv_value": "${pvValue}"
Expand Down
2 changes: 1 addition & 1 deletion public/json/digitelmpcionp.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"margin": "10px",
"file": "digitelMpcIonp2spCore.opi",
"defaultProtocol": "ca",
"macroMap": {
"macros": {
"device": "SR03S-VA-IONP-03",
"pv_name": "${pvName}",
"pv_value": "${pvValue}"
Expand Down
40 changes: 21 additions & 19 deletions public/json/mobileDemo.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"outExp": false,
"pvs": [
{
"pvName": "loc://pv1",
"pvName": "loc://pv1(0)",
"trigger": true
}
],
Expand All @@ -54,7 +54,7 @@
"position": "relative",
"width": "45%",
"margin": "2%",
"macroMap": {
"macros": {
"COLOUR": "purple"
},
"color": "${COLOUR}",
Expand All @@ -73,7 +73,7 @@
"position": "relative",
"width": "45%",
"margin": "2%",
"pvName": "loc://pv2"
"pvName": "loc://pv2(1)"
},
{
"type": "readback",
Expand Down Expand Up @@ -247,37 +247,39 @@
"position": "relative",
"children": [
{
"type": "input",
"type": "label",
"position": "relative",
"width": "45%",
"margin": "2%",
"macroMap": {
"COLOUR": "purple"
},
"color": "${COLOUR}",
"pvName": "loc://pv1"
"margin": "1%",
"transparent": true,
"text": "Choose PV to show:"
},
{
"type": "readback",
"type": "menumux",
"position": "relative",
"width": "45%",
"margin": "2%",
"pvName": "loc://pv1",
"tooltip": "Custom tooltip:\n${pvName}\n${pvValue}"
"margin": "1%",
"values": {
"loc://pv1": "1",
"loc://pv2": "2"
},
"symbol": "suffix",
"tooltip": "This menumux widget changes symbol \"${symbol}\""
},
{
"type": "input",
"type": "label",
"position": "relative",
"width": "45%",
"margin": "2%",
"pvName": "loc://pv2"
"margin": "1%",
"transparent": true,
"text": "SelectedPV: loc://pv${suffix}"
},
{
"type": "readback",
"position": "relative",
"width": "45%",
"margin": "2%",
"pvName": "loc://pv2"
"margin": "1%",
"pvName": "loc://pv${suffix}"
}
]
}
Expand Down
20 changes: 10 additions & 10 deletions public/json/modernMachineStatus.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "I02",
"GAP": "SR02I-MO-SERVC-01:CURRGAPD",
"FIELD": "SR02I-MO-SERVC-01:FIELD",
Expand All @@ -157,7 +157,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "J02",
"GAP": "SR02J-MO-SERVC-01:CURRGAPD",
"FIELD": "SR02J-MO-SERVC-01:FIELD",
Expand All @@ -174,7 +174,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "I03",
"GAP": "SR03I-MO-SERVC-01:CURRGAPD",
"FIELD": "SR03I-MO-SERVC-01:FIELD",
Expand All @@ -191,7 +191,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "I04",
"GAP": "SR04I-MO-SERVC-01:CURRGAPD",
"FIELD": "SR04I-MO-SERVC-01:FIELD",
Expand All @@ -208,7 +208,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "J04",
"GAP": "SR04J-MO-SERVC-01:CURRGAPD",
"FIELD": "SR044-MO-SERVC-01:FIELD",
Expand All @@ -225,7 +225,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "I05",
"GAP": "SR06I-MO-SERVC-01:CURRGAPD",
"FIELD": "SR06I-MO-SERVC-01:FIELD",
Expand All @@ -242,7 +242,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "I06",
"GAP": "SR06I-MO-SERVC-01:CURRGAPD",
"FIELD": "SR06I-MO-SERVC-01:FIELD",
Expand All @@ -259,7 +259,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "I07",
"GAP": "SR07I-MO-SERVC-01:CURRGAPD",
"FIELD": "SR07I-MO-SERVC-01:FIELD",
Expand All @@ -276,7 +276,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "I08",
"GAP": "SR08I-MO-SERVC-01:CURRGAPD",
"FIELD": "SR08I-MO-SERVC-01:FIELD",
Expand All @@ -293,7 +293,7 @@
"filetype": "json",
"defaultProtocol": "ca",
"margin": "5% 0 0 0",
"macroMap": {
"macros": {
"Beamline": "I09",
"GAP": "SR09I-MO-SERVC-01:CURRGAPD",
"FIELD": "SR09I-MO-SERVC-01:FIELD",
Expand Down
6 changes: 3 additions & 3 deletions public/json/motorSummary.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"file": "motor.json",
"filetype": "json",
"defaultProtocol": "ca",
"macroMap": {
"macros": {
"motor": "1"
}
},
Expand All @@ -20,7 +20,7 @@
"file": "motor.json",
"filetype": "json",
"defaultProtocol": "ca",
"macroMap": {
"macros": {
"motor": "2"
}
},
Expand All @@ -31,7 +31,7 @@
"file": "motor.json",
"filetype": "json",
"defaultProtocol": "ca",
"macroMap": {
"macros": {
"motor": "3"
}
}
Expand Down
2 changes: 1 addition & 1 deletion public/json/rffb.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"margin": "10px",
"file": "rffb.opi",
"defaultProtocol": "ca",
"macroMap": {
"macros": {
"device": "SR03S-VA-IONP-03",
"pv_name": "${pvName}",
"pv_value": "${pvValue}"
Expand Down
12 changes: 1 addition & 11 deletions src/redux/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const UNSUBSCRIBE = "unsubscribe";
export const VALUE_CHANGED = "value_changed";
export const VALUES_CHANGED = "values_changed";
export const WRITE_PV = "write_pv";
export const MACRO_UPDATED = "macro_updated";

/* The never type in the constructor ensures that TypeScript
won't allow this error to be created. This is useful in
Expand Down Expand Up @@ -65,19 +64,10 @@ export interface WritePv {
};
}

export interface MacroUpdated {
type: typeof MACRO_UPDATED;
payload: {
key: string;
value: string;
};
}

export type Action =
| ConnectionChanged
| Subscribe
| Unsubscribe
| ValueChanged
| ValuesChanged
| WritePv
| MacroUpdated;
| WritePv;
2 changes: 1 addition & 1 deletion src/redux/csState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const initialState: CsState = {
initializingPvName: ""
}
},
macroMap: {},
globalMacros: {},
subscriptions: {},
effectivePvNameMap: {}
};
Expand Down
16 changes: 3 additions & 13 deletions src/redux/csState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import {
SUBSCRIBE,
WRITE_PV,
CONNECTION_CHANGED,
MACRO_UPDATED,
UNSUBSCRIBE,
ValueChanged
} from "./actions";
import { VType } from "../types/vtypes/vtypes";
import { mergeVtype } from "../types/vtypes/merge";
import { MacroMap } from "../types/macros";

const initialState: CsState = {
valueCache: {},
macroMap: { SUFFIX: "1" },
globalMacros: { SUFFIX: "1" },
effectivePvNameMap: {},
subscriptions: {}
};
Expand All @@ -34,11 +34,6 @@ export interface ValueCache {
[key: string]: FullPvState;
}

/* A simple dictionary from key to value. */
export interface MacroMap {
[key: string]: string;
}

export interface Subscriptions {
[pv: string]: string[];
}
Expand All @@ -47,7 +42,7 @@ export interface Subscriptions {
export interface CsState {
valueCache: ValueCache;
effectivePvNameMap: { [pvName: string]: string };
macroMap: MacroMap;
globalMacros: MacroMap;
subscriptions: Subscriptions;
}

Expand Down Expand Up @@ -97,11 +92,6 @@ export function csReducer(state = initialState, action: Action): CsState {
newValueCache[action.payload.pvName] = newPvState;
return { ...state, valueCache: newValueCache };
}
case MACRO_UPDATED: {
const newMacroMap: MacroMap = { ...state.macroMap };
newMacroMap[action.payload.key] = action.payload.value;
return { ...state, macroMap: newMacroMap };
}
case SUBSCRIBE: {
const { componentId, effectivePvName } = action.payload;
const newEffectivePvMap = { ...state.effectivePvNameMap };
Expand Down
3 changes: 1 addition & 2 deletions src/types/macros.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint no-template-curly-in-string: 0 */
import { MacroMap } from "../redux/csState";
import { resolveMacros } from "./macros";
import { MacroMap, resolveMacros } from "./macros";

const macroMap: MacroMap = {
A: "B",
Expand Down
26 changes: 24 additions & 2 deletions src/types/macros.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import { MacroMap } from "../redux/csState";
import React from "react";

/* A simple dictionary from key to value. */
export interface MacroMap {
[key: string]: string;
}

/* The MacroContext, used for providing macros to children of
container widgets.
Descendants can update the macros of the nearest provider using
the updateMacro() function.
*/
export interface MacroContextType {
macros: MacroMap;
updateMacro: (key: string, value: string) => void;
}
export const MacroContext = React.createContext<MacroContextType>({
macros: {},
updateMacro: (key: string, value: string) => {}
});

function interpolate(
str: string,
Expand All @@ -20,7 +40,9 @@ function interpolate(
substitutions[missing] = "${" + missing + "}";
}

return str.replace(/\$[{(](.*?)[})]/g, (x, g): string => substitutions[g]);
return str.replace(/\$[{(](.*?)[})]/g, (x, g): string =>
substitutions[g].toString()
);
}

export function resolveMacros(
Expand Down
Loading

0 comments on commit be9617b

Please sign in to comment.