-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDebugView.java
223 lines (181 loc) · 5.59 KB
/
DebugView.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
public class DebugView {
private JFrame frame;
private JPanel panel = new JPanel();
private Border border = BorderFactory.createLineBorder(Color.BLACK, 3);
//list to dynamically store the current the labels containing the information of nodes
private List<JLabel> labels = new ArrayList<JLabel>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DebugView window = new DebugView();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public DebugView() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//Run background processes
runRos();
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
JScrollPane scrollPane = new JScrollPane();
frame.getContentPane().add(scrollPane);
scrollPane.setViewportView(panel);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
}
//Update the GUI using updated hashmap
public void updateLabel(String key, String[] statuses){
//Creating the status display using the input info
String info= "<HTML>" + key;
for (int i=0; i<3; i++){
info = info +"<br>" + statuses[i];
}
info = info + "<br><HTML>";
boolean exists = false;
//Check to see if a label exists for the input info
for (JLabel label: labels){
if(label.getText().subSequence(0,13).equals(info.subSequence(0, 13))){
//Replace the text with new info
label.setText(info);
exists = true;
}
}
//if it doesnt exist, add a label for it
if (!exists){
JLabel nodeStatus = new JLabel(info);
nodeStatus.setBorder(border);
panel.add(nodeStatus);
panel.updateUI();
labels.add(nodeStatus);
}
}
//Create background thread to run the appropriate ros and updating processes
public void runRos(){
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>(){
private HashMap<String, String[]> robotStatus = new HashMap<String, String[]>();
@Override
protected Void doInBackground() throws Exception {
/**Testing dynamic changing
*
for (int i = 0; i<10; i++){
String[] test = new String[]{"X", "Y", "Status" + i};
String name = "Robot_" + i;
robotStatus.put(name, test);
updateGUI(robotStatus);
Thread.sleep(1000);
}
for (int i = 0; i < 10; i++){
robotStatus.get("Robot_9")[2] = "Status" + i;
updateGUI(robotStatus);
Thread.sleep(1000);
}
*
**/
//TRY WHEN GUI WORKS
try {
BufferedReader br;
//Running run.bash process to create roscore, and stage with moving nodes
//This process will also be responsible reading all published messages from topic rosout
Process p = new ProcessBuilder("/bin/bash","run.bash").start();
while(true){
try{
//Reading all the published messages
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
//Sending update information to update GUI
while(true){
//Spring.split instead of substring
if((line = br.readLine()) != null && line.contains("name:")){
updateData(line.split("/")[1], br.readLine());
}
}
} catch(Exception e){
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
//Updating the hashmap with the input data
public void updateData(String name, String msg){
//Searching to see whether robot exists or not.
if (robotStatus.containsKey(name)){
//If exists edit field appropriately.
String[] statuses =robotStatus.get(name);
addStatuses(name, msg, statuses);
}
//If doesnt exist, edit information accordingly
else{
String[] statuses= new String[]{"x: ","y: ","Status: "};
addStatuses(name, msg, statuses);
}
}
//Changing statuses of existing or new Robots
public void addStatuses(String name, String msg, String[] statuses){
String[] msgSplit = msg.split("/");
if (msgSplit[1].equals("position")){
msgSplit[3] = msgSplit[3].substring(0,5);
if (msgSplit[2].equals("y")){
if (statuses[1] != msg){
statuses[1] = "y: " + msgSplit[3];
robotStatus.put(name, statuses);
updateLabel(name, statuses);
}
}
else if (msgSplit[2].equals("x")){
if (statuses[0] != msg){
statuses[0] = "x: " + msgSplit[3];
robotStatus.put(name, statuses);
updateLabel(name, statuses);
}
}
}
else if (msgSplit[1].equals("status")){
statuses[2] ="Status: " + msgSplit[2];
robotStatus.put(name, statuses);
updateLabel(name, statuses);
}
}
protected void done(){};
};
worker.execute();
}
}