Skip to content

Commit 9961123

Browse files
authored
Merge pull request #220 from TheTransitClock/tc_issue_204
Tc issue 204
2 parents 75fe387 + 1cf358f commit 9961123

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

transitclock/src/main/java/org/transitclock/custom/traccar/TraccarAVLModule.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.transitclock.db.structs.AvlReport.AssignmentType;
3131
import io.swagger.client.ApiClient;
3232
import io.swagger.client.api.DefaultApi;
33+
import io.swagger.client.model.Device;
3334
import io.swagger.client.model.Position;
3435
import io.swagger.client.model.User;
3536

@@ -47,6 +48,7 @@ public class TraccarAVLModule extends PollUrlAvlModule {
4748

4849
private User user = null;
4950
private DefaultApi api = null;
51+
5052

5153
private static StringConfigValue traccarEmail = new StringConfigValue("transitclock.avl.traccar.email", "admin",
5254
"This is the username for the traccar server api.");
@@ -71,6 +73,7 @@ public TraccarAVLModule(String agencyId) throws Throwable {
7173
client.setPassword(traccarPassword.getValue());
7274
api.setApiClient(client);
7375
user = api.sessionPost(traccarEmail.getValue(), traccarPassword.getValue());
76+
user.getId();
7477
if (user != null)
7578
logger.debug("Traccar login succeeded.");
7679
}
@@ -79,22 +82,44 @@ public TraccarAVLModule(String agencyId) throws Throwable {
7982
protected void getAndProcessData() throws Exception {
8083

8184
Collection<AvlReport> avlReportsReadIn = new ArrayList<AvlReport>();
85+
86+
List<Device> devices = api.devicesGet(true, user.getId(), null, null);
87+
8288
if (api != null && user != null) {
8389
List<Position> results = api.positionsGet(null, null, null, null);
84-
for (Position result : results) {
85-
logger.debug(result.toString());
86-
87-
AvlReport avlReport = new AvlReport(result.getDeviceId().toString(),
90+
for (Position result : results) {
91+
Device device=findDeviceById(devices, result.getDeviceId());
92+
93+
AvlReport avlReport = null;
94+
// If have device details use name.
95+
if(device!=null && device.getName()!=null && !device.getName().isEmpty())
96+
{
97+
avlReport = new AvlReport(device.getName(),
98+
result.getDeviceTime().toDate().getTime(), result.getLatitude().doubleValue(),
99+
result.getLongitude().doubleValue(), traccarSource.toString());
100+
}
101+
else
102+
{
103+
avlReport = new AvlReport(result.getDeviceId().toString(),
88104
result.getDeviceTime().toDate().getTime(), result.getLatitude().doubleValue(),
89105
result.getLongitude().doubleValue(), traccarSource.toString());
90-
91-
avlReportsReadIn.add(avlReport);
92-
}
93-
106+
}
107+
if(avlReport!=null)
108+
avlReportsReadIn.add(avlReport);
109+
}
94110
forwardAvlReports(avlReportsReadIn);
95111
}
96112
}
97-
113+
private Device findDeviceById(List<Device> devices, Integer id)
114+
{
115+
for(Device device:devices)
116+
{
117+
if(device.getId()==id)
118+
return device;
119+
}
120+
return null;
121+
}
122+
98123
@Override
99124
protected Collection<AvlReport> processData(InputStream in) throws Exception {
100125
// TODO Auto-generated method stub

0 commit comments

Comments
 (0)