HueCommandAPI.h
6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
\file HueCommandAPI.h
Copyright Notice\n
Copyright (C) 2018 Jan Rogall - developer\n
Copyright (C) 2018 Moritz Wirger - developer\n
This file is part of hueplusplus.
hueplusplus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
hueplusplus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
**/
#ifndef _HUECOMMANDAPI_H
#define _HUECOMMANDAPI_H
#include <atomic>
#include <chrono>
#include <mutex>
#include "HueException.h"
#include "IHttpHandler.h"
namespace hueplusplus
{
//! Handles communication to the bridge via IHttpHandler and enforces a timeout
//! between each request
class HueCommandAPI
{
public:
//! \brief Construct from ip, username and HttpHandler
//!
//! \param ip ip address of the Hue bridge in dotted decimal notation like "192.168.2.1"
//! \param port of the hue bridge
//! \param username username that is used to control the bridge
//! \param httpHandler HttpHandler for communication with the bridge
HueCommandAPI(const std::string& ip, int port, const std::string& username,
std::shared_ptr<const IHttpHandler> httpHandler);
//! \brief Copy construct from other HueCommandAPI
//! \note All copies refer to the same timeout data, so even calls from different objects will be delayed
HueCommandAPI(const HueCommandAPI&) = default;
//! \brief Move construct from other HueCommandAPI
//! \note All copies refer to the same timeout data, so even calls from different objects will be delayed
HueCommandAPI(HueCommandAPI&&) = default;
//! \brief Copy assign from other HueCommandAPI
//! \note All copies refer to the same timeout data, so even calls from different objects will be delayed
HueCommandAPI& operator=(const HueCommandAPI&) = default;
//! \brief Move assign from other HueCommandAPI
//! \note All copies refer to the same timeout data, so even calls from different objects will be delayed
HueCommandAPI& operator=(HueCommandAPI&&) = default;
//! \brief Sends a HTTP PUT request to the bridge and returns the response
//!
//! This function will block until at least \ref minDelay has passed to any previous request
//! \param path API request path (appended after /api/{username})
//! \param request Request to the api, may be empty
//! \returns The return value of the underlying \ref IHttpHandler::PUTJson call
//! \throws std::system_error when system or socket operations fail
//! \throws HueException when response contains no body
//! \throws HueAPIResponseException when response contains an error
nlohmann::json PUTRequest(const std::string& path, const nlohmann::json& request) const;
nlohmann::json PUTRequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const;
//! \brief Sends a HTTP GET request to the bridge and returns the response
//!
//! This function will block until at least \ref minDelay has passed to any previous request
//! \param path API request path (appended after /api/{username})
//! \param request Request to the api, may be empty
//! \returns The return value of the underlying \ref IHttpHandler::GETJson call
//! \throws std::system_error when system or socket operations fail
//! \throws HueException when response contains no body
//! \throws HueAPIResponseException when response contains an error
nlohmann::json GETRequest(const std::string& path, const nlohmann::json& request) const;
nlohmann::json GETRequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const;
//! \brief Sends a HTTP DELETE request to the bridge and returns the response
//!
//! This function will block until at least \ref minDelay has passed to any previous request
//! \param path API request path (appended after /api/{username})
//! \param request Request to the api, may be empty
//! \returns The return value of the underlying \ref IHttpHandler::DELETEJson call
//! \throws std::system_error when system or socket operations fail
//! \throws HueException when response contains no body
//! \throws HueAPIResponseException when response contains an error
nlohmann::json DELETERequest(const std::string& path, const nlohmann::json& request) const;
nlohmann::json DELETERequest(const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const;
private:
struct TimeoutData
{
std::chrono::steady_clock::time_point timeout;
std::mutex mutex;
};
//! \brief Throws an exception if response contains an error, passes though value
//! \throws HueAPIResponseException when response contains an error
//! \returns \ref response if there is no error
nlohmann::json HandleError(FileInfo fileInfo, const nlohmann::json& response) const;
//! \brief Combines path with api prefix and username
//! \returns "/api/<username>/<path>"
std::string CombinedPath(const std::string& path) const;
public:
static constexpr std::chrono::steady_clock::duration minDelay = std::chrono::milliseconds(100);
private:
std::string ip;
int port;
std::string username;
std::shared_ptr<const IHttpHandler> httpHandler;
std::shared_ptr<TimeoutData> timeout;
};
} // namespace hueplusplus
#endif