Skip to content

Instantly share code, notes, and snippets.

@dbousamra
Created December 21, 2015 22:09
Show Gist options
  • Select an option

  • Save dbousamra/475ee9f256c2b71154de to your computer and use it in GitHub Desktop.

Select an option

Save dbousamra/475ee9f256c2b71154de to your computer and use it in GitHub Desktop.
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))
}
}
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