diff --git a/src/bin/BinConditioningInteraction.tsx b/src/bin/BinConditioningInteraction.tsx index c088bdd..7f5ec13 100644 --- a/src/bin/BinConditioningInteraction.tsx +++ b/src/bin/BinConditioningInteraction.tsx @@ -94,7 +94,7 @@ export default function BinConditioningInteraction(props: Props) { const [sliderMarks, setSliderMarks] = useState>(new Map()); //this is the emc value calculated from the interactions temp and humidity conditions const [baseEMC, setBaseEMC] = useState(); - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); const classes = useStyles(); const interactionAPI = useInteractionsAPI(); const { openSnack } = useSnackbar(); @@ -144,7 +144,7 @@ export default function BinConditioningInteraction(props: Props) { const updateInteraction = () => { interactionAPI - .updateInteraction(deviceId, interaction.settings) + .updateInteraction(deviceId, interaction.settings, as) .then(resp => { openSnack("Updated Interaction Conditions"); }) diff --git a/src/bin/GrainNodeInteractions.tsx b/src/bin/GrainNodeInteractions.tsx index e01cdce..83f9350 100644 --- a/src/bin/GrainNodeInteractions.tsx +++ b/src/bin/GrainNodeInteractions.tsx @@ -108,10 +108,10 @@ export default function GrainNodeInteractions(props: Props) { }, [selectedNode]); useEffect(() => { - interactionAPI.listInteractionsByComponent(device.id(), cable.location()).then(resp => { + interactionAPI.listInteractionsByComponent(device.id(), cable.location(), undefined, as).then(resp => { setCableInteractions(resp); }); - }, [cable, device, interactionAPI]); + }, [cable, device, interactionAPI, as]); useEffect(() => { //do the reversing to make the selected node match the array in the @@ -163,7 +163,7 @@ export default function GrainNodeInteractions(props: Props) { //TODO-CS: make function to update multiple interactions at once to avoid this loop if it becomes a problem cableInteractions.forEach(interaction => { interactionAPI - .updateInteraction(device.id(), interaction.settings) + .updateInteraction(device.id(), interaction.settings, as) .then(resp => {}) .catch(err => {}); }); @@ -359,7 +359,7 @@ export default function GrainNodeInteractions(props: Props) { permissions={permissions} refreshCallback={() => { interactionAPI - .listInteractionsByComponent(device.id(), cable.location()) + .listInteractionsByComponent(device.id(), cable.location(), undefined, as) .then(resp => { setCableInteractions(resp); }); @@ -440,7 +440,7 @@ export default function GrainNodeInteractions(props: Props) { setNewInteraction(false); }} refreshCallback={() => { - interactionAPI.listInteractionsByComponent(device.id(), cable.location()).then(resp => { + interactionAPI.listInteractionsByComponent(device.id(), cable.location(), undefined, as).then(resp => { setCableInteractions(resp); }); }} diff --git a/src/device/DevicePresetsFromPicker.tsx b/src/device/DevicePresetsFromPicker.tsx index 04ce3a3..768e7a0 100644 --- a/src/device/DevicePresetsFromPicker.tsx +++ b/src/device/DevicePresetsFromPicker.tsx @@ -36,6 +36,7 @@ import CableTopNodeSummary from "bin/CableTopNodeSummary"; import { getTemperatureUnit } from "utils"; import { DevicePreset } from "models/DevicePreset"; import { Ambient } from "models/Ambient"; +import { useGlobalState } from "providers"; /*const useStyles = makeStyles((theme: Theme) => createStyles({ @@ -181,6 +182,7 @@ export default function DevicePresets(props: DevicePresetsProps) { const [heaters, setHeaters] = useState([]); const [fans, setFans] = useState([]); //const [subscribeBinToAutoTopNode, setSubscribeBinToAutoTopNode] = useState(false); + const [{as}] = useGlobalState(); useEffect(() => { if (deviceComponents.get(deviceIndex.toString())) return; @@ -190,7 +192,7 @@ export default function DevicePresets(props: DevicePresetsProps) { if (!comps) { comps = []; setLoading(true); - let interactionPromise = interactionAPI.listInteractionsByDevice(deviceIndex); + let interactionPromise = interactionAPI.listInteractionsByDevice(deviceIndex, undefined, as); let componentPromise = componentAPI.list(deviceIndex, undefined, [binKey], ["bin"], true); let dComponents = new Map(); Promise.all([componentPromise, interactionPromise]) @@ -215,7 +217,7 @@ export default function DevicePresets(props: DevicePresetsProps) { setLoading(false); }); } - }, [deviceIndex, binKey, componentAPI, interactionAPI, deviceComponents]); + }, [deviceIndex, binKey, componentAPI, interactionAPI, deviceComponents, as]); useEffect(() => { if (loading) return; @@ -604,14 +606,15 @@ export default function DevicePresets(props: DevicePresetsProps) { return conflicting; }); let deleteConflictingInteractions = conflictingInteractions.map(i => { - return interactionAPI.removeInteraction(deviceIndex, i.key()); + return interactionAPI.removeInteraction(deviceIndex, i.key(), as); }); Promise.all([...deleteConflictingInteractions]).finally(() => { if (preset === "shop") { if (heater) { interactionAPI.addInteraction( deviceIndex, - buildHeatingInteraction(preset, sensor, heater, false, "greater") + buildHeatingInteraction(preset, sensor, heater, false, "greater"), + as ); } } @@ -620,7 +623,8 @@ export default function DevicePresets(props: DevicePresetsProps) { interactionAPI .addInteraction( deviceIndex, - buildFanInteraction(preset, sensor, fan, "less", "greater") + buildFanInteraction(preset, sensor, fan, "less", "greater"), + as ) .then(() => { setConfirmPreset(null); @@ -664,7 +668,7 @@ export default function DevicePresets(props: DevicePresetsProps) { } if (multi.interactions.length > 0) { interactionAPI - .addMultiInteractions(deviceIndex, multi) + .addMultiInteractions(deviceIndex, multi, as) .then(resp => { updateControllers(); setConfirmPreset(null); diff --git a/src/device/DeviceViewer.tsx b/src/device/DeviceViewer.tsx index 830ab02..e228968 100644 --- a/src/device/DeviceViewer.tsx +++ b/src/device/DeviceViewer.tsx @@ -68,7 +68,7 @@ interface Props { export default function Device(props: Props) { const classes = useStyles(); - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); const { error } = useSnackbar(); const deviceID = props.device.id(); const deviceAPI = useDeviceAPI(); @@ -106,7 +106,7 @@ export default function Device(props: Props) { ["device"], true ); - let interactionsPromise = interactionsAPI.listInteractionsByDevice(deviceID); + let interactionsPromise = interactionsAPI.listInteractionsByDevice(deviceID, undefined, as); let groupPromise: Promise = Promise.resolve(undefined); Promise.all([userPromise, componentsPromise, interactionsPromise, groupPromise]) .then(([userRes, componentsRes, interactionsRes]) => { @@ -165,7 +165,7 @@ export default function Device(props: Props) { // } // }) // .catch(err => {}); - }, [componentAPI, props.device, deviceID, error, interactionsAPI, /*usageAPI*/, userAPI, user]); + }, [componentAPI, props.device, deviceID, error, interactionsAPI, /*usageAPI*/, userAPI, user, as]); useEffect(() => { load(); @@ -365,7 +365,7 @@ export default function Device(props: Props) { device={device} isPaused={isPaused()} components={getOrderedComponents()} - //interactions={interactions} + interactions={interactions} availablePositions={availablePositions} availableOffsets={availableOffsets} permissions={permissions} diff --git a/src/gate/GateDeviceInteraction.tsx b/src/gate/GateDeviceInteraction.tsx index 2b170b7..ca05a58 100644 --- a/src/gate/GateDeviceInteraction.tsx +++ b/src/gate/GateDeviceInteraction.tsx @@ -36,7 +36,7 @@ const densityMap = new Map([ //once we have numbers for presets it may never be used again export default function GateDeviceInteraction(props: Props) { const { open, close, gate, compDevice, densityTemp } = props; - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); const [lowDelta, setLowDelta] = useState(0); const [highDelta, setHighDelta] = useState(0); const [greenComponent, setGreenComponent] = useState(); @@ -207,7 +207,7 @@ export default function GateDeviceInteraction(props: Props) { interactions: [greenToggle, redToggle] }); interactionsAPI - .addMultiInteractions(deviceID, multi) + .addMultiInteractions(deviceID, multi, as) .then(resp => { openSnack("Interactions added"); }) diff --git a/src/interactions/InteractionSettings.tsx b/src/interactions/InteractionSettings.tsx index 82fa450..40b9132 100644 --- a/src/interactions/InteractionSettings.tsx +++ b/src/interactions/InteractionSettings.tsx @@ -134,7 +134,7 @@ export default function InteractionSettings(props: Props) { const prevComponents = usePrevious(components); const prevInitialComponent = usePrevious(initialComponent); const classes = useStyles(); - const [{ user }] = useGlobalState(); + const [{ user, as }] = useGlobalState(); const interactionsAPI = useInteractionsAPI(); const [interaction, setInteraction] = useState( Interaction.clone(initialInteraction) @@ -307,7 +307,7 @@ export default function InteractionSettings(props: Props) { switch (mode) { case "add": interactionsAPI - .addInteraction(Number(device.settings.deviceId), settings) + .addInteraction(Number(device.settings.deviceId), settings, as) .then(() => { success("Interaction was successfully created"); refreshCallback(); @@ -320,7 +320,7 @@ export default function InteractionSettings(props: Props) { let s = pond.InteractionPondSettings.create(); s.sortPriority = interaction.settings.sortPriority; interactionsAPI - .updateInteractionPondSettings(Number(device.settings.deviceId), interaction.key(), s) + .updateInteractionPondSettings(Number(device.settings.deviceId), interaction.key(), s, as) .then(() => { success("Interaction pond settings were successfully updated"); refreshCallback(); @@ -329,7 +329,7 @@ export default function InteractionSettings(props: Props) { .finally(() => close()); } else { interactionsAPI - .updateInteraction(Number(device.settings.deviceId), settings) + .updateInteraction(Number(device.settings.deviceId), settings, as) .then(() => { success("Interaction was successfully updated"); refreshCallback(); diff --git a/src/interactions/InteractionsOverview.tsx b/src/interactions/InteractionsOverview.tsx index e51b1cc..127f2a6 100644 --- a/src/interactions/InteractionsOverview.tsx +++ b/src/interactions/InteractionsOverview.tsx @@ -35,6 +35,7 @@ import { quack } from "protobuf-ts/quack"; import React, { useEffect, useState } from "react"; import InteractionSettings from "./InteractionSettings"; import { makeStyles } from "@mui/styles"; +import { useGlobalState } from "providers"; const useStyles = makeStyles((theme: Theme) => { return ({ @@ -70,6 +71,7 @@ interface Props { export default function InteractionsOverview(props: Props) { const classes = useStyles(); + const [{as}] = useGlobalState(); const interactionsAPI = useInteractionsAPI(); const { success, error } = useSnackbar(); const { device, component, components, permissions, refreshCallback } = props; @@ -200,7 +202,7 @@ export default function InteractionsOverview(props: Props) { settings: pond.IInteractionSettings ) => { interactionsAPI - .updateInteraction(Number(deviceID), settings) + .updateInteraction(Number(deviceID), settings, as) .then((_response: any) => { let updatedDirtyInteractions = cloneDeep(dirtyInteractions); updatedDirtyInteractions.set(index, false); diff --git a/src/interactions/RemoveInteraction.tsx b/src/interactions/RemoveInteraction.tsx index acfa841..b6a771e 100644 --- a/src/interactions/RemoveInteraction.tsx +++ b/src/interactions/RemoveInteraction.tsx @@ -8,6 +8,7 @@ import { } from "@mui/material"; import { useInteractionsAPI, useSnackbar } from "hooks"; import { Device, Interaction } from "models"; +import { useGlobalState } from "providers"; interface Props { device: Device; @@ -19,6 +20,7 @@ interface Props { export default function RemoveInteraction(props: Props) { const { device, interaction, isDialogOpen, closeDialogCallback, refreshCallback } = props; + const [{as}] = useGlobalState(); const interactionsAPI = useInteractionsAPI(); const { success, error, warning } = useSnackbar(); @@ -28,7 +30,7 @@ export default function RemoveInteraction(props: Props) { const submit = () => { interactionsAPI - .removeInteraction(device.id(), interaction.key()) + .removeInteraction(device.id(), interaction.key(), as) .then((response: any) => { success("Interaction was successfully removed"); closeDialog(); diff --git a/src/objects/ObjectAlerts.tsx b/src/objects/ObjectAlerts.tsx index 933fde3..ef443dc 100644 --- a/src/objects/ObjectAlerts.tsx +++ b/src/objects/ObjectAlerts.tsx @@ -154,7 +154,7 @@ export default function ObjectAlerts(props: Props) { //get the interactions for the component let device = componentDevices.get(key); if (device !== undefined) { - await interactionsAPI.listInteractionsByComponent(device, comp.location()).then(resp => { + await interactionsAPI.listInteractionsByComponent(device, comp.location(), undefined, as).then(resp => { if (resp.length > 0) { resp.forEach(interaction => { icMap.set(interaction.key(), key); @@ -366,7 +366,7 @@ export default function ObjectAlerts(props: Props) { notify: true }); interactionsAPI - .addInteractionToComponents(compIds, newAlert) + .addInteractionToComponents(compIds, newAlert, as) .then(_resp => { openSnack("interaction added to selected components"); }) diff --git a/src/pages/Bins.tsx b/src/pages/Bins.tsx index f64264c..5043b96 100644 --- a/src/pages/Bins.tsx +++ b/src/pages/Bins.tsx @@ -540,7 +540,7 @@ export default function Bins(props: Props) { }); }); interactionsAPI - .setAlertInteractions(alertList) + .setAlertInteractions(alertList, as) .then(resp => { openSnack("Successfully added " + resp.data.numAlertsSet + " alerts"); }) diff --git a/src/pages/DeviceComponent.tsx b/src/pages/DeviceComponent.tsx index 70015e1..c98f148 100644 --- a/src/pages/DeviceComponent.tsx +++ b/src/pages/DeviceComponent.tsx @@ -131,12 +131,11 @@ export default function DeviceComponent() { let isInteractionSettingsOpen = JSON.stringify(or(selectedInteraction, getDefaultInteraction())) !== JSON.stringify(getDefaultInteraction()); - const [{ user, as }] = useGlobalState(); + const [{ user, as, showErrors }] = useGlobalState(); const [unitMeasurements, setUnitMeasurements] = useState([]); const [rm, setRM] = useState(pond.Measurement.create()); const [recentUnitMeasurement, setRecentUnitMeasurement] = useState([]); const [recentGoodUnitMeasurement, setRecentGoodUnitMeasurement] = useState([]); - const [{ showErrors }] = useGlobalState(); const sampleLimit = 200; const [deviceComponentPrefs, setDeviceComponentPrefs] = useState< pond.DeviceComponentPreferences @@ -193,7 +192,7 @@ export default function DeviceComponent() { useEffect(() => { if (component.key() !== "") { interactionsAPI - .listInteractionsByComponent(deviceID, component.location()) + .listInteractionsByComponent(deviceID, component.location(), undefined, as) .then((rInteractions: Interaction[]) => { setInteractions(rInteractions); }) diff --git a/src/providers/pond/interactionsAPI.tsx b/src/providers/pond/interactionsAPI.tsx index 16e7293..c9413da 100644 --- a/src/providers/pond/interactionsAPI.tsx +++ b/src/providers/pond/interactionsAPI.tsx @@ -10,27 +10,31 @@ import { has } from "utils/types"; import { pondURL } from "./pond"; export interface IInteractionsAPIContext { - addInteraction: (device: number, settings: pond.IInteractionSettings) => Promise>; - addMultiInteractions: (device: number, settings: pond.MultiInteractionSettings) => Promise>; + addInteraction: (device: number, settings: pond.IInteractionSettings, as?: string) => Promise>; + addMultiInteractions: (device: number, settings: pond.MultiInteractionSettings, as?: string) => Promise>; addInteractionToComponents: ( fullComponentLocations: string[], - settings: pond.IInteractionSettings + settings: pond.IInteractionSettings, + as?: string ) => Promise>; - updateInteraction: (device: number, settings: pond.IInteractionSettings) => Promise>; + updateInteraction: (device: number, settings: pond.IInteractionSettings, as?: string) => Promise>; updateInteractionPondSettings: ( device: number, interaction: string, - settings: pond.IInteractionPondSettings + settings: pond.IInteractionPondSettings, + as?: string ) => Promise>; - removeInteraction: (device: number, interaction: string) => Promise>; - listInteractionsByDevice: (device: number | string, demo?: boolean) => Promise; + removeInteraction: (device: number, interaction: string, as?: string) => Promise>; + listInteractionsByDevice: (device: number | string, demo?: boolean, as?: string) => Promise; listInteractionsByComponent: ( device: number, component: quack.ComponentID, - demo?: boolean + demo?: boolean, + as?: string ) => Promise; setAlertInteractions: ( - alerts: pond.AlertData[] + alerts: pond.AlertData[], + as?: string ) => Promise>; } @@ -56,9 +60,9 @@ function sanitizeInteraction(interaction: pond.IInteractionSettings): pond.IInte export default function InteractionProvider(props: PropsWithChildren) { const { children } = props; const { get, post, put, del } = useHTTP(); - const [{ as }] = useGlobalState(); + //const [{ as }] = useGlobalState(); - const addInteraction = (device: number, settings: pond.IInteractionSettings) => { + const addInteraction = (device: number, settings: pond.IInteractionSettings, as?: string) => { return new Promise>((resolve, reject) => { post( pondURL("/devices/" + device + "/interactions" + (as ? "?as=" + as : "")), @@ -80,7 +84,8 @@ export default function InteractionProvider(props: PropsWithChildren) { */ const addInteractionToComponents = ( fullComponentLocations: string[], - settings: pond.IInteractionSettings + settings: pond.IInteractionSettings, + as?: string ) => { let url = pondURL( "/interactions/forComponents/" + @@ -98,7 +103,7 @@ export default function InteractionProvider(props: PropsWithChildren) { }) }; - const addMultiInteractions = (device: number, settings: pond.MultiInteractionSettings) => { + const addMultiInteractions = (device: number, settings: pond.MultiInteractionSettings, as?: string) => { let url = pondURL("/devices/" + device + "/interactions/multi" + (as ? "?as=" + as : "")) return new Promise>((resolve, reject) => { post(url,settings).then(resp => { @@ -110,7 +115,7 @@ export default function InteractionProvider(props: PropsWithChildren) { }) }; - const updateInteraction = (device: number, settings: pond.IInteractionSettings) => { + const updateInteraction = (device: number, settings: pond.IInteractionSettings, as?: string) => { let url = pondURL( "/devices/" + device + @@ -133,7 +138,8 @@ export default function InteractionProvider(props: PropsWithChildren) { const updateInteractionPondSettings = ( device: number, interaction: string, - settings: pond.IInteractionPondSettings + settings: pond.IInteractionPondSettings, + as?: string ) => { let url = pondURL( "/devices/" + @@ -156,7 +162,7 @@ export default function InteractionProvider(props: PropsWithChildren) { }) }; - const removeInteraction = (device: number, interaction: string) => { + const removeInteraction = (device: number, interaction: string, as?: string) => { let url = pondURL("/devices/" + device + "/interactions/" + interaction + (as ? "?as=" + as : "")) return new Promise>((resolve, reject) => { del(url).then(resp => { @@ -171,7 +177,8 @@ export default function InteractionProvider(props: PropsWithChildren) { const listInteractionsByDevice = ( device: number | string, - demo: boolean = false + demo: boolean = false, + as?: string ): Promise => { return new Promise((resolve, reject) => { get(pondURL("/devices/" + device + "/interactions" + (as ? "?as=" + as : ""), demo)) @@ -209,7 +216,8 @@ export default function InteractionProvider(props: PropsWithChildren) { const listInteractionsByComponent = ( device: number, component: quack.ComponentID, - demo: boolean = false + demo: boolean = false, + as?: string ): Promise => { return new Promise((resolve, reject) => { get( @@ -255,7 +263,8 @@ export default function InteractionProvider(props: PropsWithChildren) { }; const setAlertInteractions = ( - alerts: pond.AlertData[] + alerts: pond.AlertData[], + as?: string ): Promise> => { let url = pondURL("/interactions/setAlerts" + (as ? "?as=" + as : "")) return new Promise>((resolve, reject) => {