updated the grain describers to use the new enum in the pond for the moisture calculation, changed the extract moisture function to account for custom grain types, and if there is no equation set it will return the humidity, updated instances where the bin uses that function to pass in the grain settings in its inventory

This commit is contained in:
csawatzky 2025-12-12 15:17:37 -06:00
parent bdddcfc103
commit d6c670fb78
16 changed files with 596 additions and 140 deletions

View file

@ -0,0 +1,123 @@
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";
import { or } from "utils";
export interface IGrainInterface {
addGrain: (
settings: pond.GrainSettings,
otherTeam?: string
) => Promise<AxiosResponse<pond.AddGrainResponse>>;
getGrain: (key: string, otherTeam?: string) => Promise<AxiosResponse<pond.GetGrainResponse>>
listGrains: (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
keys?: string[],
types?: string[],
otherTeam?: string,
) => Promise<AxiosResponse<pond.ListGrainsResponse>>;
removeGrain: (key: string, otherTeam?: string) => Promise<AxiosResponse<pond.RemoveGrainResponse>>;
}
export const GrainAPIcontext = createContext<IGrainInterface>({} as IGrainInterface);
interface Props {}
export default function GrainProvider(props: PropsWithChildren<Props>) {
const { children } = props;
const { get, del, post } = useHTTP();
const [{ as }] = useGlobalState();
//add
const addGrain = (settings: pond.GrainSettings, otherTeam?: string) => {
const view = otherTeam ? otherTeam : as
if (view) {
return post<pond.AddGrainResponse>(
pondURL("/grains?&as=" + view),
settings
);
}
return post<pond.AddGrainResponse>(pondURL("/grains"), settings);
};
//get
const getGrain = (key: string, otherTeam?: string) => {
const view = otherTeam ? otherTeam : as
const url = "/grains/" + key + (view ? "?as=" + view : "")
return new Promise<AxiosResponse<pond.GetGrainResponse>>((resolve, reject)=>{
get<pond.GetGrainResponse>(pondURL(url))
.then(resp => {
if (resp.data.grain){
resp.data.grain = pond.GrainObject.fromObject(resp.data.grain)
}
resolve(resp)
})
.catch(err => {
reject(err)
})
})
}
//list
const listGrains = (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
keys?: string[],
types?: string[],
otherTeam?: string
) => {
return new Promise<AxiosResponse<pond.ListGrainsResponse>>((resolve, reject)=>{
const view = otherTeam ? otherTeam : as
get<pond.ListGrainsResponse>(
pondURL(
"/grains?limit=" +
"?limit=" +
limit +
"&offset=" +
offset +
("&order=" + or(order, "asc")) +
("&by=" + or(orderBy, "key")) +
(search ? "&search=" + search : "") +
(keys ? "&keys=" + keys.join(",") : "") +
(types ? "&types=" + types.join(",") : "") +
(view ? "&as=" + view : "")
)
).then(resp => {
resp.data = pond.ListGrainsResponse.fromObject(resp.data);
return resolve(resp)
}).catch(err => {
return reject(err)
})
})
};
//remove
const removeGrain = (key: string, otherTeam?: string) => {
const view = otherTeam ? otherTeam : as
if (view) {
return del<pond.RemoveGrainResponse>(pondURL("/grains/" + key + "?as=" + view));
}
return del<pond.RemoveGrainResponse>(pondURL("/grains/" + key));
};
return (
<GrainAPIcontext.Provider
value={{
addGrain,
getGrain,
listGrains,
removeGrain
}}>
{children}
</GrainAPIcontext.Provider>
);
}
export const useGrainAPI = () => useContext(GrainAPIcontext);

View file

@ -38,6 +38,7 @@ import JohnDeereProvider, { useJohnDeereProxyAPI } from "./johnDeereProxyAPI";
import LibraCartProvider, { useLibraCartProxyAPI } from "./libracartProxyAPI";
import CNHiProvider, { useCNHiProxyAPI } from "./cnhiProxyAPI";
import DevicePresetProvider, { useDevicePresetAPI } from "./devicePresetAPI";
import GrainProvider, { useGrainAPI } from "./grainAPI";
// import NoteProvider from "providers/noteAPI";
export const pondURL = (partial: string, demo: boolean = false): string => {
@ -93,7 +94,9 @@ export default function PondProvider(props: PropsWithChildren<any>) {
<KeyManagerProvider>
<DevicePresetProvider>
<ImagekitProvider>
{children}
<GrainProvider>
{children}
</GrainProvider>
</ImagekitProvider>
</DevicePresetProvider>
</KeyManagerProvider>
@ -173,5 +176,6 @@ export {
useJohnDeereProxyAPI,
useCNHiProxyAPI,
useLibraCartProxyAPI,
useDevicePresetAPI
useDevicePresetAPI,
useGrainAPI
};