Skip to content

Commit

Permalink
Merge pull request #14 from starwit/feature/AB#1289-change-to-onstreet
Browse files Browse the repository at this point in the history
change to on street data model
  • Loading branch information
ztarbug authored Nov 27, 2024
2 parents 017af0a + f9af018 commit a3d5b12
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 55 deletions.
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' \
Expand All @@ -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
Expand Down
32 changes: 16 additions & 16 deletions src/main/java/de/starwit/odp/ODPFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -131,15 +131,15 @@ 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");
}
}
}

private void sendOffStreetParkingUpdate(OffStreetParking ofs) {
private void sendOnStreetParkingUpdate(OnStreetParking ofs) {
HttpHeaders headers = new HttpHeaders();
headers.set("fiware-ServicePath", "/ParkingManagement");
headers.set("fiware-service","Wolfsburg");
Expand All @@ -153,7 +153,7 @@ private void sendOffStreetParkingUpdate(OffStreetParking ofs) {
}
}

private String createAvailableSpotsRequestBody(OffStreetParking ofs) {
private String createAvailableSpotsRequestBody(OnStreetParking ofs) {
return """
{
"availableSpotNumber": {
Expand All @@ -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");
Expand All @@ -178,15 +178,15 @@ private OffStreetParking getDataFromODP(String parkingSpaceId) {

try {
ResponseEntity<String> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.time.LocalDateTime;

public class OffStreetParking {
public class OnStreetParking {
/**
* ID in analytics DB.
*/
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package de.starwit.odp.model;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
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 {
Expand All @@ -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());
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions src/test/java/de/starwit/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading

0 comments on commit a3d5b12

Please sign in to comment.