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
0 commit comments