import { padStart } from "alcalzone-shared/strings" ; import { ZWaveError , ZWaveErrorCodes } from "../error/ZWaveError" ; export function isValidDSK ( dsk ) { return /^(\d{5}-){7}\d{5}$/ . test ( dsk ) ; } export function dskToString ( dsk ) { if ( dsk . length !== 16 ) { throw new ZWaveError ( `DSK length must be 16 bytes, got ${ dsk . length }` , ZWaveErrorCodes . Argument_Invalid ) ; } let ret = "" ; for ( let i = 0 ; i < 16 ; i += 2 ) { if ( i > 0 ) ret += "-" ; ret += padStart ( dsk . readUInt16BE ( i ) . toString ( 10 ) , 5 , "0" ) ; } return ret ; } export function dskFromString ( dsk ) { if ( ! isValidDSK ( dsk ) ) { throw new ZWaveError ( `The DSK must be in the form "aaaaa-bbbbb-ccccc-ddddd-eeeee-fffff-11111-22222"` , ZWaveErrorCodes . Argument_Invalid ) ; } const ret = Buffer . allocUnsafe ( 16 ) ; const parts = dsk . split ( "-" ) ; for ( let i = 0 ; i < 8 ; i ++ ) { const partAsNumber = parseInt ( parts [ i ] , 10 ) ; ret . writeUInt16BE ( partAsNumber , i * 2 ) ; } return ret ; } export function nwiHomeIdFromDSK ( dsk ) { const ret = Buffer . allocUnsafe ( 4 ) ; dsk . copy ( ret , 0 , 8 , 12 ) ; ret [ 0 ] |= 0b11000000 ; ret [ 3 ] &= 0b11111110 ; return ret ; } export function authHomeIdFromDSK ( dsk ) { const ret = Buffer . allocUnsafe ( 4 ) ; dsk . copy ( ret , 0 , 12 , 16 ) ; ret [ 0 ] &= 0b00111111 ; ret [ 3 ] |= 0b00000001 ; return ret ; }