updated as in the interactions api

This commit is contained in:
csawatzky 2025-04-21 16:40:30 -06:00
parent e8e32e1886
commit c617ebf868
12 changed files with 66 additions and 50 deletions

View file

@ -94,7 +94,7 @@ export default function BinConditioningInteraction(props: Props) {
const [sliderMarks, setSliderMarks] = useState<Map<quack.MeasurementType, number>>(new Map());
//this is the emc value calculated from the interactions temp and humidity conditions
const [baseEMC, setBaseEMC] = useState<number | undefined>();
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");
})

View file

@ -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);
});
}}

View file

@ -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<Controller[]>([]);
const [fans, setFans] = useState<Component[]>([]);
//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<string, Component[]>();
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);

View file

@ -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<any> = 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}

View file

@ -36,7 +36,7 @@ const densityMap = new Map<number, number>([
//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<Component>();
@ -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");
})

View file

@ -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>(
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();

View file

@ -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);

View file

@ -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();

View file

@ -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");
})

View file

@ -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");
})

View file

@ -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<UnitMeasurement[]>([]);
const [rm, setRM] = useState<pond.Measurement>(pond.Measurement.create());
const [recentUnitMeasurement, setRecentUnitMeasurement] = useState<UnitMeasurement[]>([]);
const [recentGoodUnitMeasurement, setRecentGoodUnitMeasurement] = useState<UnitMeasurement[]>([]);
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);
})

View file

@ -10,27 +10,31 @@ import { has } from "utils/types";
import { pondURL } from "./pond";
export interface IInteractionsAPIContext {
addInteraction: (device: number, settings: pond.IInteractionSettings) => Promise<AxiosResponse<pond.AddInteractionResponse>>;
addMultiInteractions: (device: number, settings: pond.MultiInteractionSettings) => Promise<AxiosResponse<pond.AddMultiInteractionsResponse>>;
addInteraction: (device: number, settings: pond.IInteractionSettings, as?: string) => Promise<AxiosResponse<pond.AddInteractionResponse>>;
addMultiInteractions: (device: number, settings: pond.MultiInteractionSettings, as?: string) => Promise<AxiosResponse<pond.AddMultiInteractionsResponse>>;
addInteractionToComponents: (
fullComponentLocations: string[],
settings: pond.IInteractionSettings
settings: pond.IInteractionSettings,
as?: string
) => Promise<AxiosResponse<pond.AddInteractionToComponentsResponse>>;
updateInteraction: (device: number, settings: pond.IInteractionSettings) => Promise<AxiosResponse<pond.UpdateInteractionResponse>>;
updateInteraction: (device: number, settings: pond.IInteractionSettings, as?: string) => Promise<AxiosResponse<pond.UpdateInteractionResponse>>;
updateInteractionPondSettings: (
device: number,
interaction: string,
settings: pond.IInteractionPondSettings
settings: pond.IInteractionPondSettings,
as?: string
) => Promise<AxiosResponse<pond.UpdateInteractionPondSettingsResponse>>;
removeInteraction: (device: number, interaction: string) => Promise<AxiosResponse<pond.RemoveInteractionResponse>>;
listInteractionsByDevice: (device: number | string, demo?: boolean) => Promise<Interaction[]>;
removeInteraction: (device: number, interaction: string, as?: string) => Promise<AxiosResponse<pond.RemoveInteractionResponse>>;
listInteractionsByDevice: (device: number | string, demo?: boolean, as?: string) => Promise<Interaction[]>;
listInteractionsByComponent: (
device: number,
component: quack.ComponentID,
demo?: boolean
demo?: boolean,
as?: string
) => Promise<Interaction[]>;
setAlertInteractions: (
alerts: pond.AlertData[]
alerts: pond.AlertData[],
as?: string
) => Promise<AxiosResponse<pond.SetAlertInteractionsResponse>>;
}
@ -56,9 +60,9 @@ function sanitizeInteraction(interaction: pond.IInteractionSettings): pond.IInte
export default function InteractionProvider(props: PropsWithChildren<Props>) {
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<AxiosResponse<pond.AddInteractionResponse>>((resolve, reject) => {
post<pond.AddInteractionResponse>(
pondURL("/devices/" + device + "/interactions" + (as ? "?as=" + as : "")),
@ -80,7 +84,8 @@ export default function InteractionProvider(props: PropsWithChildren<Props>) {
*/
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<Props>) {
})
};
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<AxiosResponse<pond.AddMultiInteractionsResponse>>((resolve, reject) => {
post<pond.AddMultiInteractionsResponse>(url,settings).then(resp => {
@ -110,7 +115,7 @@ export default function InteractionProvider(props: PropsWithChildren<Props>) {
})
};
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<Props>) {
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<Props>) {
})
};
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<AxiosResponse<pond.RemoveInteractionResponse>>((resolve, reject) => {
del<pond.RemoveInteractionResponse>(url).then(resp => {
@ -171,7 +177,8 @@ export default function InteractionProvider(props: PropsWithChildren<Props>) {
const listInteractionsByDevice = (
device: number | string,
demo: boolean = false
demo: boolean = false,
as?: string
): Promise<Interaction[]> => {
return new Promise((resolve, reject) => {
get(pondURL("/devices/" + device + "/interactions" + (as ? "?as=" + as : ""), demo))
@ -209,7 +216,8 @@ export default function InteractionProvider(props: PropsWithChildren<Props>) {
const listInteractionsByComponent = (
device: number,
component: quack.ComponentID,
demo: boolean = false
demo: boolean = false,
as?: string
): Promise<Interaction[]> => {
return new Promise((resolve, reject) => {
get(
@ -255,7 +263,8 @@ export default function InteractionProvider(props: PropsWithChildren<Props>) {
};
const setAlertInteractions = (
alerts: pond.AlertData[]
alerts: pond.AlertData[],
as?: string
): Promise<AxiosResponse<pond.SetAlertInteractionsResponse>> => {
let url = pondURL("/interactions/setAlerts" + (as ? "?as=" + as : ""))
return new Promise<AxiosResponse<pond.SetAlertInteractionsResponse>>((resolve, reject) => {