22#ifndef INCLUDE_HUEPLUSPLUS_RESOURCE_LIST_H
23#define INCLUDE_HUEPLUSPLUS_RESOURCE_LIST_H
45template <
typename Resource,
typename IdT>
51 static_assert(std::is_integral<IdType>::value || std::is_same<std::string, IdType>::value,
52 "IdType must be integral or string");
61 ResourceList(std::shared_ptr<APICache> baseCache,
const std::string& cacheEntry,
62 std::chrono::steady_clock::duration refreshDuration,
bool sharedState =
false,
63 const std::function<Resource(
IdType,
const nlohmann::json&,
const std::shared_ptr<APICache>&)>&
factory
77 std::chrono::steady_clock::duration refreshDuration,
78 const std::function<Resource(
IdType,
const nlohmann::json&,
const std::shared_ptr<APICache>&)>&
factory
98 stateCache->setRefreshDuration(refreshDuration);
109 nlohmann::json& state =
stateCache->getValue();
110 std::vector<Resource> result;
111 result.reserve(state.size());
112 for (
auto it = state.begin(); it != state.end(); ++it)
128 const nlohmann::json& state =
stateCache->getValue();
130 if (!state.count(key))
165 nlohmann::json result =
stateCache->getCommandAPI().DELETERequest(
166 requestPath, nlohmann::json::object(),
FileInfo {__FILE__, __LINE__, __func__});
183 std::is_constructible<Resource,
IdType,
HueCommandAPI, std::chrono::steady_clock::duration,
184 const nlohmann::json&> {});
194 Resource
construct(
const IdType&
id,
const nlohmann::json& state, std::true_type)
213 Resource
construct(
const IdType&
id,
const nlohmann::json& state, std::false_type)
218 "Resource is not constructable with default parameters, but no factory given");
224 static IdType maybeStoi(
const std::string& key, std::true_type) {
return std::stoi(key); }
225 static IdType maybeStoi(
const std::string& key, std::false_type) {
return key; }
231 std::function<Resource(
IdType,
const nlohmann::json&,
const std::shared_ptr<APICache>&)>
factory;
238template <
typename Resource>
248 void search(
const std::vector<std::string>& deviceIds = {})
250 std::string requestPath = this->
path;
252 requestPath.pop_back();
253 if (deviceIds.empty())
255 this->
stateCache->getCommandAPI().POSTRequest(
256 requestPath, nlohmann::json::object(),
FileInfo {__FILE__, __LINE__, __func__});
260 this->
stateCache->getCommandAPI().POSTRequest(
261 requestPath, nlohmann::json {{
"deviceid", deviceIds}}, FileInfo {__FILE__, __LINE__, __func__});
268 nlohmann::json response = this->
stateCache->getCommandAPI().GETRequest(
269 this->path +
"new", nlohmann::json::object(),
FileInfo {__FILE__, __LINE__, __func__});
284template <
typename BaseResourceList,
typename CreateType>
288 using BaseResourceList::BaseResourceList;
298 typename BaseResourceList::IdType
create(
const CreateType& params)
300 std::string requestPath = this->path;
302 requestPath.pop_back();
303 nlohmann::json response = this->stateCache->getCommandAPI().POSTRequest(
304 requestPath, params.getRequest(),
FileInfo {__FILE__, __LINE__, __func__});
308 std::string idStr =
id.get<std::string>();
309 if (idStr.find(this->path) == 0)
311 idStr.erase(0, this->path.size());
313 this->stateCache->refresh();
314 return this->maybeStoi(idStr);
316 return typename BaseResourceList::IdType {};
330template <
typename Resource,
typename CreateType>
339 Resource
get(
const int&
id)
341 const nlohmann::json& state = this->
stateCache->getValue();
343 if (!state.count(key) &&
id != 0)
347 return this->
construct(
id,
id == 0 ? nlohmann::json {
nullptr} : state[key]);
Caches API GET requests and refreshes regularly.
Definition APICache.h:38
Handles a ResourceList where Resources can be added by the user.
Definition ResourceList.h:286
BaseResourceList::IdType create(const CreateType ¶ms)
Create a new resource.
Definition ResourceList.h:298
CreateableResourceList & operator=(CreateableResourceList &&)=default
Protected defaulted move assignment.
CreateableResourceList(CreateableResourceList &&)=default
Protected defaulted move constructor.
Handles a group list with the special group 0.
Definition ResourceList.h:332
Resource get(const int &id)
Get group, specially handles group 0.
Definition ResourceList.h:339
GroupResourceList & operator=(GroupResourceList &&)=default
Protected defaulted move assignment.
GroupResourceList(GroupResourceList &&)=default
Protected defaulted move constructor.
bool exists(int id) const
Get group, specially handles group 0.
Definition ResourceList.h:351
Definition HueCommandAPI.h:38
Exception class with file information. Base class of all custom exception classes.
Definition HueException.h:50
List of new devices found during the last scan.
Definition NewDeviceList.h:36
static NewDeviceList parse(const nlohmann::json &json)
Parse from json response.
Definition NewDeviceList.cpp:45
Handles a list of a certain API resource.
Definition ResourceList.h:47
ResourceList(ResourceList &&)=default
Protected defaulted move constructor.
ResourceList(const ResourceList &)=delete
Deleted copy constructor.
Resource ResourceType
Definition ResourceList.h:49
ResourceList(const HueCommandAPI &commands, const std::string &path, std::chrono::steady_clock::duration refreshDuration, const std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> &factory=nullptr)
Construct ResourceList with a separate cache and optional factory function.
Definition ResourceList.h:76
void refresh()
Refreshes internal state now.
Definition ResourceList.h:92
Resource get(const IdType &id)
Get resource specified by id.
Definition ResourceList.h:126
bool exists(const IdType &id) const
Checks whether resource with id exists.
Definition ResourceList.h:151
ResourceList(std::shared_ptr< APICache > baseCache, const std::string &cacheEntry, std::chrono::steady_clock::duration refreshDuration, bool sharedState=false, const std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> &factory=nullptr)
Construct ResourceList using a base cache and optional factory function.
Definition ResourceList.h:61
void setRefreshDuration(std::chrono::steady_clock::duration refreshDuration)
Sets custom refresh interval for this list and all resources created.
Definition ResourceList.h:96
std::vector< Resource > getAll()
Get all resources that exist.
Definition ResourceList.h:107
ResourceList & operator=(const ResourceList &)=delete
Deleted copy assignment.
IdT IdType
Definition ResourceList.h:50
ResourceList & operator=(ResourceList &&)=default
Protected defaulted move assignment.
Resource construct(const IdType &id, const nlohmann::json &state)
Constructs resource using factory or constructor, if available.
Definition ResourceList.h:180
static IdType maybeStoi(const std::string &key)
Calls std::stoi if IdType is int.
Definition ResourceList.h:173
bool sharedState
Definition ResourceList.h:233
std::shared_ptr< APICache > stateCache
Definition ResourceList.h:230
std::string path
Definition ResourceList.h:232
std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> factory
Definition ResourceList.h:231
bool remove(const IdType &id)
Removes the resource.
Definition ResourceList.h:162
static std::string maybeToString(const IdType &id)
Calls std::to_string if IdType is int.
Definition ResourceList.h:176
bool exists(const IdType &id)
Checks whether resource with id exists.
Definition ResourceList.h:144
Handles a ResourceList of physical devices which can be searched for.
Definition ResourceList.h:240
NewDeviceList getNewDevices() const
Get devices found in last search.
Definition ResourceList.h:266
void search(const std::vector< std::string > &deviceIds={})
Start search for new devices.
Definition ResourceList.h:248
SearchableResourceList(SearchableResourceList &&)=default
Protected defaulted move constructor.
SearchableResourceList & operator=(SearchableResourceList &&)=default
Protected defaulted move assignment.
nlohmann::json safeGetMember(const nlohmann::json &json, Paths &&... paths)
Returns the object/array member or null if it does not exist.
Definition Utils.h:92
Namespace for the hueplusplus library.
Definition Action.h:28
Contains information about error location, use CURRENT_FILE_INFO to create.
Definition HueException.h:35