import * as ip from 'ip-address' ; import { PROLINK_HEADER } from 'src/constants' ; import { Device } from 'src/types' ; /**
 * Converts a announce packet to a device object.
 */ export function deviceFromPacket ( packet ) { if ( packet . indexOf ( PROLINK_HEADER ) !== 0 ) { throw new Error ( 'Announce packet does not start with expected header' ) ; } if ( packet [ 0x0a ] !== 0x06 ) { return null ; } const name = packet . slice ( 0x0c , 0x0c + 20 ) . toString ( ) . replace ( /\0/g , '' ) ; const device = { name , id : packet [ 0x24 ] , type : packet [ 0x34 ] , macAddr : new Uint8Array ( packet . slice ( 0x26 , 0x26 + 6 ) ) , ip : ip . Address4 . fromInteger ( packet . readUInt32BE ( 0x2c ) ) , } ; return device ; }