-
Notifications
You must be signed in to change notification settings - Fork 0
/
FifoAdapter.java
161 lines (131 loc) · 5.26 KB
/
FifoAdapter.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.niklashalle;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FifoAdapter {
private BufferedReader mBufferedReader;
private boolean connected = false;
private JsonData[] robots = new JsonData[10];
FifoAdapter() {
initialize();
if (connected) {
System.out.println("Named pipe open, ready for parsing incoming data");
} else {
System.err.println("Failed to open named pipe");
}
}
private void initialize() {
try {
System.out.println("Waiting for pipe...");
mBufferedReader = new BufferedReader(new FileReader("/tmp/fifoBlueJava"));
} catch (IOException e) {
return;
}
connected = true;
}
public void start() {
if (connected) {
(new Thread(() -> {
while (true) {
try {
parseJson(mBufferedReader.readLine());
} catch (IOException e) {
e.printStackTrace();
return;
}
}
})).start();
}
}
private void parseJson(String msg) {
//System.out.println(msg);
JSONObject jsonRoot = null;
JSONParser parser = new JSONParser();
try {
jsonRoot = (JSONObject) parser.parse(msg);
} catch (ParseException e) {
e.printStackTrace();
}
JsonData temp = new JsonData(true);
temp.header = (String) jsonRoot.get("header");
temp.version = (String) jsonRoot.get("version");
JSONObject teamRoot = (JSONObject) ((JSONObject) jsonRoot.get("data")).get("team");
temp.teamId = (Long) teamRoot.get("id");
temp.teamNumber = (Long) teamRoot.get("number");
JSONObject robotRoot = (JSONObject) ((JSONObject) jsonRoot.get("data")).get("robot");
temp.robotNumber = (Long) robotRoot.get("number");
JSONObject robotPositionRoot = (JSONObject) robotRoot.get("position");
temp.robotPositionX = (Double) robotPositionRoot.get("x");
temp.robotPositionY = (Double) robotPositionRoot.get("y");
temp.robotRotation = (Double) robotPositionRoot.get("t");
JSONObject robotVelocityRoot = (JSONObject) robotRoot.get("velocity");
temp.robotVelocityX = (Double) robotVelocityRoot.get("x");
temp.robotVelocityY = (Double) robotVelocityRoot.get("y");
temp.robotStatus = (Long) robotRoot.get("status");
temp.robotRole = (Long) robotRoot.get("role");
JSONObject ballRoot = (JSONObject) ((JSONObject) jsonRoot.get("data")).get("ball");
temp.ballStatus = (Long) ballRoot.get("status");
temp.ballAge = (Long) ballRoot.get("age");
// yes, switched, don't ask
temp.ballPositionX = -(Double) ((JSONObject) ballRoot.get("position")).get("y");
temp.ballPositionY = -(Double) ((JSONObject) ballRoot.get("position")).get("x");
temp.ballVelocityX = (Double) ((JSONObject) ballRoot.get("velocity")).get("x");
temp.ballVelocityY = (Double) ((JSONObject) ballRoot.get("velocity")).get("y");
temp.timestamp = (Long) jsonRoot.get("timestamp");
temp.ballAngle = (Double) ((JSONObject) jsonRoot.get("customData")).get("ballAngle");
temp.ballDistance = (Double) ((JSONObject) jsonRoot.get("customData")).get("ballDistance");
temp.macauStatus = (Boolean) ((JSONObject) jsonRoot.get("customData")).get("macauStatus");
temp.macauEnabled = (Boolean) ((JSONObject) jsonRoot.get("customData")).get("macauEnabled");
temp.lightbarrierStatus = (Boolean) ((JSONObject) jsonRoot.get("customData")).get("lightbarrierStatus");
temp.opponentGoal = (Long) ((JSONObject) jsonRoot.get("customData")).get("opponentGoal");
temp.cpuTemp = (String) ((JSONObject) jsonRoot.get("customData")).get("cpuTemp") + "°C";
robots[(int)temp.robotNumber] = temp;
}
public boolean isConnected() {
return connected;
}
public JsonData getJsonData(int id) {
return robots[id];
}
public class JsonData {
String header;
String version;
long teamId;
long teamNumber;
long robotNumber;
double robotPositionX;
double robotPositionY;
double robotRotation;
double robotVelocityX;
double robotVelocityY;
long robotStatus;
long robotRole;
long ballStatus;
long ballAge;
double ballPositionX;
double ballPositionY;
double ballVelocityX;
double ballVelocityY;
long timestamp;
// custom data
double ballAngle;
double ballDistance;
boolean macauStatus;
boolean macauEnabled;
boolean lightbarrierStatus;
long opponentGoal;
String cpuTemp;
// not part of the protocol
private boolean real = false;
JsonData() {}
JsonData(boolean real) {
this.real = real;
}
public boolean isReal() {
return real;
}
}
}