- PowerPoint/EnergyBar: add charger/charge fields - getPower: query charger_10m/charger_1h alongside PV and meters - getBars: compute daily charge kWh via LAG on charger_daily.eto_wh - EnergyView: green Charger series in live power chart - StatisticsView: purple Charge bars in statistics chart Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
75 lines
1.6 KiB
TypeScript
75 lines
1.6 KiB
TypeScript
const BASE = '/energy/api'
|
|
|
|
async function request<T>(path: string, options?: RequestInit): Promise<T> {
|
|
const res = await fetch(`${BASE}${path}`, {
|
|
credentials: 'include',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
...options,
|
|
})
|
|
|
|
if (res.status === 401) {
|
|
if (window.location.pathname !== '/energy/login') {
|
|
window.location.href = '/energy/login'
|
|
}
|
|
throw new Error('Unauthorized')
|
|
}
|
|
|
|
if (!res.ok) {
|
|
const body = await res.json().catch(() => ({ error: res.statusText }))
|
|
throw new Error(body.error || res.statusText)
|
|
}
|
|
|
|
return res.json()
|
|
}
|
|
|
|
export interface EnergyBar {
|
|
time: string
|
|
buy: number
|
|
sell: number
|
|
produce: number
|
|
consume: number
|
|
charge: number
|
|
}
|
|
|
|
export interface PowerPoint {
|
|
time: string
|
|
pv: number
|
|
house: number
|
|
barn: number
|
|
charger: number
|
|
}
|
|
|
|
export interface BatteryPoint {
|
|
time: string
|
|
level: number
|
|
}
|
|
|
|
export const api = {
|
|
login(username: string, password: string) {
|
|
return request<{ status: string }>('/login', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ username, password }),
|
|
})
|
|
},
|
|
|
|
logout() {
|
|
return request<{ status: string }>('/logout', { method: 'POST' })
|
|
},
|
|
|
|
checkAuth() {
|
|
return request<{ status: string }>('/auth/check')
|
|
},
|
|
|
|
getBars(period: 'day' | 'week' | 'month', count: number) {
|
|
return request<{ bars: EnergyBar[] }>(`/bars?period=${period}&count=${count}`)
|
|
},
|
|
|
|
getPower(range: string) {
|
|
return request<{ points: PowerPoint[] }>(`/power?range=${range}`)
|
|
},
|
|
|
|
getBattery(range: string) {
|
|
return request<{ points: BatteryPoint[] }>(`/battery?range=${range}`)
|
|
},
|
|
}
|