Skip to content

Commit

Permalink
Merge pull request #220 from TheTransitClock/tc_issue_204
Browse files Browse the repository at this point in the history
Tc issue 204
  • Loading branch information
scrudden committed Dec 9, 2020
2 parents 75fe387 + 1cf358f commit 9961123
Showing 1 changed file with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.transitclock.db.structs.AvlReport.AssignmentType;
import io.swagger.client.ApiClient;
import io.swagger.client.api.DefaultApi;
import io.swagger.client.model.Device;
import io.swagger.client.model.Position;
import io.swagger.client.model.User;

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

private User user = null;
private DefaultApi api = null;


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

Collection<AvlReport> avlReportsReadIn = new ArrayList<AvlReport>();

List<Device> devices = api.devicesGet(true, user.getId(), null, null);

if (api != null && user != null) {
List<Position> results = api.positionsGet(null, null, null, null);
for (Position result : results) {
logger.debug(result.toString());

AvlReport avlReport = new AvlReport(result.getDeviceId().toString(),
for (Position result : results) {
Device device=findDeviceById(devices, result.getDeviceId());

AvlReport avlReport = null;
// If have device details use name.
if(device!=null && device.getName()!=null && !device.getName().isEmpty())
{
avlReport = new AvlReport(device.getName(),
result.getDeviceTime().toDate().getTime(), result.getLatitude().doubleValue(),
result.getLongitude().doubleValue(), traccarSource.toString());
}
else
{
avlReport = new AvlReport(result.getDeviceId().toString(),
result.getDeviceTime().toDate().getTime(), result.getLatitude().doubleValue(),
result.getLongitude().doubleValue(), traccarSource.toString());

avlReportsReadIn.add(avlReport);
}

}
if(avlReport!=null)
avlReportsReadIn.add(avlReport);
}
forwardAvlReports(avlReportsReadIn);
}
}

private Device findDeviceById(List<Device> devices, Integer id)
{
for(Device device:devices)
{
if(device.getId()==id)
return device;
}
return null;
}

@Override
protected Collection<AvlReport> processData(InputStream in) throws Exception {
// TODO Auto-generated method stub
Expand Down

0 comments on commit 9961123

Please sign in to comment.