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.
229 lines
5.3 KiB
229 lines
5.3 KiB
// To parse this JSON data, do |
|
// |
|
// final trainData = trainDataFromJson(jsonString); |
|
|
|
import 'dart:convert'; |
|
|
|
TrainData trainDataFromJson(String str) => TrainData.fromJson(json.decode(str)); |
|
|
|
String trainDataToJson(TrainData data) => json.encode(data.toJson()); |
|
|
|
/// Results of scrapping InfoFer website for train info |
|
class TrainData { |
|
TrainData({ |
|
required this.date, |
|
required this.number, |
|
required this.operator, |
|
required this.rank, |
|
required this.route, |
|
required this.stations, |
|
this.status, |
|
}); |
|
|
|
final String date; |
|
final String number; |
|
final String operator; |
|
final String rank; |
|
final Route route; |
|
final List<Station> stations; |
|
final TrainDataStatus? status; |
|
|
|
factory TrainData.fromJson(Map<String, dynamic> json) => TrainData( |
|
date: json["date"], |
|
number: json["number"], |
|
operator: json["operator"], |
|
rank: json["rank"], |
|
route: Route.fromJson(json["route"]), |
|
stations: List<Station>.from( |
|
json["stations"].map((x) => Station.fromJson(x))), |
|
status: json["status"] == null |
|
? null |
|
: TrainDataStatus.fromJson(json["status"]), |
|
); |
|
|
|
Map<String, dynamic> toJson() => { |
|
"date": date, |
|
"number": number, |
|
"operator": operator, |
|
"rank": rank, |
|
"route": route.toJson(), |
|
"stations": List<dynamic>.from(stations.map((x) => x.toJson())), |
|
"status": status?.toJson(), |
|
}; |
|
} |
|
|
|
/// Route of the train |
|
class Route { |
|
Route({ |
|
required this.from, |
|
required this.to, |
|
}); |
|
|
|
final String from; |
|
final String to; |
|
|
|
factory Route.fromJson(Map<String, dynamic> json) => Route( |
|
from: json["from"], |
|
to: json["to"], |
|
); |
|
|
|
Map<String, dynamic> toJson() => { |
|
"from": from, |
|
"to": to, |
|
}; |
|
} |
|
|
|
class Station { |
|
Station({ |
|
this.arrival, |
|
this.departure, |
|
required this.km, |
|
required this.name, |
|
this.platform, |
|
this.stoppingTime, |
|
}); |
|
|
|
final StationArrDepTime? arrival; |
|
final StationArrDepTime? departure; |
|
final int km; |
|
final String name; |
|
final String? platform; |
|
final int? stoppingTime; |
|
|
|
factory Station.fromJson(Map<String, dynamic> json) => Station( |
|
arrival: json["arrival"] == null |
|
? null |
|
: StationArrDepTime.fromJson(json["arrival"]), |
|
departure: json["departure"] == null |
|
? null |
|
: StationArrDepTime.fromJson(json["departure"]), |
|
km: json["km"], |
|
name: json["name"], |
|
platform: json["platform"] == null ? null : json["platform"], |
|
stoppingTime: |
|
json["stoppingTime"] == null ? null : json["stoppingTime"], |
|
); |
|
|
|
Map<String, dynamic> toJson() => { |
|
"arrival": arrival?.toJson(), |
|
"departure": departure?.toJson(), |
|
"km": km, |
|
"name": name, |
|
"platform": platform == null ? null : platform, |
|
"stoppingTime": stoppingTime == null ? null : stoppingTime, |
|
}; |
|
} |
|
|
|
class StationArrDepTime { |
|
StationArrDepTime({ |
|
required this.scheduleTime, |
|
this.status, |
|
}); |
|
|
|
final String scheduleTime; |
|
final StationArrDepTimeStatus? status; |
|
|
|
factory StationArrDepTime.fromJson(Map<String, dynamic> json) => |
|
StationArrDepTime( |
|
scheduleTime: json["scheduleTime"], |
|
status: json["status"] == null ? null : StationArrDepTimeStatus.fromJson(json["status"]), |
|
); |
|
|
|
Map<String, dynamic> toJson() => { |
|
"scheduleTime": scheduleTime, |
|
"status": status?.toJson(), |
|
}; |
|
} |
|
|
|
class StationArrDepTimeStatus { |
|
StationArrDepTimeStatus({ |
|
required this.delay, |
|
required this.real, |
|
}); |
|
|
|
final int delay; |
|
final bool real; |
|
|
|
factory StationArrDepTimeStatus.fromJson(Map<String, dynamic> json) => |
|
StationArrDepTimeStatus( |
|
delay: json["delay"], |
|
real: json["real"], |
|
); |
|
|
|
Map<String, dynamic> toJson() => { |
|
"delay": delay, |
|
"real": real, |
|
}; |
|
} |
|
|
|
class TrainDataStatus { |
|
TrainDataStatus({ |
|
required this.delay, |
|
required this.state, |
|
required this.station, |
|
}); |
|
|
|
final int delay; |
|
final State state; |
|
final String station; |
|
|
|
factory TrainDataStatus.fromJson(Map<String, dynamic> json) => |
|
TrainDataStatus( |
|
delay: json["delay"], |
|
state: stateValues.map[json["state"]]!, |
|
station: json["station"], |
|
); |
|
|
|
Map<String, dynamic> toJson() => { |
|
"delay": delay, |
|
"state": stateValues.reverse[state], |
|
"station": station, |
|
}; |
|
|
|
@override |
|
String toString() { |
|
String result = ''; |
|
if (delay == 0) { |
|
result += 'La timp'; |
|
} |
|
else { |
|
result += '${delay.abs()} min'; |
|
} |
|
result += ' la '; |
|
switch (state) { |
|
case State.PASSING: |
|
result += 'trecerea fără oprire prin'; |
|
break; |
|
case State.ARRIVAL: |
|
result += 'sosirea în'; |
|
break; |
|
case State.DEPARTURE: |
|
result += 'plecarea din'; |
|
break; |
|
} |
|
result += station; |
|
return result; |
|
} |
|
} |
|
|
|
enum State { PASSING, ARRIVAL, DEPARTURE } |
|
|
|
final stateValues = EnumValues({ |
|
"arrival": State.ARRIVAL, |
|
"departure": State.DEPARTURE, |
|
"passing": State.PASSING |
|
}); |
|
|
|
class EnumValues<T> { |
|
Map<String, T> map; |
|
Map<T, String>? reverseMap; |
|
|
|
EnumValues(this.map); |
|
|
|
Map<T, String> get reverse { |
|
if (reverseMap == null) { |
|
reverseMap = map.map((k, v) => new MapEntry(v, k)); |
|
} |
|
return reverseMap!; |
|
} |
|
}
|
|
|