Skip to content

Commit f44b034

Browse files
committed
Implemented CameraController
1 parent 40a0d69 commit f44b034

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/Capabilities/CameraController.h

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#pragma once
2+
3+
#include "../EventLimiter.h"
4+
#include "../SinricProNamespace.h"
5+
#include "../SinricProRequest.h"
6+
#include "../SinricProStrings.h"
7+
#include <HTTPClient.h>
8+
#include <WiFiClientSecure.h>
9+
10+
namespace SINRICPRO_NAMESPACE {
11+
12+
FSTR(CAMERA, getSnapshot); // "getSnapshot"
13+
14+
using SnapshotCallback = std::function<bool(const String &)>;
15+
16+
template <typename T>
17+
class CameraController {
18+
public:
19+
CameraController();
20+
void onSnapshot(SnapshotCallback cb);
21+
int uploadImage(uint8_t* buffer, size_t len);
22+
23+
protected:
24+
bool handleCameraController(SinricProRequest &request);
25+
26+
private:
27+
SnapshotCallback getSnapshotCallback = nullptr;
28+
};
29+
30+
template <typename T>
31+
CameraController<T>::CameraController() {
32+
T *device = static_cast<T *>(this);
33+
device->registerRequestHandler(std::bind(&CameraController<T>::handleCameraController, this, std::placeholders::_1));
34+
}
35+
36+
template <typename T>
37+
void CameraController<T>::onSnapshot(SnapshotCallback cb) {
38+
getSnapshotCallback = cb;
39+
}
40+
41+
template <typename T>
42+
bool CameraController<T>::handleCameraController(SinricProRequest &request) {
43+
T *device = static_cast<T *>(this);
44+
45+
bool success = false;
46+
47+
if (request.action == FSTR_CAMERA_getSnapshot) {
48+
if (getSnapshotCallback) {
49+
success = getSnapshotCallback(device->deviceId);
50+
}
51+
}
52+
53+
return success;
54+
}
55+
56+
template <typename T>
57+
int CameraController<T>::uploadImage(uint8_t* buffer, size_t len) {
58+
T *device = static_cast<T *>(this);
59+
60+
if (!buffer) return -1;
61+
62+
WiFiClientSecure client;
63+
client.setInsecure();
64+
65+
HTTPClient http;
66+
if (!http.begin(client, SINRICPRO_CAMERA_URL, 443, "/snapshot", true)) return -1;
67+
68+
const String& deviceId = device->getDeviceId();
69+
String timestamp = String(device->getTimestamp());
70+
String signature = device->sign(deviceId+timestamp);
71+
72+
http.addHeader("deviceid", deviceId);
73+
http.addHeader("timestamp", timestamp);
74+
http.addHeader("signature", signature);
75+
76+
int resCode = http.POST(buffer, len);
77+
http.PUT(buffer, len);
78+
http.end();
79+
80+
return resCode;
81+
}
82+
83+
} // namespace SINRICPRO_NAMESPACE

src/SinricProCamera.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Capabilities/SettingController.h"
1212
#include "Capabilities/PushNotification.h"
1313
#include "Capabilities/PowerStateController.h"
14+
#include "Capabilities/CameraController.h"
1415

1516
#include "SinricProNamespace.h"
1617
namespace SINRICPRO_NAMESPACE {
@@ -23,10 +24,12 @@ namespace SINRICPRO_NAMESPACE {
2324
class SinricProCamera : public SinricProDevice,
2425
public SettingController<SinricProCamera>,
2526
public PushNotification<SinricProCamera>,
26-
public PowerStateController<SinricProCamera> {
27+
public PowerStateController<SinricProCamera>,
28+
public CameraController<SinricProCamera> {
2729
friend class SettingController<SinricProCamera>;
2830
friend class PushNotification<SinricProCamera>;
2931
friend class PowerStateController<SinricProCamera>;
32+
friend class CameraController<SinricProCamera>;
3033
public:
3134
SinricProCamera(const String &deviceId) : SinricProDevice(deviceId, "CAMERA") {}
3235
};

0 commit comments

Comments
 (0)