Skip to content

Commit

Permalink
[SERVER] notify BookingFinish to QrScanner on scan successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
Huynh Thanh Binh committed Aug 8, 2020
1 parent 8eed5d8 commit 6505c30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.logging.log4j.Level;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.WebSocketSession;

import com.bht.saigonparking.api.grpc.booking.BookingServiceGrpc.BookingServiceStub;
Expand All @@ -26,6 +27,7 @@
import io.grpc.Context;
import io.grpc.stub.StreamObserver;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;

/**
*
Expand Down Expand Up @@ -99,11 +101,9 @@ private void finishBooking(@NotNull SaigonParkingMessage message,
@NotNull WebSocketSession session) throws InvalidProtocolBufferException {

BookingFinishContent bookingFinishContent = BookingFinishContent.parseFrom(message.getContent());

String userRole = webSocketUserSessionManagement.getUserRoleFromSession(session);

if ("PARKING_LOT_EMPLOYEE".equals(userRole)) {

FinishBookingRequest request = FinishBookingRequest.newBuilder()
.setBookingId(bookingFinishContent.getBookingId())
.build();
Expand All @@ -113,7 +113,7 @@ private void finishBooking(@NotNull SaigonParkingMessage message,
context.run(() -> bookingServiceStub.finishBooking(request, new StreamObserver<FinishBookingResponse>() {
@Override
public void onNext(FinishBookingResponse response) {
notifyBookingFinish(response.getBookingId(), response.getCustomerId(), response.getParkingLotId());
notifyBookingFinish(session, response.getBookingId(), response.getCustomerId(), response.getParkingLotId());
}

@Override
Expand All @@ -132,12 +132,17 @@ public void onCompleted() {
}
}

private void notifyBookingFinish(@NotEmpty String uuidString, @NotNull Long customerId, @NotNull Long parkingLotId) {
@SneakyThrows
private void notifyBookingFinish(@NotNull WebSocketSession session,
@NotEmpty String bookingUuid,
@NotNull Long customerId,
@NotNull Long parkingLotId) {

SaigonParkingMessage.Builder saigonParkingMessageBuilder = SaigonParkingMessage.newBuilder()
.setClassification(SaigonParkingMessage.Classification.SYSTEM_MESSAGE)
.setType(SaigonParkingMessage.Type.BOOKING_FINISH)
.setSenderId(0)
.setContent(BookingFinishContent.newBuilder().setBookingId(uuidString).build().toByteString());
.setContent(BookingFinishContent.newBuilder().setBookingId(bookingUuid).build().toByteString());

/* notify customer that booking has been finished */
SaigonParkingMessage toCustomerMessage = saigonParkingMessageBuilder.setReceiverId(customerId).build();
Expand All @@ -146,5 +151,10 @@ private void notifyBookingFinish(@NotEmpty String uuidString, @NotNull Long cust
/* notify parking-lot (another concurrent account) that booking has been finished */
SaigonParkingMessage toParkingLotMessage = saigonParkingMessageBuilder.setReceiverId(parkingLotId).build();
messagingService.forwardMessageToParkingLot(toParkingLotMessage);

/* if current session is auxiliary, then notify to current session that task has been done successfully */
if (webSocketUserSessionManagement.getUserAuxiliaryFromSession(session)) {
session.sendMessage(new BinaryMessage(toParkingLotMessage.toByteArray()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.bht.saigonparking.service.contact.service.IntermediateService;
import com.bht.saigonparking.service.contact.service.MessagingService;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

/**
Expand Down Expand Up @@ -70,7 +71,7 @@ public void publishMessageToQueue(@NotNull SaigonParkingMessage saigonParkingMes
public void consumeMessageFromQueue(@NotNull SaigonParkingMessage saigonParkingMessage, @NotNull Long receiverUserId) {
Set<WebSocketSession> userSessionSet = webSocketUserSessionManagement.getAllSessionOfUser(receiverUserId);
if (userSessionSet != null) {
userSessionSet.forEach(userSession -> {
userSessionSet.stream().filter(this::isSessionConsumeMessageFromQueue).forEach(userSession -> {
try {
userSession.sendMessage(new BinaryMessage(saigonParkingMessage.toByteArray()));

Expand Down Expand Up @@ -127,4 +128,8 @@ private void preProcessingMessage(@NotNull SaigonParkingMessage.Builder delegate
break;
}
}

private boolean isSessionConsumeMessageFromQueue(@NonNull WebSocketSession session) {
return !webSocketUserSessionManagement.getUserAuxiliaryFromSession(session);
}
}

0 comments on commit 6505c30

Please sign in to comment.