Skip to content

Instantly share code, notes, and snippets.

@RaresAil
Last active September 30, 2023 21:47
Show Gist options
  • Save RaresAil/f2b735c275784163b6d6e033dd9b9db8 to your computer and use it in GitHub Desktop.
Save RaresAil/f2b735c275784163b6d6e033dd9b9db8 to your computer and use it in GitHub Desktop.
NFC Access Homebridge Minimal example
// After the example from https://github.com/KhaosT/HAP-NodeJS/commit/80cdb1535f5bee874cc06657ef283ee91f258815
import { Service } from 'homebridge';
import Platform, { DevicePlatformAccessory } from '../../platform';
import Accessory from '../../types/Accessory';
export default class NFCDoorAccessory implements Accessory {
private get Characteristic() {
return this.platform.Characteristic;
}
private lockManagementService?: Service;
private lockMechanismService?: Service;
private nfcService?: Service;
public get UUID(): string {
return this.accessory.context.uuid;
}
public handleData(): void {
//
}
constructor(
private readonly platform: Platform,
private readonly accessory: DevicePlatformAccessory
) {
console.log(`Initializing NFC Door`);
try {
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.Characteristic.Manufacturer, 'DEMO')
.setCharacteristic(this.Characteristic.Model, 'DEMO')
.setCharacteristic(this.Characteristic.SerialNumber, 'DEMO');
this.lockManagementService =
this.accessory.getService(this.platform.Service.LockManagement) ||
this.accessory.addService(this.platform.Service.LockManagement);
this.lockMechanismService =
this.accessory.getService(this.platform.Service.LockMechanism) ||
this.accessory.addService(this.platform.Service.LockMechanism);
this.nfcService =
this.accessory.getService(this.platform.Service.NFCAccess) ||
this.accessory.addService(this.platform.Service.NFCAccess);
// nfcService
this.nfcService.setCharacteristic(
this.Characteristic.NFCAccessSupportedConfiguration,
'AQEQAgEQ'
);
this.nfcService
.getCharacteristic(this.Characteristic.ConfigurationState)
.onGet(() => {
console.log('Queried config state');
return 0;
});
this.nfcService
.getCharacteristic(this.Characteristic.NFCAccessControlPoint)
.onSet((value) => {
console.log('Control Point Write: %o' + value);
});
// lockMechanismService
let lockState = this.Characteristic.LockCurrentState.UNSECURED;
const cSate = this.lockMechanismService
.getCharacteristic(this.Characteristic.LockCurrentState)
.onGet(() => {
console.log('Queried lock state: %o', lockState);
return lockState;
});
this.lockMechanismService
.getCharacteristic(this.Characteristic.LockTargetState)
.onSet((value) => {
console.log('Setting lock state to: ' + value);
lockState = parseInt(value.toString(), 10);
setTimeout(() => {
cSate.updateValue(lockState);
}, 1000);
});
// lockManagementService
this.lockManagementService
.getCharacteristic(this.Characteristic.LockControlPoint)
.onSet((value) => {
console.log('Setting lock control point to: ' + value);
});
this.lockManagementService
.getCharacteristic(this.Characteristic.Version)
.onGet(() => {
console.log('Query lock management version');
return 'lk-1.0';
});
} catch (error: any) {
platform.log.error(`Error: ${error?.message}`);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment