Skip to content

Commit 68f5e1e

Browse files
For reusability, remove Yeelight classes dependency on ESP-DS-System
1 parent b88f304 commit 68f5e1e

File tree

3 files changed

+44
-50
lines changed

3 files changed

+44
-50
lines changed

BulbManager.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,14 @@ uint8_t BulbManager::discover() {
137137
}
138138
}
139139
if (new_bulb) {
140-
System::log->printf(TIMED("Bulb already registered; ignoring\n"));
140+
System::log->printf(TIMED("Received bulb id: %s is already registered; ignoring\n"), discovered_bulb->GetID());
141141
delete discovered_bulb;
142-
} else
142+
} else {
143143
bulbs.add(discovered_bulb);
144+
System::log->printf(TIMED("Registered bulb id: %s, name: %s, model: %s, power: %s\n"),
145+
discovered_bulb->GetID(), discovered_bulb->GetName(),
146+
discovered_bulb->GetModel(), discovered_bulb->GetPower() ? "on" : "off");
147+
}
144148
}
145149

146150
System::log->printf(TIMED("Total bulbs discovered: %d\n"), bulbs.size());
@@ -188,12 +192,12 @@ void BulbManager::printStatusHTML() const {
188192
for (uint8_t i = 0; i < const_cast<BulbManager *>(this)->bulbs.size(); i++) {
189193
const auto bulb = const_cast<BulbManager *>(this)->bulbs.get(i);
190194
if (bulb->isActive())
191-
bulb->printStatusHTML();
195+
bulb->printStatusHTML(System::web_page);
192196
}
193197
}
194198

195199
// Print bulb configuration controls in HTML
196200
void BulbManager::printConfHTML() const {
197201
for (uint8_t i = 0; i < const_cast<BulbManager *>(this)->bulbs.size(); i++)
198-
const_cast<BulbManager *>(this)->bulbs.get(i)->printConfHTML(i);
202+
const_cast<BulbManager *>(this)->bulbs.get(i)->printConfHTML(System::web_page, i);
199203
}

Yeelight.cpp

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
*/
55

66
#include "Yeelight.h" // Yeelight support
7-
#include "MySystem.h" // System-level definitions
8-
9-
using namespace ds;
7+
#include <ESP8266WiFi.h> // Wi-Fi support
108

