|
| 1 | +part of 'movesense_flutter.dart'; |
| 2 | + |
| 3 | +/// Device info returned from the Movesense `/Info` endpoint. |
| 4 | +class MovesenseDeviceInfo { |
| 5 | + final String manufacturerName; |
| 6 | + final String? brandName; |
| 7 | + final String productName; |
| 8 | + final String variant; |
| 9 | + final String design; |
| 10 | + final String hwCompatibilityId; |
| 11 | + final String serial; |
| 12 | + final String pcbaSerial; |
| 13 | + final String sw; |
| 14 | + final String hw; |
| 15 | + final String? additionalVersionInfo; |
| 16 | + final List<MovesenseAddressInfo> addressInfo; |
| 17 | + final String apiLevel; |
| 18 | + |
| 19 | + const MovesenseDeviceInfo({ |
| 20 | + required this.manufacturerName, |
| 21 | + required this.brandName, |
| 22 | + required this.productName, |
| 23 | + required this.variant, |
| 24 | + required this.design, |
| 25 | + required this.hwCompatibilityId, |
| 26 | + required this.serial, |
| 27 | + required this.pcbaSerial, |
| 28 | + required this.sw, |
| 29 | + required this.hw, |
| 30 | + required this.additionalVersionInfo, |
| 31 | + required this.addressInfo, |
| 32 | + required this.apiLevel, |
| 33 | + }); |
| 34 | + |
| 35 | + /// Build from the raw JSON string returned by the device. |
| 36 | + factory MovesenseDeviceInfo.fromJsonString(String jsonString) { |
| 37 | + final dynamic decoded = json.decode(jsonString); |
| 38 | + if (decoded is! Map<String, dynamic>) { |
| 39 | + throw const FormatException('Device info JSON must be an object'); |
| 40 | + } |
| 41 | + final dynamic content = decoded['Content']; |
| 42 | + if (content is! Map<String, dynamic>) { |
| 43 | + throw const FormatException('Device info JSON missing Content object'); |
| 44 | + } |
| 45 | + return MovesenseDeviceInfo.fromMap(content); |
| 46 | + } |
| 47 | + |
| 48 | + /// Build from a JSON map. |
| 49 | + factory MovesenseDeviceInfo.fromMap(Map<String, dynamic> map) { |
| 50 | + final dynamic addresses = map['addressInfo']; |
| 51 | + if (addresses is! List) { |
| 52 | + throw const FormatException('Device info missing addressInfo list'); |
| 53 | + } |
| 54 | + |
| 55 | + return MovesenseDeviceInfo( |
| 56 | + manufacturerName: map['manufacturerName'] as String? ?? '', |
| 57 | + brandName: map['brandName'] as String?, |
| 58 | + productName: map['productName'] as String? ?? '', |
| 59 | + variant: map['variant'] as String? ?? '', |
| 60 | + design: map['design'] as String? ?? '', |
| 61 | + hwCompatibilityId: map['hwCompatibilityId'] as String? ?? '', |
| 62 | + serial: map['serial'] as String? ?? '', |
| 63 | + pcbaSerial: map['pcbaSerial'] as String? ?? '', |
| 64 | + sw: map['sw'] as String? ?? '', |
| 65 | + hw: map['hw'] as String? ?? '', |
| 66 | + additionalVersionInfo: map['additionalVersionInfo'] as String?, |
| 67 | + addressInfo: addresses |
| 68 | + .map( |
| 69 | + (entry) => MovesenseAddressInfo.fromMap( |
| 70 | + entry is Map<String, dynamic> ? entry : <String, dynamic>{}, |
| 71 | + ), |
| 72 | + ) |
| 73 | + .toList(growable: false), |
| 74 | + apiLevel: map['apiLevel'] as String? ?? '', |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + Map<String, dynamic> toMap() => <String, dynamic>{ |
| 79 | + 'manufacturerName': manufacturerName, |
| 80 | + 'brandName': brandName, |
| 81 | + 'productName': productName, |
| 82 | + 'variant': variant, |
| 83 | + 'design': design, |
| 84 | + 'hwCompatibilityId': hwCompatibilityId, |
| 85 | + 'serial': serial, |
| 86 | + 'pcbaSerial': pcbaSerial, |
| 87 | + 'sw': sw, |
| 88 | + 'hw': hw, |
| 89 | + 'additionalVersionInfo': additionalVersionInfo, |
| 90 | + 'addressInfo': addressInfo.map((a) => a.toMap()).toList(growable: false), |
| 91 | + 'apiLevel': apiLevel, |
| 92 | + }; |
| 93 | + |
| 94 | + /// Serialize to JSON string with the original Content wrapper. |
| 95 | + String toJsonString() => json.encode(<String, dynamic>{'Content': toMap()}); |
| 96 | +} |
| 97 | + |
| 98 | +/// Address info entry in the Movesense device info. |
| 99 | +class MovesenseAddressInfo { |
| 100 | + final String name; |
| 101 | + final String address; |
| 102 | + |
| 103 | + const MovesenseAddressInfo({required this.name, required this.address}); |
| 104 | + |
| 105 | + factory MovesenseAddressInfo.fromMap(Map<String, dynamic> map) { |
| 106 | + return MovesenseAddressInfo( |
| 107 | + name: map['name'] as String? ?? '', |
| 108 | + address: map['address'] as String? ?? '', |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + Map<String, dynamic> toMap() => <String, dynamic>{ |
| 113 | + 'name': name, |
| 114 | + 'address': address, |
| 115 | + }; |
| 116 | +} |
0 commit comments