Skip to content

Commit

Permalink
Merge branch 'opentransportro:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
wkulesza authored May 6, 2024
2 parents 60d4d65 + 9f2d710 commit f5595c7
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 186 deletions.
20 changes: 10 additions & 10 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", "refactoring" ]
branches: [ "main", "refactor" ]
pull_request:
branches: [ "main" ]

Expand All @@ -34,11 +34,11 @@ 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 == 'goeuropa' && github.event_name == 'push' && (github.ref == 'refs/heads/main')
if: github.repository_owner == 'opentransportro' && github.event_name == 'push' && (github.ref == 'refs/heads/main')
runs-on: ubuntu-latest
needs:
- build
Expand All @@ -57,24 +57,24 @@ jobs:
node-version: 18
- name: Build container image with Jib, push to Dockerhub
env:
CONTAINER_REPO: docker.io/wkulesza/transitclock-server
CONTAINER_REPO: docker.io/otrro/transitclock-server
CONTAINER_REGISTRY_USER: ${{secrets.DOCKER_USER}}
CONTAINER_REGISTRY_PASSWORD: ${{secrets.DOCKER_AUTH}}
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
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ public Response getVehicles(
public Response getVehiclesToBlock(
@BeanParam StandardParameters stdParameters,
@Parameter(description = "If set 'true', returns only the data with actual time windows.", required = false)
@QueryParam(value = "actual") boolean isActual,
@QueryParam(value = "actual")
boolean isActual,
@Parameter(description = "If set, returns only the data for that block Id.", required = false)
@QueryParam(value = "blockId") String blockId )
@QueryParam(value = "blockId")
String blockId)
throws WebApplicationException {

stdParameters.validate();
Expand Down
102 changes: 56 additions & 46 deletions core/src/main/java/org/transitclock/core/avl/TraccarAVLModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,37 @@
*/
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.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.transitclock.domain.structs.AvlReport;
import org.transitclock.extension.traccar.ApiClient;
import org.transitclock.extension.traccar.ApiException;
import org.transitclock.extension.traccar.api.DefaultApi;
import org.transitclock.extension.traccar.model.DeviceDto;
import org.transitclock.extension.traccar.model.PositionDto;
import org.transitclock.extension.traccar.model.UserDto;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.auth.AuthCache;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.HttpHost;

import static org.transitclock.config.data.TraccarConfig.*;
import static org.transitclock.config.data.TraccarConfig.TRACCARBASEURL;
import static org.transitclock.config.data.TraccarConfig.TRACCAREMAIL;
import static org.transitclock.config.data.TraccarConfig.TRACCARPASSWORD;
import static org.transitclock.config.data.TraccarConfig.TRACCARSOURCE;


/**
Expand All @@ -47,68 +60,64 @@
*/
@Slf4j
public class TraccarAVLModule extends PollUrlAvlModule {
public TraccarAVLModule(String agencyId) {
super(agencyId);
@NonNull
private final DefaultApi api;
@NonNull
private final UserDto user;

public TraccarAVLModule(String agencyId) throws URISyntaxException {
super(agencyId);
useCompression = false;
}

var host = HttpHost.create(TRACCARBASEURL.getValue());
var httpClientBuilder = HttpClientBuilder.create();

private DefaultApi initApiClient() throws RuntimeException {
DefaultApi api = new DefaultApi();
ApiClient client = new ApiClient();
final AuthCache authCache = new BasicAuthCache();
authCache.put(host, new BasicScheme());

var provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(host), new UsernamePasswordCredentials(TRACCAREMAIL.getValue(), TRACCARPASSWORD.getValue().toCharArray()));
httpClientBuilder.setDefaultCredentialsProvider(provider);

ApiClient client = new ApiClient(httpClientBuilder.build());
client.setBasePath(TRACCARBASEURL.getValue());
client.setUsername(TRACCAREMAIL.getValue());
client.setUsername(TRACCARPASSWORD.getValue());
client.setPassword(TRACCARPASSWORD.getValue());
api.setApiClient(client);
return api;
}
this.api = new DefaultApi(client);


private User initUser(DefaultApi api) throws RuntimeException {
User user;
try {
user = api
this.user = this.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 denied", e);
}
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<DeviceDto> 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());
List<PositionDto> results = api.positionsGet(null, null, null, null);
for (PositionDto result : results) {
DeviceDto 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.getDeviceTime().toEpochSecond(), 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.getDeviceTime().toEpochSecond(), 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);
avlReportsReadIn.add(avlReport);
}
forwardAvlReports(avlReportsReadIn);
}
Expand All @@ -117,10 +126,11 @@ 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))
private DeviceDto findDeviceById(List<DeviceDto> devices, Integer id) {
for (DeviceDto device : devices) {
if (Objects.equals(device.getId(), id)) {
return device;
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.querydsl.jpa.impl.JPAQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.transitclock.core.BlocksInfo;
import org.transitclock.core.dataCache.VehicleDataCache;
Expand All @@ -16,6 +17,7 @@
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Implements the VehiclesInterface interface on the server side such that a VehiclessClient can
Expand Down Expand Up @@ -401,33 +403,33 @@ public List<IpcVehicleToBlockConfig> getActualVehicleToBlockConfigs() {

@Override
public List<IpcVehicleToBlockConfig> getVehicleToBlockConfigByBlockId(String blockId) {
List<IpcVehicleToBlockConfig> result = new ArrayList<>();
try (Session session = HibernateUtils.getSession()) {
if (blockId != null) {
for (VehicleToBlockConfig vehicleToBlockConfig : VehicleToBlockConfig.getVehicleToBlockConfigsByBlockId(session, blockId)) {
result.add(new IpcVehicleToBlockConfig(vehicleToBlockConfig));
}
} else {
for (VehicleToBlockConfig vehicleToBlockConfig : VehicleToBlockConfig.getVehicleToBlockConfigs(session)) {
result.add(new IpcVehicleToBlockConfig(vehicleToBlockConfig));
}
if (StringUtils.isEmpty(blockId)) {
return VehicleToBlockConfig.getVehicleToBlockConfigsByBlockId(session, blockId)
.stream()
.map(IpcVehicleToBlockConfig::new)
.collect(Collectors.toList());
}
return VehicleToBlockConfig.getVehicleToBlockConfigs(session)
.stream()
.map(IpcVehicleToBlockConfig::new)
.collect(Collectors.toList());
} catch (Exception ex) {
logger.error("Something happened while fetching the data.", ex);
}
return result;
return List.of();
}

@Override
public List<IpcVehicleToBlockConfig> getVehicleToBlockConfigByVehicleId(String vehicleId) {
List<IpcVehicleToBlockConfig> result = new ArrayList<>();
try (Session session = HibernateUtils.getSession()) {
for (var vehicleToBlockConfig : VehicleToBlockConfig.getVehicleToBlockConfigsByVehicleId(session, vehicleId)) {
result.add(new IpcVehicleToBlockConfig(vehicleToBlockConfig));
}
return VehicleToBlockConfig.getVehicleToBlockConfigsByVehicleId(session, vehicleId)
.stream()
.map(IpcVehicleToBlockConfig::new)
.collect(Collectors.toList());
} catch (Exception ex) {
logger.error("Something happened while fetching the data.", ex);
}
return result;
return List.of();
}
}
Loading

0 comments on commit f5595c7

Please sign in to comment.