feat(unifi): implement comprehensive UniFi API client with controllers, protect, access, account, managers, resources, HTTP client, interfaces, logging, plugins, and tests
This commit is contained in:
62
ts/classes.sitemanager.ts
Normal file
62
ts/classes.sitemanager.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import type { UnifiAccount } from './classes.unifi-account.js';
|
||||
import { UnifiSite } from './classes.site.js';
|
||||
import type { IUnifiSite, ISiteManagerListResponse } from './interfaces/index.js';
|
||||
import { logger } from './unifi.logger.js';
|
||||
|
||||
/**
|
||||
* Manager for UniFi sites via Site Manager API
|
||||
*/
|
||||
export class SiteManager {
|
||||
private unifiAccount: UnifiAccount;
|
||||
|
||||
constructor(unifiAccount: UnifiAccount) {
|
||||
this.unifiAccount = unifiAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all sites
|
||||
*/
|
||||
public async listSites(): Promise<UnifiSite[]> {
|
||||
logger.log('debug', 'Fetching all sites from Site Manager');
|
||||
|
||||
const response = await this.unifiAccount.request<ISiteManagerListResponse<IUnifiSite>>(
|
||||
'GET',
|
||||
'/ea/sites'
|
||||
);
|
||||
|
||||
const sites: UnifiSite[] = [];
|
||||
for (const siteData of response.data || []) {
|
||||
sites.push(UnifiSite.createFromApiObject(siteData, this.unifiAccount));
|
||||
}
|
||||
|
||||
logger.log('info', `Found ${sites.length} sites`);
|
||||
return sites;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a site by ID
|
||||
*/
|
||||
public async getSiteById(siteId: string): Promise<UnifiSite | null> {
|
||||
logger.log('debug', `Fetching site: ${siteId}`);
|
||||
|
||||
try {
|
||||
const response = await this.unifiAccount.request<IUnifiSite>(
|
||||
'GET',
|
||||
`/ea/sites/${siteId}`
|
||||
);
|
||||
|
||||
return UnifiSite.createFromApiObject(response, this.unifiAccount);
|
||||
} catch (error) {
|
||||
logger.log('warn', `Site not found: ${siteId}`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a site by name
|
||||
*/
|
||||
public async findSiteByName(name: string): Promise<UnifiSite | null> {
|
||||
const sites = await this.listSites();
|
||||
return sites.find((site) => site.name === name) || null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user