Skip to content

Commit 2d6653e

Browse files
authored
Merge pull request #9 from gmalmiski/add_planned_location
Add addExtraInfo method to populate planned location in task outputs
2 parents baa7d8c + 0536b65 commit 2d6653e

10 files changed

Lines changed: 207 additions & 6 deletions

File tree

cloud-functions/src/main/java/com/google/fleetevents/FleetEventCreator.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import com.google.fleetevents.common.database.FirestoreDatabaseClient;
2323
import com.google.fleetevents.common.models.FleetEvent;
2424
import com.google.fleetevents.common.models.Pair;
25+
import com.google.fleetevents.common.util.FleetEngineClient;
26+
import com.google.fleetevents.lmfs.models.DeliveryTaskFleetEvent;
27+
import com.google.fleetevents.lmfs.models.LatLng;
2528
import com.google.fleetevents.lmfs.models.outputs.OutputEvent;
2629
import com.google.fleetevents.lmfs.transactions.BatchCreateDeliveryTasksTransaction;
2730
import com.google.fleetevents.lmfs.transactions.CreateDeliveryTaskTransaction;
@@ -30,6 +33,7 @@
3033
import com.google.fleetevents.lmfs.transactions.UpdateDeliveryVehicleTransaction;
3134
import com.google.logging.v2.LogEntry;
3235
import com.google.protobuf.InvalidProtocolBufferException;
36+
import google.maps.fleetengine.delivery.v1.Task;
3337
import java.util.ArrayList;
3438
import java.util.List;
3539
import java.util.concurrent.ExecutionException;
@@ -156,5 +160,33 @@ public List<OutputEvent> processCloudLog(
156160
return outputEventsBuilder.build();
157161
}
158162

163+
// Enrich output events with information retrieved from the Fleet Engine service
164+
public void addExtraInfo(List<OutputEvent> outputs) {
165+
for (OutputEvent output : outputs) {
166+
if (output.getFleetEvent() == null) {
167+
continue;
168+
}
169+
if (output.getFleetEvent().getEventType() == FleetEvent.Type.DELIVERY_TASK_FLEET_EVENT) {
170+
DeliveryTaskFleetEvent taskFleetEvent = (DeliveryTaskFleetEvent) output.getFleetEvent();
171+
Task task =
172+
getFleetEngineClient().getTask(taskFleetEvent.newDeliveryTask().getDeliveryTaskId());
173+
DeliveryTaskFleetEvent enrichedTaskFleetEvent =
174+
taskFleetEvent.toBuilder()
175+
.setPlannedLocation(
176+
new LatLng.Builder()
177+
.setLatitude(task.getPlannedLocation().getPoint().getLatitude())
178+
.setLongitude(task.getPlannedLocation().getPoint().getLongitude())
179+
.build())
180+
.build();
181+
output.setFleetEvent(enrichedTaskFleetEvent);
182+
} else if (output.getFleetEvent().getEventType()
183+
== FleetEvent.Type.DELIVERY_VEHICLE_FLEET_EVENT) {
184+
// not implemented
185+
}
186+
}
187+
}
188+
159189
public abstract FirestoreDatabaseClient getDatabase();
190+
191+
public abstract FleetEngineClient getFleetEngineClient();
160192
}

cloud-functions/src/main/java/com/google/fleetevents/lmfs/DefaultFleetEventCreator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,31 @@
1616

1717
package com.google.fleetevents.lmfs;
1818

19+
import com.google.fleetengine.auth.token.factory.signer.SignerInitializationException;
1920
import com.google.fleetevents.FleetEventCreator;
2021
import com.google.fleetevents.common.database.FirestoreDatabaseClient;
22+
import com.google.fleetevents.common.util.FleetEngineClient;
2123
import java.io.IOException;
2224

