@@ -63,8 +63,8 @@ class SinricProClass : public SinricProInterface {
63
63
void disconnect ();
64
64
void reconnect ();
65
65
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 " ); }
68
68
69
69
bool verifyDeviceId (const char * id);
70
70
bool verifyAppKey (const char * key);
@@ -74,7 +74,7 @@ class SinricProClass : public SinricProInterface {
74
74
SinricProDeviceInterface* getDevice (String deviceId);
75
75
76
76
template <typename DeviceType>
77
- DeviceType& getDeviceInstance (String deviceId) { return (DeviceType&) * getDevice (deviceId); }
77
+ DeviceType& getDeviceInstance (String deviceId);
78
78
79
79
std::vector<SinricProDeviceInterface*> devices;
80
80
String socketAuthToken;
@@ -99,6 +99,22 @@ SinricProDeviceInterface* SinricProClass::getDevice(String deviceId) {
99
99
return nullptr ;
100
100
}
101
101
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
+
102
118
void SinricProClass::begin (String socketAuthToken, String signingKey, String serverURL) {
103
119
bool success = true ;
104
120
if (!verifyAppKey (socketAuthToken.c_str ())) {
@@ -126,10 +142,11 @@ template <typename DeviceType>
126
142
DeviceType& SinricProClass::add (const char * deviceId, unsigned long eventWaitTime) {
127
143
DeviceType* newDevice = new DeviceType (deviceId, eventWaitTime);
128
144
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);
130
146
newDevice->begin (this );
147
+ if (verifyAppKey (socketAuthToken.c_str ()) && verifyAppSecret (signingKey.c_str ())) _begin = true ;
131
148
} 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);
133
150
}
134
151
devices.push_back (newDevice);
135
152
return *newDevice;
@@ -298,6 +315,7 @@ void SinricProClass::connect() {
298
315
}
299
316
if (i==0 ) { // no device have been added! -> do not connect!
300
317
_begin = false ;
318
+ DEBUG_SINRIC (" [SinricPro]: ERROR! No valid devices available. Please add a valid device first!\r\n " );
301
319
return ;
302
320
}
303
321
@@ -316,11 +334,9 @@ bool SinricProClass::isConnected() {
316
334
317
335
318
336
void SinricProClass::reconnect () {
319
- DEBUG_SINRIC (" SinricProClass. reconnect(): disconnecting\r\n " );
337
+ DEBUG_SINRIC (" SinricPro: reconnect(): disconnecting\r\n " );
320
338
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 " );
324
340
connect ();
325
341
}
326
342
0 commit comments