Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85cd47d

Browse files
committed
adding more result types
see #18
1 parent d2b4036 commit 85cd47d

File tree

12 files changed

+1075
-27
lines changed

12 files changed

+1075
-27
lines changed

src/de.taimos.gpsd4java/src/de/taimos/gpsd4java/Tester.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
*/
1616
package de.taimos.gpsd4java;
1717

18+
import de.taimos.gpsd4java.api.ObjectListener;
1819
import de.taimos.gpsd4java.internal.backend.GPSdEndpoint;
20+
import de.taimos.gpsd4java.types.DeviceObject;
21+
import de.taimos.gpsd4java.types.DevicesObject;
22+
import de.taimos.gpsd4java.types.PollObject;
23+
import de.taimos.gpsd4java.types.TPVObject;
1924

2025
/**
2126
* This class provides tests during the startup phase of GPSd4Java<br>
@@ -30,8 +35,43 @@ public class Tester {
3035
* @param args the args
3136
*/
3237
public static void main(String[] args) {
33-
GPSdEndpoint ep = new GPSdEndpoint("192.168.1.115", 2947);
34-
ep.start();
35-
ep.watch(true);
38+
try {
39+
GPSdEndpoint ep = new GPSdEndpoint("192.168.1.115", 2947);
40+
41+
ep.addListener(new ObjectListener() {
42+
43+
@Override
44+
public void handleTPV(TPVObject tpv) {
45+
System.out.println("Listener: " + tpv);
46+
}
47+
48+
@Override
49+
public void handleDevices(DevicesObject devices) {
50+
for (DeviceObject d : devices.getDevices()) {
51+
System.out.println(d);
52+
}
53+
}
54+
});
55+
56+
ep.start();
57+
58+
System.out.println(ep.version());
59+
60+
System.out.println(ep.watch(true, true));
61+
62+
System.out.println("Polling...");
63+
PollObject poll = ep.poll();
64+
System.out.println(poll);
65+
for (TPVObject tpv : poll.getFixes()) {
66+
System.out.println(tpv);
67+
}
68+
System.out.println("...polling done");
69+
70+
while (true) {
71+
//
72+
}
73+
} catch (Exception e) {
74+
e.printStackTrace();
75+
}
3676
}
3777
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2011 Thorsten Höger, Taimos GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package de.taimos.gpsd4java.api;
17+
18+
import de.taimos.gpsd4java.types.DeviceObject;
19+
import de.taimos.gpsd4java.types.DevicesObject;
20+
import de.taimos.gpsd4java.types.SKYObject;
21+
import de.taimos.gpsd4java.types.TPVObject;
22+
23+
/**
24+
* Listener to receive response objects
25+
*
26+
* created: 18.01.2011
27+
*/
28+
public interface IObjectListener {
29+
30+
/**
31+
* @param tpv the TPV object
32+
*/
33+
void handleTPV(TPVObject tpv);
34+
35+
/**
36+
* @param sky the SKY object
37+
*/
38+
void handleSKY(SKYObject sky);
39+
40+
/**
41+
* @param att the ATT object
42+
*/
43+
void handleATT(TPVObject att);
44+
45+
/**
46+
* @param devices the devices object
47+
*/
48+
void handleDevices(DevicesObject devices);
49+
50+
/**
51+
* @param device the device object
52+
*/
53+
void handleDevice(DeviceObject device);
54+
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2011 Thorsten Höger, Taimos GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package de.taimos.gpsd4java.api;
17+
18+
import de.taimos.gpsd4java.types.DeviceObject;
19+
import de.taimos.gpsd4java.types.DevicesObject;
20+
import de.taimos.gpsd4java.types.SKYObject;
21+
import de.taimos.gpsd4java.types.TPVObject;
22+
23+
/**
24+
* Adapter class for {@link IObjectListener}
25+
*
26+
* created: 18.01.2011
27+
*/
28+
public class ObjectListener implements IObjectListener {
29+
30+
@Override
31+
public void handleTPV(TPVObject tpv) {
32+
// implement in subclass if needed
33+
}
34+
35+
@Override
36+
public void handleSKY(SKYObject sky) {
37+
// implement in subclass if needed
38+
}
39+
40+
@Override
41+
public void handleATT(TPVObject att) {
42+
// implement in subclass if needed
43+
}
44+
45+
@Override
46+
public void handleDevices(DevicesObject devices) {
47+
// implement in subclass if needed
48+
}
49+
50+
@Override
51+
public void handleDevice(DeviceObject device) {
52+
// implement in subclass if needed
53+
}
54+
55+
}

src/de.taimos.gpsd4java/src/de/taimos/gpsd4java/internal/backend/GPSdEndpoint.java

Lines changed: 148 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,22 @@
2222
import java.io.OutputStreamWriter;
2323
import java.net.Socket;
2424
import java.net.UnknownHostException;
25+
import java.util.ArrayList;
26+
import java.util.List;
2527

28+
import org.json.JSONException;
29+
import org.json.JSONObject;
30+
31+
import de.taimos.gpsd4java.api.IObjectListener;
32+
import de.taimos.gpsd4java.types.DeviceObject;
33+
import de.taimos.gpsd4java.types.DevicesObject;
2634
import de.taimos.gpsd4java.types.IGPSObject;
2735
import de.taimos.gpsd4java.types.ParseException;
36+
import de.taimos.gpsd4java.types.PollObject;
37+
import de.taimos.gpsd4java.types.SKYObject;
38+
import de.taimos.gpsd4java.types.TPVObject;
39+
import de.taimos.gpsd4java.types.VersionObject;
40+
import de.taimos.gpsd4java.types.WatchObject;
2841

2942
/**
3043
* GPSd client endpoint
@@ -40,6 +53,12 @@ public class GPSdEndpoint {
4053

4154
private Thread listenThread;
4255

56+
private List<IObjectListener> listeners = new ArrayList<IObjectListener>();
57+
58+
private IGPSObject asnycResult = null;
59+
private Object asyncMutex = new Object();
60+
private Object asyncWaitMutex = new Object();
61+
4362

4463
/**
4564
* @param server the server name or IP
@@ -63,42 +82,159 @@ public GPSdEndpoint(String server, int port) {
6382
public void start() {
6483
this.listenThread = new SocketThread(this.in, this);
6584
this.listenThread.start();
85+
86+
try {
87+
Thread.sleep(500);
88+
} catch (InterruptedException e) {
89+
e.printStackTrace();
90+
}
91+
}
92+
93+
/**
94+
* send WATCH command
95+
*
96+
* @param enable enable/disable watch mode
97+
* @param dumpData enable/disable dumping of data
98+
* @return {@link WatchObject}
99+
* @throws IOException on IO error in socket
100+
*/
101+
public WatchObject watch(boolean enable, boolean dumpData) throws IOException {
102+
return this.watch(enable, dumpData, null);
66103
}
67104

68105
/**
69106
* send WATCH command
70107
*
71108
* @param enable enable/disable watch mode
109+
* @param dumpData enable/disable dumping of data
110+
* @param device If present, enable watching only of the specified device
111+
* rather than all devices
112+
* @return {@link WatchObject}
113+
* @throws IOException on IO error in socket
72114
*/
73-
public void watch(boolean enable) {
115+
public WatchObject watch(boolean enable, boolean dumpData, String device) throws IOException {
74116
try {
75-
this.out.write("?WATCH={\"enable\":true,\"json\":true}");
76-
this.out.flush();
77-
} catch (IOException e) {
117+
JSONObject watch = new JSONObject();
118+
watch.put("class", "WATCH");
119+
watch.put("enable", enable);
120+
watch.put("json", dumpData);
121+
if (device != null) {
122+
watch.put("device", device);
123+
}
124+
return this.syncCommand("?WATCH=" + watch.toString(), WatchObject.class);
125+
} catch (JSONException e) {
78126
e.printStackTrace();
79127
}
128+
return null;
80129
}
81130

82131
/**
83132
* Poll GPSd for Message
84133
*
85-
* @return {@link IGPSObject}
134+
* @return {@link PollObject}
135+
* @throws IOException on IO error in socket
136+
* @throws ParseException on illegal response
137+
*/
138+
public PollObject poll() throws IOException, ParseException {
139+
return this.syncCommand("?POLL;", PollObject.class);
140+
}
141+
142+
/**
143+
* Poll GPSd version
144+
*
145+
* @return {@link VersionObject}
86146
* @throws IOException on IO error in socket
87147
* @throws ParseException on illegal response
88148
*/
89-
public IGPSObject poll() throws IOException, ParseException {
90-
this.out.write("?POLL;\n");
91-
this.out.flush();
92-
return ResultParser.parse(this.in.readLine());
149+
public VersionObject version() throws IOException, ParseException {
150+
return this.syncCommand("?VERSION;", VersionObject.class);
93151
}
94152

95153
// TODO implement rest of commands
96154

97155
// ########################################################
98156

99-
void handle(IGPSObject object) {
100-
// TODO handle response
101-
System.out.println(object);
157+
/**
158+
* @param listener the listener to add
159+
*/
160+
public void addListener(IObjectListener listener) {
161+
this.listeners.add(listener);
162+
}
163+
164+
/**
165+
* @param listener the listener to remove
166+
*/
167+
public void removeListener(IObjectListener listener) {
168+
this.listeners.remove(listener);
102169
}
103170

171+
// ########################################################
172+
173+
private <T extends IGPSObject> T syncCommand(String command, Class<T> responseClass) throws IOException {
174+
synchronized (this.asyncMutex) {
175+
this.out.write(command + "\n");
176+
this.out.flush();
177+
178+
while (true) {
179+
// wait for awaited message
180+
IGPSObject result = this.waitForResult();
181+
if ((result == null) || result.getClass().equals(responseClass)) {
182+
return responseClass.cast(result);
183+
}
184+
}
185+
}
186+
}
187+
188+
// will be used later on
189+
@SuppressWarnings("unused")
190+
private void voidCommand(String command) throws IOException {
191+
synchronized (this.asyncMutex) {
192+
this.out.write(command + "\n");
193+
this.out.flush();
194+
}
195+
}
196+
197+
private IGPSObject waitForResult() {
198+
synchronized (this.asyncWaitMutex) {
199+
this.asnycResult = null;
200+
try {
201+
this.asyncWaitMutex.wait(1000);
202+
} catch (InterruptedException e) {
203+
e.printStackTrace();
204+
}
205+
if (this.asnycResult != null) {
206+
return this.asnycResult;
207+
}
208+
}
209+
return null;
210+
}
211+
212+
void handle(IGPSObject object) {
213+
if (object instanceof TPVObject) {
214+
for (IObjectListener l : this.listeners) {
215+
l.handleTPV((TPVObject) object);
216+
}
217+
} else if (object instanceof SKYObject) {
218+
for (IObjectListener l : this.listeners) {
219+
l.handleSKY((SKYObject) object);
220+
}
221+
// } else if (object instanceof ATTObject) {
222+
// for (IObjectListener l : this.listeners) {
223+
// l.handleATT((ATTObject) object);
224+
// }
225+
} else if (object instanceof DevicesObject) {
226+
for (IObjectListener l : this.listeners) {
227+
l.handleDevices((DevicesObject) object);
228+
}
229+
} else if (object instanceof DeviceObject) {
230+
for (IObjectListener l : this.listeners) {
231+
l.handleDevice((DeviceObject) object);
232+
}
233+
} else {
234+
synchronized (this.asyncWaitMutex) {
235+
this.asnycResult = object;
236+
this.asyncWaitMutex.notifyAll();
237+
}
238+
}
239+
}
104240
}

0 commit comments

Comments
 (0)