-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create utility functions for storing and retrieving Cockpit data from…
… BlueOS vehicle
- Loading branch information
1 parent
db88ade
commit 87625b1
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export const getBagOfHoldingFromVehicle = async ( | ||
vehicleAddress: string, | ||
bagName: string | ||
): Promise<Record<string, any>> => { | ||
try { | ||
const response = await fetch(`http://${vehicleAddress}/bag/v1.0/get/${bagName}`) | ||
if (!(await response.ok)) { | ||
throw new Error(await response.text()) | ||
} | ||
return await response.json() | ||
} catch (error) { | ||
throw new Error(`Could not get bag of holdings for ${bagName}. ${error}`) | ||
} | ||
} | ||
|
||
export const getCockpitStorageFromVehicle = async (vehicleAddress: string): Promise<Record<string, any>> => { | ||
try { | ||
return await getBagOfHoldingFromVehicle(vehicleAddress, 'cockpit') | ||
} catch (error) { | ||
throw new Error(`Could not get Cockpit's storage data from vehicle. ${error}`) | ||
} | ||
} | ||
|
||
export const getKeyDataFromCockpitVehicleStorage = async ( | ||
vehicleAddress: string, | ||
storageKey: string | ||
): Promise<Record<string, any> | undefined> => { | ||
const cockpitVehicleStorage = await getCockpitStorageFromVehicle(vehicleAddress) | ||
return cockpitVehicleStorage[storageKey] | ||
} | ||
|
||
export const setBagOfHoldingOnVehicle = async ( | ||
vehicleAddress: string, | ||
bagName: string, | ||
bagData: Record<string, any> | any | ||
): Promise<void> => { | ||
try { | ||
await fetch(`http://${vehicleAddress}/bag/v1.0/set/${bagName}`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(bagData), | ||
}) | ||
} catch (error) { | ||
throw new Error(`Could not set bag of holdings for ${bagName}. ${error}`) | ||
} | ||
} | ||
|
||
export const setCockpitStorageOnVehicle = async ( | ||
vehicleAddress: string, | ||
storageData: Record<string, any> | any | ||
): Promise<void> => { | ||
try { | ||
await setBagOfHoldingOnVehicle(vehicleAddress, 'cockpit', storageData) | ||
} catch (error) { | ||
throw new Error(`Could not set Cockpit's storage data on vehicle. ${error}`) | ||
} | ||
} | ||
|
||
export const setKeyDataOnCockpitVehicleStorage = async ( | ||
vehicleAddress: string, | ||
storageKey: string, | ||
storageData: Record<string, any> | any | ||
): Promise<void> => { | ||
let previousVehicleStorage: Record<string, any> = {} | ||
try { | ||
previousVehicleStorage = await getCockpitStorageFromVehicle(vehicleAddress) | ||
} catch (error) { | ||
console.error(error) | ||
} | ||
const newVehicleStorage = previousVehicleStorage | ||
newVehicleStorage[storageKey] = storageData | ||
|
||
await setCockpitStorageOnVehicle(vehicleAddress, newVehicleStorage) | ||
} |