-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebsite.java
More file actions
199 lines (182 loc) · 8.5 KB
/
Website.java
File metadata and controls
199 lines (182 loc) · 8.5 KB
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
package uk.ac.ed.inf;
import com.google.gson.Gson;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static uk.ac.ed.inf.App.*;
/**
* Website class represents the website accessed by the drone system.
* It contains methods used to obtain and process data from this website.
*/
public class Website {
final private String name;
final private String port;
/**
* Constructor for the Website object
*
* @param name is the name of the machine
* @param port is the port of the machine
*/
public Website(String name, String port) {
this.name = name;
this.port = port;
}
/**
* This method returns the delivery cost of items chosen from the website.
*
* @param chosenItems is a String of variable length, each string being an item which has been chosen and will have a cost associated
* @return total cost of the delivery including the standard delivery charge however will return 0 if nothing is selected
*/
public int getDeliveryCost(String... chosenItems) {
String urlString = "http://" + this.name + ":" + this.port + "/menus/menus.json";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString)).build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
Shop[] shops = new Gson().fromJson(response.body(), Shop[].class);
int deliveryTotal = STANDARD_DELIVERY_CHARGE;
for (String singleItem : chosenItems) {
for (Shop singleShop : shops) {
ArrayList<Shop.Item> shopMenu = singleShop.getMenu();
for (Shop.Item item : shopMenu) {
String itemString = item.getItem();
if (itemString.equals(singleItem)) {
deliveryTotal = deliveryTotal + item.getPence();
}
}
}
}
return deliveryTotal;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return 0;
}
/**
* Given an item in a shop will return the what3words location of the shop from the website.
* @param orderItem : string name of item in shop
* @return What 3 words locaiton of shop
*/
public String getDeliveryLocation(String orderItem) {
String location = "";
String urlString = "http://" + this.name + ":" + this.port + "/menus/menus.json";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString)).build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
Shop[] shops = new Gson().fromJson(response.body(), Shop[].class);
for (Shop singleShop : shops) {
ArrayList<Shop.Item> shopMenu = singleShop.getMenu();
for (Shop.Item item : shopMenu) {
String itemString = item.getItem();
if (itemString.equals(orderItem)) {
location = singleShop.getLocation();
break;
}
}
}
} catch (IOException | InterruptedException ioException) {
ioException.printStackTrace();
}
return location;
}
/**
* Provides the list of points defining the polygon borders of the no-fly zone.
* @return Array list of polygons defined as an array list of geojson points
*/
public List<List<Point>> noFlyZone() {
String urlString = "http://" + this.name + ":" + this.port + "/buildings/no-fly-zones.geojson";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString)).build();
List<List<Point>> totalPoints = new ArrayList<>();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
FeatureCollection featCol = FeatureCollection.fromJson(response.body());
List<Feature> list = featCol.features();
assert list != null;
for (Feature feat : list) {
Polygon geo = (Polygon) feat.geometry();
assert geo != null;
List<List<Point>> allPoint = geo.coordinates();
List<Point> allPointNeat = allPoint.stream().flatMap(List::stream).collect(Collectors.toList());
totalPoints.add(allPointNeat);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return totalPoints;
}
/**
* Returns all landmarks used for traversing the area, including the ones defined within the website and the extra
* calculated by using the boundary of the confinement zone.
* @return list of landmarks defined as longlat locations
*/
public List<LongLat> getLandmarks() {
String urlString = "http://" + this.name + ":" + this.port + "/buildings/landmarks.geojson";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString)).build();
List<LongLat> totalPoints = new ArrayList<>();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
FeatureCollection featCol = FeatureCollection.fromJson(response.body());
List<Feature> list = featCol.features();
assert list != null;
for (Feature feat : list) {
Point geo = (Point) feat.geometry();
assert geo != null;
List<Double> allPoint = geo.coordinates();
LongLat longLat = new LongLat(allPoint.get(0), allPoint.get(1));
totalPoints.add(longLat);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
totalPoints.add(APPLETONTOWER);
ArrayList<LongLat> boundaryLandmarks = new ArrayList<>();
double longCent = (BOUNDARY_LONG1 + BOUNDARY_LONG2) / 2;
double latCent = (BOUNDARY_LAT1 + BOUNDARY_LAT2) / 2;
LongLat center = new LongLat(longCent, latCent);
LongLat midTop = new LongLat(longCent, (BOUNDARY_LAT2-((BOUNDARY_LAT2-BOUNDARY_LAT1)/100)));
LongLat midBottom = new LongLat(longCent, (BOUNDARY_LAT1-((BOUNDARY_LAT1-BOUNDARY_LAT2)/100)));
LongLat midLeft = new LongLat((BOUNDARY_LONG1-((BOUNDARY_LONG1-BOUNDARY_LONG2)/100)),latCent);
LongLat midRight = new LongLat((BOUNDARY_LONG2-((BOUNDARY_LONG2-BOUNDARY_LONG1)/100)),latCent);
boundaryLandmarks.add(midTop);
boundaryLandmarks.add(midBottom);
boundaryLandmarks.add(midLeft);
boundaryLandmarks.add(midRight);
boundaryLandmarks.add(center);
for (LongLat lngLat : boundaryLandmarks) {
if (lngLat.isConfined() && totalPoints.size() <TOTALLANDMARKS){
totalPoints.add(lngLat);
}
}
return totalPoints;
}
/**
* Given a what 3 words location will turn this into a usable lonlat location representation using the data from
* the website.
* @param what3words string what3words location found on website
* @return Corresponding longitude and latitude location representation in the form of class longlat
*/
public LongLat whatThreetoLongLat(String what3words) {
//check if words is three long
String[] splitWords = what3words.split("\\.");
String urlString = "http://" + this.name + ":" + this.port + "/words/" + splitWords[0] + "/" + splitWords[1] + "/" + splitWords[2] + "/details.json";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(urlString)).build();
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
What3Words w3w = new Gson().fromJson(response.body(), What3Words.class);
double lat = w3w.getCoordinates().getLat();
double lng = w3w.getCoordinates().getLng();
return new LongLat(lng, lat);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
System.err.println("what three words has no associated Longitude or Latitude");
return new LongLat(0, 0);
}
}