115 lines
4.0 KiB
TypeScript
115 lines
4.0 KiB
TypeScript
/**
|
|||
|
|
* Energy / Modbus hooks — typed TanStack Query wrappers for /api/modbus/*.
|
||
|
|
*
|
||
|
|
* All write operations go through the typed apiClient (openapi-fetch), which
|
||
|
|
* injects CSRF via the csrfMiddleware already wired into the client.
|
||
|
|
*
|
||
|
|
* Query-key conventions:
|
||
|
|
* ['modbus-devices'] — device list
|
||
|
|
* ['modbus-device', uuid] — single device
|
||
|
|
* ['modbus-profiles'] — profile list (rarely changes)
|
||
|
|
*
|
||
|
|
* On success, mutations invalidate the device list so the UI refreshes.
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||
|
|
import apiClient from '../api/client'
|
||
|
|
import type { components } from '../api/schema.d.ts'
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Re-exported types for consumers
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
export type ModbusDevice = components['schemas']['ModbusDeviceResponse']
|
||
|
|
export type ModbusDeviceCreate = components['schemas']['ModbusDeviceCreate']
|
||
|
|
export type ModbusDeviceUpdate = components['schemas']['ModbusDeviceUpdate']
|
||
|
|
export type ProfileSummary = components['schemas']['ProfileSummary']
|
||
|
|
export type ModbusTestReadResponse = components['schemas']['ModbusTestReadResponse']
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Query: list all devices
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
export function useDevices() {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['modbus-devices'],
|
||
|
|
queryFn: async () => {
|
||
|
|
const res = await apiClient.GET('/api/modbus/devices')
|
||
|
|
return res.data
|
||
|
|
},
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Query: list available profiles
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
export function useProfiles() {
|
||
|
|
return useQuery({
|
||
|
|
queryKey: ['modbus-profiles'],
|
||
|
|
queryFn: async () => {
|
||
|
|
const res = await apiClient.GET('/api/modbus/profiles')
|
||
|
|
return res.data
|
||
|
|
},
|
||
|
|
// Profiles are static — 5 min stale time.
|
||
|
|
staleTime: 5 * 60 * 1000,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Mutation: create device
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
export function useCreateDevice() {
|
||
|
|
const qc = useQueryClient()
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: (body: ModbusDeviceCreate) =>
|
||
|
|
apiClient.POST('/api/modbus/devices', { body }),
|
||
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['modbus-devices'] }),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Mutation: update (PATCH) device
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
export function useUpdateDevice() {
|
||
|
|
const qc = useQueryClient()
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: ({ uuid, body }: { uuid: string; body: ModbusDeviceUpdate }) =>
|
||
|
|
apiClient.PATCH('/api/modbus/devices/{uuid}', {
|
||
|
|
params: { path: { uuid } },
|
||
|
|
body,
|
||
|
|
}),
|
||
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['modbus-devices'] }),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Mutation: delete device
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
export function useDeleteDevice() {
|
||
|
|
const qc = useQueryClient()
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: (uuid: string) =>
|
||
|
|
apiClient.DELETE('/api/modbus/devices/{uuid}', {
|
||
|
|
params: { path: { uuid } },
|
||
|
|
}),
|
||
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: ['modbus-devices'] }),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
// Mutation: test-read device (POST /devices/{uuid}/test, does NOT persist)
|
||
|
|
// ---------------------------------------------------------------------------
|
||
|
|
|
||
|
|
export function useTestReadDevice() {
|
||
|
|
return useMutation({
|
||
|
|
mutationFn: (uuid: string) =>
|
||
|
|
apiClient.POST('/api/modbus/devices/{uuid}/test', {
|
||
|
|
params: { path: { uuid } },
|
||
|
|
}),
|
||
|
|
})
|
||
|
|
}
|