UNPKG

530 BPlain TextView Raw
1import DeviceId from './types/DeviceId'
2import RandomIntGenerator from './RandomIntGenerator'
3
4const makeUuidv4 = () => {
5 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
6 const r = (Math.random() * 16) | 0
7 const v = c === 'x' ? r : (r & 0x3) | 0x8
8 return v.toString(16)
9 })
10}
11
12export default () => {
13 const uuid = makeUuidv4()
14 const deviceId = uuid
15 const clientId = uuid.substring(0, 19)
16 const mqttId = RandomIntGenerator.generate()
17 return { clientId, deviceId, mqttId } as DeviceId
18}