Skip to content

Commit 36c825c

Browse files
committed
* Refactor notification service and update related components to use IAppNotificationService
* Fix temporary exit state problem
1 parent 98a6357 commit 36c825c

12 files changed

Lines changed: 91 additions & 116 deletions

File tree

client/lib/app.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import 'package:borneo_app/services/default_notification_service.dart';
2-
import 'package:borneo_app/services/inotification_service.dart';
1+
import 'package:borneo_app/services/default_app_notification_service.dart';
2+
import 'package:borneo_app/services/i_app_notification_service.dart';
33
import 'package:borneo_app/views/main_screen.dart';
44
import 'package:borneo_kernel_abstractions/ikernel.dart';
55
import 'package:event_bus/event_bus.dart';
@@ -57,7 +57,7 @@ class BorneoApp extends StatelessWidget {
5757
final gt = GettextLocalizations.of(context);
5858
return MultiProvider(
5959
providers: [
60-
Provider<INotificationService>(create: (context) => DefaultNotificationService()),
60+
Provider<IAppNotificationService>(create: (context) => DefaultAppNotificationService()),
6161
// Here >>> register all providers that need to access the gettext interface <<<
6262
// SceneManager
6363
Provider<SceneManager>(

client/lib/devices/borneo/lyfi/manifest.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:borneo_app/devices/borneo/lyfi/view_models/lyfi_view_model.dart'
22
import 'package:borneo_app/devices/borneo/lyfi/views/lyfi_view.dart';
33
import 'package:borneo_app/models/devices/device_module_metadata.dart';
44
import 'package:borneo_app/services/device_manager.dart';
5-
import 'package:borneo_app/services/inotification_service.dart';
5+
import 'package:borneo_app/services/i_app_notification_service.dart';
66
import 'package:event_bus/event_bus.dart';
77
import 'package:flutter/material.dart';
88
import 'package:logger/logger.dart';
@@ -23,7 +23,7 @@ class LyfiDeviceModuleMetadata extends DeviceModuleMetadata {
2323
deviceID: deviceID,
2424
deviceManager: context.read<DeviceManager>(),
2525
globalEventBus: context.read<EventBus>(),
26-
notification: context.read<INotificationService>(),
26+
notification: context.read<IAppNotificationService>(),
2727
logger: context.read<Logger>(),
2828
),
2929
deviceIconBuilder: (BuildContext context) => Icon(Icons.lightbulb_outline),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:borneo_app/devices/borneo/lyfi/view_models/constants.dart';
22
import 'package:borneo_app/devices/borneo/lyfi/view_models/settings_view_model.dart';
33
import 'package:borneo_app/devices/borneo/lyfi/view_models/editor/sun_editor_view_model.dart';
44
import 'package:borneo_app/devices/borneo/view_models/base_borneo_device_view_model.dart';
5-
import 'package:borneo_app/services/inotification_service.dart';
5+
import 'package:borneo_app/services/i_app_notification_service.dart';
66
import 'package:borneo_kernel/drivers/borneo/lyfi/lyfi_driver.dart';
77
import 'package:flutter/foundation.dart';
88
import 'package:intl/intl.dart';
@@ -20,7 +20,7 @@ class LyfiViewModel extends BaseBorneoDeviceViewModel {
2020

2121
static final DateFormat deviceDateFormat = DateFormat('yyyy-MM-dd HH:mm');
2222

23-
final INotificationService notification;
23+
final IAppNotificationService notification;
2424

2525
ILyfiDeviceApi get _deviceApi => super.borneoDeviceApi as ILyfiDeviceApi;
2626

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:borneo_app/devices/borneo/lyfi/view_models/base_lyfi_device_view_model.dart';
22
import 'package:borneo_app/infrastructure/timezone.dart';
3-
import 'package:borneo_app/services/inotification_service.dart';
3+
import 'package:borneo_app/services/i_app_notification_service.dart';
44
import 'package:borneo_common/exceptions.dart' as bo_ex;
55
import 'package:geolocator/geolocator.dart';
66

@@ -9,7 +9,7 @@ import 'package:borneo_kernel/drivers/borneo/lyfi/lyfi_driver.dart';
99
import 'package:latlong2/latlong.dart';
1010

1111
class SettingsViewModel extends BaseLyfiDeviceViewModel {
12-
final INotificationService notification;
12+
final IAppNotificationService notification;
1313
final Uri address;
1414
final GeneralBorneoDeviceStatus borneoStatus;
1515
final GeneralBorneoDeviceInfo borneoInfo;
@@ -57,6 +57,7 @@ class SettingsViewModel extends BaseLyfiDeviceViewModel {
5757
@override
5858
Future<void> onInitialize() async {
5959
_correctionMethod = await api.getCorrectionMethod(boundDevice!.device);
60+
_temporaryDuration = await api.getTemporaryDuration(boundDevice!.device);
6061
}
6162

6263
Future<void> updateGeoLocation(LatLng location) async {

client/lib/devices/borneo/lyfi/views/lyfi_view.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'package:borneo_app/devices/borneo/lyfi/views/editor/sun_editor_view.dart';
22
import 'package:borneo_app/models/devices/device_entity.dart';
3-
import 'package:borneo_app/services/inotification_service.dart';
3+
import 'package:borneo_app/services/i_app_notification_service.dart';
44
import 'package:borneo_common/io/net/rssi.dart';
55
import 'package:borneo_kernel/drivers/borneo/lyfi/lyfi_driver.dart';
66
import 'package:event_bus/event_bus.dart';
@@ -329,7 +329,7 @@ class LyfiView extends StatelessWidget {
329329
deviceID: device.id,
330330
deviceManager: cb.read<DeviceManager>(),
331331
globalEventBus: cb.read<EventBus>(),
332-
notification: cb.read<INotificationService>(),
332+
notification: cb.read<IAppNotificationService>(),
333333
logger: cb.read<Logger>(),
334334
),
335335
builder: (context, child) {

client/lib/services/default_notification_service.dart renamed to client/lib/services/default_app_notification_service.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import 'package:borneo_app/services/inotification_service.dart';
1+
import 'package:borneo_app/services/i_app_notification_service.dart';
22
import 'package:flutter/material.dart';
33
import 'package:toastification/toastification.dart';
44

5-
class DefaultNotificationService implements INotificationService {
5+
class DefaultAppNotificationService implements IAppNotificationService {
66
@override
77
void showError(String title, {String? body}) {
88
toastification.show(
@@ -36,6 +36,9 @@ class DefaultNotificationService implements INotificationService {
3636
type: ToastificationType.success,
3737
title: Text(title),
3838
description: body != null ? Text(body) : null,
39+
autoCloseDuration: Duration(seconds: 5),
40+
closeOnClick: true,
41+
dragToClose: true,
3942
);
4043
}
4144

client/lib/services/inotification_service.dart renamed to client/lib/services/i_app_notification_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
abstract class INotificationService {
1+
abstract class IAppNotificationService {
22
void showSuccess(String title, {String? body});
33
void showError(String title, {String? body});
44
void showWarning(String title, {String? body});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ static void coap_hnd_temporary_duration_get(coap_resource_t* resource, coap_sess
472472
size_t encoded_size = 0;
473473
const struct led_user_settings* settings = led_get_settings();
474474

475-
uint8_t buf[128];
475+
uint8_t buf[32];
476476

477477
cbor_encoder_init(&encoder, buf, sizeof(buf), 0);
478478
BO_COAP_TRY_ENCODE_CBOR(cbor_encode_uint(&encoder, settings->temporary_duration));
@@ -497,11 +497,11 @@ static void coap_hnd_temporary_duration_put(coap_resource_t* resource, coap_sess
497497
int duration;
498498
BO_COAP_VERIFY(cbor_value_get_int_checked(&value, &duration));
499499

500-
if (duration <= 0 || duration > 0xFFFF) {
500+
if (duration <= 0 || duration > INT32_MAX - 1) {
501501
coap_pdu_set_code(response, BO_COAP_CODE_400_BAD_REQUEST);
502502
return;
503503
}
504-
BO_COAP_TRY(led_set_temporary_duration((uint16_t)duration), COAP_RESPONSE_CODE_INTERNAL_ERROR);
504+
BO_COAP_TRY(led_set_temporary_duration((uint32_t)duration), COAP_RESPONSE_CODE_INTERNAL_ERROR);
505505
coap_pdu_set_code(response, BO_COAP_CODE_204_CHANGED);
506506
}
507507

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

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ static void led_events_handler(void* handler_args, esp_event_base_t base, int32_
3535

3636
static void led_render_task();
3737

38-
void led_temporary_state_entry();
39-
void led_temporary_state_run();
40-
void led_temporary_state_exit();
38+
static void led_temporary_state_entry();
39+
static void led_temporary_state_run();
40+
static void led_temporary_state_exit();
4141

4242
static void normal_state_entry();
4343
static void normal_state_run();
@@ -57,6 +57,8 @@ static void dimming_state_exit();
5757
#define LED_MAX_DUTY ((1 << LEDC_TIMER_12_BIT) - 1)
5858
#define LED_DUTY_RES LEDC_TIMER_10_BIT
5959

60+
#define TEMPORARY_FADE_PERIOD_MS 5000
61+
6062
static inline led_duty_t channel_brightness_to_duty(led_brightness_t power);
6163
static inline void color_to_duties(const led_color_t color, led_duty_t* duties);
6264
static int led_set_channel_duty(uint8_t ch, led_duty_t duty);
@@ -836,3 +838,64 @@ static void preview_state_exit()
836838
led_update_color(_led.color_to_resume);
837839
ESP_LOGI(TAG, "Preview state ended.");
838840
}
841+
842+
int led_set_temporary_duration(uint32_t duration)
843+
{
844+
_led.settings.temporary_duration = duration;
845+
BO_TRY(led_save_user_settings());
846+
return 0;
847+
}
848+
849+
int32_t led_get_temporary_remaining()
850+
{
851+
if (led_get_state() == LED_STATE_TEMPORARY) {
852+
int64_t now = (esp_timer_get_time() + 500LL) / 1000LL;
853+
return (int32_t)((_led.temporary_off_time - now + 500LL) / 1000LL);
854+
}
855+
else {
856+
return -1;
857+
}
858+
}
859+
860+
void led_temporary_state_entry()
861+
{
862+
if (!bo_power_is_on()) {
863+
return;
864+
}
865+
866+
if (_led.settings.mode != LED_MODE_SUN && _led.settings.mode != LED_MODE_SCHEDULED) {
867+
return;
868+
}
869+
870+
int64_t now = (esp_timer_get_time() + 500LL) / 1000LL;
871+
872+
_led.temporary_off_time = now + (_led.settings.temporary_duration * 60 * 1000) + TEMPORARY_FADE_PERIOD_MS;
873+
874+
BO_MUST(led_fade_to_color(_led.settings.manual_color, TEMPORARY_FADE_PERIOD_MS));
875+
}
876+
877+
void led_temporary_state_exit()
878+
{
879+
assert(led_get_state() == LED_STATE_TEMPORARY);
880+
881+
_led.temporary_off_time = 0;
882+
}
883+
884+
void led_temporary_state_run()
885+
{
886+
assert(led_get_state() == LED_STATE_TEMPORARY);
887+
888+
int64_t now = (esp_timer_get_time() + 500LL) / 1000LL;
889+
890+
if (now >= _led.temporary_off_time) {
891+
smf_set_state(SMF_CTX(&_led), &LED_STATE_TABLE[LED_STATE_NORMAL]);
892+
}
893+
else {
894+
if (!led_is_fading()) {
895+
led_update_color(_led.settings.manual_color);
896+
}
897+
else {
898+
led_fade_drive();
899+
}
900+
}
901+
}

fw/lyfi/main/src/led/led.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ struct led_acclimation_settings {
8484
struct led_user_settings {
8585
uint8_t mode; ///< Running mode, see `enum led_running_modes`
8686

87-
uint16_t temporary_duration; ///< Night lighting state duration (in seconds)
87+
uint32_t temporary_duration; ///< Night lighting state duration (in seconds)
8888
struct led_scheduler scheduler; ///< Scheduling scheduler for scheduled state
8989
led_color_t manual_color; ///< Manual dimming color settings.
9090
led_color_t sun_color; ///< Sun simulation color settings.
@@ -161,7 +161,7 @@ int led_switch_mode(uint8_t mode);
161161

162162
bool led_is_blank();
163163

164-
int led_set_temporary_duration(uint16_t duration);
164+
int led_set_temporary_duration(uint32_t duration);
165165
int32_t led_get_temporary_remaining();
166166

167167
int led_set_correction_method(uint8_t correction_method);

0 commit comments

Comments
 (0)