Skip to content

Commit

Permalink
Merge pull request #1 from goeuropa/traccar-module
Browse files Browse the repository at this point in the history
Traccar module
  • Loading branch information
wkulesza authored Mar 5, 2024
2 parents 9f2f42e + a2d7d82 commit f3f8b7f
Show file tree
Hide file tree
Showing 9 changed files with 2,677 additions and 45 deletions.
88 changes: 44 additions & 44 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ name: Java CI with Maven

on:
push:
branches: [ "main", "refactor" ]
branches: [ "main", "refactoring" ]
pull_request:
branches: [ "main" ]

Expand All @@ -34,47 +34,47 @@ jobs:
run: mvn -B package --file pom.xml

# Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
- name: Update dependency graph
uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
# - name: Update dependency graph
# uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6

container-image:
if: github.repository_owner == 'opentransportro' && github.event_name == 'push' && (github.ref == 'refs/heads/main')
runs-on: ubuntu-latest
needs:
- build
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: temurin
cache: maven
- uses: actions/setup-node@v4
with:
node-version: 18
- name: Build container image with Jib, push to Dockerhub
env:
CONTAINER_REPO: docker.io/otrro/transitclock-server
CONTAINER_REGISTRY_USER: ${{secrets.DOCKER_USER}}
CONTAINER_REGISTRY_PASSWORD: ${{ secrets.DOCKER_AUTH }}
run: |
# we give the container two tags
# - "latest"
# - a string like "2.3_2022-12-12T21-38"
version_with_snapshot=`mvn -q help:evaluate -Dexpression=project.version -q -DforceStdout`
version=${version_with_snapshot/-SNAPSHOT/}
image_version=${version}
## if the Maven version contains SNAPSHOT, then add date to tag
if [[ $version_with_snapshot == *"SNAPSHOT"* ]]; then
image_date=`date +%Y-%m-%dT%H-%M`
image_version="${version}_${image_date}"
echo "Maven version ${version_with_snapshot} contains SNAPSHOT, adding date to container image tag"
fi
mvn install jib:build -Djib.to.tags=latest,$image_version
# container-image:
# if: github.repository_owner == 'opentransportro' && github.event_name == 'push' && (github.ref == 'refs/heads/main')
# runs-on: ubuntu-latest
# needs:
# - build
# steps:
# - uses: actions/checkout@v4
# with:
# fetch-depth: 0
# - name: Set up JDK 17
# uses: actions/setup-java@v4
# with:
# java-version: 17
# distribution: temurin
# cache: maven
# - uses: actions/setup-node@v4
# with:
# node-version: 18
# - name: Build container image with Jib, push to Dockerhub
# env:
# CONTAINER_REPO: docker.io/otrro/transitclock-server
# CONTAINER_REGISTRY_USER: ${{secrets.DOCKER_USER}}
# CONTAINER_REGISTRY_PASSWORD: ${{ secrets.DOCKER_AUTH }}
# run: |
# # we give the container two tags
# # - "latest"
# # - a string like "2.3_2022-12-12T21-38"
#
# version_with_snapshot=`mvn -q help:evaluate -Dexpression=project.version -q -DforceStdout`
# version=${version_with_snapshot/-SNAPSHOT/}
#
# image_version=${version}
#
# ## if the Maven version contains SNAPSHOT, then add date to tag
# if [[ $version_with_snapshot == *"SNAPSHOT"* ]]; then
# image_date=`date +%Y-%m-%dT%H-%M`
# image_version="${version}_${image_date}"
# echo "Maven version ${version_with_snapshot} contains SNAPSHOT, adding date to container image tag"
# fi
#
# mvn install jib:build -Djib.to.tags=latest,$image_version
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jakarta-xmlbind-annotations</artifactId>
</dependency>
<dependency>
<groupId>pl.goeuropa</groupId>
<artifactId>traccar-client</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>

<build>
Expand Down
25 changes: 25 additions & 0 deletions core/src/main/java/org/transitclock/config/data/TraccarConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* (C)2023 */
package org.transitclock.config.data;

import org.transitclock.config.StringConfigValue;


public class TraccarConfig {
/**
* Traccar properties for log in by "TraccarAVLModule"
*
* @return
*/
public static final StringConfigValue TRACCAREMAIL = new StringConfigValue("transitclock.avl.traccar.email", null,
"This is the username for the traccar server api.");

public static final StringConfigValue TRACCARPASSWORD = new StringConfigValue("transitclock.avl.traccar.password",
null, "This is the password for the traccar server api");

public static final StringConfigValue TRACCARBASEURL = new StringConfigValue("transitclock.avl.traccar.baseurl",
null, "This is the url for the traccar server api.");

public static final StringConfigValue TRACCARSOURCE = new StringConfigValue("transitclock.avl.traccar.source",
"TRACCAR", "This is the value recorded in the source for the AVL Report.");

}
134 changes: 134 additions & 0 deletions core/src/main/java/org/transitclock/core/avl/TraccarAVLModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* This file is part of thetransitclock.org
*
* thetransitclock.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License (GPL) as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* thetransitclock.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with thetransitclock.org . If not, see <http://www.gnu.org/licenses/>.
*/
package org.transitclock.core.avl;

