Skip to content

Commit 341c5cc

Browse files
committed
feat: comments + http/https support.
1 parent 7171300 commit 341c5cc

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed

src/Capabilities/CameraController.h

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
11
#pragma once
22

3+
// Required header includes
34
#include "../EventLimiter.h"
45
#include "../SinricProNamespace.h"
56
#include "../SinricProRequest.h"
67
#include "../SinricProStrings.h"
78
#include <HTTPClient.h>
8-
#include <WiFiClientSecure.h>
9+
10+
#if defined(ESP32)
11+
#include <WiFiClientSecure.h>
12+
#endif
913

1014
namespace SINRICPRO_NAMESPACE {
1115

1216
FSTR(CAMERA, getSnapshot); // "getSnapshot"
1317

1418
using SnapshotCallback = std::function<bool(const String &)>;
1519

20+
/**
21+
* @brief Camera
22+
* @ingroup Capabilities
23+
**/
1624
template <typename T>
1725
class CameraController {
1826
public:
1927
CameraController();
28+
29+
/**
30+
* @brief Sets the callback function for snapshot requests
31+
* @param cb Callback function to handle snapshot requests
32+
*/
2033
void onSnapshot(SnapshotCallback cb);
34+
35+
/**
36+
* @brief Sends a camera snapshot to the SinricPro server
37+
* @param buffer Pointer to the image data buffer
38+
* @param len Length of the image data in bytes
39+
* @return HTTP response code
40+
*/
2141
int sendSnapshot(uint8_t* buffer, size_t len);
2242

2343
protected:
44+
/**
45+
* @brief Handles incoming camera control requests
46+
* @param request The incoming request object
47+
* @return true if request was handled successfully, false otherwise
48+
*/
2449
bool handleCameraController(SinricProRequest &request);
2550

2651
private:
52+
// Callback handler for snapshot requests
2753
SnapshotCallback getSnapshotCallback = nullptr;
2854
};
2955

@@ -41,29 +67,47 @@ void CameraController<T>::onSnapshot(SnapshotCallback cb) {
4167
template <typename T>
4268
bool CameraController<T>::handleCameraController(SinricProRequest &request) {
4369
T *device = static_cast<T *>(this);
44-
4570
bool success = false;
4671

72+
// Handle getSnapshot action
4773
if (request.action == FSTR_CAMERA_getSnapshot) {
4874
if (getSnapshotCallback) {
4975
success = getSnapshotCallback(device->deviceId);
5076
}
5177
}
52-
5378
return success;
5479
}
5580

5681
template <typename T>
5782
int CameraController<T>::sendSnapshot(uint8_t* buffer, size_t len) {
58-
T *device = static_cast<T *>(this);
59-
60-
if (!buffer) return -1;
83+
int resCode = -1;
6184

62-
WiFiClientSecure client;
63-
client.setInsecure();
85+
#if defined(ESP32)
86+
T *device = static_cast<T *>(this);
87+
88+
// Validate input buffer
89+
if (!buffer) return resCode;
6490

6591
HTTPClient http;
66-
if (!http.begin(client, SINRICPRO_CAMERA_URL, 443, SINRICPRO_CAMERA_PATH, true)) return -1;
92+
bool beginSuccess = false;
93+
94+
#ifdef SINRICPRO_NOSSL
95+
WiFiClient *client = new WiFiClient();
96+
if (!client) return resCode;
97+
98+
beginSuccess = http.begin(*client, SINRICPRO_CAMERA_URL, 80, SINRICPRO_CAMERA_PATH, false);
99+
#else
100+
WiFiClientSecure *secureClient = new WiFiClientSecure();
101+
if (!secureClient) return resCode;
102+
103+
secureClient->setInsecure(); // Skip certificate validation
104+
beginSuccess = http.begin(*secureClient, SINRICPRO_CAMERA_URL, 443, SINRICPRO_CAMERA_PATH, true);
105+
#endif
106+
107+
if (!beginSuccess) {
108+
http.end();
109+
return resCode;
110+
}
67111

68112
const String& deviceId = device->getDeviceId();
69113
String createdAt = String(device->getTimestamp());
@@ -73,9 +117,10 @@ int CameraController<T>::sendSnapshot(uint8_t* buffer, size_t len) {
73117
http.addHeader(FSTR_SINRICPRO_createdAt, createdAt);
74118
http.addHeader(FSTR_SINRICPRO_signature, signature);
75119

76-
int resCode = http.POST(buffer, len);
120+
resCode = http.POST(buffer, len);
77121
http.end();
78-
122+
#endif
123+
79124
return resCode;
80125
}
81126

0 commit comments

Comments
 (0)