Unofficial Dart implementation of Deutsche Bahn’s construction site API
https://pub.dev/packages/db_construction_site
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
3.0 KiB
113 lines
3.0 KiB
import 'package:json_annotation/json_annotation.dart';
|
|
|
|
part 'construction_report.g.dart';
|
|
|
|
@JsonSerializable(createToJson: false)
|
|
class ConstructionReport {
|
|
final String id;
|
|
@JsonKey(name: 'nr', fromJson: int.parse)
|
|
final int index;
|
|
|
|
@JsonKey(name: 'termin', fromJson: _parseListOrString)
|
|
final List<String> timePeriodTexts;
|
|
@JsonKey(name: 'termin_pins', fromJson: _parseListOrString)
|
|
final List<String> timePeriodPinsTexts;
|
|
@JsonKey(name: 'termin_zeit', fromJson: _parseListOrString)
|
|
final List<String> timePeriodTimeTexts;
|
|
|
|
final String headline;
|
|
final String headlinePins;
|
|
|
|
@JsonKey(name: 'meldung', fromJson: _parseListOrString)
|
|
final List<String> messages;
|
|
@JsonKey(name: 'meldung_pins', fromJson: _parseListOrString)
|
|
final List<String> messagesPins;
|
|
@JsonKey(name: 'meldung_hinweis', fromJson: _parseListOrString)
|
|
final List<String> messagesHint;
|
|
|
|
@JsonKey(name: 'hinweis_tabelle', fromJson: _parseListOrString)
|
|
final List<String> hintTable;
|
|
|
|
@JsonKey(name: 'grund')
|
|
final String reason;
|
|
|
|
@JsonKey(name: 'abschnitt')
|
|
final List<String> affectedStations;
|
|
|
|
final DateTime lastChange;
|
|
|
|
@JsonKey(name: 'ics')
|
|
final String icsUrl;
|
|
|
|
@JsonKey(name: 'termine', fromJson: _parseTimeRanges)
|
|
final List<TimeRange> timeRanges;
|
|
|
|
@JsonKey(name: 'von')
|
|
final DateTime startDate;
|
|
@JsonKey(name: 'bis')
|
|
final DateTime endDate;
|
|
|
|
ConstructionReport(
|
|
{this.id,
|
|
this.index,
|
|
this.timePeriodTexts,
|
|
this.timePeriodPinsTexts,
|
|
this.timePeriodTimeTexts,
|
|
this.headline,
|
|
this.headlinePins,
|
|
this.messages,
|
|
this.messagesPins,
|
|
this.messagesHint,
|
|
this.hintTable,
|
|
this.reason,
|
|
this.affectedStations,
|
|
this.lastChange,
|
|
this.icsUrl,
|
|
this.timeRanges,
|
|
this.startDate,
|
|
this.endDate});
|
|
|
|
factory ConstructionReport.fromJson(Map<String, dynamic> json) =>
|
|
_$ConstructionReportFromJson(json);
|
|
|
|
static List<TimeRange> _parseTimeRanges(List<dynamic> ranges) {
|
|
return ranges.map((range) => TimeRange._fromJson(range)).toList();
|
|
}
|
|
|
|
static List<String> _parseListOrString(dynamic input) {
|
|
switch (input.runtimeType) {
|
|
case String:
|
|
return [input];
|
|
case List:
|
|
return input;
|
|
default:
|
|
return [input.toString()];
|
|
}
|
|
}
|
|
}
|
|
|
|
class TimeRange {
|
|
final DateTime begin;
|
|
final DateTime end;
|
|
|
|
final bool allDay;
|
|
|
|
final List<int> weekdays;
|
|
|
|
const TimeRange({this.begin, this.end, this.allDay, this.weekdays});
|
|
|
|
TimeRange._fromJson(Map<String, dynamic> json)
|
|
: begin = _parseTimeStamp(json, 'von'),
|
|
end = _parseTimeStamp(json, 'bis'),
|
|
allDay = json['ganztaegig'] == '1',
|
|
weekdays = json['wochentage'] is List
|
|
? (json['wochentage'] as List<dynamic>)
|
|
.map((day) => int.parse(day))
|
|
.toList()
|
|
: null;
|
|
|
|
static DateTime _parseTimeStamp(Map<String, dynamic> json, String suffix) {
|
|
return DateTime.parse(
|
|
"${json["datum_$suffix"]} ${json["st_$suffix"]}:${json["min_$suffix"]}:00");
|
|
}
|
|
}
|
|
|