forked from technyon/nuki_hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNukiDeviceId.cpp
44 lines (38 loc) · 843 Bytes
/
NukiDeviceId.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstring>
#include <Arduino.h>
#include "NukiDeviceId.h"
#include "PreferencesKeys.h"
NukiDeviceId::NukiDeviceId(Preferences* preferences, const std::string& preferencesId)
: _preferences(preferences),
_preferencesId(preferencesId)
{
_deviceId = _preferences->getUInt(_preferencesId.c_str());
if(_deviceId == 0)
{
assignNewId();
}
}
uint32_t NukiDeviceId::get()
{
return _deviceId;
}
void NukiDeviceId::assignId(const uint32_t& id)
{
_deviceId = id;
_preferences->putUInt(_preferencesId.c_str(), id);
}
void NukiDeviceId::assignNewId()
{
assignId(getRandomId());
}
uint32_t NukiDeviceId::getRandomId()
{
uint8_t rnd[4];
for(int i=0; i<4; i++)
{
rnd[i] = random(255);
}
uint32_t deviceId;
memcpy(&deviceId, &rnd, sizeof(deviceId));
return deviceId;
}