43 lines
989 B
TypeScript
43 lines
989 B
TypeScript
import { request } from './request'
|
|
|
|
export type SensorInfo = {
|
|
id: number
|
|
name: string
|
|
authKey: string
|
|
lastContactAt?: number
|
|
type: 'rest' | 'mqtt'
|
|
mqttTopic?: string
|
|
mqttBrokerId?: number
|
|
mqttPath?: string
|
|
}
|
|
|
|
export type SensorModifiableData = {
|
|
name: string
|
|
type: 'rest' | 'mqtt'
|
|
mqttTopic?: string
|
|
mqttBrokerId?: number
|
|
mqttPath?: string
|
|
}
|
|
|
|
export const getSensors = () => request<SensorInfo[]>('/api/sensors')
|
|
|
|
export const createSensor = (data: SensorModifiableData) =>
|
|
request<SensorInfo>('/api/sensors', {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
|
|
export const updateSensor = ({
|
|
id,
|
|
...body
|
|
}: { id: number } & SensorModifiableData) =>
|
|
request<SensorInfo>(`/api/sensors/${id}`, {
|
|
method: 'PUT',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})
|
|
|
|
export const deleteSensor = (id: number) =>
|
|
request<void, 'void'>(`/api/sensors/${id}`, { method: 'DELETE' }, 'void')
|