SinricPro Library
Loading...
Searching...
No Matches
CameraController.h
1#pragma once
2
3#include "../SinricProRequest.h"
4
5#include "../EventLimiter.h"
6#include "../SinricProStrings.h"
7#include "../SinricProNamespace.h"
8
9#include <FS.h>
10
11#if defined(ESP32)
12 #include <HTTPClient.h>
13 #include <WiFiClientSecure.h>
14#endif
15
16namespace SINRICPRO_NAMESPACE {
17
18FSTR(CAMERA, getSnapshot); // "getSnapshot"
19FSTR(CAMERA, POST); // "POST"
20
21using SnapshotCallback = std::function<bool(const String &)>;
22
27template <typename T>
29 public:
31
36 void onSnapshot(SnapshotCallback cb);
37
45
52 int sendMotion(fs::FS &fs, const char * path);
53
54
55 protected:
61 bool handleCameraController(SinricProRequest &request);
62
63 private:
64 SnapshotCallback getSnapshotCallback = nullptr;
65 EventLimiter event_limiter;
66
67#if defined(ESP32)
68 std::unique_ptr<WiFiClient> createClient() {
69#ifdef SINRICPRO_NOSSL
70 auto client = std::make_unique<WiFiClient>();
71#else
72 auto client = std::make_unique<WiFiClientSecure>();
73 client->setInsecure();
74#endif
75 if (client) client->setTimeout(TCP_CONNECTION_TIMEOUT_VALUE);
76 return client;
77 }
78
80#ifdef SINRICPRO_NOSSL
81 return http.begin(*client, SINRICPRO_CAMERA_URL, 80, path, false);
82#else
83 return http.begin(*client, SINRICPRO_CAMERA_URL, 443, path, true);
84#endif
85 }
86
87 void setupHttpHeaders(HTTPClient& http, T* device) {
88 const String& deviceId = device->getDeviceId();
89 String createdAt = String(device->getTimestamp());
90 String signature = device->sign(deviceId + createdAt);
91
92 http.addHeader(FSTR_SINRICPRO_deviceId, deviceId);
93 http.addHeader(FSTR_SINRICPRO_createdAt, createdAt);
94 http.addHeader(FSTR_SINRICPRO_signature, signature);
95 http.setTimeout(HTTP_TIMEOUT_VALUE);
96 }
97#endif
98};
99
100template <typename T>
101CameraController<T>::CameraController()
102: event_limiter (EVENT_LIMIT_STATE) {
103 T *device = static_cast<T *>(this);
104 device->registerRequestHandler(std::bind(&CameraController<T>::handleCameraController, this, std::placeholders::_1));
105}
106
107template <typename T>
108void CameraController<T>::onSnapshot(SnapshotCallback cb) {
109 getSnapshotCallback = cb;
110}
111
112template <typename T>
114 if (event_limiter) return false;
115 T *device = static_cast<T *>(this);
116 bool success = false;
117
118 // Handle getSnapshot action
119 if (request.action == FSTR_CAMERA_getSnapshot) {
120 if (getSnapshotCallback) {
121 success = getSnapshotCallback(device->deviceId);
122 }
123 }
124 return success;
125}
126
127template <typename T>
129
130#if defined(ESP32)
131 if (!buffer || len == 0) return -1;
132
133 T* device = static_cast<T*>(this);
135 auto client = createClient();
136
137 if (!setupHttpConnection(http, client.get(), SINRICPRO_CAMERA_API_SNAPSHOT_PATH)) {
138 http.end();
139 return -1;
140 }
141
143
144 int resCode = http.POST(buffer, len);
145 http.end();
146
147 return resCode;
148#else
149 return -1;
150#endif
151}
152
153template <typename T>
154int CameraController<T>::sendMotion(fs::FS &fs, const char * path) {
155
156#if defined(ESP32)
157 File file = fs.open(path);
158 if (!file) return -1;
159
160 T* device = static_cast<T*>(this);
162 auto client = createClient();
163
164 if (!setupHttpConnection(http, client.get(), SINRICPRO_CAMERA_API_MOTION_PATH)) {
165 http.end();
166 return -1;
167 }
168
170
171 int resCode = http.sendRequest(FSTR_CAMERA_POST, &file, file.size());
172
173 file.close();
174 http.end();
175
176 return resCode;
177#else
178 return -1;
179#endif
180}
181
182} // namespace SINRICPRO_NAMESPACE
183
184template <typename T>
185using CameraController = SINRICPRO_NAMESPACE::CameraController<T>;
AirQuality.
Definition AirQualitySensor.h:19
CameraController class for managing camera operations in SinricPro.
Definition CameraController.h:28
void onSnapshot(SnapshotCallback cb)
Sets the callback function for snapshot requests.
Definition CameraController.h:108
int sendSnapshot(uint8_t *buffer, size_t len)
Sends a camera snapshot to the SinricPro server.
Definition CameraController.h:128
int sendMotion(fs::FS &fs, const char *path)
Sends motion detection data from file.
Definition CameraController.h:154
bool handleCameraController(SinricProRequest &request)
Handles incoming camera control requests.
Definition CameraController.h:113