rot finished

This commit is contained in:
2026-01-09 11:56:40 +01:00
parent 1ee0afa088
commit ac99f291a7
30 changed files with 5581 additions and 293 deletions

92
web/src/lib/api.js Normal file
View File

@@ -0,0 +1,92 @@
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 }),
}),
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' }),
},
};

82
web/src/lib/websocket.js Normal file
View File

@@ -0,0 +1,82 @@
import { writable } from 'svelte/store';
export const connected = writable(false);
export const systemStatus = writable(null);
export const lastUpdate = writable(null);
class WebSocketService {
constructor() {
this.ws = null;
this.reconnectTimeout = null;
this.reconnectDelay = 3000;
}
connect() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsUrl = `${protocol}//${window.location.host}/ws`;
try {
this.ws = new WebSocket(wsUrl);
this.ws.onopen = () => {
console.log('WebSocket connected');
connected.set(true);
};
this.ws.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
if (message.type === 'update') {
console.log('System status updated:', message.data);
systemStatus.set(message.data);
lastUpdate.set(new Date(message.timestamp));
}
} catch (err) {
console.error('Error parsing message:', err);
}
};
this.ws.onerror = (error) => {
console.error('WebSocket error:', error);
};
this.ws.onclose = () => {
console.log('WebSocket disconnected');
connected.set(false);
this.scheduleReconnect();
};
} catch (err) {
console.error('Error creating WebSocket:', err);
this.scheduleReconnect();
}
}
scheduleReconnect() {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
}
this.reconnectTimeout = setTimeout(() => {
console.log('Attempting to reconnect...');
this.connect();
}, this.reconnectDelay);
}
send(message) {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(message));
}
}
disconnect() {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
}
if (this.ws) {
this.ws.close();
}
}
}
export const wsService = new WebSocketService();