forked from badbadc0ffee/ogn-client-java
/
OgnDemoAircraftBeaconsClient2.java
99 lines (73 loc) · 3.12 KB
/
OgnDemoAircraftBeaconsClient2.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
/**
* Copyright (c) 2014 OGN, All Rights Reserved.
*/
package org.ogn.client.demo;
import static java.lang.System.out;
import java.util.Optional;
import org.ogn.client.AircraftBeaconListener;
import org.ogn.client.OgnClient;
import org.ogn.client.OgnClientFactory;
import org.ogn.commons.beacon.AircraftBeacon;
import org.ogn.commons.beacon.AircraftDescriptor;
import org.ogn.commons.beacon.descriptor.AircraftDescriptorProvider;
import org.ogn.commons.beacon.impl.AircraftDescriptorImpl;
import org.ogn.commons.db.FileDbDescriptorProvider;
import org.ogn.commons.db.ogn.OgnDb;
import org.ogn.commons.igc.IgcLogger;
import org.ogn.commons.utils.JsonUtils;
/**
* A small demo program demonstrating the usage of the ogn-client with aircraft descriptor providers.
*
* @author wbuczak
*/
public class OgnDemoAircraftBeaconsClient2 {
static IgcLogger igcLogger = new IgcLogger();
// enable if you want to log to IGC files
static boolean logIGC = false;
static class AcListener implements AircraftBeaconListener {
@Override
public void onUpdate(AircraftBeacon beacon, Optional<AircraftDescriptor> descriptor) {
out.println("*********************************************");
// print the beacon
out.println(JsonUtils.toJson(beacon));
// if the aircraft has been recognized print its descriptor too
if (descriptor.isPresent()) {
out.println(JsonUtils.toJson(descriptor.get()));
}
if (logIGC)
igcLogger.log(beacon, descriptor);
out.println("*********************************************");
}
}
/**
* A custom descriptor provider. For the use of this demo this custom descriptor provider always returns the same
* descriptor, no matter what address is passed to it
*
* @author wbuczak
*/
static class MyCustomAircraftDescriptorProvider implements AircraftDescriptorProvider {
@Override
public Optional<AircraftDescriptor> findDescriptor(String address) {
// return always the same descriptor (just for this demo)
return Optional
.of((AircraftDescriptor) new AircraftDescriptorImpl("SP-NZA", "ZA", "Cessna 172S", true, true));
}
}
public static void main(String[] args) throws Exception {
// create an instance of OGN descriptor provider
final AircraftDescriptorProvider adp1 = new FileDbDescriptorProvider<OgnDb>(OgnDb.class, 10);
// create an instance of "custom" descriptor provider
final AircraftDescriptorProvider adp2 = new MyCustomAircraftDescriptorProvider();
// NOTE: the order matters. The OGN client will try to query for the
// aircraft information the first provider in the list. Only if no match is found it will continue with the
// second provider etc..
// Create ogn client and give it the previously created descriptor providers
final OgnClient client = OgnClientFactory.createClient(new AircraftDescriptorProvider[]{adp1, adp2});
System.out.println("connecting...");
// connect with no filter
client.connect();
// client.connect("r/+49.98284/+20.09165/100");
client.subscribeToAircraftBeacons(new AcListener());
Thread.sleep(Long.MAX_VALUE);
}
}