Files
unifi/ts/classes.host.ts

86 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

import type { UnifiAccount } from './classes.unifi-account.js';
import type { IUnifiHost } from './interfaces/index.js';
/**
* Represents a UniFi host device from Site Manager
*/
export class UnifiHost implements IUnifiHost {
/** Reference to parent account */
private unifiAccount?: UnifiAccount;
// IUnifiHost properties
public id: string;
public hardwareId?: string;
public name?: string;
public type?: string;
public firmwareVersion?: string;
public isOnline?: boolean;
public ipAddress?: string;
public macAddress?: string;
public siteId?: string;
public status?: {
state?: string;
lastSeen?: string;
};
public features?: string[];
public reportedState?: Record<string, unknown>;
constructor() {
this.id = '';
}
/**
* Create a host instance from API response object
*/
public static createFromApiObject(
apiObject: IUnifiHost,
unifiAccount?: UnifiAccount
): UnifiHost {
const host = new UnifiHost();
Object.assign(host, apiObject);
host.unifiAccount = unifiAccount;
return host;
}
/**
* Get the raw API object representation
*/
public toApiObject(): IUnifiHost {
return {
id: this.id,
hardwareId: this.hardwareId,
name: this.name,
type: this.type,
firmwareVersion: this.firmwareVersion,
isOnline: this.isOnline,
ipAddress: this.ipAddress,
macAddress: this.macAddress,
siteId: this.siteId,
status: this.status,
features: this.features,
reportedState: this.reportedState,
};
}
/**
* Check if host is online
*/
public checkOnline(): boolean {
return this.isOnline === true;
}
/**
* Get host type (e.g., 'udm-pro', 'cloud-key')
*/
public getType(): string {
return this.type || 'unknown';
}
/**
* Check if host supports a specific feature
*/
public hasFeature(feature: string): boolean {
return this.features?.includes(feature) ?? false;
}
}