Skip to content

Commit 2d42e3c

Browse files
authored
Merge pull request #103 from robertlipe/schedule-and-robustness
schedule and robustness
2 parents f1b25b0 + 9287572 commit 2d42e3c

4 files changed

Lines changed: 39 additions & 14 deletions

File tree

canvas.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Canvas : public ICanvas
2121
EffectsManager _effects;
2222
string _name;
2323
vector<shared_ptr<ILEDFeature>> _features;
24-
mutable mutex _featuresMutex;
24+
mutable recursive_mutex _featuresMutex;
2525

2626
public:
2727
Canvas(string name, uint32_t width, uint32_t height, uint16_t fps = 30) :
@@ -172,7 +172,7 @@ inline void from_json(const nlohmann::json& j, shared_ptr<ICanvas> & canvas)
172172
canvas = make_shared<Canvas>(
173173
canvasName,
174174
j.at("width").get<uint32_t>(),
175-
j.at("height").get<uint32_t>()
175+
j.value("height", uint32_t(1))
176176
);
177177

178178
if (!autoAssignCanvasId)

controller.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class Controller : public IController
6363
nlohmann::json jsonData;
6464
file >> jsonData;
6565

66-
return jsonData.get<unique_ptr<Controller>>();
66+
unique_ptr<Controller> ptr;
67+
from_json(jsonData, ptr);
68+
return ptr;
6769
}
6870

6971
void WriteToFile(const string& filePath) const override
@@ -559,7 +561,7 @@ inline void from_json(const nlohmann::json &j, unique_ptr<Controller> & ptrContr
559561
try
560562
{
561563
// Extract port
562-
uint16_t port = j.at("port").get<uint16_t>();
564+
uint16_t port = j.value("port", uint16_t(7777));
563565

564566
// Create controller
565567
ptrController = make_unique<Controller>(port);

pixeltypes.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,8 +1358,11 @@ inline void from_json(const nlohmann::json& j, CRGB& color)
13581358
return;
13591359
}
13601360
if (s[0] == '#') s = s.substr(1);
1361-
uint32_t val = static_cast<uint32_t>(stoul(s, nullptr, 16));
1362-
color = CRGB(val);
1361+
try {
1362+
color = CRGB(static_cast<uint32_t>(stoul(s, nullptr, 16)));
1363+
} catch (...) {
1364+
color = CRGB::Black;
1365+
}
13631366
} else if (j.is_array()) {
13641367
color = CRGB(
13651368
j.at(0).get<uint8_t>(),

schedule.h

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ class Schedule : public ISchedule
7070
// Note: localTime->tm_wday: Sunday == 0, Monday == 1, etc.
7171
if (daysOfWeek) {
7272
uint8_t todayBit = 1 << localTime->tm_wday;
73-
if (!(*daysOfWeek & todayBit))
73+
if (!(*daysOfWeek & todayBit)) {
74+
logger->debug("Schedule inactive: day bit {} not in mask {}", todayBit, *daysOfWeek);
7475
return false;
76+
}
7577
}
7678

7779
// Format current date and time as strings in "YYYY-MM-DD" and "HH:MM:SS" formats.
@@ -82,18 +84,36 @@ class Schedule : public ISchedule
8284
string currentTime = timeStream.str();
8385

8486
// Check start and stop dates if set.
85-
if (startDate && currentDate < *startDate)
87+
if (startDate && currentDate < *startDate) {
8688
return false;
89+
}
8790

88-
if (stopDate && currentDate > *stopDate)
91+
if (stopDate && currentDate > *stopDate) {
8992
return false;
93+
}
9094

9195
// Check start and stop times if set.
92-
if (startTime && currentTime < *startTime)
93-
return false;
94-
95-
if (stopTime && currentTime > *stopTime)
96-
return false;
96+
if (startTime && stopTime) {
97+
if (*startTime <= *stopTime) {
98+
// Normal range (e.g., 08:00:00 to 17:00:00)
99+
if (currentTime < *startTime || currentTime > *stopTime) {
100+
return false;
101+
}
102+
} else {
103+
// Overnight range (e.g., 22:00:00 to 06:00:00)
104+
if (currentTime < *startTime && currentTime > *stopTime) {
105+
return false;
106+
}
107+
}
108+
} else if (startTime) {
109+
if (currentTime < *startTime) {
110+
return false;
111+
}
112+
} else if (stopTime) {
113+
if (currentTime > *stopTime) {
114+
return false;
115+
}
116+
}
97117

98118
return true;
99119
}

0 commit comments

Comments
 (0)