Skip to content

Commit 770fb97

Browse files
committed
feat: enhance Lyfi actions with error handling and logging; update version to 0.5.1
1 parent 8a79002 commit 770fb97

File tree

5 files changed

+128
-44
lines changed

5 files changed

+128
-44
lines changed

client/packages/borneo_wot/lib/borneo/lyfi/wot_actions.dart

Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,65 @@ import 'package:borneo_kernel/drivers/borneo/device_api.dart';
44
import 'package:borneo_kernel/drivers/borneo/lyfi/api.dart';
55
import 'package:borneo_kernel/drivers/borneo/lyfi/models.dart';
66
import 'package:borneo_kernel_abstractions/device.dart';
7+
import 'package:logger/logger.dart';
78
import 'package:lw_wot/wot.dart';
89

910
/// Custom action for switching Lyfi states
1011
class LyfiSwitchStateAction extends WotAction<Map<String, dynamic>> {
11-
final LyfiState targetState;
1212
final ILyfiDeviceApi lyfiApi;
1313
final Device device;
14+
final Logger? logger;
1415

1516
LyfiSwitchStateAction({
1617
required super.id,
1718
required super.thing,
18-
required this.targetState,
1919
required this.lyfiApi,
2020
required this.device,
21-
}) : super(name: 'switchState', input: {'state': targetState.name});
21+
required super.input,
22+
this.logger,
23+
}) : super(name: 'switchState');
2224

2325
@override
2426
Future<void> performAction() async {
25-
await lyfiApi.switchState(device, targetState);
26-
thing.findProperty('state')?.value.notifyOfExternalUpdate(targetState);
27+
try {
28+
final targetState = LyfiState.fromString(input['state']);
29+
await lyfiApi.switchState(device, targetState);
30+
thing.findProperty('state')?.value.notifyOfExternalUpdate(input['state']);
31+
} catch (e, st) {
32+
logger?.e('switchState failed for device ${device.id}', error: e, stackTrace: st);
33+
rethrow;
34+
}
2735
}
2836
}
2937

3038
/// Custom action for switching Lyfi modes
3139
class LyfiSwitchModeAction extends WotAction<Map<String, dynamic>?> {
32-
final LyfiMode targetMode;
3340
final ILyfiDeviceApi lyfiApi;
3441
final Device device;
35-
final List<int>? color;
42+
final Logger? logger;
3643

3744
LyfiSwitchModeAction({
3845
required super.id,
3946
required super.thing,
40-
required this.targetMode,
4147
required this.lyfiApi,
4248
required this.device,
43-
this.color,
44-
}) : super(name: 'switchMode', input: {'mode': targetMode.name, 'color': ?color});
49+
required super.input,
50+
this.logger,
51+
}) : super(name: 'switchMode');
4552

4653
@override
4754
Future<void> performAction() async {
48-
await lyfiApi.switchMode(device, targetMode);
49-
thing.findProperty('mode')?.value.notifyOfExternalUpdate(targetMode);
50-
if (color != null && targetMode == LyfiMode.manual) {
51-
await lyfiApi.setColor(device, color!);
55+
try {
56+
final targetMode = LyfiMode.fromString(input!['mode']);
57+
final targetColor = input?['color'];
58+
await lyfiApi.switchMode(device, targetMode);
59+
thing.findProperty('mode')?.value.notifyOfExternalUpdate(input!['mode']);
60+
if (targetColor != null && targetMode == LyfiMode.manual) {
61+
await lyfiApi.setColor(device, targetColor);
62+
}
63+
} catch (e, st) {
64+
logger?.e('switchMode failed for device ${device.id}', error: e, stackTrace: st);
65+
rethrow;
5266
}
5367
}
5468
}
@@ -58,19 +72,26 @@ class LyfiSetColorAction extends WotAction<Map<String, dynamic>> {
5872
final List<int> color;
5973
final ILyfiDeviceApi lyfiApi;
6074
final Device device;
75+
final Logger? logger;
6176

6277
LyfiSetColorAction({
6378
required super.id,
6479
required super.thing,
6580
required this.color,
6681
required this.lyfiApi,
6782
required this.device,
83+
this.logger,
6884
}) : super(name: 'setColor', input: {'color': color});
6985

7086
@override
7187
Future<void> performAction() async {
72-
await lyfiApi.setColor(device, color);
73-
thing.findProperty('color')!.value.notifyOfExternalUpdate(color);
88+
try {
89+
await lyfiApi.setColor(device, color);
90+
thing.findProperty('color')!.value.notifyOfExternalUpdate(color);
91+
} catch (e, st) {
92+
logger?.e('setColor failed for device ${device.id}', error: e, stackTrace: st);
93+
rethrow;
94+
}
7495
}
7596
}
7697