import io.swagger.client.ApiClient;
import io.swagger.client.ApiException;
import io.swagger.client.api.DefaultApi;
import io.swagger.client.model.Device;
import io.swagger.client.model.Position;
import io.swagger.client.model.User;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.transitclock.domain.structs.AvlReport;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.transitclock.config.data.TraccarConfig.*;


/**
* @author Sean Óg Crudden This module integrates TheTransitClock with the API of a traccar
* server to get vehicle locations.
* <p>
* See http://www.traccar.org
* <p>
* It uses classes that where generated using the swagger file provided
* with traccar.
*/
@Slf4j
public class TraccarAVLModule extends PollUrlAvlModule {
public TraccarAVLModule(String agencyId) {
super(agencyId);

useCompression = false;
}


private DefaultApi initApiClient() throws RuntimeException {
DefaultApi api = new DefaultApi();
ApiClient client = new ApiClient();
client.setBasePath(TRACCARBASEURL.getValue());
client.setUsername(TRACCAREMAIL.getValue());
client.setPassword(TRACCARPASSWORD.getValue());
api.setApiClient(client);
return api;
}


private User initUser(DefaultApi api) throws RuntimeException {
User user;
try {
user = api
.sessionPost(TRACCAREMAIL.getValue(), TRACCARPASSWORD.getValue());
logger.info("Traccar login succeeded.");
return user;
} catch (ApiException e) {
logger.error(e.getMessage() + e.getCause());
}
throw new RuntimeException("Traccar login deny");
}

@NonNull
private final DefaultApi API = initApiClient();
@NonNull
private final User USER = initUser(API);

@Override
protected void getAndProcessData() throws Exception {

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

List<Device> devices = API.devicesGet(true, USER.getId(), null, null);

List<Position> results = API.positionsGet(null, null, null, null);
for (Position result : results) {
Device device = findDeviceById(devices, result.getDeviceId());

AvlReport avlReport;

// If have device details use name.
if (device != null && device.getUniqueId() != null && !device.getUniqueId().isEmpty()) {
//Traccar return speed in kt
avlReport = new AvlReport(device.getUniqueId(), device.getName(),
result.getDeviceTime().toDate().getTime(), result.getLatitude().doubleValue(),
result.getLongitude().doubleValue(), result.getSpeed().multiply(BigDecimal.valueOf(0.5144444)).floatValue(), result.getCourse().floatValue(), TRACCARSOURCE.toString());
} else {
avlReport = new AvlReport(result.getDeviceId().toString(),
result.getDeviceTime().toDate().getTime(), result.getLatitude().doubleValue(),
result.getLongitude().doubleValue(), result.getSpeed().multiply(BigDecimal.valueOf(0.5144444)).floatValue(), result.getCourse().floatValue(), TRACCARSOURCE.toString());
}
if (avlReport != null)
avlReportsReadIn.add(avlReport);
}
forwardAvlReports(avlReportsReadIn);
}

protected void forwardAvlReports(Collection<AvlReport> avlReportsReadIn) {
processAvlReports(avlReportsReadIn);
}

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

@Override
protected Collection<AvlReport> processData(InputStream in) throws Exception {
// Auto-generated method stub
return null;
}

}
25 changes: 25 additions & 0 deletions core/src/main/java/org/transitclock/domain/structs/AvlReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,31 @@ protected AvlReport() {
* if speed not available
* @param source Text describing the source of the report
*/

public AvlReport(String vehicleId, String vehicleName, long time, double lat, double lon, float speed, float heading, String source) {
// Store the values
this.vehicleId = vehicleId;
this.time = new Date(time);
this.location = new Location(lat, lon);
// DB requires null instead of NaN
this.speed = Float.isNaN(speed) ? null : speed;
this.heading = Float.isNaN(heading) ? null : heading;
this.source = sized(source);
this.assignmentId = null;
this.assignmentType = AssignmentType.UNSET;
this.leadVehicleId = null;
this.driverId = null;
this.licensePlate = null;
this.passengerCount = null;
this.passengerFullness = null;
this.field1Name = null;
this.field1Value = null;
this.vehicleName = vehicleName;

// Don't yet know when processed so set timeProcessed to null
this.timeProcessed = null;
}

public AvlReport(String vehicleId, long time, double lat, double lon, float speed, float heading, String source) {
// Store the values
this.vehicleId = vehicleId;
Expand Down
1 change: 1 addition & 0 deletions extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<version>${revision}</version>

<modules>
<module>traccar</module>
<module>api</module>
</modules>

Expand Down
Loading

0 comments on commit f3f8b7f

Please sign in to comment.