Skip to content

Commit 3c0d970

Browse files
committed
fix acclimation
1 parent 630f9b0 commit 3c0d970

6 files changed

Lines changed: 44 additions & 22 deletions

File tree

client/lib/devices/borneo/lyfi/view_models/acclimation_view_model.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class AcclimationViewModel extends BaseLyfiDeviceViewModel {
2525

2626
Future<void> setEanbled(bool value) async {
2727
_enabled = value;
28+
setChanged();
2829
notifyListeners();
2930
}
3031

@@ -69,12 +70,8 @@ class AcclimationViewModel extends BaseLyfiDeviceViewModel {
6970
}
7071

7172
bool validate() {
72-
final localNow = DateTime.now();
73-
final now = DateTime(localNow.year, localNow.month, localNow.day).toUtc();
7473
return _startTimestamp.isUtc &&
75-
_startTimestamp.year >= now.year &&
76-
_startTimestamp.month >= now.month &&
77-
_startTimestamp.day >= now.day &&
74+
_startTimestamp.isAfter(DateTime(2025, 1, 1).toUtc()) &&
7875
_days >= 5 &&
7976
_days <= 100 &&
8077
_startPercent >= 10 &&

client/packages/borneo_kernel/lib/drivers/borneo/borneo_device_api.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ class GeneralBorneoDeviceStatus {
166166
final dynamic map = cborMap.toObject();
167167
return GeneralBorneoDeviceStatus(
168168
power: map['power'],
169-
timestamp: DateTime.fromMillisecondsSinceEpoch(map['timestamp'] * 1000),
169+
timestamp: DateTime.fromMillisecondsSinceEpoch(map['timestamp'] * 1000,
170+
isUtc: true),
170171
bootDuration: Duration(milliseconds: map['bootDuration']),
171172
timezone: map['timezone'] ?? '',
172173
wifiStatus: map['wifiStatus'],
@@ -175,7 +176,8 @@ class GeneralBorneoDeviceStatus {
175176
error: map['error'],
176177
shutdownReason: map['shutdownReason'],
177178
shutdownTimestamp: map['shutdownTimestamp'] > 0
178-
? DateTime.fromMillisecondsSinceEpoch(map['shutdownTimestamp'] * 1000)
179+
? DateTime.fromMillisecondsSinceEpoch(map['shutdownTimestamp'] * 1000,
180+
isUtc: true)
179181
: null,
180182
temperature: map['temperature'],
181183
powerVoltage:

client/packages/borneo_kernel/lib/drivers/borneo/lyfi/lyfi_driver.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ class AcclimationSettings {
167167
factory AcclimationSettings.fromMap(dynamic map) {
168168
return AcclimationSettings(
169169
enabled: map["enabled"],
170-
startTimestamp:
171-
DateTime.fromMillisecondsSinceEpoch(map['startTimestamp'] * 1000),
170+
startTimestamp: DateTime.fromMillisecondsSinceEpoch(
171+
map['startTimestamp'] * 1000,
172+
isUtc: true),
172173
days: map["days"],
173174
startPercent: map["startPercent"],
174175
);

client/packages/borneo_kernel/lib/kernel.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,19 @@ final class DefaultKernel implements IKernel {
235235
});
236236
}
237237

238+
Future<bool> _tryDoHeartBeat(BoundDevice bd) async {
239+
try {
240+
await bd.driver
241+
.heartbeat(bd.device, cancelToken: _heartbeatPollingTaskCancelToken)
242+
.timeout(kHeartbeatPollingInterval)
243+
.asCancellable(_heartbeatPollingTaskCancelToken);
244+
return true;
245+
} catch (e, stackTrace) {
246+
_logger.t("Failed to do heartbeat", error: e, stackTrace: stackTrace);
247+
return false;
248+
}
249+
}
250+
238251
Future<void> _heartbeatPollingPeriodicTask() async {
239252
if (!isInitialized) {
240253
return;
@@ -246,11 +259,7 @@ final class DefaultKernel implements IKernel {
246259
final devices = <BoundDevice>[];
247260
for (final bd in _boundDevices.values) {
248261
if (_pollIDList.contains(bd.device.id)) {
249-
futures.add(bd.driver
250-
.heartbeat(bd.device,
251-
cancelToken: _heartbeatPollingTaskCancelToken)
252-
.timeout(kHeartbeatPollingInterval)
253-
.asCancellable(_heartbeatPollingTaskCancelToken));
262+
futures.add(_tryDoHeartBeat(bd));
254263
devices.add(bd);
255264
}
256265
}

fw/lyfi/main/src/coap-resources/acclimation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static void coap_hnd_acclimation_post(coap_resource_t* resource, coap_session_t*
9797
return;
9898
}
9999

100-
if (start_percent < 0 || start_percent > 100) {
100+
if (start_percent < 10 || start_percent > 90) {
101101
coap_pdu_set_code(response, BO_COAP_CODE_400_BAD_REQUEST);
102102
return;
103103
}

fw/lyfi/main/src/led/acclimation.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,33 @@ int led_acclimation_drive(time_t utc_now, led_color_t color)
4141
return 0;
4242
}
4343

44+
if (!led_acclimation_is_enabled()) {
45+
return 0;
46+
}
47+
4448
struct led_acclimation_settings* acc = &_led.settings.acclimation;
4549
time_t end_time_utc = acc->start_utc + (SECS_PER_DAY * acc->duration);
46-
if (utc_now <= end_time_utc) {
50+
51+
if (utc_now >= acc->start_utc && utc_now <= end_time_utc) {
4752
if (!_led.acclimation_activated) {
4853
_led.acclimation_activated = true;
4954
}
50-
int days_elapsed = (int)((end_time_utc - utc_now) / SECS_PER_DAY);
55+
56+
int days_elapsed = (int)((utc_now - acc->start_utc) / SECS_PER_DAY);
57+
58+
if (days_elapsed > acc->duration) {
59+
days_elapsed = acc->duration;
60+
}
61+
5162
int total_increment = 100 - acc->start_percent;
52-
int current_increment = days_elapsed * total_increment;
53-
int percent = acc->start_percent + (current_increment + acc->duration / 2) / acc->duration;
63+
int percent = acc->start_percent + (days_elapsed * total_increment) / acc->duration;
64+
65+
if (percent > 100) {
66+
percent = 100;
67+
}
68+
5469
for (size_t ch = 0; ch < LYFI_LED_CHANNEL_COUNT; ch++) {
55-
if (end_time_utc <= utc_now) {
56-
return (led_brightness_t)(((uint32_t)color[ch] * percent + 50) / 100);
57-
}
70+
color[ch] = (led_brightness_t)(((uint32_t)color[ch] * percent + 50) / 100);
5871
}
5972
return 0;
6073
}

0 commit comments

Comments
 (0)