@@ -79,19 +100,26 @@ class LyfiSetScheduleAction extends WotAction<Map<String, dynamic>> {
79100
final ScheduleTable schedule;
80101
final ILyfiDeviceApi lyfiApi;
81102
final Device device;
103+
final Logger? logger;
82104

83105
LyfiSetScheduleAction({
84106
required super.id,
85107
required super.thing,
86108
required this.schedule,
87109
required this.lyfiApi,
88110
required this.device,
111+
this.logger,
89112
}) : super(name: 'setSchedule', input: {'schedule': schedule.map((s) => s.toPayload()).toList()});
90113

91114
@override
92115
Future<void> performAction() async {
93-
await lyfiApi.setSchedule(device, schedule);
94-
thing.findProperty('schedule')!.value.notifyOfExternalUpdate(schedule);
116+
try {
117+
await lyfiApi.setSchedule(device, schedule);
118+
thing.findProperty('schedule')!.value.notifyOfExternalUpdate(schedule);
119+
} catch (e, st) {
120+
logger?.e('setSchedule failed for device ${device.id}', error: e, stackTrace: st);
121+
rethrow;
122+
}
95123
}
96124
}
97125

@@ -100,13 +128,15 @@ class LyfiSetAcclimationAction extends WotAction<Map<String, dynamic>> {
100128
final AcclimationSettings settings;
101129
final ILyfiDeviceApi lyfiApi;
102130
final Device device;
131+
final Logger? logger;
103132

104133
LyfiSetAcclimationAction({
105134
required super.id,
106135
required super.thing,
107136
required this.settings,
108137
required this.lyfiApi,
109138
required this.device,
139+
this.logger,
110140
}) : super(
111141
name: 'setAcclimation',
112142
input: {
@@ -119,8 +149,13 @@ class LyfiSetAcclimationAction extends WotAction<Map<String, dynamic>> {
119149

120150
@override
121151
Future<void> performAction() async {
122-
await lyfiApi.setAcclimation(device, settings);
123-
thing.findProperty('acclimation')?.value.notifyOfExternalUpdate(settings);
152+
try {
153+
await lyfiApi.setAcclimation(device, settings);
154+
thing.findProperty('acclimation')?.value.notifyOfExternalUpdate(settings);
155+
} catch (e, st) {
156+
logger?.e('setAcclimation failed for device ${device.id}', error: e, stackTrace: st);
157+
rethrow;
158+
}
124159
}
125160
}
126161

@@ -129,18 +164,25 @@ class LyfiSetLocationAction extends WotAction<Map<String, dynamic>> {
129164
final GeoLocation location;
130165
final ILyfiDeviceApi lyfiApi;
131166
final Device device;
167+
final Logger? logger;
132168

133169
LyfiSetLocationAction({
134170
required super.id,
135171
required super.thing,
136172
required this.location,
137173
required this.lyfiApi,
138174
required this.device,
175+
this.logger,
139176
}) : super(name: 'setLocation', input: {'lat': location.lat, 'lng': location.lng});
140177

141178
@override
142179
Future<void> performAction() async {
143-
await lyfiApi.setLocation(device, location);
180+
try {
181+
await lyfiApi.setLocation(device, location);
182+
} catch (e, st) {
183+
logger?.e('setLocation failed for device ${device.id}', error: e, stackTrace: st);
184+
rethrow;
185+
}
144186
}
145187
}
146188

@@ -149,18 +191,25 @@ class LyfiSetCorrectionMethodAction extends WotAction<Map<String, dynamic>> {
149191
final LedCorrectionMethod method;
150192
final ILyfiDeviceApi lyfiApi;
151193
final Device device;
194+
final Logger? logger;
152195

153196
LyfiSetCorrectionMethodAction({
154197
required super.id,
155198
required super.thing,
156199
required this.method,
157200
required this.lyfiApi,
158201
required this.device,
202+
this.logger,
159203
}) : super(name: 'setCorrectionMethod', input: {'method': method.name});
160204

161205
@override
162206
Future<void> performAction() async {
163-
await lyfiApi.setCorrectionMethod(device, method);
207+
try {
208+
await lyfiApi.setCorrectionMethod(device, method);
209+
} catch (e, st) {
210+
logger?.e('setCorrectionMethod failed for device ${device.id}', error: e, stackTrace: st);
211+
rethrow;
212+
}
164213
}
165214
}
166215

@@ -169,18 +218,25 @@ class LyfiSetPowerBehaviorAction extends WotAction<Map<String, dynamic>> {
169218
final PowerBehavior behavior;
170219
final IBorneoDeviceApi borneoApi;
171220
final Device device;
221+
final Logger? logger;
172222

173223
LyfiSetPowerBehaviorAction({
174224
required super.id,
175225
required super.thing,
176226
required this.behavior,
177227
required this.borneoApi,
178228
required this.device,
229+
this.logger,
179230
}) : super(name: 'setPowerBehavior', input: {'behavior': behavior.name});
180231

181232
@override
182233
Future<void> performAction() async {
183-
await borneoApi.setPowerBehavior(device, behavior);
234+
try {
235+
await borneoApi.setPowerBehavior(device, behavior);
236+
} catch (e, st) {
237+
logger?.e('setPowerBehavior failed for device ${device.id}', error: e, stackTrace: st);
238+
rethrow;
239+
}
184240
}
185241
}
186242

@@ -189,20 +245,27 @@ class LyfiSetMoonConfigAction extends WotAction<Map<String, dynamic>> {
189245
final MoonConfig config;
190246
final ILyfiDeviceApi lyfiApi;
191247
final Device device;
248+
final Logger? logger;
192249

193250
LyfiSetMoonConfigAction({
194251
required super.id,
195252
required super.thing,
196253
required this.config,
197254
required this.lyfiApi,
198255
required this.device,
256+
this.logger,
199257
}) : super(name: 'setMoonConfig', input: {'config': config.toPayload()});
200258

201259
@override
202260
Future<void> performAction() async {
203-
await lyfiApi.setMoonConfig(device, config);
204-
final curve = await lyfiApi.getMoonCurve(device);
205-
thing.findProperty('moonConfig')!.value.notifyOfExternalUpdate(config);
206-
thing.findProperty('moonCurve')!.value.notifyOfExternalUpdate(curve);
261+
try {
262+
await lyfiApi.setMoonConfig(device, config);
263+
final curve = await lyfiApi.getMoonCurve(device);
264+
thing.findProperty('moonConfig')!.value.notifyOfExternalUpdate(config);
265+
thing.findProperty('moonCurve')!.value.notifyOfExternalUpdate(curve);
266+
} catch (e, st) {
267+
logger?.e('setMoonConfig failed for device ${device.id}', error: e, stackTrace: st);
268+
rethrow;
269+
}
207270
}
208271
}

client/packages/borneo_wot/lib/borneo/lyfi/wot_thing.actions.dart

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ extension LyfiThingActions on LyfiThing {
1212
input: {'state': 'string'},
1313
),
1414
(thing, input) {
15-
final stateName = input['state'] as String;
16-
final targetState = LyfiState.values.firstWhere((e) => e.name == stateName);
1715
return LyfiSwitchStateAction(
1816
id: DateTime.now().millisecondsSinceEpoch.toString(),
1917
thing: thing,
20-
targetState: targetState,
2118
lyfiApi: _requireLyfiApi(),
2219
device: _requireDevice(),
20+
logger: logger,
21+
input: input,
2322
);
2423
},
2524
);
@@ -33,16 +32,13 @@ extension LyfiThingActions on LyfiThing {
3332
input: {'mode': 'string', 'color': 'array'},
3433
),
3534
(thing, input) {
36-
final modeName = input['mode'] as String;
37-
final targetMode = LyfiMode.values.firstWhere((e) => e.name == modeName);
38-
final color = input['color'] as List<int>?;
3935
return LyfiSwitchModeAction(
4036
id: DateTime.now().millisecondsSinceEpoch.toString(),
4137
thing: thing,
42-
targetMode: targetMode,
4338
lyfiApi: _requireLyfiApi(),
4439
device: _requireDevice(),
45-
color: color,
40+
input: input,
41+
logger: logger,
4642
);
4743
},
4844
);
@@ -63,6 +59,7 @@ extension LyfiThingActions on LyfiThing {
6359
color: color,
6460
lyfiApi: _requireLyfiApi(),
6561
device: _requireDevice(),
62+
logger: logger,
6663
);
6764
},
6865
);
@@ -83,6 +80,7 @@ extension LyfiThingActions on LyfiThing {
8380
schedule: schedule,
8481
lyfiApi: _requireLyfiApi(),
8582
device: _requireDevice(),
83+
logger: logger,
8684
);
8785
},
8886
);
@@ -108,6 +106,7 @@ extension LyfiThingActions on LyfiThing {
108106
settings: settings,
109107
lyfiApi: _requireLyfiApi(),
110108
device: _requireDevice(),
109+
logger: logger,
111110
);
112111
},
113112
);
@@ -128,6 +127,7 @@ extension LyfiThingActions on LyfiThing {
128127
location: location,
129128
lyfiApi: _requireLyfiApi(),
130129
device: _requireDevice(),
130+
logger: logger,
131131
);
132132
},
133133
);
@@ -149,6 +149,7 @@ extension LyfiThingActions on LyfiThing {
149149
method: method,
150150
lyfiApi: _requireLyfiApi(),
151151
device: _requireDevice(),
152+
logger: logger,
152153
);
153154
},
154155
);
@@ -170,6 +171,7 @@ extension LyfiThingActions on LyfiThing {
170171
behavior: behavior,
171172
borneoApi: _requireBorneoApi(),
172173
device: _requireDevice(),
174+
logger: logger,
173175
);
174176
},
175177
);
@@ -190,6 +192,7 @@ extension LyfiThingActions on LyfiThing {
190192
config: config,
191193
lyfiApi: _requireLyfiApi(),
192194
device: _requireDevice(),
195+
logger: logger,
193196
);
194197
},
195198
);

0 commit comments

Comments
 (0)