merged tag stuff

This commit is contained in:
Carter 2025-01-27 12:16:47 -06:00
commit 4f31d036ef
41 changed files with 5028 additions and 126 deletions

View file

@ -3,13 +3,14 @@ import { createContext, useContext, useReducer, Dispatch } from "react";
import { pond } from "protobuf-ts/pond";
import { User } from "../models/user";
import { Team } from "../models/team";
import { Firmware } from "models";
export interface GlobalState {
// tags: Tag[];
user: User;
team: Team;
as: string;
// firmware: Map<string, Firmware>;
firmware: Map<string, Firmware>;
// newStructure: boolean;
showErrors: boolean;
userTeamPermissions: pond.Permission[];

View file

@ -15,7 +15,7 @@ export {
// useDeviceWebsocket,
// useFieldAPI,
// useFieldMarkerAPI,
// useFirmwareAPI,
useFirmwareAPI,
useGroupAPI,
// useHarvestPlanAPI,
// useHarvestYearAPI,
@ -26,7 +26,7 @@ export {
usePermissionAPI,
// usePreferenceAPI,
// useSiteAPI,
// useTagAPI,
useTagAPI,
useTeamAPI,
useImagekitAPI,
// useTaskAPI,

View file

@ -100,6 +100,10 @@ export interface IDeviceAPIContext {
keys?: string[],
types?: string[]
) => Promise<any>;
statistics: (
keys?: string[],
types?: string[]
) => Promise<any>;
tag: (id: number, tag: string) => Promise<any>;
untag: (id: number, tag: string) => Promise<any>;
getDatacap: (id: number) => Promise<any>;
@ -177,6 +181,22 @@ export default function DeviceProvider(props: PropsWithChildren<Props>) {
return get(url);
};
const statistics = (
keys?: string[],
types?: string[]
) => {
if (types && types.length > 0 && types[0] === "team")
return get(
pondURL(
"/deviceStatistics/" +
(keys ? "?keys=" + keys.toString() : "") +
(types ? "&types=" + types.toString() : "")
)
);
const url = pondURL("/deviceStatistics");
return get<pond.GetDeviceStatisticsResponse>(url);
};
const getDevicePageData = (
id: number | string,
keys?: string[],
@ -615,6 +635,7 @@ export default function DeviceProvider(props: PropsWithChildren<Props>) {
update,
remove,
get: getDevice,
statistics,
getPageData: getDevicePageData,
getMulti,
getGeoJson: getDeviceGeoJSON,

View file

@ -0,0 +1,205 @@
import { useHTTP } from "hooks";
import { pond } from "protobuf-ts/pond";
import { useGlobalState } from "providers/StateContainer";
import { createContext, PropsWithChildren, useContext } from "react";
import { pondURL } from "./pond";
export interface IFirmwareAPIContext {
getFirmwareStatus: (deviceID: number | Long | string, demo?: boolean) => Promise<any>;
cancelUpgrade: (deviceID: number | Long | string) => Promise<any>;
getLatestFirmware: (
platform: pond.DevicePlatform,
channel: pond.UpgradeChannel,
demo?: boolean
) => Promise<any>;
getAllLatestFirmware: () => Promise<any>;
listFirmware: (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
settings?: string,
status?: string
) => Promise<any>;
removeFirmware: (platform: pond.DevicePlatform, version: string) => Promise<any>;
downloadInstaller: (platform: pond.DevicePlatform, version: string) => Promise<any>;
uploadFirmware: (
version: string,
channel: pond.UpgradeChannel,
platform: pond.DevicePlatform,
file: Blob,
breaksStorage: boolean
) => Promise<any>;
updateChannel: (
platform: pond.DevicePlatform,
version: string,
channel: pond.UpgradeChannel
) => Promise<any>;
upgradeFirmware: (device: number | string) => Promise<any>;
}
export const FirmwareAPIContext = createContext<IFirmwareAPIContext>({} as IFirmwareAPIContext);
interface Props {}
function platformRequest(platform: pond.DevicePlatform): string {
return platformBody(platform)
.toString()
.replace("DEVICE_PLATFORM_", "")
.toLowerCase();
}
function channelRequest(channel: pond.UpgradeChannel): string {
return channelBody(channel)
.replace("UPGRADE_CHANNEL_", "")
.toLowerCase();
}
function platformBody(platform: pond.DevicePlatform): string {
return pond.DevicePlatform[platform].toString();
}
function channelBody(channel: pond.UpgradeChannel): string {
return pond.UpgradeChannel[channel].toString();
}
export default function FirmwareProvider(props: PropsWithChildren<Props>) {
const { children } = props;
const { get, post, put, del, options } = useHTTP();
const [{ as }] = useGlobalState();
const getFirmwareStatus = (deviceID: number | Long | string, demo: boolean = false) => {
return get(pondURL("/devices/" + deviceID.toString() + "/firmware", demo));
};
const cancelUpgrade = (deviceID: number | Long | string) => {
return post(pondURL("/devices/" + deviceID.toString() + "/cancelUpgrade"));
};
const getLatestFirmware = (
platform: pond.DevicePlatform,
channel: pond.UpgradeChannel,
demo: boolean = false
) => {
return get(
pondURL(
"/firmware/latest?platform=" +
platformRequest(platform) +
"&channel=" +
channelRequest(channel),
demo
)
);
};
const getAllLatestFirmware = () => {
return get(pondURL("/firmware/all"));
};
const listFirmware = (
limit: number,
offset: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
settings?: string,
status?: string
) => {
return get(
pondURL(
"/firmware" +
"?limit=" +
limit +
"&offset=" +
offset +
("&order=" + (order ? order : "asc")) +
("&by=" + (orderBy ? orderBy : "uploaded")) +
(search ? "&search=" + search : "") +
(settings ? "&settings=" + settings : "") +
(status ? "&status=" + status : "")
)
);
};
const removeFirmware = (platform: pond.DevicePlatform, version: string) => {
return del(pondURL("/firmware/" + platformRequest(platform) + "/" + version));
};
const downloadInstaller = (platform: pond.DevicePlatform, version: string) => {
let headers = {
Accept: "application/octet-stream",
...options().headers
};
headers = { ...headers, ...options().headers };
let opt = {
responseType: "arraybuffer",
headers: headers
};
return get(
pondURL("/firmware/download?platform=" + platformRequest(platform) + "&version=" + version),
opt as any
);
};
const uploadFirmware = (
version: string,
channel: pond.UpgradeChannel,
platform: pond.DevicePlatform,
file: Blob,
breaksStorage: boolean
) => {
// let headers: { "Content-Type": string } = {
// "Content-Type": "multipart/form-data"
// };
let headers = {
"Content-Type": "multipart/form-data",
...options().headers
};
headers = { ...headers, ...options().headers };
let opt = { headers };
let data = new FormData();
data.append("version", version);
data.append("channel", channelBody(channel));
data.append("platform", platformBody(platform));
data.append("file", file);
data.append("breaksStorage", breaksStorage.toString());
return post(pondURL("/firmware"), data, opt);
};
const updateChannel = (
platform: pond.DevicePlatform,
version: string,
channel: pond.UpgradeChannel
) => {
return put(pondURL("/firmware/channel"), {
platform: platformBody(platform),
version: version,
channel: channelBody(channel)
});
};
const upgradeFirmware = (device: number | string) => {
return post(pondURL("/devices/" + device + "/upgrade" + (as ? "?as=" + as : "")), {});
};
return (
<FirmwareAPIContext.Provider
value={{
getFirmwareStatus,
cancelUpgrade,
getLatestFirmware,
getAllLatestFirmware,
listFirmware,
removeFirmware,
downloadInstaller,
uploadFirmware,
updateChannel,
upgradeFirmware
}}>
{children}
</FirmwareAPIContext.Provider>
);
}
export const useFirmwareAPI = () => useContext(FirmwareAPIContext);

View file

@ -11,6 +11,8 @@ import DeviceProvider, { useDeviceAPI } from "./deviceAPI";
import BackpackProvider, { useBackpackAPI } from "./backpackAPI";
import GroupProvider, { useGroupAPI } from "./groupAPI";
import NotificationProvider, { useNotificationAPI } from "./notificationAPI";
import TagProvider, { useTagAPI } from "./tagAPI";
import FirmwareProvider, { useFirmwareAPI } from "./firmwareAPI";
// import NoteProvider from "providers/noteAPI";
export const pondURL = (partial: string, demo: boolean = false): string => {
@ -38,7 +40,11 @@ export default function PondProvider(props: PropsWithChildren<any>) {
<GroupProvider>
<BackpackProvider>
<NotificationProvider>
{children}
<TagProvider>
<FirmwareProvider>
{children}
</FirmwareProvider>
</TagProvider>
</NotificationProvider>
</BackpackProvider>
</GroupProvider>
@ -55,7 +61,9 @@ export default function PondProvider(props: PropsWithChildren<any>) {
export {
useBackpackAPI,
useDeviceAPI,
useFirmwareAPI,
useUserAPI,
useTagAPI,
useTeamAPI,
useImagekitAPI,
usePermissionAPI,

View file

@ -0,0 +1,96 @@
import { useHTTP } from "hooks";
import { pond } from "protobuf-ts/pond";
import { createContext, PropsWithChildren, useContext } from "react";
import { pondURL } from "./pond";
import { useGlobalState } from "providers/StateContainer";
export interface ITagAPIContext {
addTag: (tag: pond.TagSettings) => Promise<any>;
updateTag: (tagID: string, tag: pond.TagSettings) => Promise<any>;
removeTag: (tagID: string) => Promise<any>;
getTag: (tagID: string) => Promise<any>;
listTags: (
limit?: number,
offset?: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
asRoot?: boolean,
keys?: string[],
types?: string[]
) => Promise<any>;
}
export const TagAPIContext = createContext<ITagAPIContext>({} as ITagAPIContext);
interface Props {}
export default function TagProvider(props: PropsWithChildren<Props>) {
const { children } = props;
const { get, del, put, post } = useHTTP();
const [{ as }] = useGlobalState()
const addTag = (tag: pond.TagSettings) => {
return post(pondURL("/tags"), tag);
};
const updateTag = (tagID: string, tag: pond.TagSettings) => {
return put(pondURL("/tags/" + tagID), tag);
};
const removeTag = (tagID: string) => {
return del(pondURL("/tags/" + tagID));
};
const getTag = (tagID: string) => {
return get(pondURL("/tags/" + tagID));
};
// const listTags = () => {
// return get(pondURL("/tags"));
// };
const listTags = (
limit?: number,
offset?: number,
order?: "asc" | "desc",
orderBy?: string,
search?: string,
asRoot?: boolean,
keys?: string[],
types?: string[]
) => {
limit = limit ? limit : 100
const url = pondURL(
"/tags" +
"?limit=" +
limit +
"&offset=" +
offset +
("&order=" + (order ? order : "asc")) +
("&by=" + (orderBy ? orderBy : "deviceId")) +
(search ? "&search=" + search : "") +
(asRoot ? "&asRoot=" + asRoot.toString() : "") +
(as && !(types && types.length > 0 && types[0] === "team") ? "&as=" + as : "") +
(keys ? "&keys=" + keys.toString() : "") +
(types ? "&types=" + types.toString() : "")
);
return get<pond.ListDevicesResponse>(url);
};
return (
<TagAPIContext.Provider
value={{
addTag,
updateTag,
removeTag,
getTag,
listTags
}}>
{children}
</TagAPIContext.Provider>
);
}
export const useTagAPI = () => useContext(TagAPIContext);