Initial commit: add .gitignore and README
This commit is contained in:
95
src/services/switchService.ts
Normal file
95
src/services/switchService.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import httpClient from '../lib/httpClient';
|
||||
import { config } from '../config';
|
||||
import { ensureAuthenticated } from './authService';
|
||||
import logger from '../lib/logger';
|
||||
import { OmadaApiResponse, OmadaSwitchPort, OmadaVlanConfig } from '../types/omada';
|
||||
|
||||
const BASE_URL = `${config.omada.northboundBase}/openapi/v1/omada/${config.omada.id}`;
|
||||
|
||||
/**
|
||||
* Gets all ports for a switch device
|
||||
*/
|
||||
export async function getPorts(
|
||||
siteId: string,
|
||||
deviceId: string
|
||||
): Promise<OmadaSwitchPort[]> {
|
||||
await ensureAuthenticated();
|
||||
|
||||
try {
|
||||
const url = `${BASE_URL}/sites/${siteId}/devices/${deviceId}/ports`;
|
||||
logger.debug('Fetching switch ports', { siteId, deviceId });
|
||||
|
||||
const response = await httpClient.get<OmadaApiResponse<OmadaSwitchPort[]>>(url);
|
||||
const data = response.data;
|
||||
|
||||
if (data.errorCode !== 0) {
|
||||
throw new Error(`Failed to fetch ports: ${data.msg || 'Unknown error'}`);
|
||||
}
|
||||
|
||||
const ports = data.result || data.data || [];
|
||||
logger.info(`Fetched ${ports.length} ports for switch ${deviceId}`);
|
||||
return ports;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching switch ports', { siteId, deviceId, error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets VLAN configuration for a specific port
|
||||
*/
|
||||
export async function setPortVlan(
|
||||
siteId: string,
|
||||
deviceId: string,
|
||||
portIndex: number,
|
||||
vlanConfig: OmadaVlanConfig
|
||||
): Promise<void> {
|
||||
await ensureAuthenticated();
|
||||
|
||||
try {
|
||||
const url = `${BASE_URL}/sites/${siteId}/devices/${deviceId}/ports/${portIndex}/vlan`;
|
||||
logger.info('Setting port VLAN', { siteId, deviceId, portIndex, vlanConfig });
|
||||
|
||||
const response = await httpClient.post<OmadaApiResponse>(url, vlanConfig);
|
||||
const data = response.data;
|
||||
|
||||
if (data.errorCode !== 0) {
|
||||
throw new Error(`Failed to set port VLAN: ${data.msg || 'Unknown error'}`);
|
||||
}
|
||||
|
||||
logger.info('Port VLAN updated', { siteId, deviceId, portIndex });
|
||||
} catch (error) {
|
||||
logger.error('Error setting port VLAN', { siteId, deviceId, portIndex, error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggles a port on/off
|
||||
*/
|
||||
export async function togglePort(
|
||||
siteId: string,
|
||||
deviceId: string,
|
||||
portIndex: number,
|
||||
enabled: boolean
|
||||
): Promise<void> {
|
||||
await ensureAuthenticated();
|
||||
|
||||
try {
|
||||
const url = `${BASE_URL}/sites/${siteId}/devices/${deviceId}/ports/${portIndex}`;
|
||||
logger.info('Toggling port', { siteId, deviceId, portIndex, enabled });
|
||||
|
||||
const response = await httpClient.put<OmadaApiResponse>(url, { enabled });
|
||||
const data = response.data;
|
||||
|
||||
if (data.errorCode !== 0) {
|
||||
throw new Error(`Failed to toggle port: ${data.msg || 'Unknown error'}`);
|
||||
}
|
||||
|
||||
logger.info('Port toggled', { siteId, deviceId, portIndex, enabled });
|
||||
} catch (error) {
|
||||
logger.error('Error toggling port', { siteId, deviceId, portIndex, error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user