2325
/** Default implementation of the fleet event creator class. Modify for custom logic. */
2426
public class DefaultFleetEventCreator extends FleetEventCreator {
2527

2628
private static FirestoreDatabaseClient db;
29+
private static FleetEngineClient fleetEngineClient;
2730

28-
public DefaultFleetEventCreator() throws IOException {
31+
public DefaultFleetEventCreator() throws IOException, SignerInitializationException {
2932
super();
3033
db = new FirestoreDatabaseClient();
34+
fleetEngineClient = new FleetEngineClient();
3135
}
3236

3337
@Override
3438
public FirestoreDatabaseClient getDatabase() {
3539
return db;
3640
}
41+
42+
@Override
43+
public FleetEngineClient getFleetEngineClient() {
44+
return fleetEngineClient;
45+
}
3746
}

cloud-functions/src/main/java/com/google/fleetevents/lmfs/DefaultFleetEventsFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.fleetevents.lmfs;
1818

19+
import com.google.fleetengine.auth.token.factory.signer.SignerInitializationException;
1920
import com.google.fleetevents.FleetEventsFunction;
2021
import com.google.fleetevents.lmfs.handlers.DistanceRemainingHandler;
2122
import com.google.fleetevents.lmfs.handlers.EtaChangeHandler;
@@ -30,7 +31,7 @@
3031
*/
3132
public class DefaultFleetEventsFunction extends FleetEventsFunction {
3233

33-
public DefaultFleetEventsFunction() throws IOException {
34+
public DefaultFleetEventsFunction() throws IOException, SignerInitializationException {
3435
super(new DefaultFleetEventCreator());
3536
registerFleetEventHandler(new TaskOutcomeHandler());
3637
registerFleetEventHandler(new TimeRemainingHandler());

cloud-functions/src/main/java/com/google/fleetevents/lmfs/models/DeliveryTaskFleetEvent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public FleetEvent.Type getEventType() {
6262

6363
public abstract boolean taskMovedFromCurrentToPlanned();
6464

65+
@Nullable
66+
public abstract LatLng plannedLocation();
67+
6568
@AutoValue.Builder
6669
public abstract static class Builder {
6770

@@ -81,6 +84,8 @@ public abstract static class Builder {
8184

8285
public abstract Builder setTaskMovedFromCurrentToPlanned(boolean taskMovedFromCurrentToPlanned);
8386

87+
public abstract Builder setPlannedLocation(LatLng plannedLocation);
88+
8489
public abstract DeliveryTaskFleetEvent build();
8590
}
8691
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.google.fleetevents.lmfs.models;
2+
3+
import java.util.Objects;
4+
5+
public class LatLng {
6+
private Double latitude;
7+
8+
private Double longitude;
9+
10+
public Double getLatitude() {
11+
return latitude;
12+
}
13+
14+
public void setLatitude(Double latitude) {
15+
this.latitude = latitude;
16+
}
17+
18+
public Double getLongitude() {
19+
return longitude;
20+
}
21+
22+
public void setLongitude(Double longitude) {
23+
this.longitude = longitude;
24+
}
25+
26+
@Override
27+
public boolean equals(Object object) {
28+
if (object instanceof LatLng that) {
29+
return Objects.equals(that.latitude, this.latitude)
30+
&& Objects.equals(that.longitude, this.longitude);
31+
}
32+
return false;
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return "LatLng{" + "latitute=" + latitude + ", longitude=" + longitude + '}';
38+
}
39+
40+
public static final class Builder {
41+
private Double latitude;
42+
private Double longitude;
43+
44+
public Builder setLatitude(Double latitude) {
45+
this.latitude = latitude;
46+
return this;
47+
}
48+
49+
public Builder setLongitude(Double longitude) {
50+
this.longitude = longitude;
51+
return this;
52+
}
53+
54+
public LatLng build() {
55+
LatLng latLng = new LatLng();
56+
latLng.setLatitude(latitude);
57+
latLng.setLongitude(longitude);
58+
return latLng;
59+
}
60+
}
61+
}

cloud-functions/src/main/java/com/google/fleetevents/lmfs/models/TaskInfo.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class TaskInfo {
2424
// duration in milliseconds.
2525
private Long taskDuration;
2626
private String taskId;
27+
private LatLng plannedlocation;
2728

2829
public Long getTaskDuration() {
2930
return taskDuration;
@@ -41,25 +42,44 @@ public void setTaskId(String taskId) {
4142
this.taskId = taskId;
4243
}
4344

45+
public LatLng getPlannedlocation() {
46+
return plannedlocation;
47+
}
48+
49+
public void setPlannedlocation(LatLng plannedlocation) {
50+
this.plannedlocation = plannedlocation;
51+
}
52+
4453
@Override
4554
public boolean equals(Object object) {
4655
if (object instanceof TaskInfo that) {
4756
return Objects.equals(that.taskId, this.taskId)
48-
&& Objects.equals(that.taskDuration, this.taskDuration);
57+
&& Objects.equals(that.taskDuration, this.taskDuration)
58+
&& Objects.equals(that.plannedlocation, this.plannedlocation);
4959
}
5060
return false;
5161
}
5262

5363
@Override
5464
public String toString() {
55-
return "TaskInfo{" + "taskId=" + taskId + ", taskDuration=" + taskDuration + "ms}";
65+
return "TaskInfo{"
66+
+ "taskDuration="
67+
+ taskDuration
68+
+ ", taskId='"
69+
+ taskId
70+
+ '\''
71+
+ ", plannedlocation="
72+
+ plannedlocation
73+
+ '}';
5674
}
5775

5876
public static final class Builder {
5977

6078
private Long taskDuration;
6179
private String taskId;
6280

81+
private LatLng plannedlocation;
82+
6383
public Builder setTaskDuration(Long taskDuration) {
6484
this.taskDuration = taskDuration;
6585
return this;
@@ -70,10 +90,16 @@ public Builder setTaskId(String taskId) {
7090
return this;
7191
}
7292

93+
public Builder setPlannedLocation(LatLng plannedLocation) {
94+
this.plannedlocation = plannedLocation;
95+
return this;
96+
}
97+
7398
public TaskInfo build() {
7499
TaskInfo taskInfo = new TaskInfo();
75100
taskInfo.setTaskDuration(taskDuration);
76101
taskInfo.setTaskId(taskId);
102+
taskInfo.setPlannedlocation(plannedlocation);
77103
return taskInfo;
78104
}
79105
}

cloud-functions/src/main/java/com/google/fleetevents/odrd/DefaultFleetEventCreator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,31 @@
1616

1717
package com.google.fleetevents.odrd;
1818

19+
import com.google.fleetengine.auth.token.factory.signer.SignerInitializationException;
1920
import com.google.fleetevents.FleetEventCreator;
2021
import com.google.fleetevents.common.database.FirestoreDatabaseClient;
22+
import com.google.fleetevents.common.util.FleetEngineClient;
2123
import java.io.IOException;
2224

2325
/** Default implementation of the fleet event creator class. Modify for custom logic. */
2426
public class DefaultFleetEventCreator extends FleetEventCreator {
2527

2628
private static FirestoreDatabaseClient db;
29+
private static FleetEngineClient fleetEngineClient;
2730

28-
public DefaultFleetEventCreator() throws IOException {
31+
public DefaultFleetEventCreator() throws IOException, SignerInitializationException {
2932
super();
3033
db = new FirestoreDatabaseClient();
34+
fleetEngineClient = new FleetEngineClient();
3135
}
3236

3337
@Override
3438
public FirestoreDatabaseClient getDatabase() {
3539
return db;
3640
}
41+
42+
@Override
43+
public FleetEngineClient getFleetEngineClient() {
44+
return fleetEngineClient;
45+
}
3746
}

cloud-functions/src/main/java/com/google/fleetevents/odrd/DefaultFleetEventsFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.fleetevents.odrd;
1818

19+
import com.google.fleetengine.auth.token.factory.signer.SignerInitializationException;
1920
import com.google.fleetevents.FleetEventsFunction;
2021
import java.io.IOException;
2122

@@ -25,7 +26,7 @@
2526
*/
2627
public class DefaultFleetEventsFunction extends FleetEventsFunction {
2728

28-
public DefaultFleetEventsFunction() throws IOException {
29+
public DefaultFleetEventsFunction() throws IOException, SignerInitializationException {
2930
super(new DefaultFleetEventCreator());
3031
}
3132
}

cloud-functions/src/test/java/com/google/fleetevents/FleetEventCreatorTests.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.cloud.firestore.Transaction.Function;
2525
import com.google.common.collect.ImmutableList;
2626
import com.google.fleetevents.common.database.FirestoreDatabaseClient;
27+
import com.google.fleetevents.common.util.FleetEngineClient;
2728
import com.google.fleetevents.helpers.FleetEventsTestHelper;
2829
import com.google.fleetevents.lmfs.models.DeliveryTaskData;
2930
import com.google.fleetevents.lmfs.models.DeliveryTaskFleetEvent;
@@ -32,7 +33,11 @@
3233
import com.google.fleetevents.lmfs.models.outputs.OutputEvent;
3334
import com.google.fleetevents.mocks.MockFleetEventCreator;
3435
import com.google.logging.v2.LogEntry;
36+
import com.google.type.LatLng;
37+
import google.maps.fleetengine.delivery.v1.LocationInfo;
38+
import google.maps.fleetengine.delivery.v1.Task;
3539
import java.io.IOException;
40+
import java.util.Arrays;
3641
import java.util.Collections;
3742
import java.util.List;
3843
import java.util.concurrent.ExecutionException;
@@ -227,4 +232,46 @@ public void processCloudLogEntry_returnsEmptyForNonFleetLogs()
227232
spyFleetEventCreator.processCloudLog(logEntry, ImmutableList.of());
228233
assertEquals(Collections.emptyList(), outputEvents);
229234
}
235+
236+
@Test
237+
public void addExtraInfo_addPlannedLocationToTask() {
238+
FleetEventCreator spyFleetEventCreator = Mockito.spy(new MockFleetEventCreator());
239+
FleetEngineClient mockFleetEngineClient = spyFleetEventCreator.getFleetEngineClient();
240+
241+
Task task =
242+
Task.newBuilder()
243+
.setPlannedLocation(
244+
LocationInfo.newBuilder()
245+
.setPoint(LatLng.newBuilder().setLatitude(111).setLongitude(222)))
246+
.build();
247+
doReturn(task).when(mockFleetEngineClient).getTask(any(String.class));
248+
249+
DeliveryTaskData taskData =
250+
DeliveryTaskData.builder()
251+
.setDeliveryVehicleId("testDeliveryVehicleId1")
252+
.setDeliveryTaskId("testDeliveryTaskId1")
253+
.setName("providers/test-123/deliveryVehicles/testDeliveryVehicleId1")
254+
.build();
255+
DeliveryTaskFleetEvent taskFleetEvent =
256+
DeliveryTaskFleetEvent.builder()
257+
.setDeliveryTaskId("testDeliveryTaskId1")
258+
.setNewDeliveryTask(taskData)
259+
.build();
260+
OutputEvent outputEvent = new OutputEvent();
261+
outputEvent.setFleetEvent(taskFleetEvent);
262+
List<OutputEvent> outputEvents = Arrays.asList(outputEvent);
263+
spyFleetEventCreator.addExtraInfo(outputEvents);
264+
assertEquals(outputEvents.size(), 1);
265+
OutputEvent enrichedOutputEvent = outputEvents.get(0);
266+
DeliveryTaskFleetEvent deliveryTaskFleetEvent =
267+
(DeliveryTaskFleetEvent) enrichedOutputEvent.getFleetEvent();
268+
assertEquals(
269+
deliveryTaskFleetEvent.plannedLocation().getLatitude().doubleValue(),
270+
task.getPlannedLocation().getPoint().getLatitude(),
271+
0);
272+
assertEquals(
273+
deliveryTaskFleetEvent.plannedLocation().getLongitude().doubleValue(),
274+
task.getPlannedLocation().getPoint().getLongitude(),
275+
0);
276+
}
230277
}

0 commit comments

Comments
 (0)