diff --git a/include/hueplusplus/BaseDevice.h b/include/hueplusplus/BaseDevice.h index e6d311d..1b7d979 100644 --- a/include/hueplusplus/BaseDevice.h +++ b/include/hueplusplus/BaseDevice.h @@ -145,7 +145,6 @@ protected: protected: int id; //!< holds the id of the device - std::string path; //!< holds the path of the device APICache state; //!< holds the current state of the device }; } // namespace hueplusplus diff --git a/src/BaseDevice.cpp b/src/BaseDevice.cpp index 02ba3d5..72b13d3 100644 --- a/src/BaseDevice.cpp +++ b/src/BaseDevice.cpp @@ -97,7 +97,7 @@ BaseDevice::BaseDevice(int id, const std::shared_ptr& baseCache) BaseDevice::BaseDevice( int id, const HueCommandAPI& commands, const std::string& path, std::chrono::steady_clock::duration refreshDuration, const nlohmann::json& currentState) - : id(id), path(path), state(path + std::to_string(id), commands, refreshDuration, currentState) + : id(id), state(path + std::to_string(id), commands, refreshDuration, currentState) { // Initialize value if not null state.getValue(); @@ -105,7 +105,7 @@ BaseDevice::BaseDevice( nlohmann::json BaseDevice::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) { - return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); + return state.getCommandAPI().PUTRequest(state.getRequestPath() + subPath, request, std::move(fileInfo)); } void BaseDevice::refresh(bool force) diff --git a/src/Sensor.cpp b/src/Sensor.cpp index 4c90e9d..fc74540 100644 --- a/src/Sensor.cpp +++ b/src/Sensor.cpp @@ -205,7 +205,7 @@ void Sensor::setStateAttribute(const std::string& key, const nlohmann::json& val std::string Sensor::getStateAddress(const std::string& key) const { - return path + "/state/" + key; + return state.getRequestPath() + "/state/" + key; } nlohmann::json Sensor::getConfig() const diff --git a/test/test_BaseDevice.cpp b/test/test_BaseDevice.cpp index dc82d59..0ebca5f 100644 --- a/test/test_BaseDevice.cpp +++ b/test/test_BaseDevice.cpp @@ -122,4 +122,4 @@ TEST_F(BaseDeviceTest, setName) EXPECT_CALL(*handler, PUTJson("/api/" + getBridgeUsername() + path + "1/name", request, getBridgeIp(), getBridgePort())) .WillOnce(Return(response)); EXPECT_TRUE(device.setName(name)); -} \ No newline at end of file +} diff --git a/test/test_Bridge.cpp b/test/test_Bridge.cpp index 6e9980e..036df62 100644 --- a/test/test_Bridge.cpp +++ b/test/test_Bridge.cpp @@ -265,7 +265,7 @@ TEST(Bridge, getLight) *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) .Times(1) .WillOnce(Return(hue_bridge_state)); - + // Refresh cache test_bridge = Bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler); @@ -281,6 +281,45 @@ TEST(Bridge, getLight) EXPECT_EQ(test_light_1.getColorType(), ColorType::TEMPERATURE); } +TEST(Bridge, SharedState) +{ + using namespace ::testing; + std::shared_ptr handler = std::make_shared(); + + nlohmann::json hue_bridge_state {{"lights", + {{"1", + {{"state", + {{"on", true}, {"bri", 254}, {"ct", 366}, {"alert", "none"}, {"colormode", "ct"}, + {"reachable", true}}}, + {"swupdate", {{"state", "noupdates"}, {"lastinstall", nullptr}}}, {"type", "Color temperature light"}, + {"name", "Hue ambiance lamp 1"}, {"modelid", "LTW001"}, {"manufacturername", "Philips"}, + {"uniqueid", "00:00:00:00:00:00:00:00-00"}, {"swversion", "5.50.1.19085"}}}}}}; + + EXPECT_CALL( + *handler, GETJson("/api/" + getBridgeUsername(), nlohmann::json::object(), getBridgeIp(), getBridgePort())) + .Times(1) + .WillOnce(Return(hue_bridge_state)); + Bridge test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler, std::chrono::seconds(10), true); + + // Test when correct data is sent + Light test_light_1 = test_bridge.lights().get(1); + + Light test_light_copy = test_bridge.lights().get(1); + const std::string newName = "New light name"; + EXPECT_CALL(*handler, PUTJson("/api/" + getBridgeUsername() + "/lights/1/name", _, getBridgeIp(), getBridgePort())) + .WillOnce(Return(nlohmann::json({{"success", {{{"/lights/1/name", newName}}}}}))); + test_light_1.setName(newName); + hue_bridge_state["lights"]["1"]["name"] = newName; + + EXPECT_CALL(*handler, + GETJson("/api/" + getBridgeUsername() + "/lights/1", nlohmann::json::object(), getBridgeIp(), getBridgePort())) + .Times(1) + .WillOnce(Return(hue_bridge_state["lights"]["1"])); + test_light_1.refresh(true); + + EXPECT_EQ(newName, test_light_copy.getName()); +} + TEST(Bridge, removeLight) { using namespace ::testing; @@ -396,7 +435,6 @@ TEST(Bridge, getGroup) .Times(1) .WillOnce(Return(hue_bridge_state)); - // Refresh cache test_bridge = Bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);