const API_BASE = '/api'; async function request(endpoint, options = {}) { try { const response = await fetch(`${API_BASE}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', ...options.headers, }, }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return await response.json(); } catch (error) { console.error('API request failed:', error); throw error; } } export const api = { // Status getStatus: () => request('/status'), getConfig: () => request('/config'), // WebSwitch webswitch: { relayOn: (relay) => request(`/webswitch/relay/on?relay=${relay}`, { method: 'POST' }), relayOff: (relay) => request(`/webswitch/relay/off?relay=${relay}`, { method: 'POST' }), allOn: () => request('/webswitch/all/on', { method: 'POST' }), allOff: () => request('/webswitch/all/off', { method: 'POST' }), }, // Rotator rotator: { move: (rotator, azimuth) => request('/rotator/move', { method: 'POST', body: JSON.stringify({ rotator, azimuth }), }), cw: (rotator) => request(`/rotator/cw?rotator=${rotator}`, { method: 'POST' }), ccw: (rotator) => request(`/rotator/ccw?rotator=${rotator}`, { method: 'POST' }), stop: () => request('/rotator/stop', { method: 'POST' }), }, // Tuner tuner: { setOperate: (value) => request('/tuner/operate', { method: 'POST', body: JSON.stringify({ value }), }), setBypass: (value) => request('/tuner/bypass', { method: 'POST', body: JSON.stringify({ value }), }), autoTune: () => request('/tuner/autotune', { method: 'POST' }), }, // Antenna Genius antenna: { selectAntenna: (port, antenna) => request('/antenna/select', { method: 'POST', body: JSON.stringify({ port, antenna }), }), deselectAntenna: (port, antenna) => request('/antenna/deselect', { method: 'POST', body: JSON.stringify({ port, antenna }), }), reboot: () => request('/antenna/reboot', { method: 'POST' }), }, // Power Genius power: { setFanMode: (mode) => request('/power/fanmode', { method: 'POST', body: JSON.stringify({ mode }), }), setOperate: (value) => request('/power/operate', { method: 'POST', body: JSON.stringify({ value }), }), }, // Rotator Genius rotator: { setHeading: (heading) => request('/rotator/heading', { method: 'POST', body: JSON.stringify({ heading }), }), rotateCW: () => request('/rotator/cw', { method: 'POST' }), rotateCCW: () => request('/rotator/ccw', { method: 'POST' }), stop: () => request('/rotator/stop', { method: 'POST' }), }, // Ultrabeam ultrabeam: { setFrequency: (frequency, direction) => request('/ultrabeam/frequency', { method: 'POST', body: JSON.stringify({ frequency, direction }), }), retract: () => request('/ultrabeam/retract', { method: 'POST' }), setAutoTrack: (enabled, threshold) => request('/ultrabeam/autotrack', { method: 'POST', body: JSON.stringify({ enabled, threshold }), }), setDirection: (direction) => request('/ultrabeam/direction', { method: 'POST', body: JSON.stringify({ direction }), }), }, };