-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_train_data.pde
More file actions
125 lines (102 loc) · 3.36 KB
/
Copy pathget_train_data.pde
File metadata and controls
125 lines (102 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
With this sketch you can request train location data from the Dutch Railways (NS).
To get this to work, add the HTTP Requests Library by going to:
Sketch -> Import Library -> Add Library -> Find HTTP Requests for Processing by Rune Madsen and Daniel Shiffman and click Install
Request a free API key by registering in the API store of NS: https://apiportal.ns.nl/
This code uses the Virtual Train API. Add your key in the code below.
Structure:
links, payload, treinen
"treinNummer": 32230,
"ritId": "32230",
"lat": 51.6389,
"lng": 5.945404,
"snelheid": 65.64729,
"richting": 312.0,
"horizontaleNauwkeurigheid": 2333.3333,
"type": "ARR",
"bron": "KV6"
*/
import http.requests.*;
JSONObject json;
JSONObject payLoad;
JSONArray treinPosities;
JSONObject trein;
import java.io.BufferedWriter;
import java.io.FileWriter;
String outFilename = "output.txt";
//Interval in seconds when data is saved. Default is every 10 seconds.
int interval = 10;
int count = 1;
int prevCount = 21;
int errorCount = 0;
void setup(){
size(100,100);
outFilename = year() +"_"+month()+"_"+day()+"_"+hour()+"_"+minute()+"_trains.txt";
}
void draw(){
//Save data every interval.
count = second();
if (count%interval==0){
if (count!=prevCount){
loadData();
}
}
prevCount = count;
//An API can sometimes throw an error, if it's a single one it will be ignored but if we get 6 of them in a row it might be better to stop.
if (errorCount>6){
println("Too many errors");
exit();
}
}
void loadData() {
try{
GetRequest get = new GetRequest("https://gateway.apiportal.ns.nl/virtual-train-api/api/vehicle");
// Fill your API key in the line below between double quotes like: get.addHeader("Ocp-Apim-Subscription-Key", "f1894d0c");
get.addHeader("Ocp-Apim-Subscription-Key", "YOUR_API_KEY_HERE");
get.send();
json = parseJSONObject(get.getContent());
println(json);
payLoad = json.getJSONObject("payload");
treinPosities = payLoad.getJSONArray("treinen");
for (int i = 0; i < treinPosities.size() ; i++) {
trein = treinPosities.getJSONObject(i);
int trainNumber = trein.getInt("treinNummer");
float lat = trein.getFloat("lat");
float lon = trein.getFloat("lng");
errorCount = 0;
appendTextToFile(outFilename, year() +"-"+nf(month(),2)+"-"+nf(day(),2)+" "+nf(hour(),2)+ ":" +nf(minute(),2) + ":" +nf(second(),2) + "," +lat+"," + lon + "," + trainNumber);
}
}
catch (Exception E){
errorCount++;
System.out.println(E);
appendTextToFile(outFilename,"error");
}
}
// Code to add data to a textfile.
// Original code from https://stackoverflow.com/questions/17010222/how-do-i-append-text-to-a-csv-txt-file-in-processing
void appendTextToFile(String filename, String text){
File f = new File(dataPath(filename));
if(!f.exists()){
createFile(f);
}
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f, true)));
out.println(text);
out.close();
}catch (IOException e){
e.printStackTrace();
}
}
/**
* Creates a new file including all subfolders
*/
void createFile(File f){
File parentDir = f.getParentFile();
try{
parentDir.mkdirs();
f.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
}