Skip to content

Commit

Permalink
3차 배포
Browse files Browse the repository at this point in the history
* [FEAT] 센서 데이터 api 구현

* FEAT : 다이나모 기본 설정

* FEAT : gitignore 추가

* FEAT : 센서 데이터 api

* FEAT : build.gradle 수정

* Feat/#9 혼잡도 조회

* FEAT : 다이나모 기본 설정

* FEAT : gitignore 추가

* FEAT : 센서 데이터 api

* FEAT : build.gradle 수정

* FEAT : 혼잡도 조회 구현

* FIX : 혼잡도 조회 수정

* FIX : 예상 대기 시간 수정
  • Loading branch information
gourderased authored Jun 19, 2024
1 parent c018a0e commit 0a59576
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 20 deletions.
2 changes: 1 addition & 1 deletion config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.cloudcomputing.ohhanahana.dto.response.SensorDataResponse;
import com.cloudcomputing.ohhanahana.service.SensorDataService;
import jakarta.xml.bind.JAXBException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -15,8 +16,8 @@ public class SensorDataController {

private final SensorDataService sensorDataService;

@GetMapping
public ResponseEntity<SensorDataResponse> findSensorData() {
return ResponseEntity.ok(sensorDataService.findSensorData());
@GetMapping("/")
public ResponseEntity<Object> findTest() throws JAXBException {
return ResponseEntity.ok(sensorDataService.findLatestSensorData());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class SensorDataResponse {

private String currentLocation;
private String currentDateTime;
private String congestion;
private int expectedWaitingTime;
private int expectedWaitingPeople;
private Congestion congestion;
private String expectedWaitingTime;
private String expectedWaitingPeople;

public static SensorDataResponse toDTO(String currentLocation, String currentDateTime,
String congestion,
int expectedWaitingTime, int expectedWaitingPeople) {
Congestion congestion,
String expectedWaitingTime, String expectedWaitingPeople) {

return SensorDataResponse.builder()
.currentLocation(currentLocation)
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/cloudcomputing/ohhanahana/entity/SensorData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cloudcomputing.ohhanahana.entity;


import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@DynamoDBTable(tableName = "inha-team-project-03-dynamodb")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SensorData {
@DynamoDBHashKey(attributeName = "DeviceID")
private String deviceId;

@DynamoDBRangeKey(attributeName = "Timestamp")
private Long timestamp;

@DynamoDBAttribute(attributeName = "payload")
private Map<String, AttributeValue> payload;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public enum Congestion {
SPARE(1, "여유"),
NORMAL(2, "일반"),
CONGESTION(3, "혼잡"),
EXTRA(255, "모름")
EXTRA(255, "모름"),

;
SENSOR_INACTIVE(4, "센서 비작동 시간"),
SENSOR_ERROR(5, "센서 오류");
private final int number;
private final String toKorean;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
package com.cloudcomputing.ohhanahana.repository;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression;
import com.cloudcomputing.ohhanahana.dto.response.SensorDataResponse;
import com.cloudcomputing.ohhanahana.entity.SensorData;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class SensorDataRepository {

public void findLatest() {
//TODO - 다이나모 디비에서 최신값 불러오기
private final DynamoDBMapper dynamoDBMapper;

public List<SensorData> findAll() {
return dynamoDBMapper.scan(SensorData.class, new DynamoDBScanExpression());
}

public SensorData findLatestByDeviceId(String deviceId) {
DynamoDBQueryExpression<SensorData> queryExpression = new DynamoDBQueryExpression<SensorData>()
.withHashKeyValues(SensorData.builder().deviceId(deviceId).build())
.withScanIndexForward(true) //오름차순
.withLimit(50); //가장 최신값 하나만 가져오기

List<SensorData> result = dynamoDBMapper.query(SensorData.class, queryExpression);

if(result.isEmpty()) {
return null;
}
else {
return result.get(0);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ public RecommendResponse.Bus get511Bus() throws JAXBException {
return null;
}

public int get511BusRemainMinute() throws JAXBException {
String apiUrl = buildApiUrl(BusStop.INHA_BACK_GATE.getBusStopId());
String response = restTemplate.getForObject(apiUrl, String.class);

if (response != null) {
return parseBusArrivalData(response, BusStop.INHA_BACK_GATE).stream()
.findFirst()
.map(bus -> {
bus.setBusNumber("511");
return (bus.getRemainTime()/60);
})
.orElse(0);
}
return 0;
}

private BusResponse.BusStop getShuttleBusStop(Optional<ShuttleBus> shuttleBus) {
ShuttleBus bus = ShuttleBus.SHUTTLE_NONE;
int secondsUntilBus = -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package com.cloudcomputing.ohhanahana.service;

import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.cloudcomputing.ohhanahana.dto.response.SensorDataResponse;
import com.cloudcomputing.ohhanahana.entity.SensorData;
import com.cloudcomputing.ohhanahana.enums.Congestion;
import com.cloudcomputing.ohhanahana.repository.SensorDataRepository;
import jakarta.xml.bind.JAXBException;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -11,16 +18,88 @@
public class SensorDataService {

private final SensorDataRepository sensorDataRepository;
private final BusService busService;

public SensorDataResponse findSensorData() {
sensorDataRepository.findLatest();
public SensorDataResponse findLatestSensorData() throws JAXBException {
Congestion congestion = null;

String congestion = Congestion.NORMAL.getToKorean();
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm a");

return SensorDataResponse.toDTO("인하대학교",
"2024-05-31 16:30 PM",
//시간 체크(10 : 00 ~ 20 : 00)
LocalTime currentTime = LocalTime.now();
LocalTime startTime = LocalTime.of(10,0);
LocalTime endTime = LocalTime.of(22,0);

if(currentTime.isBefore(startTime) || currentTime.isAfter(endTime)) {
congestion = Congestion.SENSOR_INACTIVE;
}

else {
SensorData sensorData1 = sensorDataRepository.findLatestByDeviceId("dev_1");
SensorData sensorData2 = sensorDataRepository.findLatestByDeviceId("dev_2");

if(sensorData1 != null && sensorData2 != null) {
Map<String, AttributeValue> payload1 = sensorData1.getPayload();
Map<String, AttributeValue> payload2 = sensorData2.getPayload();

int sensor1Value = Integer.parseInt(payload1.get("Sensor1").getN());
int sensor2Value = Integer.parseInt(payload1.get("Sensor2").getN());
int sensor3Value = Integer.parseInt(payload2.get("Sensor3").getN());
int sensor4Value = Integer.parseInt(payload2.get("Sensor4").getN());

congestion = determineCongestion(sensor1Value, sensor2Value, sensor3Value, sensor4Value);

}
else {
congestion = Congestion.SENSOR_ERROR;
}
}

return SensorDataResponse.toDTO(
"인하대학교",
now.format(formatter),
congestion,
25,
20);
calculateWaitingTime(congestion),
calculatePeople(congestion)
);
}

private Congestion determineCongestion(int sensor1Value, int sensor2Value, int sensor3Value, int sensor4Value) {
if (isWithinRange(sensor1Value) && isWithinRange(sensor2Value)) {
if (isWithinRange(sensor3Value) && isWithinRange(sensor4Value)) {
return Congestion.CONGESTION;
} else {
return Congestion.NORMAL;
}
} else {
return Congestion.SPARE;
}
}

private String calculateWaitingTime(Congestion congestion) throws JAXBException {
int remain511Minute = busService.get511BusRemainMinute();

return switch (congestion) {
case SPARE -> remain511Minute + "분";
case NORMAL -> remain511Minute + 5 + "분";
case CONGESTION -> remain511Minute + 10 + "분";
default -> "-";
};
}

private String calculatePeople(Congestion congestion) {
return switch (congestion) {
case SPARE -> "15명";
case NORMAL -> "25명";
case CONGESTION -> "35명";
default -> "-";
};
}

private boolean isWithinRange(int value) {
int SENSOR_MAX_VALUE = 150;
int SENSOR_MIN_VALUE = 1;
return value >= SENSOR_MIN_VALUE && value <= SENSOR_MAX_VALUE;
}
}

0 comments on commit 0a59576

Please sign in to comment.