2121
2222class SinricProClass : public EventSender {
2323 public:
24- void begin (String socketAuthToken, String signingKey, String serverURL = SERVER_URL);
25- template <typename DeviceType>
26- DeviceType& add (const char * deviceId, unsigned long eventWaitTime = 1000 );
27- void add (SinricProDevice& newDevice);
28- void add (SinricProDevice* newDevice);
29- void handle ();
30- void stop ();
31- bool isConnected ();
32-
33- DynamicJsonDocument prepareResponse (JsonDocument& requestMessage);
34- DynamicJsonDocument prepareEvent (const char * deviceId, const char * action, const char * cause) override ;
35- void sendEvent (JsonDocument& event) override ;
24+ void begin (String socketAuthToken, String signingKey, String serverURL = SERVER_URL);
25+ template <typename DeviceType>
26+ DeviceType& add (const char * deviceId, unsigned long eventWaitTime = 1000 );
27+
28+ void add (SinricProDevice& newDevice);
29+ void add (SinricProDevice* newDevice);
30+ void handle ();
31+ void stop ();
32+ bool isConnected ();
33+
34+ DynamicJsonDocument prepareResponse (JsonDocument& requestMessage);
35+ DynamicJsonDocument prepareEvent (const char * deviceId, const char * action, const char * cause) override ;
36+ void sendEvent (JsonDocument& event) override ;
37+
38+ struct proxy {
39+ proxy (SinricProClass* ptr, String deviceId) : ptr(ptr), deviceId(deviceId) {}
40+ SinricProClass* ptr;
41+ String deviceId;
42+ template <typename DeviceType>
43+ operator DeviceType&() { return ptr->getDeviceInstance <DeviceType>(deviceId); }
44+ };
45+
46+ proxy operator [](const String deviceId) { return proxy (this , deviceId); }
47+
3648 private:
37- void handleRequest ();
38- void handleSendQueue ();
39- void connect ();
40- void disconnect ();
41- void reconnect ();
42-
43- std::vector<SinricProDevice*> devices;
44- String socketAuthToken;
45- String signingKey;
46- String serverURL;
47-
48- websocketListener _websocketListener;
49- udpListener _udpListener;
50- myNTP _ntp;
51- SinricProQueue_t receiveQueue;
52- SinricProQueue_t sendQueue;
49+ void handleRequest ();
50+ void handleSendQueue ();
51+ void connect ();
52+ void disconnect ();
53+ void reconnect ();
54+ bool checkDeviceId (String deviceId);
55+
56+ SinricProDevice* getDevice (String deviceId);
57+
58+ template <typename DeviceType>
59+ DeviceType& getDeviceInstance (String deviceId) { return (DeviceType&) *getDevice (deviceId); }
60+
61+ std::vector<SinricProDevice*> devices;
62+ String socketAuthToken;
63+ String signingKey;
64+ String serverURL;
65+
66+ websocketListener _websocketListener;
67+ udpListener _udpListener;
68+ myNTP _ntp;
69+ SinricProQueue_t receiveQueue;
70+ SinricProQueue_t sendQueue;
5371};
5472
73+ SinricProDevice* SinricProClass::getDevice (String deviceId) {
74+ for (auto & device : devices) {
75+ if (deviceId == String (device->getDeviceId ())) return device;
76+ }
77+ return nullptr ;
78+ }
79+
5580void SinricProClass::begin (String socketAuthToken, String signingKey, String serverURL) {
5681 this ->socketAuthToken = socketAuthToken;
5782 this ->signingKey = signingKey;
@@ -62,12 +87,15 @@ void SinricProClass::begin(String socketAuthToken, String signingKey, String ser
6287template <typename DeviceType>
6388DeviceType& SinricProClass::add (const char * deviceId, unsigned long eventWaitTime) {
6489 DeviceType* newDevice = new DeviceType (deviceId, eventWaitTime);
65- newDevice->begin (this );
66- devices.push_back (newDevice);
90+ if (checkDeviceId (String (deviceId))){
91+ newDevice->begin (this );
92+ devices.push_back (newDevice);
93+ }
6794 return *newDevice;
6895}
6996
7097void SinricProClass::add (SinricProDevice* newDevice) {
98+ if (!checkDeviceId (String (newDevice->getDeviceId ()))) return ;
7199 newDevice->begin (this );
72100 devices.push_back (newDevice);
73101}
@@ -186,6 +214,23 @@ void SinricProClass::reconnect() {
186214 connect ();
187215}
188216
217+ bool SinricProClass::checkDeviceId (String deviceId) {
218+ if (deviceId.length () != 24 ) {
219+ DEBUG_SINRIC (" [SinricPro.add()]: Invalid deviceId \" %s\" ! Device will be ignored!\r\n " , deviceId.c_str ());
220+ return false ;
221+ }
222+
223+ for (size_t i = 0 ; i < deviceId.length (); i++) {
224+ char current = deviceId[i];
225+ if (current >= ' 0' && current <= ' 9' ) continue ;
226+ if (current >= ' A' && current <= ' F' ) continue ;
227+ if (current >= ' a' && current <= ' f' ) continue ;
228+ DEBUG_SINRIC (" [SinricPro.add()]: Invalid deviceId \" %s\" ! Device will be ignored!\r\n " , deviceId.c_str ());
229+ return false ;
230+ }
231+ return true ;
232+ }
233+
189234
190235void SinricProClass::sendEvent (JsonDocument& event) {
191236 String messageString = signMessage (signingKey, event);
0 commit comments