Skip to content

Commit

Permalink
#287 - Fixed parsing for legacy neopixel config
Browse files Browse the repository at this point in the history
  • Loading branch information
Clon1998 committed Nov 23, 2023
1 parent 19a3d9f commit ef02c83
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
23 changes: 20 additions & 3 deletions common/lib/data/converters/pixel_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,34 @@ import 'package:json_annotation/json_annotation.dart';

import '../dto/machine/leds/led.dart';

class PixelConverter extends JsonConverter<Pixel, List<dynamic>> {
class PixelConverter extends JsonConverter<Pixel, dynamic> {
const PixelConverter();

@override
Pixel fromJson(List<dynamic> json) {
Pixel fromJson(dynamic json) {
if (json is Map) {
return fromMap((json).cast<String, dynamic>());
} else if (json is List) {
return fromList(json);
} else {
throw ArgumentError('PixelConverter: Unknown type: ${json.runtimeType}');
}
}

Pixel fromList(List<dynamic> json) {
if (json.isEmpty) return const Pixel();

var list = json.map((e) => (e as num).toDouble()).toList();
return Pixel.fromList(list);
}

Pixel fromMap(Map<String, dynamic> json) {
if (json.isEmpty) return const Pixel();

var map = json.map((key, value) => MapEntry(key.toUpperCase(), (value as num).toDouble()));
return Pixel.fromMap(map);
}

@override
List<dynamic> toJson(Pixel object) => object.asList().toList(growable: false);
dynamic toJson(Pixel object) => object.legacy ? object.asMap() : object.asList().toList(growable: false);
}
12 changes: 11 additions & 1 deletion common/lib/data/dto/config/led/config_neopixel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ part 'config_neopixel.g.dart';

String unpackColorOrder(List<dynamic> e) => e.isEmpty ? '' : e.cast<String>().first.toUpperCase();

String versionedColorOrder(dynamic e) {
if (e is String) {
return e.toUpperCase();
} else if (e is List) {
return unpackColorOrder(e);
} else {
throw ArgumentError('Invalid color order type: ${e.runtimeType}');
}
}

@freezed
class ConfigNeopixel extends ConfigLed with _$ConfigNeopixel {
const ConfigNeopixel._();
Expand All @@ -21,7 +31,7 @@ class ConfigNeopixel extends ConfigLed with _$ConfigNeopixel {
required String name,
@JsonKey(required: true) required String pin,
required int chainCount,
@JsonKey(fromJson: unpackColorOrder) @Default('RGB') String colorOrder,
@JsonKey(fromJson: versionedColorOrder) @Default('RGB') String colorOrder,
@Default(0) double initialRed,
@Default(0) double initialGreen,
@Default(0) double initialBlue,
Expand Down

0 comments on commit ef02c83

Please sign in to comment.