156 lines
3.7 KiB
TypeScript
156 lines
3.7 KiB
TypeScript
import type { UnifiAccess } from './classes.unifi-access.js';
|
|
import type { IAccessDoor, IAccessDoorRule, IAccessFloor } from './interfaces/index.js';
|
|
|
|
/**
|
|
* Represents a UniFi Access door
|
|
*/
|
|
export class UnifiDoor implements IAccessDoor {
|
|
/** Reference to parent access instance */
|
|
private access?: UnifiAccess;
|
|
|
|
// IAccessDoor properties
|
|
public unique_id: string;
|
|
public name: string;
|
|
public alias?: string;
|
|
public door_type?: string;
|
|
public door_lock_relay_status?: 'lock' | 'unlock';
|
|
public door_position_status?: 'open' | 'close';
|
|
public device_id?: string;
|
|
public camera_resource_id?: string;
|
|
public location_id?: string;
|
|
public full_name?: string;
|
|
public extra_type?: string;
|
|
public door_guard?: boolean;
|
|
public rules?: IAccessDoorRule[];
|
|
public level_id?: string;
|
|
public floor?: IAccessFloor;
|
|
|
|
constructor() {
|
|
this.unique_id = '';
|
|
this.name = '';
|
|
}
|
|
|
|
/**
|
|
* Create a door instance from API response object
|
|
*/
|
|
public static createFromApiObject(
|
|
apiObject: IAccessDoor,
|
|
access?: UnifiAccess
|
|
): UnifiDoor {
|
|
const door = new UnifiDoor();
|
|
Object.assign(door, apiObject);
|
|
door.access = access;
|
|
return door;
|
|
}
|
|
|
|
/**
|
|
* Get the raw API object representation
|
|
*/
|
|
public toApiObject(): IAccessDoor {
|
|
return {
|
|
unique_id: this.unique_id,
|
|
name: this.name,
|
|
alias: this.alias,
|
|
door_type: this.door_type,
|
|
door_lock_relay_status: this.door_lock_relay_status,
|
|
door_position_status: this.door_position_status,
|
|
device_id: this.device_id,
|
|
camera_resource_id: this.camera_resource_id,
|
|
location_id: this.location_id,
|
|
full_name: this.full_name,
|
|
extra_type: this.extra_type,
|
|
door_guard: this.door_guard,
|
|
rules: this.rules,
|
|
level_id: this.level_id,
|
|
floor: this.floor,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Get display name (alias or name)
|
|
*/
|
|
public getDisplayName(): string {
|
|
return this.alias || this.name;
|
|
}
|
|
|
|
/**
|
|
* Check if door is currently locked
|
|
*/
|
|
public isLocked(): boolean {
|
|
return this.door_lock_relay_status === 'lock';
|
|
}
|
|
|
|
/**
|
|
* Check if door is currently open (contact sensor)
|
|
*/
|
|
public isOpen(): boolean {
|
|
return this.door_position_status === 'open';
|
|
}
|
|
|
|
/**
|
|
* Check if door is currently closed
|
|
*/
|
|
public isClosed(): boolean {
|
|
return this.door_position_status === 'close';
|
|
}
|
|
|
|
/**
|
|
* Unlock the door
|
|
*/
|
|
public async unlock(): Promise<void> {
|
|
if (!this.access) {
|
|
throw new Error('Cannot unlock door: no access reference');
|
|
}
|
|
|
|
await this.access.unlockDoor(this.unique_id);
|
|
this.door_lock_relay_status = 'unlock';
|
|
}
|
|
|
|
/**
|
|
* Lock the door
|
|
*/
|
|
public async lock(): Promise<void> {
|
|
if (!this.access) {
|
|
throw new Error('Cannot lock door: no access reference');
|
|
}
|
|
|
|
await this.access.lockDoor(this.unique_id);
|
|
this.door_lock_relay_status = 'lock';
|
|
}
|
|
|
|
/**
|
|
* Update door settings
|
|
*/
|
|
public async updateSettings(settings: Partial<IAccessDoor>): Promise<void> {
|
|
if (!this.access) {
|
|
throw new Error('Cannot update door: no access reference');
|
|
}
|
|
|
|
await this.access.request('PUT', `/door/${this.unique_id}`, settings);
|
|
Object.assign(this, settings);
|
|
}
|
|
|
|
/**
|
|
* Rename the door
|
|
*/
|
|
public async rename(newName: string): Promise<void> {
|
|
await this.updateSettings({ name: newName });
|
|
}
|
|
|
|
/**
|
|
* Set door alias
|
|
*/
|
|
public async setAlias(alias: string): Promise<void> {
|
|
await this.updateSettings({ alias });
|
|
}
|
|
|
|
/**
|
|
* Get door status summary
|
|
*/
|
|
public getStatus(): string {
|
|
const lockStatus = this.isLocked() ? 'Locked' : 'Unlocked';
|
|
const positionStatus = this.isOpen() ? 'Open' : 'Closed';
|
|
return `${lockStatus}, ${positionStatus}`;
|
|
}
|
|
}
|