119
// Yeelight protocol; see https://www.yeelight.com/en_US/developer
1210
const char *YL_MSG_TOGGLE = "{\"id\":1,\"method\":\"toggle\",\"params\":[]}\r\n";
@@ -52,36 +50,36 @@ int YBulb::Flip(WiFiClient &wfc) const {
5250

5351
// Print bulb status in HTML
5452
// TODO: synergy between two functions?
55-
void YBulb::printStatusHTML() const {
56-
System::web_page += "<li>id(";
57-
System::web_page += id;
58-
System::web_page += "), ip(";
59-
System::web_page += ip;
60-
System::web_page += "), name(";
61-
System::web_page += name;
62-
System::web_page += "), model(";
63-
System::web_page += model;
64-
System::web_page += ")</li>";
53+
void YBulb::printStatusHTML(String& str) const {
54+
str += "<li>id(";
55+
str += id;
56+
str += "), ip(";
57+
str += ip;
58+
str += "), name(";
59+
str += name;
60+
str += "), model(";
61+
str += model;
62+
str += ")</li>";
6563
}
6664

6765
// Print bulb configuration controls in HTML
68-
void YBulb::printConfHTML(uint8_t num) const {
69-
System::web_page += "<input type=\"checkbox\" name=\"bulb\" value=\"";
70-
System::web_page += num;
71-
System::web_page += "\"";
66+
void YBulb::printConfHTML(String& str, uint8_t num) const {
67+
str += "<input type=\"checkbox\" name=\"bulb\" value=\"";
68+
str += num;
69+
str += "\"";
7270
if (active)
73-
System::web_page += " checked";
74-
System::web_page += "/> ";
75-
System::web_page += ip;
76-
System::web_page += " id(";
77-
System::web_page += id;
78-
System::web_page += ") name(";
79-
System::web_page += name;
80-
System::web_page += ") model(";
81-
System::web_page += model;
82-
System::web_page += ") power(";
83-
System::web_page += power ? "<b>on</b>" : "off";
84-
System::web_page += ")<br/>";
71+
str += " checked";
72+
str += "/> ";
73+
str += ip;
74+
str += " id(";
75+
str += id;
76+
str += ") name(";
77+
str += name;
78+
str += ") model(";
79+
str += model;
80+
str += ") power(";
81+
str += power ? "<b>on</b>" : "off";
82+
str += ")<br/>";
8583
}
8684

8785

@@ -128,7 +126,6 @@ YBulb *YDiscovery::receive() {
128126
while (isInProgress()) {
129127
int len = udp.parsePacket();
130128
if (len > 0) {
131-
System::log->printf(TIMED("Received %d bytes from %s, port %d\n"), len, udp.remoteIP().toString().c_str(), udp.remotePort());
132129
len = udp.read(discovery_reply, sizeof(discovery_reply));
133130
if (len > 0) {
134131
discovery_reply[len] = 0;
@@ -150,23 +147,16 @@ YBulb *YDiscovery::receive() {
150147
port = strtok(nullptr, ":");
151148
if (host && port) {
152149
new_bulb = new YBulb(token, host, atoi(port));
153-
} else
154-
System::log->printf(TIMED("Bad address; ignoring 1 bulb\n"));
150+
}
155151
} else if (!strncmp(token, "model: ", 7)) {
156-
if (strtok(token, " ") && new_bulb) {
152+
if (strtok(token, " ") && new_bulb)
157153
new_bulb->SetModel(strtok(nullptr, " "));
158-
System::log->printf(TIMED("Bulb model: %s\n"), new_bulb->GetModel());
159-
}
160154
} else if (!strncmp(token, "name: ", 6)) {
161-
if (strtok(token, " ") && new_bulb) {
162-
new_bulb->SetName(strtok(nullptr, " "));
163-
System::log->printf(TIMED("Bulb name: %s\n"), new_bulb->GetName()); // Currently, Yeelights always seem to return an empty name here :(
164-
}
155+
if (strtok(token, " ") && new_bulb)
156+
new_bulb->SetName(strtok(nullptr, " ")); // Currently, Yeelights always seem to return an empty name here :(
165157
} else if (!strncmp(token, "power: ", 7)) {
166-
if (strtok(token, " ") && new_bulb) {
158+
if (strtok(token, " ") && new_bulb)
167159
new_bulb->SetPower(strcmp(strtok(nullptr, " "), "off"));
168-
System::log->printf(TIMED("Bulb power: %s\n"), new_bulb->GetPower() ? "on" : "off");
169-
}
170160
}
171161
token = strtok_r(nullptr, "\r\n", &line_ctx);
172162
}

Yeelight.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class YBulb {
2828

2929
public:
3030

31-
static const uint8_t ID_LENGTH = 18; // Length of the Yeelight device ID (chars)
32-
static const uint16_t TIMEOUT = 1000; // Bulb connection timeout (ms)
31+
static const uint8_t ID_LENGTH = 18; // Length of the Yeelight device ID (chars)
32+
static const uint16_t TIMEOUT = 1000; // Bulb connection timeout (ms)
3333

3434
YBulb(const char *, const char *, const uint16_t);
3535
~YBulb(){};
@@ -47,8 +47,8 @@ class YBulb {
4747
void Activate() { active = true; }
4848
void Deactivate() { active = false; }
4949
int Flip(WiFiClient&) const;
50-
void printStatusHTML() const; // Print bulb status in HTML
51-
void printConfHTML(uint8_t) const; // Print bulb configuration controls in HTML
50+
void printStatusHTML(String&) const; // Print bulb status in HTML
51+
void printConfHTML(String&, uint8_t) const; // Print bulb configuration controls in HTML
5252
bool operator==(const char *id2) const {
5353
return !strcmp(id, id2);
5454
}

0 commit comments

Comments
 (0)