Created
December 21, 2015 22:09
-
-
Save dbousamra/475ee9f256c2b71154de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def getDeviceStatus(deviceId: DeviceId): Task[DeviceServiceError \/ DeviceState] = { | |
| websockets.publishWithResponse[StatusResponseMessage](DeviceMessage(deviceId, StatusUpdateRequestMessage)) | |
| .map(_.leftMap[DeviceServiceError](WsError.apply)) | |
| .flatMap { | |
| case \/-(res) => | |
| val state = DeviceState(DeviceStateId.random, deviceId, res.cameras.map(CameraStatus.fromCameraStatusDto), DeviceStatus.fromDeviceStatusDto(res.device), Instant.now()) | |
| databaseDeviceService.updateDeviceState(deviceId, state) | |
| case -\/(err) => Task.apply(-\/(err)) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def publishWithResponse[A <: DeviceResponsePayload : DecodeJson](deviceMessage: DeviceMessage): Task[\/[WebSocketServiceError, A]] = { | |
| val handleSessionNotFound: Task[ WebSocketServiceError \/ ResponseMessage] = server.publishWithResponse(deviceMessage) | |
| .map(_.toRightDisjunction(SessionNotFoundWebSocketError(deviceMessage.deviceId))) | |
| val liftedErrors = handleSessionNotFound.timed(timeout).attempt.map { x => | |
| x.leftMap[WebSocketServiceError](r => liftError(r, deviceMessage.deviceId)) | |
| }.map(_.flatMap(identity)) | |
| liftedErrors.map { y => | |
| y.flatMap { x => | |
| x.payload.as[A].toDisjunction.leftMap[WebSocketServiceError] { | |
| case (message, history) => UnableToParseResponsePayloadError(deviceMessage.deviceId, message) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment