Skip to content

Commit 1f42981

Browse files
committed
New interfaces introduced:
- SinricProInterface - SinricProDeviceInterface Marked deprecated functions: - add(SinricProDeviceInterface& newDevice); - add(SinricProDeviceInterface* newDevice);
1 parent 01bbbf7 commit 1f42981

5 files changed

+46
-24
lines changed

src/SinricPro.h

+18-12
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#ifndef _SINRIC_H_
99
#define _SINRIC_H_
1010

11-
#include "SinricProEventSender.h"
12-
#include "SinricProDevice.h"
11+
#include "SinricProInterface.h"
12+
#include "SinricProDeviceInterface.h"
1313
#include "SinricProWebsocket.h"
1414
#include "SinricProUDP.h"
1515
#include "SinricProSignature.h"
@@ -19,14 +19,14 @@
1919

2020
#include "extralib/ESPTrueRandom/ESPTrueRandom.h"
2121

22-
class SinricProClass : public EventSender {
22+
class SinricProClass : public SinricProInterface {
2323
public:
2424
void begin(String socketAuthToken, String signingKey, String serverURL = SERVER_URL);
2525
template <typename DeviceType>
2626
DeviceType& add(const char* deviceId, unsigned long eventWaitTime = 1000);
2727

28-
void add(SinricProDevice& newDevice);
29-
void add(SinricProDevice* newDevice);
28+
void add(SinricProDeviceInterface& newDevice);
29+
void add(SinricProDeviceInterface* newDevice);
3030
void handle();
3131
void stop();
3232
bool isConnected();
@@ -40,7 +40,9 @@ class SinricProClass : public EventSender {
4040
SinricProClass* ptr;
4141
String deviceId;
4242
template <typename DeviceType>
43-
operator DeviceType&() { return ptr->getDeviceInstance<DeviceType>(deviceId); }
43+
operator DeviceType&() { return as<DeviceType>(); }
44+
template <typename DeviceType>
45+
DeviceType& as() { return ptr->getDeviceInstance<DeviceType>(deviceId); }
4446
};
4547

4648
proxy operator[](const String deviceId) { return proxy(this, deviceId); }
@@ -53,12 +55,12 @@ class SinricProClass : public EventSender {
5355
void reconnect();
5456
bool checkDeviceId(String deviceId);
5557

56-
SinricProDevice* getDevice(String deviceId);
58+
SinricProDeviceInterface* getDevice(String deviceId);
5759

5860
template <typename DeviceType>
5961
DeviceType& getDeviceInstance(String deviceId) { return (DeviceType&) *getDevice(deviceId); }
6062

61-
std::vector<SinricProDevice*> devices;
63+
std::vector<SinricProDeviceInterface*> devices;
6264
String socketAuthToken;
6365
String signingKey;
6466
String serverURL;
@@ -70,7 +72,7 @@ class SinricProClass : public EventSender {
7072
SinricProQueue_t sendQueue;
7173
};
7274

73-
SinricProDevice* SinricProClass::getDevice(String deviceId) {
75+
SinricProDeviceInterface* SinricProClass::getDevice(String deviceId) {
7476
for (auto& device : devices) {
7577
if (deviceId == String(device->getDeviceId())) return device;
7678
}
@@ -94,14 +96,18 @@ DeviceType& SinricProClass::add(const char* deviceId, unsigned long eventWaitTim
9496
return *newDevice;
9597
}
9698

97-
void SinricProClass::add(SinricProDevice* newDevice) {
99+
__attribute__ ((deprecated("Please use DeviceType& myDevice = SinricPro.add<DeviceType>(DeviceId);")))
100+
void SinricProClass::add(SinricProDeviceInterface* newDevice) {
98101
if (!checkDeviceId(String(newDevice->getDeviceId()))) return;
99102
newDevice->begin(this);
100103
devices.push_back(newDevice);
101104
}
102105

103-
void SinricProClass::add(SinricProDevice& newDevice) {
104-
add(&newDevice);
106+
__attribute__ ((deprecated("Please use DeviceType& myDevice = SinricPro.add<DeviceType>(DeviceId);")))
107+
void SinricProClass::add(SinricProDeviceInterface& newDevice) {
108+
if (!checkDeviceId(String(newDevice.getDeviceId()))) return;
109+
newDevice.begin(this);
110+
devices.push_back(&newDevice);
105111
}
106112

107113
void SinricProClass::handle() {

src/SinricProDevice.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
#ifndef _SINRICDEVICE_H_
99
#define _SINRICDEVICE_H_
1010

11-
#include "SinricProEventSender.h"
11+
#include "SinricProDeviceInterface.h"
1212
#include <map>
1313

14-
class SinricProDevice {
14+
class SinricProDevice : public SinricProDeviceInterface {
1515
public:
1616
SinricProDevice(const char* newDeviceId, unsigned long eventWaitTime=100);
1717
virtual ~SinricProDevice();
1818
virtual bool handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) { return false; };
19-
const char* getDeviceId();
20-
void begin(EventSender* eventSender);
19+
virtual const char* getDeviceId();
20+
virtual void begin(SinricProInterface* eventSender);
2121
protected:
22+
virtual bool sendEvent(JsonDocument& event);
23+
virtual DynamicJsonDocument prepareEvent(const char* deviceId, const char* action, const char* cause);
2224
char* deviceId;
23-
bool sendEvent(JsonDocument& event);
24-
DynamicJsonDocument prepareEvent(const char* deviceId, const char* action, const char* cause);
2525
private:
26-
EventSender* eventSender;
26+
SinricProInterface* eventSender;
2727
unsigned long eventWaitTime;
2828
std::map<String, unsigned long> eventFilter;
2929
};
@@ -39,7 +39,7 @@ SinricProDevice::~SinricProDevice() {
3939
if (deviceId) free(deviceId);
4040
}
4141

42-
void SinricProDevice::begin(EventSender* eventSender) {
42+
void SinricProDevice::begin(SinricProInterface* eventSender) {
4343
this->eventSender = eventSender;
4444
}
4545

src/SinricProDeviceInterface.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef _SINRICPRODEVICEINTERFACE_
2+
#define _SINRICPRODEVICEINTERFACE_
3+
4+
#include <SinricProInterface.h>
5+
6+
class SinricProDeviceInterface {
7+
public:
8+
virtual bool handleRequest(const char* deviceId, const char* action, JsonObject &request_value, JsonObject &response_value) = 0;
9+
virtual const char* getDeviceId() = 0;
10+
virtual void begin(SinricProInterface* eventSender) = 0;
11+
protected:
12+
virtual bool sendEvent(JsonDocument& event) = 0;
13+
virtual DynamicJsonDocument prepareEvent(const char* deviceId, const char* action, const char* cause) = 0;
14+
};
15+
16+
#endif

src/SinricProEventSender.h renamed to src/SinricProInterface.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* This file is part of the Sinric Pro (https://github.com/sinricpro/)
66
*/
77

8-
#ifndef _EVENTSENDER_H_
9-
#define _EVENTSENDER_H_
8+
#ifndef _SINRICPRO_INTERFACE_H_
9+
#define _SINRICPRO_INTERFACE_H_
1010

1111
#include "ArduinoJson.h"
1212
#include "SinricProQueue.h"
1313

14-
class EventSender {
14+
class SinricProInterface {
1515
public:
1616
virtual void sendEvent(JsonDocument& jsonEvent);
1717
virtual DynamicJsonDocument prepareEvent(const char* deviceId, const char* action, const char* cause);

src/SinricProWebsocket.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include "SinricProDebug.h"
2222
#include "SinricProConfig.h"
2323
#include "SinricProQueue.h"
24-
#include "SinricProEventSender.h"
24+
#include "SinricProInterface.h"
2525

2626
class websocketListener
2727
{

0 commit comments

Comments
 (0)