diff --git a/Readme.md b/Readme.md index 13f03bc..2341c05 100644 --- a/Readme.md +++ b/Readme.md @@ -29,7 +29,7 @@ odp.parking.url=https://api.staging.wolfsburg.digital/context/v2/entities/ odp.update_frequency=30000 # Configure mapping to parking area -odp.parkingareaid=OffStreetParking-Pkpd-787878 +odp.parkingareaid=OnStreetParking:38444039 # fallback default, if reading value from ODP fails odp.parkingareaid.defaulttotal=70 # prefix for observation areas to sum @@ -54,12 +54,12 @@ curl -H application/x-www-form-urlencoded -d "realm=default" -d "client_id=api" Get latest OffStreetParking data ```bash -curl --location 'https://api.staging.wolfsburg.digital/context/v2/entities/OffStreetParking-Pkpd-787878/' -H 'fiware-ServicePath: /ParkingManagement' -H 'fiware-service: Wolfsburg' -H 'Authorization: Bearer TOKEN' +curl --location 'https://api.staging.wolfsburg.digital/context/v2/entities/OnStreetParking:38444039/' -H 'fiware-ServicePath: /ParkingManagement' -H 'fiware-service: Wolfsburg' -H 'Authorization: Bearer TOKEN' ``` Update OffStreetParking data ```bash -curl --location --request PATCH 'https://api.staging.wolfsburg.digital/context/v2/entities/OffStreetParking-Pkpd-787878/attrs/' \ +curl --location --request PATCH 'https://api.staging.wolfsburg.digital/context/v2/entities/OnStreetParking:38444039/attrs/' \ --header 'fiware-ServicePath: /ParkingManagement' \ --header 'fiware-service: Wolfsburg' \ --header 'content-type: application/json' \ @@ -74,7 +74,7 @@ curl --location --request PATCH 'https://api.staging.wolfsburg.digital/context/v Query ParkingSpots ```bash -curl --location 'https://api.staging.wolfsburg.digital/context/v2/entities?type=ParkingSpot' -H 'fiware-ServicePath: /ParkingManagement/Meckauer' -H 'fiware-service: Wolfsburg' -H 'Authorization: Bearer ' +curl --location 'https://api.staging.wolfsburg.digital/context/v2/entities?type=ParkingSpot' -H 'fiware-ServicePath: /ParkingManagement/Meckauer' -H 'fiware-service: Wolfsburg' -H 'Authorization: Bearer TOKEN' ``` ## License & Contribution diff --git a/src/main/java/de/starwit/odp/ODPFunctions.java b/src/main/java/de/starwit/odp/ODPFunctions.java index 022ee02..d7ceee2 100644 --- a/src/main/java/de/starwit/odp/ODPFunctions.java +++ b/src/main/java/de/starwit/odp/ODPFunctions.java @@ -25,8 +25,8 @@ import de.starwit.odp.analytics.AnalyticsRepository; import de.starwit.odp.model.AuthTokenResponse; -import de.starwit.odp.model.OffStreetParking; -import de.starwit.odp.model.OffStreetParkingFunctions; +import de.starwit.odp.model.OnStreetParking; +import de.starwit.odp.model.OnStreetParkingFunctions; import jakarta.annotation.PostConstruct; @Service @@ -55,7 +55,7 @@ public class ODPFunctions { @Value("${config.autostart}") private boolean sendUpdates; - private OffStreetParking ofs; + private OnStreetParking ofs; private LocalDateTime tokenTimeStamp; private String token = null; @@ -131,7 +131,7 @@ private void updateParkingState() { if(token != null) { if(ofs.isSynched()) { ofs.setAvailableParkingSpots(ofs.getTotalSpotNumber() - repository.getParkedCars()); - sendOffStreetParkingUpdate(ofs); + sendOnStreetParkingUpdate(ofs); } } else { log.info("No valid token, can't update ODP"); @@ -139,7 +139,7 @@ private void updateParkingState() { } } - private void sendOffStreetParkingUpdate(OffStreetParking ofs) { + private void sendOnStreetParkingUpdate(OnStreetParking ofs) { HttpHeaders headers = new HttpHeaders(); headers.set("fiware-ServicePath", "/ParkingManagement"); headers.set("fiware-service","Wolfsburg"); @@ -153,7 +153,7 @@ private void sendOffStreetParkingUpdate(OffStreetParking ofs) { } } - private String createAvailableSpotsRequestBody(OffStreetParking ofs) { + private String createAvailableSpotsRequestBody(OnStreetParking ofs) { return """ { "availableSpotNumber": { @@ -165,10 +165,10 @@ private String createAvailableSpotsRequestBody(OffStreetParking ofs) { /* ********************** Synching with ODP ************************ */ - private OffStreetParking getDataFromODP(String parkingSpaceId) { - OffStreetParking offStreetParking = new OffStreetParking(); - offStreetParking.setOdpID(parkingSpaceId); - offStreetParking.setSynched(false); + private OnStreetParking getDataFromODP(String parkingSpaceId) { + OnStreetParking onStreetParking = new OnStreetParking(); + onStreetParking.setOdpID(parkingSpaceId); + onStreetParking.setSynched(false); HttpHeaders headers = new HttpHeaders(); headers.set("fiware-ServicePath", "/ParkingManagement"); @@ -178,15 +178,15 @@ private OffStreetParking getDataFromODP(String parkingSpaceId) { try { ResponseEntity response = restTemplate.exchange(parkingSpaceUrl + "/" + parkingSpaceId, HttpMethod.GET, request, String.class); - offStreetParking = OffStreetParkingFunctions.extractOffstreetParking(response.getBody()); - offStreetParking.setOdpID(parkingSpaceId); - offStreetParking.setSynched(true); - log.info("Get data from ODP for " + parkingSpaceId + " - " + offStreetParking.toString()); + onStreetParking = OnStreetParkingFunctions.extractOnstreetParking(response.getBody()); + onStreetParking.setOdpID(parkingSpaceId); + onStreetParking.setSynched(true); + log.info("Get data from ODP for " + parkingSpaceId + " - " + onStreetParking.toString()); } catch (HttpClientErrorException e) { log.info("Can't get parking space data for " + parkingSpaceId + " with response " + e.getStatusCode()); - offStreetParking.setTotalSpotNumber(parkingSpaceDefaultTotalSpots); + onStreetParking.setTotalSpotNumber(parkingSpaceDefaultTotalSpots); } - return offStreetParking; + return onStreetParking; } public boolean isSendUpdates() { diff --git a/src/main/java/de/starwit/odp/model/OffStreetParking.java b/src/main/java/de/starwit/odp/model/OnStreetParking.java similarity index 75% rename from src/main/java/de/starwit/odp/model/OffStreetParking.java rename to src/main/java/de/starwit/odp/model/OnStreetParking.java index aff3e7e..22ea2c3 100644 --- a/src/main/java/de/starwit/odp/model/OffStreetParking.java +++ b/src/main/java/de/starwit/odp/model/OnStreetParking.java @@ -2,7 +2,7 @@ import java.time.LocalDateTime; -public class OffStreetParking { +public class OnStreetParking { /** * ID in analytics DB. */ @@ -19,7 +19,6 @@ public class OffStreetParking { private String odpID; private int availableParkingSpots; private int totalSpotNumber; - private LocalDateTime lastUpdate; private boolean synched = false; public int getAnalyticsId() { @@ -62,14 +61,6 @@ public void setTotalSpotNumber(int totalSpotNumber) { this.totalSpotNumber = totalSpotNumber; } - public LocalDateTime getLastUpdate() { - return lastUpdate; - } - - public void setLastUpdate(LocalDateTime lastUpdate) { - this.lastUpdate = lastUpdate; - } - public boolean isSynched() { return synched; } @@ -80,18 +71,17 @@ public void setSynched(boolean synched) { @Override public String toString() { - return "OffStreetParking [name=" + odpName + ", availableParkingSpots=" + availableParkingSpots - + ", totalSpotNumber=" + totalSpotNumber + ", lastUpdate=" + lastUpdate + "]"; + return "OnStreetParking [name=" + odpName + ", availableParkingSpots=" + availableParkingSpots + + ", totalSpotNumber=" + totalSpotNumber + "]"; } - public void copyContent(OffStreetParking tmp) { + public void copyContent(OnStreetParking tmp) { if(tmp != null && tmp.synched) { analyticsId = tmp.analyticsId; odpName = tmp.odpName; odpID = tmp.odpID; availableParkingSpots = tmp.availableParkingSpots; totalSpotNumber = tmp.totalSpotNumber; - lastUpdate = tmp.lastUpdate; synched = tmp.synched; } } diff --git a/src/main/java/de/starwit/odp/model/OffStreetParkingFunctions.java b/src/main/java/de/starwit/odp/model/OnStreetParkingFunctions.java similarity index 65% rename from src/main/java/de/starwit/odp/model/OffStreetParkingFunctions.java rename to src/main/java/de/starwit/odp/model/OnStreetParkingFunctions.java index b62a7c2..58bc778 100644 --- a/src/main/java/de/starwit/odp/model/OffStreetParkingFunctions.java +++ b/src/main/java/de/starwit/odp/model/OnStreetParkingFunctions.java @@ -1,8 +1,5 @@ package de.starwit.odp.model; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -10,12 +7,12 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -public class OffStreetParkingFunctions { +public class OnStreetParkingFunctions { - private static Logger log = LoggerFactory.getLogger(OffStreetParkingFunctions.class); + private static Logger log = LoggerFactory.getLogger(OnStreetParkingFunctions.class); - public static OffStreetParking extractOffstreetParking(String jsonString) { - OffStreetParking result = new OffStreetParking(); + public static OnStreetParking extractOnstreetParking(String jsonString) { + OnStreetParking result = new OnStreetParking(); ObjectMapper om = new ObjectMapper(); JsonNode rootNode; try { @@ -35,12 +32,6 @@ public static OffStreetParking extractOffstreetParking(String jsonString) { JsonNode valueNode = nameNode.get("value"); result.setOdpName(valueNode.asText()); } - JsonNode timestampNode = rootNode.get("dateObserved"); - if(totalSpots != null) { - JsonNode valueNode = timestampNode.get("value"); - LocalDateTime lastUpdate = LocalDateTime.parse(valueNode.asText(), DateTimeFormatter.ofPattern ("yyyy-MM-dd'T'HH:mm:ss'Z'")); - result.setLastUpdate(lastUpdate); - } result.setSynched(true); } catch (JsonProcessingException e) { log.info("Can't parse available parking spots " + e.getMessage()); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a036951..6f8a6eb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -15,9 +15,13 @@ odp.auth.username=username odp.auth.password=secret # URL to get auth token odp.auth.url=https://auth.staging.wolfsburg.digital/auth/realms/default/protocol/openid-connect/token +# prod +# odp.auth.url=https://auth.wolfsburg.digital/auth/realms/default/protocol/openid-connect/token # URL to read & update parking space data odp.parking.url=https://api.staging.wolfsburg.digital/context/v2/entities/ +#prod +# odp.parking.url=https://api.wolfsburg.digital/context/v2/entities/ # How often updates will be send odp.update_frequency=30000 @@ -33,7 +37,7 @@ logging.level.org.springframework.web=INFO logging.level.de.starwit=INFO # Configure mapping to parking area -odp.parkingareaid=OffStreetParking-Pkpd-787878 +odp.parkingareaid=OnStreetParking:38444039 # fallback default, if reading value from ODP fails odp.parkingareaid.defaulttotal=70 # prefix for observation areas to sum diff --git a/src/test/java/de/starwit/AppTest.java b/src/test/java/de/starwit/AppTest.java index 2ebea1b..820fe0c 100644 --- a/src/test/java/de/starwit/AppTest.java +++ b/src/test/java/de/starwit/AppTest.java @@ -9,21 +9,20 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.util.FileCopyUtils; -import de.starwit.odp.model.OffStreetParking; -import de.starwit.odp.model.OffStreetParkingFunctions; +import de.starwit.odp.model.OnStreetParking; +import de.starwit.odp.model.OnStreetParkingFunctions; public class AppTest { @Test public void parseODPResult() throws Exception { - ClassPathResource odpResultRes = new ClassPathResource("SampleOffStreetParking.json"); + ClassPathResource odpResultRes = new ClassPathResource("SampleOnStreetParking.json"); byte[] binaryData = FileCopyUtils.copyToByteArray(odpResultRes.getInputStream()); String strJson = new String(binaryData, StandardCharsets.UTF_8); - OffStreetParking osp = OffStreetParkingFunctions.extractOffstreetParking(strJson); + OnStreetParking osp = OnStreetParkingFunctions.extractOnstreetParking(strJson); - assertTrue(osp.getAvailableParkingSpots() == 52); - assertEquals("Meckauer Weg", osp.getOdpName()); + assertTrue(osp.getAvailableParkingSpots() == 39); + assertEquals("38444039", osp.getOdpName()); assertTrue(osp.getTotalSpotNumber() == 70); - assertTrue(osp.getLastUpdate().getYear() == 2024); } } diff --git a/src/test/resources/SampleOnStreetParking.json b/src/test/resources/SampleOnStreetParking.json new file mode 100644 index 0000000..fc9f768 --- /dev/null +++ b/src/test/resources/SampleOnStreetParking.json @@ -0,0 +1,169 @@ +{ + "id": "OnStreetParking:38444039", + "type": "OnStreetParking", + "alternateName": { + "type": "Text", + "value": "Meckauerweg", + "metadata": {} + }, + "availableEvSpotNumber": { + "type": "Integer", + "value": 0, + "metadata": {} + }, + "availableSpotNumber": { + "type": "Integer", + "value": 39, + "metadata": {} + }, + "charge": { + "type": "StructuredValue", + "value": { + "intervalPrice": { + "price": 1.3, + "valueAddedTax": 0, + "priceCurrency": "EUR", + "referenceQuantity": { + "value": 30, + "unitCode": "MIN" + } + } + }, + "metadata": {} + }, + "chargeType": { + "type": "StructuredValue", + "value": [ + "intervalPrice" + ], + "metadata": {} + }, + "evSpotNumber": { + "type": "Integer", + "value": 0, + "metadata": {} + }, + "location": { + "type": "geo:json", + "value": { + "type": "Polygon", + "coordinates": [ + [ + [ + 10.786745821, + 52.42397175 + ], + [ + 10.786990288, + 52.423968988 + ], + [ + 10.786994241, + 52.423974366 + ], + [ + 10.787006339, + 52.423994996 + ], + [ + 10.787253938, + 52.424355513 + ], + [ + 10.787150343, + 52.424365383 + ], + [ + 10.787208151, + 52.424566903 + ], + [ + 10.786804801, + 52.424594769 + ], + [ + 10.786682621, + 52.423980446 + ], + [ + 10.786675599, + 52.423977535 + ], + [ + 10.786745821, + 52.42397175 + ] + ] + ] + }, + "metadata": {} + }, + "name": { + "type": "Text", + "value": "38444039", + "metadata": {} + }, + "operator": { + "type": "Text", + "value": "Stadt Wolfsburg", + "metadata": {} + }, + "parkingZone": { + "type": "Text", + "value": "1", + "metadata": {} + }, + "permitActiveHours": { + "type": "StructuredValue", + "value": { + "parkingTicket": [ + { + "opens": "07:00:00", + "dayOfWeek": "https://schema.org/Monday", + "closes": "18:00:00" + }, + { + "opens": "07:00:00", + "dayOfWeek": "https://schema.org/Tuesday", + "closes": "18:00:00" + }, + { + "opens": "07:00:00", + "dayOfWeek": "https://schema.org/Wednesday", + "closes": "18:00:00" + }, + { + "opens": "07:00:00", + "dayOfWeek": "https://schema.org/Thursday", + "closes": "18:00:00" + }, + { + "opens": "07:00:00", + "dayOfWeek": "https://schema.org/Friday", + "closes": "18:00:00" + }, + { + "opens": "07:00:00", + "dayOfWeek": "https://schema.org/Saturday", + "closes": "14:00:00" + }, + { + "dayOfWeek": "https://schema.org/PublicHolidays" + } + ] + }, + "metadata": {} + }, + "requiredPermit": { + "type": "StructuredValue", + "value": [ + "parkingTicket" + ], + "metadata": {} + }, + "totalSpotNumber": { + "type": "Integer", + "value": 70, + "metadata": {} + } +} \ No newline at end of file