-
Notifications
You must be signed in to change notification settings - Fork 1
/
WifiConnection.java
196 lines (162 loc) · 4.7 KB
/
WifiConnection.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
/*
* @author Sean Lawlor
* @date November 3, 2011
* @class ECSE 211 - Design Principle and Methods
*
* Modified by F.P. Ferrie
* February 28, 2014
* Changed parameters for W2014 competition
*
* Modified by Francois OD
* November 11, 2015
* Ported to EV3 and wifi (from NXT and bluetooth)
* Changed parameters for F2015 competition
*
* Modified by Michael Smith
* November 1, 2016
* Removed LCD printing, added optional debug print statements
*/
package trotty02;
import java.io.*;
import java.net.Socket;
import java.util.HashMap;
/*
* This class opens a wifi connection, waits for the data
* and then allows access to the data after closing the wifi socket.
*
* It should be used by calling the constructor which will automatically wait for
* data without any further user command
*
* Then, once completed, it will allow access to an instance of the Transmission
* class which has access to all of the data needed
*/
public class WifiConnection {
/**
*
*/
public HashMap<String, Integer> StartData;
public static int role;
public static int corner;
public static int xHalf;
public static int yHalf;
public static int LRZy;
public static int UGZy;
public static int LRZx;
public static int UGZx;
public static int LGZy;
public static int LGZx;
public static int URZy;
public static int URZx;
public static int TEAM_NUMBER = 2;
/**
*
* @param serverIP receives address to connect to server using
* @param teamNumber which bot we are
* @throws IOException to account for errors
*/
public WifiConnection(String serverIP, int teamNumber) throws IOException {
this(serverIP, teamNumber, true);
}
/**
*
* @param serverIP receives address to connect to server using
* @param teamNumber which bot we are
* @param debugPrint boolean that can turn print statements on and off
* @throws IOException in case of errors
*/
public WifiConnection(String serverIP, int teamNumber, boolean debugPrint) throws IOException {
// Open connection to the server and data streams
int port = 2000 + teamNumber; // semi-abritrary port number"
Socket socketClient = new Socket(serverIP, port);
DataOutputStream dos = new DataOutputStream(socketClient.getOutputStream());
DataInputStream dis = new DataInputStream(socketClient.getInputStream());
if (debugPrint) {
System.out.println("Connected\nWaiting for data");
}
// Wait for the server transmission to arrive
while (dis.available() <= 0)
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
// Parse transmission
this.StartData = ParseTransmission.parseData(dis);
if (debugPrint) {
//System.out.println("Data received");
}
role = 1;
if((StartData.get("BTN")) == TEAM_NUMBER)
role = 1; //builder
if((StartData.get("CTN")) == TEAM_NUMBER)
role = 2; //collector
corner = 1;
if(role == 1)
corner = ((int)(StartData.get("BSC")));
if(role == 2)
corner = ((int)(StartData.get("CSC")));
if (role == 1)
xHalf = ((int)(((StartData.get("LGZx") + ((StartData.get("UGZx")-StartData.get("LGZx"))/2))/30.48)/6));
else
xHalf = ((int) (((StartData.get("LRZx") + ((StartData.get("URZx")-StartData.get("LRZx"))/2))/30.48)/6));
if (role == 1)
yHalf = ((int) (((StartData.get("LGZy") + ((StartData.get("UGZy")-StartData.get("LGZy"))/2))/30.48)/6));
else
yHalf = ((int) (((StartData.get("LRZy") + ((StartData.get("URZy")-StartData.get("LRZy"))/2))/30.48)/6));
LRZy = StartData.get("LRZy");
UGZy = StartData.get("UGZy");
LRZx = StartData.get("LRZx");
UGZx = StartData.get("UGZx");
LGZy = StartData.get("LGZy");
LGZx = StartData.get("LGZx");
URZy = StartData.get("URZy");
URZx = StartData.get("URZx");
// End the wifi connection
dis.close();
dos.close();
socketClient.close();
}
/**
* gets information received from the wifi
* @return information received over wifi as a hashmap so we can access it
*/
HashMap<String, Integer> getStartData(){
System.out.println(this.StartData);
return this.StartData;
}
public int getRole(){
return this.role;
}
public int getCorner(){
return this.corner;
}
public int getxHalf(){
return this.xHalf;
}
public int getyHalf(){
return this.yHalf;
}
public int getLRZy(){
return this.LRZy;
}
public int getUGZy(){
return this.UGZx;
}
public int getLRZx(){
return this.LRZx;
}
public int getUGZx(){
return this.UGZx;
}
public int getLGZy(){
return this.LGZy;
}
public int getLGZx(){
return this.LGZx;
}
public int getURZy(){
return this.URZy;
}
public int getURZx(){
return this.URZx;
}
}