Commit 86d60f05435eb42df13b51c96de3b712e9363793

Authored by Sieren
Committed by Moritz Wirger
1 parent 704f96a7

Support Ports other than 80

Occasionally Hue-protocol emulating systems like DeCONZ,
Phoscon / ConBee can run on ports other than 80.
Especially if running on a Raspberry Pi server,
a user might run this on a different port than 80.
hueplusplus/Hue.cpp
@@ -52,8 +52,11 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const { @@ -52,8 +52,11 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const {
52 size_t start = p.first.find("//") + 2; 52 size_t start = p.first.find("//") + 2;
53 size_t length = p.first.find(":", start) - start; 53 size_t length = p.first.find(":", start) - start;
54 bridge.ip = p.first.substr(start, length); 54 bridge.ip = p.first.substr(start, length);
  55 + auto portLength = p.first.find("/", start + length) - (start + length + 1);
  56 + auto port = p.first.substr(start + length + 1, portLength);
  57 + bridge.port = std::stoi(port);
55 std::string desc = http_handler->GETString( 58 std::string desc = http_handler->GETString(
56 - "/description.xml", "application/xml", "", bridge.ip); 59 + "/description.xml", "application/xml", "", bridge.ip, bridge.port);
57 std::string mac = ParseDescription(desc); 60 std::string mac = ParseDescription(desc);
58 if (!mac.empty()) { 61 if (!mac.empty()) {
59 bridge.mac = NormalizeMac(mac); 62 bridge.mac = NormalizeMac(mac);
@@ -68,9 +71,10 @@ Hue HueFinder::GetBridge(const HueIdentification &identification) { @@ -68,9 +71,10 @@ Hue HueFinder::GetBridge(const HueIdentification &identification) {
68 std::string normalizedMac = NormalizeMac(identification.mac); 71 std::string normalizedMac = NormalizeMac(identification.mac);
69 auto pos = usernames.find(normalizedMac); 72 auto pos = usernames.find(normalizedMac);
70 if (pos != usernames.end()) { 73 if (pos != usernames.end()) {
71 - return Hue(identification.ip, pos->second, http_handler); 74 + return Hue(identification.ip, identification.port,
  75 + pos->second, http_handler);
72 } 76 }
73 - Hue bridge(identification.ip, "", http_handler); 77 + Hue bridge(identification.ip, identification.port, "", http_handler);
74 bridge.requestUsername(identification.ip); 78 bridge.requestUsername(identification.ip);
75 if (bridge.getUsername().empty()) { 79 if (bridge.getUsername().empty()) {
76 std::cerr << "Failed to request username for ip " << identification.ip 80 std::cerr << "Failed to request username for ip " << identification.ip
@@ -122,9 +126,9 @@ std::string HueFinder::ParseDescription(const std::string &amp; description) @@ -122,9 +126,9 @@ std::string HueFinder::ParseDescription(const std::string &amp; description)
122 return std::string(); 126 return std::string();
123 } 127 }
124 128
125 -Hue::Hue(const std::string &ip, const std::string &username, 129 +Hue::Hue(const std::string &ip, const int port, const std::string &username,
126 std::shared_ptr<const IHttpHandler> handler) 130 std::shared_ptr<const IHttpHandler> handler)
127 - : ip(ip), username(username), 131 + : ip(ip), port(port), username(username),
128 simpleBrightnessStrategy(std::make_shared<SimpleBrightnessStrategy>()), 132 simpleBrightnessStrategy(std::make_shared<SimpleBrightnessStrategy>()),
129 simpleColorHueStrategy(std::make_shared<SimpleColorHueStrategy>()), 133 simpleColorHueStrategy(std::make_shared<SimpleColorHueStrategy>()),
130 extendedColorHueStrategy(std::make_shared<ExtendedColorHueStrategy>()), 134 extendedColorHueStrategy(std::make_shared<ExtendedColorHueStrategy>()),
@@ -132,10 +136,13 @@ Hue::Hue(const std::string &amp;ip, const std::string &amp;username, @@ -132,10 +136,13 @@ Hue::Hue(const std::string &amp;ip, const std::string &amp;username,
132 std::make_shared<SimpleColorTemperatureStrategy>()), 136 std::make_shared<SimpleColorTemperatureStrategy>()),
133 extendedColorTemperatureStrategy( 137 extendedColorTemperatureStrategy(
134 std::make_shared<ExtendedColorTemperatureStrategy>()), 138 std::make_shared<ExtendedColorTemperatureStrategy>()),
135 - http_handler(std::move(handler)), commands(ip, username, http_handler) {} 139 + http_handler(std::move(handler)), commands(ip, port, username,
  140 + http_handler) {}
136 141
137 std::string Hue::getBridgeIP() { return ip; } 142 std::string Hue::getBridgeIP() { return ip; }
138 143
  144 +int Hue::getBridgePort() { return port; }
  145 +
139 std::string Hue::requestUsername(const std::string &ip) { 146 std::string Hue::requestUsername(const std::string &ip) {
140 std::cout 147 std::cout
141 << "Please press the link Button! You've got 35 secs!\n"; // when the link 148 << "Please press the link Button! You've got 35 secs!\n"; // when the link
@@ -158,14 +165,14 @@ std::string Hue::requestUsername(const std::string &amp;ip) { @@ -158,14 +165,14 @@ std::string Hue::requestUsername(const std::string &amp;ip) {
158 if (std::chrono::steady_clock::now() - lastCheck > 165 if (std::chrono::steady_clock::now() - lastCheck >
159 std::chrono::seconds(1)) { 166 std::chrono::seconds(1)) {
160 lastCheck = std::chrono::steady_clock::now(); 167 lastCheck = std::chrono::steady_clock::now();
161 - answer = http_handler->POSTJson("/api", request, ip); 168 + answer = http_handler->POSTJson("/api", request, ip, port);
162 169
163 if (answer[0]["success"] != Json::nullValue) { 170 if (answer[0]["success"] != Json::nullValue) {
164 // [{"success":{"username": "<username>"}}] 171 // [{"success":{"username": "<username>"}}]
165 username = answer[0]["success"]["username"].asString(); 172 username = answer[0]["success"]["username"].asString();
166 this->ip = ip; 173 this->ip = ip;
167 // Update commands with new username and ip 174 // Update commands with new username and ip
168 - commands = HueCommandAPI(ip, username, http_handler); 175 + commands = HueCommandAPI(ip, port, username, http_handler);
169 std::cout << "Success! Link button was pressed!\n"; 176 std::cout << "Success! Link button was pressed!\n";
170 std::cout << "Username is \"" << username << "\"\n"; 177 std::cout << "Username is \"" << username << "\"\n";
171 break; 178 break;
@@ -182,6 +189,8 @@ std::string Hue::getUsername() { return username; } @@ -182,6 +189,8 @@ std::string Hue::getUsername() { return username; }
182 189
183 void Hue::setIP(const std::string &ip) { this->ip = ip; } 190 void Hue::setIP(const std::string &ip) { this->ip = ip; }
184 191
  192 +void Hue::setPort(const int port) { this->port = port; }
  193 +
185 HueLight &Hue::getLight(int id) { 194 HueLight &Hue::getLight(int id) {
186 auto pos = lights.find(id); 195 auto pos = lights.find(id);
187 if (pos != lights.end()) { 196 if (pos != lights.end()) {
hueplusplus/HueCommandAPI.cpp
@@ -23,9 +23,10 @@ @@ -23,9 +23,10 @@
23 23
24 constexpr std::chrono::steady_clock::duration HueCommandAPI::minDelay; 24 constexpr std::chrono::steady_clock::duration HueCommandAPI::minDelay;
25 25
26 -HueCommandAPI::HueCommandAPI(const std::string &ip, const std::string &username, 26 +HueCommandAPI::HueCommandAPI(const std::string &ip, const int port,
  27 + const std::string &username,
27 std::shared_ptr<const IHttpHandler> httpHandler) 28 std::shared_ptr<const IHttpHandler> httpHandler)
28 - : ip(ip), username(username), httpHandler(std::move(httpHandler)), 29 + : ip(ip), port(port), username(username), httpHandler(std::move(httpHandler)),
29 timeout(new TimeoutData{std::chrono::steady_clock::now()}) {} 30 timeout(new TimeoutData{std::chrono::steady_clock::now()}) {}
30 31
31 Json::Value HueCommandAPI::PUTRequest(const std::string &path, 32 Json::Value HueCommandAPI::PUTRequest(const std::string &path,
@@ -40,7 +41,7 @@ Json::Value HueCommandAPI::PUTRequest(const std::string &amp;path, @@ -40,7 +41,7 @@ Json::Value HueCommandAPI::PUTRequest(const std::string &amp;path,
40 "/api/" + username + (path.empty() || path.front() == '/' ? "" : "/") + 41 "/api/" + username + (path.empty() || path.front() == '/' ? "" : "/") +
41 path; 42 path;
42 try { 43 try {
43 - Json::Value v = httpHandler->PUTJson(combinedPath, request, ip); 44 + Json::Value v = httpHandler->PUTJson(combinedPath, request, ip, port);
44 timeout->timeout = now + minDelay; 45 timeout->timeout = now + minDelay;
45 return v; 46 return v;
46 } catch (const std::system_error &e) { 47 } catch (const std::system_error &e) {
@@ -69,7 +70,7 @@ Json::Value HueCommandAPI::GETRequest(const std::string &amp;path, @@ -69,7 +70,7 @@ Json::Value HueCommandAPI::GETRequest(const std::string &amp;path,
69 "/api/" + username + (path.empty() || path.front() == '/' ? "" : "/") + 70 "/api/" + username + (path.empty() || path.front() == '/' ? "" : "/") +
70 path; 71 path;
71 try { 72 try {
72 - Json::Value v = httpHandler->GETJson(combinedPath, request, ip); 73 + Json::Value v = httpHandler->GETJson(combinedPath, request, ip, port);
73 timeout->timeout = now + minDelay; 74 timeout->timeout = now + minDelay;
74 return v; 75 return v;
75 } catch (const std::system_error &e) { 76 } catch (const std::system_error &e) {
@@ -77,7 +78,7 @@ Json::Value HueCommandAPI::GETRequest(const std::string &amp;path, @@ -77,7 +78,7 @@ Json::Value HueCommandAPI::GETRequest(const std::string &amp;path,
77 e.code() == std::errc::timed_out) { 78 e.code() == std::errc::timed_out) {
78 // Happens when hue is too busy, wait and try again (once) 79 // Happens when hue is too busy, wait and try again (once)
79 std::this_thread::sleep_for(minDelay); 80 std::this_thread::sleep_for(minDelay);
80 - Json::Value v = httpHandler->GETJson(combinedPath, request, ip); 81 + Json::Value v = httpHandler->GETJson(combinedPath, request, ip, port);
81 timeout->timeout = std::chrono::steady_clock::now() + minDelay; 82 timeout->timeout = std::chrono::steady_clock::now() + minDelay;
82 return v; 83 return v;
83 } 84 }
@@ -98,7 +99,7 @@ Json::Value HueCommandAPI::DELETERequest(const std::string &amp;path, @@ -98,7 +99,7 @@ Json::Value HueCommandAPI::DELETERequest(const std::string &amp;path,
98 "/api/" + username + (path.empty() || path.front() == '/' ? "" : "/") + 99 "/api/" + username + (path.empty() || path.front() == '/' ? "" : "/") +
99 path; 100 path;
100 try { 101 try {
101 - Json::Value v = httpHandler->DELETEJson(combinedPath, request, ip); 102 + Json::Value v = httpHandler->DELETEJson(combinedPath, request, ip, port);
102 timeout->timeout = now + minDelay; 103 timeout->timeout = now + minDelay;
103 return v; 104 return v;
104 } catch (const std::system_error &e) { 105 } catch (const std::system_error &e) {
@@ -106,7 +107,7 @@ Json::Value HueCommandAPI::DELETERequest(const std::string &amp;path, @@ -106,7 +107,7 @@ Json::Value HueCommandAPI::DELETERequest(const std::string &amp;path,
106 e.code() == std::errc::timed_out) { 107 e.code() == std::errc::timed_out) {
107 // Happens when hue is too busy, wait and try again (once) 108 // Happens when hue is too busy, wait and try again (once)
108 std::this_thread::sleep_for(minDelay); 109 std::this_thread::sleep_for(minDelay);
109 - Json::Value v = httpHandler->DELETEJson(combinedPath, request, ip); 110 + Json::Value v = httpHandler->DELETEJson(combinedPath, request, ip, port);
110 timeout->timeout = std::chrono::steady_clock::now() + minDelay; 111 timeout->timeout = std::chrono::steady_clock::now() + minDelay;
111 return v; 112 return v;
112 } 113 }
hueplusplus/include/Hue.h
@@ -45,6 +45,7 @@ class HueFinder { @@ -45,6 +45,7 @@ class HueFinder {
45 public: 45 public:
46 struct HueIdentification { 46 struct HueIdentification {
47 std::string ip; 47 std::string ip;
  48 + int port = 80;
48 std::string mac; 49 std::string mac;
49 }; 50 };
50 51
@@ -107,18 +108,23 @@ public: @@ -107,18 +108,23 @@ public:
107 //! \brief Constructor of Hue class 108 //! \brief Constructor of Hue class
108 //! 109 //!
109 //! \param ip String that specifies the ip address of the Hue bridge in dotted 110 //! \param ip String that specifies the ip address of the Hue bridge in dotted
110 - //! decimal notation like "192.168.2.1" \param username String that specifies  
111 - //! the username that is used to control the bridge. This needs to be acquired  
112 - //! in \ref requestUsername \param handler HttpHandler of type \ref  
113 - //! IHttpHandler for communication with the bridge  
114 - Hue(const std::string &ip, const std::string &username,  
115 - std::shared_ptr<const IHttpHandler> handler); 111 + //! decimal notation like "192.168.2.1" \param port Port of the hue bridge
  112 + //! \param username String that specifies the username that is used to control
  113 + //! the bridge. This needs to be acquired in \ref requestUsername \param handler
  114 + //! HttpHandler of type \ref IHttpHandler for communication with the bridge
  115 + Hue(const std::string &ip, const int port, const std::string &username,
  116 + std::shared_ptr<const IHttpHandler> handler);
116 117
117 //! \brief Function to get the ip address of the hue bridge 118 //! \brief Function to get the ip address of the hue bridge
118 //! 119 //!
119 //! \return string containing ip 120 //! \return string containing ip
120 std::string getBridgeIP(); 121 std::string getBridgeIP();
121 122
  123 + //! \brief Function to get the port of the hue bridge
  124 + //!
  125 + //! \return integer containing port
  126 + int getBridgePort();
  127 +
122 //! \brief Function that sends a username request to the Hue bridge. 128 //! \brief Function that sends a username request to the Hue bridge.
123 //! 129 //!
124 //! It does that for about 30 seconds and you have 5 seconds to prepare 130 //! It does that for about 30 seconds and you have 5 seconds to prepare
@@ -141,6 +147,12 @@ public: @@ -141,6 +147,12 @@ public:
141 //! "192.168.2.1" 147 //! "192.168.2.1"
142 void setIP(const std::string &ip); 148 void setIP(const std::string &ip);
143 149
  150 + //! \brief Function to set the port of this class representing a bridge
  151 + //!
  152 + //! \param port Integer that specifies the port of an address like
  153 + //! "192.168.2.1:8080"
  154 + void setPort(const int port);
  155 +
144 //! \brief Function that returns a \ref Hue::HueLight of specified id 156 //! \brief Function that returns a \ref Hue::HueLight of specified id
145 //! 157 //!
146 //! \param id Integer that specifies the ID of a Hue light 158 //! \param id Integer that specifies the ID of a Hue light
@@ -209,7 +221,7 @@ public: @@ -209,7 +221,7 @@ public:
209 //! \param handler a HttpHandler of type \ref IHttpHandler 221 //! \param handler a HttpHandler of type \ref IHttpHandler
210 void setHttpHandler(std::shared_ptr<const IHttpHandler> handler) { 222 void setHttpHandler(std::shared_ptr<const IHttpHandler> handler) {
211 http_handler = std::move(handler); 223 http_handler = std::move(handler);
212 - commands = HueCommandAPI(ip, username, http_handler); 224 + commands = HueCommandAPI(ip, port, username, http_handler);
213 }; 225 };
214 226
215 private: 227 private:
@@ -219,6 +231,7 @@ private: @@ -219,6 +231,7 @@ private:
219 private: 231 private:
220 std::string ip; //!< IP-Address of the hue bridge in dotted decimal notation 232 std::string ip; //!< IP-Address of the hue bridge in dotted decimal notation
221 //!< like "192.168.2.1" 233 //!< like "192.168.2.1"
  234 + int port;
222 std::string username; //!< Username that is ussed to access the hue bridge 235 std::string username; //!< Username that is ussed to access the hue bridge
223 Json::Value state; //!< The state of the hue bridge as it is returned from it 236 Json::Value state; //!< The state of the hue bridge as it is returned from it
224 std::map<uint8_t, HueLight> 237 std::map<uint8_t, HueLight>
hueplusplus/include/HueCommandAPI.h
@@ -33,10 +33,11 @@ public: @@ -33,10 +33,11 @@ public:
33 //! \brief Construct from ip, username and HttpHandler 33 //! \brief Construct from ip, username and HttpHandler
34 //! 34 //!
35 //! \param ip String that specifies the ip address of the Hue bridge in dotted 35 //! \param ip String that specifies the ip address of the Hue bridge in dotted
36 - //! decimal notation like "192.168.2.1" \param username String that specifies  
37 - //! the username that is used to control the bridge \param handler HttpHandler  
38 - //! of type \ref IHttpHandler for communication with the bridge  
39 - HueCommandAPI(const std::string &ip, const std::string &username, 36 + //! decimal notation like "192.168.2.1" \param port of the hue bridge
  37 + //! \param username String that specifies the username that is used to control
  38 + //! the bridge \param handler HttpHandler of type \ref IHttpHandler for
  39 + //! communication with the bridge
  40 + HueCommandAPI(const std::string &ip, const int port, const std::string &username,
40 std::shared_ptr<const IHttpHandler> httpHandler); 41 std::shared_ptr<const IHttpHandler> httpHandler);
41 42
42 //! \brief Copy construct from other HueCommandAPI 43 //! \brief Copy construct from other HueCommandAPI
@@ -100,6 +101,7 @@ private: @@ -100,6 +101,7 @@ private:
100 static constexpr std::chrono::steady_clock::duration minDelay = 101 static constexpr std::chrono::steady_clock::duration minDelay =
101 std::chrono::milliseconds(100); 102 std::chrono::milliseconds(100);
102 std::string ip; 103 std::string ip;
  104 + int port;
103 std::string username; 105 std::string username;
104 std::shared_ptr<const IHttpHandler> httpHandler; 106 std::shared_ptr<const IHttpHandler> httpHandler;
105 std::shared_ptr<TimeoutData> timeout; 107 std::shared_ptr<TimeoutData> timeout;
hueplusplus/test/mocks/mock_HueLight.h
@@ -33,7 +33,9 @@ @@ -33,7 +33,9 @@
33 class MockHueLight : public HueLight 33 class MockHueLight : public HueLight
34 { 34 {
35 public: 35 public:
36 - MockHueLight(std::shared_ptr<const IHttpHandler> handler) : HueLight(1, HueCommandAPI(getBridgeIp(), getBridgeUsername(), handler)) {}; 36 + MockHueLight(std::shared_ptr<const IHttpHandler> handler) : HueLight(1,
  37 + HueCommandAPI(getBridgeIp(), getBridgePort(), getBridgeUsername(),
  38 + handler)) {};
37 39
38 Json::Value& getState() { return state; }; 40 Json::Value& getState() { return state; };
39 41
hueplusplus/test/test_Hue.cpp
@@ -45,16 +45,19 @@ TEST_F(HueFinderTest, FindBridges) { @@ -45,16 +45,19 @@ TEST_F(HueFinderTest, FindBridges) {
45 45
46 HueFinder::HueIdentification bridge_to_comp; 46 HueFinder::HueIdentification bridge_to_comp;
47 bridge_to_comp.ip = getBridgeIp(); 47 bridge_to_comp.ip = getBridgeIp();
  48 + bridge_to_comp.port = getBridgePort();
48 bridge_to_comp.mac = getBridgeMac(); 49 bridge_to_comp.mac = getBridgeMac();
49 50
50 EXPECT_EQ(bridges.size(), 1) << "HueFinder found more than one Bridge"; 51 EXPECT_EQ(bridges.size(), 1) << "HueFinder found more than one Bridge";
51 EXPECT_EQ(bridges[0].ip, bridge_to_comp.ip) 52 EXPECT_EQ(bridges[0].ip, bridge_to_comp.ip)
52 << "HueIdentification ip does not match"; 53 << "HueIdentification ip does not match";
  54 + EXPECT_EQ(bridges[0].port, bridge_to_comp.port)
  55 + << "HueIdentification port does not match";
53 EXPECT_EQ(bridges[0].mac, bridge_to_comp.mac) 56 EXPECT_EQ(bridges[0].mac, bridge_to_comp.mac)
54 << "HueIdentification mac does not match"; 57 << "HueIdentification mac does not match";
55 58
56 // Test invalid description 59 // Test invalid description
57 - EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", getBridgeIp(), 80)) 60 + EXPECT_CALL(*handler, GETString("/description.xml", "application/xml", "", getBridgeIp(), getBridgePort()))
58 .Times(1) 61 .Times(1)
59 .WillOnce(::testing::Return("invalid stuff")); 62 .WillOnce(::testing::Return("invalid stuff"));
60 bridges = finder.FindBridges(); 63 bridges = finder.FindBridges();
@@ -100,6 +103,8 @@ TEST_F(HueFinderTest, GetBridge) { @@ -100,6 +103,8 @@ TEST_F(HueFinderTest, GetBridge) {
100 103
101 EXPECT_EQ(test_bridge.getBridgeIP(), getBridgeIp()) 104 EXPECT_EQ(test_bridge.getBridgeIP(), getBridgeIp())
102 << "Bridge IP not matching"; 105 << "Bridge IP not matching";
  106 + EXPECT_EQ(test_bridge.getBridgePort(), getBridgePort())
  107 + << "Bridge Port not matching";
103 EXPECT_EQ(test_bridge.getUsername(), getBridgeUsername()) 108 EXPECT_EQ(test_bridge.getUsername(), getBridgeUsername())
104 << "Bridge username not matching"; 109 << "Bridge username not matching";
105 110
@@ -126,6 +131,8 @@ TEST_F(HueFinderTest, AddUsername) { @@ -126,6 +131,8 @@ TEST_F(HueFinderTest, AddUsername) {
126 131
127 EXPECT_EQ(test_bridge.getBridgeIP(), getBridgeIp()) 132 EXPECT_EQ(test_bridge.getBridgeIP(), getBridgeIp())
128 << "Bridge IP not matching"; 133 << "Bridge IP not matching";
  134 + EXPECT_EQ(test_bridge.getBridgePort(), getBridgePort())
  135 + << "Bridge Port not matching";
129 EXPECT_EQ(test_bridge.getUsername(), getBridgeUsername()) 136 EXPECT_EQ(test_bridge.getUsername(), getBridgeUsername())
130 << "Bridge username not matching"; 137 << "Bridge username not matching";
131 } 138 }
@@ -144,10 +151,12 @@ TEST_F(HueFinderTest, GetAllUsernames) { @@ -144,10 +151,12 @@ TEST_F(HueFinderTest, GetAllUsernames) {
144 TEST(Hue, Constructor) { 151 TEST(Hue, Constructor) {
145 std::shared_ptr<MockHttpHandler> handler = 152 std::shared_ptr<MockHttpHandler> handler =
146 std::make_shared<MockHttpHandler>(); 153 std::make_shared<MockHttpHandler>();
147 - Hue test_bridge(getBridgeIp(), getBridgeUsername(), handler); 154 + Hue test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
148 155
149 EXPECT_EQ(test_bridge.getBridgeIP(), getBridgeIp()) 156 EXPECT_EQ(test_bridge.getBridgeIP(), getBridgeIp())
150 << "Bridge IP not matching"; 157 << "Bridge IP not matching";
  158 + EXPECT_EQ(test_bridge.getBridgePort(), getBridgePort())
  159 + << "Bridge Port not matching";
151 EXPECT_EQ(test_bridge.getUsername(), getBridgeUsername()) 160 EXPECT_EQ(test_bridge.getUsername(), getBridgeUsername())
152 << "Bridge username not matching"; 161 << "Bridge username not matching";
153 } 162 }
@@ -167,11 +176,11 @@ TEST(Hue, requestUsername) { @@ -167,11 +176,11 @@ TEST(Hue, requestUsername) {
167 user_ret_uns[0]["error"]["address"] = ""; 176 user_ret_uns[0]["error"]["address"] = "";
168 user_ret_uns[0]["error"]["description"] = "link button not pressed"; 177 user_ret_uns[0]["error"]["description"] = "link button not pressed";
169 178
170 - EXPECT_CALL(*handler, POSTJson("/api", request, getBridgeIp(), 80)) 179 + EXPECT_CALL(*handler, POSTJson("/api", request, getBridgeIp(), getBridgePort()))
171 .Times(AtLeast(1)) 180 .Times(AtLeast(1))
172 .WillRepeatedly(Return(user_ret_uns)); 181 .WillRepeatedly(Return(user_ret_uns));
173 182
174 - Hue test_bridge(getBridgeIp(), "", handler); 183 + Hue test_bridge(getBridgeIp(), getBridgePort(), "", handler);
175 184
176 test_bridge.requestUsername(test_bridge.getBridgeIP()); 185 test_bridge.requestUsername(test_bridge.getBridgeIP());
177 EXPECT_EQ(test_bridge.getUsername(), "") << "Bridge username not matching"; 186 EXPECT_EQ(test_bridge.getUsername(), "") << "Bridge username not matching";
@@ -185,7 +194,7 @@ TEST(Hue, requestUsername) { @@ -185,7 +194,7 @@ TEST(Hue, requestUsername) {
185 .Times(1) 194 .Times(1)
186 .WillRepeatedly(Return(user_ret_suc)); 195 .WillRepeatedly(Return(user_ret_suc));
187 196
188 - test_bridge = Hue(getBridgeIp(), "", handler); 197 + test_bridge = Hue(getBridgeIp(), getBridgePort(), "", handler);
189 198
190 test_bridge.requestUsername(test_bridge.getBridgeIP()); 199 test_bridge.requestUsername(test_bridge.getBridgeIP());
191 200
@@ -209,7 +218,7 @@ TEST(Hue, requestUsername) { @@ -209,7 +218,7 @@ TEST(Hue, requestUsername) {
209 TEST(Hue, setIP) { 218 TEST(Hue, setIP) {
210 std::shared_ptr<MockHttpHandler> handler = 219 std::shared_ptr<MockHttpHandler> handler =
211 std::make_shared<MockHttpHandler>(); 220 std::make_shared<MockHttpHandler>();
212 - Hue test_bridge(getBridgeIp(), "", handler); 221 + Hue test_bridge = Hue(getBridgeIp(), getBridgePort(), "", handler);
213 EXPECT_EQ(test_bridge.getBridgeIP(), getBridgeIp()) 222 EXPECT_EQ(test_bridge.getBridgeIP(), getBridgeIp())
214 << "Bridge IP not matching after initialization"; 223 << "Bridge IP not matching after initialization";
215 test_bridge.setIP("192.168.2.112"); 224 test_bridge.setIP("192.168.2.112");
@@ -217,6 +226,17 @@ TEST(Hue, setIP) { @@ -217,6 +226,17 @@ TEST(Hue, setIP) {
217 << "Bridge IP not matching after setting it"; 226 << "Bridge IP not matching after setting it";
218 } 227 }
219 228
  229 +TEST(Hue, setPort) {
  230 + std::shared_ptr<MockHttpHandler> handler =
  231 + std::make_shared<MockHttpHandler>();
  232 + Hue test_bridge = Hue(getBridgeIp(), getBridgePort(), "", handler);
  233 + EXPECT_EQ(test_bridge.getBridgePort(), getBridgePort())
  234 + << "Bridge Port not matching after initialization";
  235 + test_bridge.setPort(81);
  236 + EXPECT_EQ(test_bridge.getBridgePort(), 81)
  237 + << "Bridge Port not matching after setting it";
  238 +}
  239 +
220 TEST(Hue, getLight) { 240 TEST(Hue, getLight) {
221 using namespace ::testing; 241 using namespace ::testing;
222 std::shared_ptr<MockHttpHandler> handler = 242 std::shared_ptr<MockHttpHandler> handler =
@@ -226,7 +246,7 @@ TEST(Hue, getLight) { @@ -226,7 +246,7 @@ TEST(Hue, getLight) {
226 Json::Value(Json::objectValue), getBridgeIp(), 80)) 246 Json::Value(Json::objectValue), getBridgeIp(), 80))
227 .Times(1); 247 .Times(1);
228 248
229 - Hue test_bridge(getBridgeIp(), getBridgeUsername(), handler); 249 + Hue test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
230 250
231 // Test exception 251 // Test exception
232 ASSERT_THROW(test_bridge.getLight(1), std::runtime_error); 252 ASSERT_THROW(test_bridge.getLight(1), std::runtime_error);
@@ -286,7 +306,7 @@ TEST(Hue, getLight) { @@ -286,7 +306,7 @@ TEST(Hue, getLight) {
286 Json::Value(Json::objectValue), getBridgeIp(), 80)) 306 Json::Value(Json::objectValue), getBridgeIp(), 80))
287 .Times(AtLeast(1)) 307 .Times(AtLeast(1))
288 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); 308 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"]));
289 - test_bridge = Hue(getBridgeIp(), getBridgeUsername(), handler); 309 + test_bridge = Hue(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
290 310
291 // Test when correct data is sent 311 // Test when correct data is sent
292 test_light_1 = test_bridge.getLight(1); 312 test_light_1 = test_bridge.getLight(1);
@@ -305,7 +325,7 @@ TEST(Hue, getLight) { @@ -305,7 +325,7 @@ TEST(Hue, getLight) {
305 Json::Value(Json::objectValue), getBridgeIp(), 80)) 325 Json::Value(Json::objectValue), getBridgeIp(), 80))
306 .Times(AtLeast(1)) 326 .Times(AtLeast(1))
307 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); 327 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"]));
308 - test_bridge = Hue(getBridgeIp(), getBridgeUsername(), handler); 328 + test_bridge = Hue(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
309 329
310 // Test when correct data is sent 330 // Test when correct data is sent
311 test_light_1 = test_bridge.getLight(1); 331 test_light_1 = test_bridge.getLight(1);
@@ -324,7 +344,7 @@ TEST(Hue, getLight) { @@ -324,7 +344,7 @@ TEST(Hue, getLight) {
324 Json::Value(Json::objectValue), getBridgeIp(), 80)) 344 Json::Value(Json::objectValue), getBridgeIp(), 80))
325 .Times(AtLeast(1)) 345 .Times(AtLeast(1))
326 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); 346 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"]));
327 - test_bridge = Hue(getBridgeIp(), getBridgeUsername(), handler); 347 + test_bridge = Hue(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
328 348
329 // Test when correct data is sent 349 // Test when correct data is sent
330 test_light_1 = test_bridge.getLight(1); 350 test_light_1 = test_bridge.getLight(1);
@@ -343,7 +363,7 @@ TEST(Hue, getLight) { @@ -343,7 +363,7 @@ TEST(Hue, getLight) {
343 Json::Value(Json::objectValue), getBridgeIp(), 80)) 363 Json::Value(Json::objectValue), getBridgeIp(), 80))
344 .Times(AtLeast(1)) 364 .Times(AtLeast(1))
345 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); 365 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"]));
346 - test_bridge = Hue(getBridgeIp(), getBridgeUsername(), handler); 366 + test_bridge = Hue(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
347 367
348 // Test when correct data is sent 368 // Test when correct data is sent
349 test_light_1 = test_bridge.getLight(1); 369 test_light_1 = test_bridge.getLight(1);
@@ -356,7 +376,7 @@ TEST(Hue, getLight) { @@ -356,7 +376,7 @@ TEST(Hue, getLight) {
356 Json::Value(Json::objectValue), getBridgeIp(), 80)) 376 Json::Value(Json::objectValue), getBridgeIp(), 80))
357 .Times(1) 377 .Times(1)
358 .WillOnce(Return(hue_bridge_state)); 378 .WillOnce(Return(hue_bridge_state));
359 - test_bridge = Hue(getBridgeIp(), getBridgeUsername(), handler); 379 + test_bridge = Hue(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
360 380
361 ASSERT_THROW(test_bridge.getLight(1), std::runtime_error); 381 ASSERT_THROW(test_bridge.getLight(1), std::runtime_error);
362 } 382 }
@@ -390,7 +410,7 @@ TEST(Hue, removeLight) { @@ -390,7 +410,7 @@ TEST(Hue, removeLight) {
390 .Times(1) 410 .Times(1)
391 .WillOnce(Return(hue_bridge_state)); 411 .WillOnce(Return(hue_bridge_state));
392 412
393 - Hue test_bridge(getBridgeIp(), getBridgeUsername(), handler); 413 + Hue test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
394 414
395 EXPECT_CALL(*handler, 415 EXPECT_CALL(*handler,
396 GETJson("/api/" + getBridgeUsername() + "/lights/1", 416 GETJson("/api/" + getBridgeUsername() + "/lights/1",
@@ -452,7 +472,7 @@ TEST(Hue, getAllLights) { @@ -452,7 +472,7 @@ TEST(Hue, getAllLights) {
452 .Times(2) 472 .Times(2)
453 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); 473 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"]));
454 474
455 - Hue test_bridge(getBridgeIp(), getBridgeUsername(), handler); 475 + Hue test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
456 476
457 std::vector<std::reference_wrapper<HueLight>> test_lights = 477 std::vector<std::reference_wrapper<HueLight>> test_lights =
458 test_bridge.getAllLights(); 478 test_bridge.getAllLights();
@@ -495,7 +515,7 @@ TEST(Hue, lightExists) { @@ -495,7 +515,7 @@ TEST(Hue, lightExists) {
495 .Times(AtLeast(1)) 515 .Times(AtLeast(1))
496 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); 516 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"]));
497 517
498 - Hue test_bridge(getBridgeIp(), getBridgeUsername(), handler); 518 + Hue test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
499 519
500 EXPECT_EQ(true, test_bridge.lightExists(1)); 520 EXPECT_EQ(true, test_bridge.lightExists(1));
501 EXPECT_EQ(false, test_bridge.lightExists(2)); 521 EXPECT_EQ(false, test_bridge.lightExists(2));
@@ -545,7 +565,7 @@ TEST(Hue, getPictureOfLight) { @@ -545,7 +565,7 @@ TEST(Hue, getPictureOfLight) {
545 .Times(AtLeast(1)) 565 .Times(AtLeast(1))
546 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"])); 566 .WillRepeatedly(Return(hue_bridge_state["lights"]["1"]));
547 567
548 - Hue test_bridge(getBridgeIp(), getBridgeUsername(), handler); 568 + Hue test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler);
549 569
550 test_bridge.getLight(1); 570 test_bridge.getLight(1);
551 571
@@ -557,7 +577,7 @@ TEST(Hue, getPictureOfLight) { @@ -557,7 +577,7 @@ TEST(Hue, getPictureOfLight) {
557 TEST(Hue, refreshState) { 577 TEST(Hue, refreshState) {
558 std::shared_ptr<MockHttpHandler> handler = 578 std::shared_ptr<MockHttpHandler> handler =
559 std::make_shared<MockHttpHandler>(); 579 std::make_shared<MockHttpHandler>();
560 - Hue test_bridge(getBridgeIp(), "", 580 + Hue test_bridge(getBridgeIp(), getBridgePort(), "",
561 handler); // NULL as username leads to segfault 581 handler); // NULL as username leads to segfault
562 582
563 std::vector<std::reference_wrapper<HueLight>> test_lights = 583 std::vector<std::reference_wrapper<HueLight>> test_lights =
hueplusplus/test/test_HueCommandAPI.cpp
@@ -30,7 +30,8 @@ TEST(HueCommandAPI, PUTRequest) { @@ -30,7 +30,8 @@ TEST(HueCommandAPI, PUTRequest) {
30 std::shared_ptr<MockHttpHandler> httpHandler = 30 std::shared_ptr<MockHttpHandler> httpHandler =
31 std::make_shared<MockHttpHandler>(); 31 std::make_shared<MockHttpHandler>();
32 32
33 - HueCommandAPI api(getBridgeIp(), getBridgeUsername(), httpHandler); 33 + HueCommandAPI api(getBridgeIp(), getBridgePort(), getBridgeUsername(),
  34 + httpHandler);
34 Json::Value request; 35 Json::Value request;
35 Json::Value result = Json::objectValue; 36 Json::Value result = Json::objectValue;
36 result["ok"] = true; 37 result["ok"] = true;
@@ -102,7 +103,8 @@ TEST(HueCommandAPI, GETRequest) { @@ -102,7 +103,8 @@ TEST(HueCommandAPI, GETRequest) {
102 std::shared_ptr<MockHttpHandler> httpHandler = 103 std::shared_ptr<MockHttpHandler> httpHandler =
103 std::make_shared<MockHttpHandler>(); 104 std::make_shared<MockHttpHandler>();
104 105
105 - HueCommandAPI api(getBridgeIp(), getBridgeUsername(), httpHandler); 106 + HueCommandAPI api(getBridgeIp(), getBridgePort(), getBridgeUsername(),
  107 + httpHandler);
106 Json::Value request; 108 Json::Value request;
107 Json::Value result = Json::objectValue; 109 Json::Value result = Json::objectValue;
108 result["ok"] = true; 110 result["ok"] = true;
@@ -174,7 +176,8 @@ TEST(HueCommandAPI, DELETERequest) { @@ -174,7 +176,8 @@ TEST(HueCommandAPI, DELETERequest) {
174 std::shared_ptr<MockHttpHandler> httpHandler = 176 std::shared_ptr<MockHttpHandler> httpHandler =
175 std::make_shared<MockHttpHandler>(); 177 std::make_shared<MockHttpHandler>();
176 178
177 - HueCommandAPI api(getBridgeIp(), getBridgeUsername(), httpHandler); 179 + HueCommandAPI api(getBridgeIp(), getBridgePort(), getBridgeUsername(),
  180 + httpHandler);
178 Json::Value request; 181 Json::Value request;
179 Json::Value result = Json::objectValue; 182 Json::Value result = Json::objectValue;
180 result["ok"] = true; 183 result["ok"] = true;
hueplusplus/test/test_HueLight.cpp
@@ -16,7 +16,8 @@ protected: @@ -16,7 +16,8 @@ protected:
16 protected: 16 protected:
17 HueLightTest() 17 HueLightTest()
18 : handler(std::make_shared<MockHttpHandler>()), 18 : handler(std::make_shared<MockHttpHandler>()),
19 - test_bridge(getBridgeIp(), getBridgeUsername(), handler) { 19 + test_bridge(getBridgeIp(), getBridgePort(), getBridgeUsername(),
  20 + handler) {
20 using namespace ::testing; 21 using namespace ::testing;
21 hue_bridge_state["lights"] = Json::Value(Json::objectValue); 22 hue_bridge_state["lights"] = Json::Value(Json::objectValue);
22 hue_bridge_state["lights"]["1"] = Json::Value(Json::objectValue); 23 hue_bridge_state["lights"]["1"] = Json::Value(Json::objectValue);
hueplusplus/test/testhelper.h
@@ -6,6 +6,10 @@ inline std::string getBridgeIp() { @@ -6,6 +6,10 @@ inline std::string getBridgeIp() {
6 //!< decimal notation like "192.168.2.1" 6 //!< decimal notation like "192.168.2.1"
7 } 7 }
8 8
  9 +inline int getBridgePort() {
  10 + return 80;
  11 +}
  12 +
9 inline std::string getBridgeUsername() { 13 inline std::string getBridgeUsername() {
10 return "83b7780291a6ceffbe0bd049104df"; //!< Username that is ussed to access 14 return "83b7780291a6ceffbe0bd049104df"; //!< Username that is ussed to access
11 //!< the fake hue bridge 15 //!< the fake hue bridge