Skip to content

Commit 5c9e96b

Browse files
committed
Automatic device add
DeviceType& myDevice = SinricPro[DEVICEID]; will return new device if device doesn't exist yet
1 parent c99a12b commit 5c9e96b

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,13 @@ bool onPowerState(const String deviceId, bool &state) {
7474
## How to add a device?
7575
Syntax is
7676
```C++
77-
DeviceType& myDevice = SinricPro.add<DeviceType>(DEVICE_ID);
77+
DeviceType& myDevice = SinricPro[DEVICE_ID];
7878
```
7979
Example
80+
```C++
81+
SinricProSwitch& mySwitch = SinricPro["YOUR-SWITCH-ID-HERE"];
82+
```
83+
*Example 2 (alternatively)*
8084
```C++
8185
SinricProSwitch& mySwitch = SinricPro.add<SinricProSwitch>("YOUR-SWITCH-ID-HERE");
8286
```
@@ -93,7 +97,7 @@ Example 1
9397
myDoorbell.sendDoorbellEvent();
9498
```
9599

96-
Example 2 (alternatively)
100+
*Example 2 (alternatively)*
97101
```C++
98102
SinricPro["YOUR-DOORBELL-ID-HERE"].as<SinricProDoorbell>().sendDoorbellEvent();
99103
```

src/SinricPro.h

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class SinricProClass : public SinricProInterface {
6363
void disconnect();
6464
void reconnect();
6565

66-
void onConnect() { DEBUG_SINRIC("[SinricPro:onConnect()]\r\n"); }
67-
void onDisconnect() { DEBUG_SINRIC("[SinricPro:onDisconnect()]\r\n"); }
66+
void onConnect() { DEBUG_SINRIC("[SinricPro]: Connected to \"%s\"!]\r\n", serverURL.c_str()); }
67+
void onDisconnect() { DEBUG_SINRIC("[SinricPro]: Disconnect\r\n"); }
6868

6969
bool verifyDeviceId(const char* id);
7070
bool verifyAppKey(const char* key);
@@ -74,7 +74,7 @@ class SinricProClass : public SinricProInterface {
7474
SinricProDeviceInterface* getDevice(String deviceId);
7575

7676
template <typename DeviceType>
77-
DeviceType& getDeviceInstance(String deviceId) { return (DeviceType&) *getDevice(deviceId); }
77+
DeviceType& getDeviceInstance(String deviceId);
7878

7979
std::vector<SinricProDeviceInterface*> devices;
8080
String socketAuthToken;
@@ -99,6 +99,22 @@ SinricProDeviceInterface* SinricProClass::getDevice(String deviceId) {
9999
return nullptr;
100100
}
101101

102+
template <typename DeviceType>
103+
DeviceType& SinricProClass::getDeviceInstance(String deviceId) {
104+
DeviceType* tmp_device = (DeviceType*) getDevice(deviceId);
105+
if (tmp_device) return *tmp_device;
106+
107+
DEBUG_SINRIC("[SinricPro]: Device \"%s\" does not exist. Creating new device\r\n", deviceId.c_str());
108+
DeviceType& tmp_deviceInstance = add<DeviceType>(deviceId.c_str());
109+
110+
if (isConnected()) {
111+
DEBUG_SINRIC("[SinricPro]: Reconnecting to server.\r\n");
112+
reconnect();
113+
}
114+
115+
return tmp_deviceInstance;
116+
}
117+
102118
void SinricProClass::begin(String socketAuthToken, String signingKey, String serverURL) {
103119
bool success = true;
104120
if (!verifyAppKey(socketAuthToken.c_str())) {
@@ -126,10 +142,11 @@ template <typename DeviceType>
126142
DeviceType& SinricProClass::add(const char* deviceId, unsigned long eventWaitTime) {
127143
DeviceType* newDevice = new DeviceType(deviceId, eventWaitTime);
128144
if (verifyDeviceId(deviceId)){
129-
DEBUG_SINRIC("[SinricPro:add(\"%s\")]: Adding device with id \"%s\".\r\n", deviceId, deviceId);
145+
DEBUG_SINRIC("[SinricPro:add()]: Adding device with id \"%s\".\r\n", deviceId);
130146
newDevice->begin(this);
147+
if (verifyAppKey(socketAuthToken.c_str()) && verifyAppSecret(signingKey.c_str())) _begin = true;
131148
} else {
132-
DEBUG_SINRIC("[SinricPro:add(\"%s\")]: DeviceId \"%s\" is invalid!! Device will be ignored and will NOT WORK!\r\n", deviceId, deviceId);
149+
DEBUG_SINRIC("[SinricPro:add()]: DeviceId \"%s\" is invalid!! Device will be ignored and will NOT WORK!\r\n", deviceId);
133150
}
134151
devices.push_back(newDevice);
135152
return *newDevice;
@@ -298,6 +315,7 @@ void SinricProClass::connect() {
298315
}
299316
if (i==0) { // no device have been added! -> do not connect!
300317
_begin = false;
318+
DEBUG_SINRIC("[SinricPro]: ERROR! No valid devices available. Please add a valid device first!\r\n");
301319
return;
302320
}
303321

@@ -316,11 +334,9 @@ bool SinricProClass::isConnected() {
316334

317335

318336
void SinricProClass::reconnect() {
319-
DEBUG_SINRIC("SinricProClass.reconnect(): disconnecting\r\n");
337+
DEBUG_SINRIC("SinricPro:reconnect(): disconnecting\r\n");
320338
stop();
321-
DEBUG_SINRIC("SinricProClass.reconnect(): wait 1second\r\n");
322-
delay(1000);
323-
DEBUG_SINRIC("SinricProClass.reconnect(): connecting\r\n");
339+
DEBUG_SINRIC("SinricPro:reconnect(): connecting\r\n");
324340
connect();
325341
}
326342

0 commit comments

Comments
 (0)