added in the device preset stuff for bin modes since it was missed in the initial update to the new platform
This commit is contained in:
parent
4d758ef26f
commit
ad479e2b45
3 changed files with 210 additions and 56 deletions
|
|
@ -6,7 +6,7 @@ import {
|
|||
DialogActions,
|
||||
DialogContent,
|
||||
DialogTitle,
|
||||
Grid,
|
||||
Grid2 as Grid,
|
||||
InputAdornment,
|
||||
MenuItem,
|
||||
Slider,
|
||||
|
|
@ -19,7 +19,7 @@ import { DevicePreset } from "models/DevicePreset";
|
|||
import { ObjectTypeString } from "objects/ObjectDescriber";
|
||||
import { pond } from "protobuf-ts/pond";
|
||||
import { useSnackbar } from "providers";
|
||||
// import { useDevicePresetAPI } from "providers/pond/devicePresetAPI";
|
||||
import { useDevicePresetAPI } from "providers/pond/devicePresetAPI";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { getTemperatureUnit } from "utils";
|
||||
|
||||
|
|
@ -43,7 +43,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
removeCallback,
|
||||
updateCallback
|
||||
} = props;
|
||||
// const devicePresetAPI = useDevicePresetAPI();
|
||||
const devicePresetAPI = useDevicePresetAPI();
|
||||
const [presetName, setPresetName] = useState("");
|
||||
const [presetType, setPresetType] = useState<pond.PresetType>(1);
|
||||
const [controllerType, setControllerType] = useState<pond.ControllerType>(1);
|
||||
|
|
@ -81,22 +81,22 @@ export default function DevicePresetCard(props: Props) {
|
|||
p.settings.temperature = tempC;
|
||||
p.settings.humidity = hum;
|
||||
|
||||
// devicePresetAPI
|
||||
// .addDevicePreset(p.name, p.settings, objectKey, typestring)
|
||||
// .then(resp => {
|
||||
// //get the temp key from the preset
|
||||
// let temp = p.key;
|
||||
// //update the key in the preset with the key that came back from the add
|
||||
// p.key = resp.data.key;
|
||||
// //use the callback function passing in the preset and the temp key
|
||||
// if (addCallback) {
|
||||
// addCallback(p, temp);
|
||||
// }
|
||||
// openSnack("New preset saved");
|
||||
// })
|
||||
// .catch(err => {
|
||||
// openSnack("Failed to save new preset");
|
||||
// });
|
||||
devicePresetAPI
|
||||
.addDevicePreset(p.name, p.settings, objectKey, typestring)
|
||||
.then(resp => {
|
||||
//get the temp key from the preset
|
||||
let temp = p.key;
|
||||
//update the key in the preset with the key that came back from the add
|
||||
p.key = resp.data.key;
|
||||
//use the callback function passing in the preset and the temp key
|
||||
if (addCallback) {
|
||||
addCallback(p, temp);
|
||||
}
|
||||
openSnack("New preset saved");
|
||||
})
|
||||
.catch(err => {
|
||||
openSnack("Failed to save new preset");
|
||||
});
|
||||
};
|
||||
|
||||
const updatePreset = () => {
|
||||
|
|
@ -108,32 +108,32 @@ export default function DevicePresetCard(props: Props) {
|
|||
newPreset.settings.temperature = tempC;
|
||||
newPreset.settings.humidity = hum;
|
||||
|
||||
// devicePresetAPI
|
||||
// .updateDevicePreset(newPreset.key, newPreset.name, newPreset.settings)
|
||||
// .then(resp => {
|
||||
// if (updateCallback) {
|
||||
// updateCallback(oldPreset, newPreset);
|
||||
// }
|
||||
// openSnack("Preset Updated");
|
||||
// })
|
||||
// .catch(err => {
|
||||
// openSnack("There was a problem updating the preset");
|
||||
// });
|
||||
devicePresetAPI
|
||||
.updateDevicePreset(newPreset.key, newPreset.name, newPreset.settings)
|
||||
.then(resp => {
|
||||
if (updateCallback) {
|
||||
updateCallback(oldPreset, newPreset);
|
||||
}
|
||||
openSnack("Preset Updated");
|
||||
})
|
||||
.catch(err => {
|
||||
openSnack("There was a problem updating the preset");
|
||||
});
|
||||
};
|
||||
|
||||
const removePreset = () => {
|
||||
// devicePresetAPI
|
||||
// .removeDevicePreset(preset.key)
|
||||
// .then(resp => {
|
||||
// openSnack("Preset has been deleted");
|
||||
// setOpenDeleteConfirmation(false);
|
||||
// if (removeCallback) {
|
||||
// removeCallback(preset);
|
||||
// }
|
||||
// })
|
||||
// .catch(err => {
|
||||
// openSnack("There was a problem deleting this preset");
|
||||
// });
|
||||
devicePresetAPI
|
||||
.removeDevicePreset(preset.key)
|
||||
.then(resp => {
|
||||
openSnack("Preset has been deleted");
|
||||
setOpenDeleteConfirmation(false);
|
||||
if (removeCallback) {
|
||||
removeCallback(preset);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
openSnack("There was a problem deleting this preset");
|
||||
});
|
||||
};
|
||||
|
||||
const deleteConfirmation = () => {
|
||||
|
|
@ -192,7 +192,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
<Card raised style={{ padding: 10, width: "100%" }}>
|
||||
{deleteConfirmation()}
|
||||
<Grid container direction="row" alignContent="center" alignItems="center">
|
||||
<Grid item xs={12} sm={6}>
|
||||
<Grid size={{xs:12, sm:6}}>
|
||||
<TextField
|
||||
variant="outlined"
|
||||
margin="normal"
|
||||
|
|
@ -203,7 +203,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
}}
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12} sm={6}>
|
||||
<Grid size={{xs:12, sm:6}}>
|
||||
{presetType !== pond.PresetType.PRESET_TYPE_UNKNOWN &&
|
||||
controllerType !== pond.ControllerType.CONTROLLER_TYPE_UNKNOWN && (
|
||||
<Box paddingLeft={2}>
|
||||
|
|
@ -216,7 +216,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
</Box>
|
||||
)}
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<Grid size={6}>
|
||||
{/* type */}
|
||||
<TextField
|
||||
style={{ width: "100%", marginRight: 5 }}
|
||||
|
|
@ -250,7 +250,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
</MenuItem>
|
||||
</TextField>
|
||||
</Grid>
|
||||
<Grid item xs={6}>
|
||||
<Grid size={6}>
|
||||
{/* controller type */}
|
||||
<TextField
|
||||
style={{ width: "100%", marginLeft: 5 }}
|
||||
|
|
@ -281,7 +281,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
</Grid>
|
||||
{/* temp slider */}
|
||||
{isMobile ? (
|
||||
<Grid item xs={12}>
|
||||
<Grid size={12}>
|
||||
<Box
|
||||
display="flex"
|
||||
alignContent="center"
|
||||
|
|
@ -338,10 +338,10 @@ export default function DevicePresetCard(props: Props) {
|
|||
</Grid>
|
||||
) : (
|
||||
<React.Fragment>
|
||||
<Grid item xs={2}>
|
||||
<Grid size={2}>
|
||||
<Typography>Temperature</Typography>
|
||||
</Grid>
|
||||
<Grid item xs={7}>
|
||||
<Grid size={7}>
|
||||
<Box paddingRight={2}>
|
||||
<Slider
|
||||
//orientation="vertical"
|
||||
|
|
@ -362,7 +362,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
/>
|
||||
</Box>
|
||||
</Grid>
|
||||
<Grid item xs={3}>
|
||||
<Grid size={3}>
|
||||
<TextField
|
||||
style={{ width: "100%" }}
|
||||
margin="dense"
|
||||
|
|
@ -395,7 +395,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
)}
|
||||
{/* humidity slider */}
|
||||
{isMobile ? (
|
||||
<Grid item xs={12}>
|
||||
<Grid size={12}>
|
||||
<Box
|
||||
display="flex"
|
||||
alignContent="center"
|
||||
|
|
@ -437,10 +437,10 @@ export default function DevicePresetCard(props: Props) {
|
|||
</Grid>
|
||||
) : (
|
||||
<React.Fragment>
|
||||
<Grid item xs={2}>
|
||||
<Grid size={2}>
|
||||
<Typography>Humidity</Typography>
|
||||
</Grid>
|
||||
<Grid item xs={7}>
|
||||
<Grid size={7}>
|
||||
<Box paddingRight={2}>
|
||||
<Slider
|
||||
//orientation="vertical"
|
||||
|
|
@ -456,7 +456,7 @@ export default function DevicePresetCard(props: Props) {
|
|||
/>
|
||||
</Box>
|
||||
</Grid>
|
||||
<Grid item xs={3}>
|
||||
<Grid size={3}>
|
||||
<TextField
|
||||
style={{ width: "100%" }}
|
||||
margin="dense"
|
||||
|
|
|
|||
150
src/providers/pond/devicePresetAPI.tsx
Normal file
150
src/providers/pond/devicePresetAPI.tsx
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
import { useHTTP } from "hooks";
|
||||
import { createContext, PropsWithChildren, useContext } from "react";
|
||||
import { pond } from "protobuf-ts/pond";
|
||||
import { pondURL } from "./pond";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { useGlobalState } from "providers";
|
||||
|
||||
export interface IDevicePresetInterface {
|
||||
//add
|
||||
addDevicePreset: (
|
||||
name: string,
|
||||
settings: pond.DevicePresetSettings,
|
||||
parentKey?: string,
|
||||
parentTypes?: string
|
||||
) => Promise<AxiosResponse<pond.AddDevicePresetResponse>>;
|
||||
//list
|
||||
listDevicePresets: (
|
||||
limit: number,
|
||||
offset: number,
|
||||
order?: "asc" | "desc",
|
||||
orderBy?: string,
|
||||
search?: string,
|
||||
keys?: string[],
|
||||
types?: string[]
|
||||
) => Promise<AxiosResponse<pond.ListDevicePresetResponse>>;
|
||||
//update
|
||||
updateDevicePreset: (
|
||||
key: string,
|
||||
name: string,
|
||||
settings: pond.DevicePresetSettings
|
||||
) => Promise<AxiosResponse<pond.UpdateDevicePresetResponse>>;
|
||||
|
||||
//remove
|
||||
removeDevicePreset: (key: string) => Promise<AxiosResponse<pond.DeleteDevicePresetResponse>>;
|
||||
|
||||
//get
|
||||
getDevicePreset: (key: string) => Promise<AxiosResponse<pond.GetDevicePresetResponse>>;
|
||||
}
|
||||
|
||||
export const DevicePresetAPIcontext = createContext<IDevicePresetInterface>(
|
||||
{} as IDevicePresetInterface
|
||||
);
|
||||
|
||||
interface Props {}
|
||||
|
||||
export default function DevicePresetProvider(props: PropsWithChildren<Props>) {
|
||||
const { children } = props;
|
||||
const { get, del, post, put } = useHTTP();
|
||||
const [{ as }] = useGlobalState();
|
||||
|
||||
//add
|
||||
const addDevicePreset = (
|
||||
name: string,
|
||||
settings: pond.DevicePresetSettings,
|
||||
parentKey?: string,
|
||||
parentType?: string
|
||||
) => {
|
||||
let key = "";
|
||||
let type = "";
|
||||
if (parentKey && parentType) {
|
||||
key = "&parentKey=" + parentKey;
|
||||
type = "&parentType=" + parentType;
|
||||
}
|
||||
if (as) {
|
||||
return post<pond.AddDevicePresetResponse>(
|
||||
pondURL("/devicePreset?name=" + name + "&as=" + as + key + type),
|
||||
settings
|
||||
);
|
||||
}
|
||||
return post<pond.AddDevicePresetResponse>(
|
||||
pondURL("/devicePreset?name=" + name + key + type),
|
||||
settings
|
||||
);
|
||||
};
|
||||
//list
|
||||
const listDevicePresets = (
|
||||
limit: number,
|
||||
offset: number,
|
||||
order?: "asc" | "desc",
|
||||
orderBy?: string,
|
||||
search?: string,
|
||||
keys?: string[],
|
||||
types?: string[]
|
||||
) => {
|
||||
let asText = "";
|
||||
if (as) asText = "&as=" + as;
|
||||
return get<pond.ListDevicePresetResponse>(
|
||||
pondURL(
|
||||
"/devicePresets?limit=" +
|
||||
limit +
|
||||
"&offset=" +
|
||||
offset +
|
||||
("&order=" + (order ? order : "asc")) +
|
||||
("&by=" + (orderBy ? orderBy : "key")) +
|
||||
(search ? "&search=" + search : "") +
|
||||
(keys ? "&keys=" + keys.toString() : "") +
|
||||
(types ? "&types=" + types.toString() : "") +
|
||||
asText
|
||||
)
|
||||
);
|
||||
};
|
||||
//update
|
||||
const updateDevicePreset = (key: string, name: string, settings: pond.DevicePresetSettings) => {
|
||||
if (as) {
|
||||
return put<pond.UpdateDevicePresetResponse>(
|
||||
pondURL("/devicePreset/" + key + "?as=" + as + "&name=" + name),
|
||||
settings
|
||||
);
|
||||
}
|
||||
return put<pond.UpdateDevicePresetResponse>(
|
||||
pondURL("/devicePreset/" + key + "?name=" + name),
|
||||
settings
|
||||
);
|
||||
};
|
||||
|
||||
//remove
|
||||
const removeDevicePreset = (key: string) => {
|
||||
if (as) {
|
||||
return del<pond.DeleteDevicePresetResponse>(pondURL("/devicePreset/" + key + "?as=" + as));
|
||||
}
|
||||
return del<pond.DeleteDevicePresetResponse>(pondURL("/devicePreset/" + key + "?as=" + as));
|
||||
};
|
||||
|
||||
//get
|
||||
const getDevicePreset = (key: string) => {
|
||||
if (as) {
|
||||
return del<pond.GetDevicePresetResponse>(pondURL("/devicePreset/" + key + "?as=" + as));
|
||||
}
|
||||
return del<pond.GetDevicePresetResponse>(pondURL("/devicePreset/" + key + "?as=" + as));
|
||||
};
|
||||
return (
|
||||
<DevicePresetAPIcontext.Provider
|
||||
value={{
|
||||
//add
|
||||
addDevicePreset,
|
||||
//list
|
||||
listDevicePresets,
|
||||
//update
|
||||
updateDevicePreset,
|
||||
//remove
|
||||
removeDevicePreset,
|
||||
//get
|
||||
getDevicePreset
|
||||
}}>
|
||||
{children}
|
||||
</DevicePresetAPIcontext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export const useDevicePresetAPI = () => useContext(DevicePresetAPIcontext);
|
||||
|
|
@ -36,6 +36,7 @@ import UsageProvider, { useUsageAPI } from "./usageAPI";
|
|||
import KeyManagerProvider, { useKeyManagerAPI } from "./keyManagerAPI";
|
||||
import JohnDeereProvider, { useJohnDeereProxyAPI } from "./johnDeereProxyAPI";
|
||||
import CNHiProvider, { useCNHiProxyAPI } from "./cnhiProxyAPI";
|
||||
import DevicePresetProvider, { useDevicePresetAPI } from "./devicePresetAPI";
|
||||
// import NoteProvider from "providers/noteAPI";
|
||||
|
||||
export const pondURL = (partial: string, demo: boolean = false): string => {
|
||||
|
|
@ -88,7 +89,9 @@ export default function PondProvider(props: PropsWithChildren<any>) {
|
|||
<CNHiProvider>
|
||||
<UsageProvider>
|
||||
<KeyManagerProvider>
|
||||
{children}
|
||||
<DevicePresetProvider>
|
||||
{children}
|
||||
</DevicePresetProvider>
|
||||
</KeyManagerProvider>
|
||||
</UsageProvider>
|
||||
</CNHiProvider>
|
||||
|
|
@ -163,5 +166,6 @@ export {
|
|||
useUsageAPI,
|
||||
useKeyManagerAPI,
|
||||
useJohnDeereProxyAPI,
|
||||
useCNHiProxyAPI
|
||||
useCNHiProxyAPI,
|
||||
useDevicePresetAPI
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue