Skip to content

Commit

Permalink
Create utility functions for storing and retrieving Cockpit data from…
Browse files Browse the repository at this point in the history
… BlueOS vehicle
  • Loading branch information
rafaellehmkuhl committed Nov 16, 2023
1 parent db88ade commit 87625b1
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/libs/blueos.ts
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)
}

0 comments on commit 87625b1

Please sign in to comment.