diff --git a/data-collectors/traffic-a22-forecast/.env.example b/data-collectors/traffic-a22-forecast/.env.example new file mode 100644 index 000000000..adb627993 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/.env.example @@ -0,0 +1,29 @@ +# Data Collector config +A22_URL= +A22_USER= +A22_PASSWORD= + +SCHEDULER_CRON=0 */10 * * * * + +SCHEDULER_POOL_SIZE=1 + + +# OpenDataHub O-AUTH Keycloak configuration +# (These are standard values for local development) +OAUTH_AUTH_URI=https://auth.opendatahub.testingmachine.eu/auth +OAUTH_TOKEN_URI=https://auth.opendatahub.testingmachine.eu/auth/realms/noi/protocol/openid-connect/token +OAUTH_BASE_URI=http://localhost:8999/json +OAUTH_CLIENT_ID=odh-mobility-datacollector-bike-boxes +OAUTH_CLIENT_NAME=odh-mobility-datacollector-bike-boxes +OAUTH_CLIENT_SECRET=7bd46f8f-c296-416d-a13d-dc81e68d0830 + +ODH_CLIENT_STATIONTYPE=TrafficForecast +ODH_CLIENT_PROVENANCE_NAME=odh-mobility-dc-traffic-a22-forecast +ODH_CLIENT_PROVENANCE_VERSION=0.1.0 +ODH_CLIENT_PROVENANCE_ORIGIN=A22 +ODH_CLIENT_PERIOD=600 + +# Logging level and style (text or json) +LOG_LEVEL=info +LOG_STYLE=text +ROOT_LOG_LEVEL=info \ No newline at end of file diff --git a/data-collectors/traffic-a22-forecast/README.md b/data-collectors/traffic-a22-forecast/README.md new file mode 100644 index 000000000..dd809dfdc --- /dev/null +++ b/data-collectors/traffic-a22-forecast/README.md @@ -0,0 +1,154 @@ + + +# A22 Road weather + + +Application which takes A22 road weather data from a web service parses it and sends it to the opendatahub. + +[![CI traffic-a22-forecast](https://github.com/noi-techpark/bdp-commons/actions/workflows/ci-traffic-a22-forecast.yml/badge.svg)](https://github.com/noi-techpark/bdp-commons/actions/workflows/ci-traffic-a22-forecast.yml) + +## Table of contents + +- [A22 Road weather](#a22-road-weather) + - [Table of contents](#table-of-contents) + - [Getting started](#getting-started) + - [Prerequisites](#prerequisites) + - [Source code](#source-code) + - [Build](#build) + - [Running tests](#running-tests) + - [Deployment](#deployment) + - [Information](#information) + - [Support](#support) + - [Contributing](#contributing) + - [Documentation](#documentation) + - [License](#license) + +## Getting started + +These instructions will get you a copy of the project up and running +on your local machine for development and testing purposes. + +### Prerequisites + +To build the project, the following prerequisites must be met: + +- Java JDK 1.8 or higher (e.g. [OpenJDK](https://openjdk.java.net/)) +- [Maven](https://maven.apache.org/) 3.x +- A postgres database with the schema intimev2 and elaboration (bdp) already installed + +### Source code + +Get a copy of the repository: + +```bash +git clone git clone https://github.com/noi-techpark/bdp-commons +``` + +Change directory: + +```bash +cd bdp-commons/data-collectors/traffic-a22-forecast +``` + +### Build + +Build the project: + +```bash +mvn clean package +``` + +## Running tests + +The unit tests can be executed with the following command: + +```bash +mvn clean test +``` + +## Deployment + +This is a maven project and will produce a war that can be deployed in any j2ee container like tomcat or jetty. + +Steps: + +* change the file src/main/resources/META-INF/spring/application.properties. set the url, the username and the + password to connect to the A22 API to query historic values of road weather (or configure it within a CI tool) + +``` +url= +user= +password= +``` + +* optionally change frequence of elaborations in the file src/main/resources/it/bz/noi/a22/roadweather/cron.properties. + (or configure it within a CI tool) + +``` +schedule=0 0/10 * * * ? +``` + +* optionally change the origin, the provenance, the station type, the last timestamp and the scan window (in seconds) in the file +src/main/resources/it/bz/noi/a22/roadweather/a22roadweather.properties. (or configure it within a CI tool) + +``` +origin=A22 +provenance.name=dc- +provenance.version=1.0.0-SNAPSHOT +stationtype=RWISstation +lastTimestamp=2017-01-01 00:00:00 +scanWindowSeconds=604800 +``` + +* optionally change the data types in the file src/main/resources/it/bz/noi/a22/roadweather/a22roadweatherdatatypes.propertiess. + (or configure it within a CI tool) + +``` +a22roadweather.datatype.temp_aria.key=temp_aria +a22roadweather.datatype.temp_aria.unit=[°C] +a22roadweather.datatype.temp_aria.description=Temperatura dell’aria +a22roadweather.datatype.temp_aria.rtype=Mean +a22roadweather.datatype.temp_aria.mapping=false +... +``` + +* configure the log4j.properties file as desidered + +* create the war executing the following command + +``` +mvn clean package +``` + +* deploy the war to a j2ee container like tomcat or jetty + + +## Information + +### Support + +For support, please contact [info@opendatahub.com](mailto:info@opendatahub.com). + +### Contributing + +If you'd like to contribute, please follow the following instructions: + +- Fork the repository. + +- Checkout a topic branch from the `development` branch. + +- Make sure the tests are passing. + +- Create a pull request against the `development` branch. + +### Documentation + +More documentation can be found at [https://opendatahub.readthedocs.io/en/latest/index.html](https://opendatahub.readthedocs.io/en/latest/index.html). + +### License + +The code in this project is licensed under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 license. See the [LICENSE.md](LICENSE.md) file for more information. diff --git a/data-collectors/traffic-a22-forecast/calls.http b/data-collectors/traffic-a22-forecast/calls.http new file mode 100644 index 000000000..5fb587024 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/calls.http @@ -0,0 +1,12 @@ +@host = {{$dotenv A22_URL}} +@user = {{$dotenv A22_USER}} +@password = {{$dotenv A22_PASSWORD}} + + +### GetPrevisioniTrafficoComplete +POST {{host}}/GetPrevisioniTrafficoComplete +User-Agent: NOI/A22TrafficForecastConnector +Content-Type: application/json +Authorization: Basic {{user}}:{{password}} +Month: 10 +Year: 2023 \ No newline at end of file diff --git a/data-collectors/traffic-a22-forecast/docker-compose.yml b/data-collectors/traffic-a22-forecast/docker-compose.yml new file mode 100644 index 000000000..e4c0349cc --- /dev/null +++ b/data-collectors/traffic-a22-forecast/docker-compose.yml @@ -0,0 +1,29 @@ +# SPDX-FileCopyrightText: NOI Techpark +# +# SPDX-License-Identifier: CC0-1.0 + +version: "3.4" + +services: + app: + image: maven:3-openjdk-17-slim + env_file: + - .env + environment: + MAVEN_CONFIG: /var/maven/.m2 + MAVEN_OPTS: -Dspring-boot.run.jvmArguments="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:9000" + command: > + mvn + -Duser.home=/var/maven + clean + compile + spring-boot:run + ports: + - 9000:9000 + - 9001:9001 + volumes: + - ~/.m2/:/var/maven/.m2 + - ./:/code + working_dir: /code + tty: true + network_mode: host diff --git a/data-collectors/traffic-a22-forecast/infrastructure/ansible/ansible.cfg b/data-collectors/traffic-a22-forecast/infrastructure/ansible/ansible.cfg new file mode 100644 index 000000000..bec5ce050 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/ansible/ansible.cfg @@ -0,0 +1,4 @@ +[defaults] +inventory = ./hosts +roles_path = ./roles +retry_files_enabled = False diff --git a/data-collectors/traffic-a22-forecast/infrastructure/ansible/deploy.yml b/data-collectors/traffic-a22-forecast/infrastructure/ansible/deploy.yml new file mode 100644 index 000000000..c01d4feb1 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/ansible/deploy.yml @@ -0,0 +1,19 @@ +--- +- hosts: all + vars: + ansible_python_interpreter: /usr/bin/python3 + tasks: + - name: Login to GitHub Container Registry + ansible.builtin.shell: + cmd: echo "{{ docker_password }}" | docker login "{{ docker_host }}" --username "{{ docker_username }}" --password-stdin + - name: Execute Docker deployment + ansible.builtin.include_role: + name: ansible-docker-deployment + vars: + docker_deployment_project_name: '{{ project_name }}' + docker_deployment_release_name: '{{ release_name }}' + docker_deployment_release_files: + - local: ../docker-compose.run.yml + remote: docker-compose.yml + - local: ../../.env + remote: .env diff --git a/data-collectors/traffic-a22-forecast/infrastructure/ansible/hosts b/data-collectors/traffic-a22-forecast/infrastructure/ansible/hosts new file mode 100644 index 000000000..23d2639b7 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/ansible/hosts @@ -0,0 +1,5 @@ +[test] +docker01.testingmachine.eu ansible_user='noi-techpark-bot' ansible_ssh_common_args='-o StrictHostKeyChecking=no' + +[prod] +docker02.opendatahub.com ansible_user='noi-techpark-bot' ansible_ssh_common_args='-o StrictHostKeyChecking=no' diff --git a/data-collectors/traffic-a22-forecast/infrastructure/ansible/requirements.yml b/data-collectors/traffic-a22-forecast/infrastructure/ansible/requirements.yml new file mode 100644 index 000000000..cb9e81bb8 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/ansible/requirements.yml @@ -0,0 +1,2 @@ +- src: git+https://github.com/noi-techpark/ansible-docker-deployment.git + version: "2.0" diff --git a/data-collectors/traffic-a22-forecast/infrastructure/ansible/roles/.gitignore b/data-collectors/traffic-a22-forecast/infrastructure/ansible/roles/.gitignore new file mode 100644 index 000000000..d6b7ef32c --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/ansible/roles/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/data-collectors/traffic-a22-forecast/infrastructure/docker-compose.build.yml b/data-collectors/traffic-a22-forecast/infrastructure/docker-compose.build.yml new file mode 100644 index 000000000..93d36f027 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/docker-compose.build.yml @@ -0,0 +1,8 @@ +version: "3.4" + +services: + app: + image: ${DOCKER_IMAGE}:${DOCKER_TAG} + build: + context: ../ + dockerfile: infrastructure/docker/Dockerfile diff --git a/data-collectors/traffic-a22-forecast/infrastructure/docker-compose.run.yml b/data-collectors/traffic-a22-forecast/infrastructure/docker-compose.run.yml new file mode 100644 index 000000000..8be5e08fa --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/docker-compose.run.yml @@ -0,0 +1,10 @@ +version: "3.4" + +services: + app: + image: ${DOCKER_IMAGE}:${DOCKER_TAG} + restart: unless-stopped + environment: + _JAVA_OPTIONS: ${JAVA_OPTIONS} + env_file: + - .env diff --git a/data-collectors/traffic-a22-forecast/infrastructure/docker/.gitignore b/data-collectors/traffic-a22-forecast/infrastructure/docker/.gitignore new file mode 100644 index 000000000..0c84ffb73 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/docker/.gitignore @@ -0,0 +1 @@ +/.m2 diff --git a/data-collectors/traffic-a22-forecast/infrastructure/docker/Dockerfile b/data-collectors/traffic-a22-forecast/infrastructure/docker/Dockerfile new file mode 100644 index 000000000..e696abc1a --- /dev/null +++ b/data-collectors/traffic-a22-forecast/infrastructure/docker/Dockerfile @@ -0,0 +1,3 @@ +FROM maven:3-openjdk-17-slim as base +COPY target/ROOT.jar app.jar +CMD [ "java", "-jar", "app.jar" ] \ No newline at end of file diff --git a/data-collectors/traffic-a22-forecast/pom.xml b/data-collectors/traffic-a22-forecast/pom.xml new file mode 100644 index 000000000..c9b3bc33c --- /dev/null +++ b/data-collectors/traffic-a22-forecast/pom.xml @@ -0,0 +1,83 @@ + + + + + + 4.0.0 + com.opendatahub.bdp + dc-traffic-a22-forecast + 0.1.0 + A22 traffic forecast data collector + + + 17 + UTF-8 + 17 + 17 + ROOT + + + + org.springframework.boot + spring-boot-starter-parent + 2.7.8 + + + + + + + maven-repo.opendatahub.com + https://maven-repo.opendatahub.com/release + + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-webflux + + + org.springframework.security + spring-security-web + + + org.springframework.security + spring-security-config + + + it.bz.idm.bdp + dc-interface + 7.4.0 + + + org.projectlombok + lombok + 1.18.26 + provided + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + ${finalName} + + diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/JobScheduler.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/JobScheduler.java new file mode 100644 index 000000000..edc23a011 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/JobScheduler.java @@ -0,0 +1,55 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast; + +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; +import com.opendatahub.traffic.a22.forecast.config.DataConfig; +import com.opendatahub.traffic.a22.forecast.config.StationConfig; +import com.opendatahub.traffic.a22.forecast.dto.ForecastDto; +import com.opendatahub.traffic.a22.forecast.services.A22Client; +import com.opendatahub.traffic.a22.forecast.services.OdhClient; +import com.opendatahub.traffic.a22.forecast.config.ProvenanceConfig; + + +@Service +public class JobScheduler { + private static final Logger LOG = LoggerFactory.getLogger(JobScheduler.class); + + @Lazy + @Autowired + private OdhClient odhClient; + + @Lazy + @Autowired + private A22Client a22Client; + + @Autowired + private DataConfig dataC; + + @Autowired + private StationConfig stationC; + + @Autowired + private ProvenanceConfig provC; + + @Scheduled(cron = "${scheduler.job}") + public void collectBikeBoxData() { + LOG.info("Cron job started"); + + ForecastDto forecasts = a22Client.getForecasts("2023", "9"); + LOG.info("DONE"); + + } +} diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/Main.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/Main.java new file mode 100644 index 000000000..ddb1ba9cb --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/Main.java @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableScheduling; + +@ComponentScan({"com.opendatahub.traffic.a22.forecast", "it.bz.idm.bdp"}) +@EnableScheduling +@SpringBootApplication +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } +} diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/DataConfig.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/DataConfig.java new file mode 100644 index 000000000..7d01e0eac --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/DataConfig.java @@ -0,0 +1,14 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class DataConfig { + @Value("${data.period}") + public int period; +} diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/DataTypes.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/DataTypes.java new file mode 100644 index 000000000..4ea0b3e88 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/DataTypes.java @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast.config; + +/** + * define datatype enums for ODH + */ +import it.bz.idm.bdp.dto.DataTypeDto; + +public enum DataTypes { + /** FREE, OCCUPIED, OUT OF SERVICE */ + usageState("usageState", "Usage state", true, "state"), + freeSpotsRegularBikes("freeSpotsRegularBike", "Free parking spots (regular bikes)", true, "count"), + freeSpotsElectricBikes("freeSpotsElectricBike", "Free parking spots (electric bikes)", true, "count"), + free("free", "Free parking spots", false, "count"); + + private DataTypes(String key, String description, boolean sync, String unit) { + this.key = key; + this.description = description; + this.syncToOdh = sync; + this.unit = unit; + } + + public final boolean syncToOdh; + public final String key; + public final String unit; + public final String description; + public final String rtype = "Instantaneous"; + + public DataTypeDto toDataTypeDto() { + return new DataTypeDto(key, unit, description, rtype); + } +} diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/ProvenanceConfig.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/ProvenanceConfig.java new file mode 100644 index 000000000..2d3210ad3 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/ProvenanceConfig.java @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ProvenanceConfig { + @Value("${provenance.name}") + public String name; + + @Value("${provenance.version}") + public String version; + + @Value("${provenance.origin}") + public String origin; +} diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/StationConfig.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/StationConfig.java new file mode 100644 index 000000000..958b83612 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/config/StationConfig.java @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class StationConfig { + @Value("${station.stationType}") + public String stationType; + +} diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/dto/ForecastDto.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/dto/ForecastDto.java new file mode 100644 index 000000000..902328190 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/dto/ForecastDto.java @@ -0,0 +1,109 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast.dto; + +import java.util.List; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +import lombok.ToString; + +@ToString +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonDeserialize +public class ForecastDto { + + @JsonProperty("d") + public Data data; + + @ToString + public static class Data { + @JsonProperty("__type") + public String type; + + @JsonProperty("MesePrevisioniTraffico") + public String month; + + @JsonProperty("AnnoPrevisioniTraffico") + public int year; + + @JsonProperty("NextMonth") + public boolean hasNextMonth; + + @JsonProperty("PrevMonth") + public boolean hasPrevMonth; + + @JsonProperty("RighePrevisioniTraffico") + public List trafficData; + } + + @ToString + public static class TrafficData { + @JsonProperty("Titolo") + public String titolo; + + @JsonProperty("Data") + public String date; + + @JsonProperty("Giorno") + public int day; + + @JsonProperty("Mese") + public int month; + + @JsonProperty("Anno") + public int year; + + @JsonProperty("Nord") + public Direction north; + + @JsonProperty("Sud") + public Direction south; + } + + @ToString + public static class Direction { + + @JsonProperty("Tipo") + public int type; + + @JsonProperty("Brennero") + public Values brennero; + + @JsonProperty("Bolzano") + public Values bolzano; + + @JsonProperty("Verona") + public Values verona; + + } + + @ToString + public static class Values { + @JsonProperty("Max") + public int max; + + @JsonProperty("_0_6") + public Value value0to6; + + @JsonProperty("_6_12") + public Value value6to12; + + @JsonProperty("_12_18") + public Value value12to18; + + @JsonProperty("_18_24") + public Value value18to24; + + } + + @ToString + public static class Value { + @JsonProperty("Tipo") + public int type; + + } +} \ No newline at end of file diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/services/A22Client.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/services/A22Client.java new file mode 100644 index 000000000..54c1701b2 --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/services/A22Client.java @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast.services; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.web.reactive.function.client.WebClient; + +import com.opendatahub.traffic.a22.forecast.dto.ForecastDto; + +@Service +public class A22Client { + private final Logger LOG = LoggerFactory.getLogger(A22Client.class); + private final String ENDPOINT_FORECAST = "/GetPrevisioniTrafficoComplete"; + + @Value("${a22.url}") + private String url; + + @Value("${a22.username}") + private String userName; + + @Value("${a22.password}") + private String password; + + // public A22Client() { + + // LOG.info("username {}", userName); + // LOG.info("password {}", password); + // client = WebClient.builder() + // .defaultHeaders(header -> header.setBasicAuth(userName, password)) + // .baseUrl(url) + // .build(); + // } + + public ForecastDto getForecasts(String year, String month) { + WebClient client = WebClient.builder() + .defaultHeaders(header -> header.setBasicAuth(userName, password)) + .baseUrl(url) + .build(); + + return client.post().uri(u -> u.path(ENDPOINT_FORECAST).build()).accept(MediaType.ALL) + .contentType(MediaType.APPLICATION_JSON) + .header("month", month) + .header("year", year) + .header("User-Agent", "NOI/A22TrafficForecastConnector") + .retrieve() + .bodyToMono(ForecastDto.class) + .block(); + } + +} diff --git a/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/services/OdhClient.java b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/services/OdhClient.java new file mode 100644 index 000000000..874ff869d --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/java/com/opendatahub/traffic/a22/forecast/services/OdhClient.java @@ -0,0 +1,44 @@ +// SPDX-FileCopyrightText: NOI Techpark +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +package com.opendatahub.traffic.a22.forecast.services; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Service; + +import com.opendatahub.traffic.a22.forecast.config.ProvenanceConfig; + +import it.bz.idm.bdp.dto.DataMapDto; +import it.bz.idm.bdp.dto.ProvenanceDto; +import it.bz.idm.bdp.dto.RecordDtoImpl; +import it.bz.idm.bdp.json.NonBlockingJSONPusher; + +@Lazy +@Service +public class OdhClient extends NonBlockingJSONPusher { + + @Autowired + private ProvenanceConfig provenanceConfig; + + @Override + public DataMapDto mapData(T data) { + /* You can ignore this legacy method call */ + return null; + } + + @Override + public String initIntegreenTypology() { + return "placeholder"; + } + + @Override + public ProvenanceDto defineProvenance() { + return new ProvenanceDto(null, provenanceConfig.name, provenanceConfig.version, provenanceConfig.origin); + } + + public ProvenanceDto getProvenanceConfig() { + return provenance; + } +} diff --git a/data-collectors/traffic-a22-forecast/src/main/resources/application.properties b/data-collectors/traffic-a22-forecast/src/main/resources/application.properties new file mode 100644 index 000000000..605b3318e --- /dev/null +++ b/data-collectors/traffic-a22-forecast/src/main/resources/application.properties @@ -0,0 +1,42 @@ +a22.url=${A22_URL} +a22.username=${A22_USER} +a22.password=${A22_PASSWORD} + +# Cron definition to start jobs +scheduler.job=${SCHEDULER_CRON:*/10 * * * * *} + +# set scheduler pool size +spring.task.scheduling.pool.size=${SCHEDULER_POOL_SIZE:1} + +##### Open Data Hub Configuration +# Data provenance (Where does our data come from?) +provenance.name=${ODH_CLIENT_PROVENANCE_NAME} +provenance.version=${ODH_CLIENT_PROVENANCE_VERSION} +provenance.origin=${ODH_CLIENT_PROVENANCE_ORIGIN} + +# Station config +station.stationType=${ODH_CLIENT_STATIONTYPE} + +# Measurement config +data.period=${ODH_CLIENT_PERIOD:600} + +# dc-interface configuration (Writer API endpoint) +bdp_host=localhost +bdp_port=8999 +bdp_endpoint=localhost + + +# Keycloak configuration (default = setup for local development) +authorizationUri=${OAUTH_AUTH_URI:https://auth.opendatahub.testingmachine.eu/auth} +tokenUri=${OAUTH_TOKEN_URI:https://auth.opendatahub.testingmachine.eu/auth/realms/noi/protocol/openid-connect/token} +BASE_URI=${OAUTH_BASE_URI:http://localhost:8999/json} +clientId=${OAUTH_CLIENT_ID:odh-mobility-datacollector-development} +clientName=${OAUTH_CLIENT_NAME:odh-mobility-datacollector-development} +clientSecret=${OAUTH_CLIENT_SECRET:7bd46f8f-c296-416d-a13d-dc81e68d0830} +scope=openid + +# sets web type to none, since we don't need a web server +# fixes cicrcular dependency issue +spring.main.web-application-type=NONE + +# logging.level.com.opendatahub.bdp.commons.dc.bikeboxes=trace \ No newline at end of file