|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Repositories\SlDataRepository; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Sl\SlWrapper; |
| 8 | +use Trafiklab\Common\Model\Exceptions\InvalidKeyException; |
| 9 | +use Trafiklab\Common\Model\Exceptions\InvalidRequestException; |
| 10 | +use Trafiklab\Common\Model\Exceptions\InvalidStoplocationException; |
| 11 | +use Trafiklab\Common\Model\Exceptions\KeyRequiredException; |
| 12 | +use Trafiklab\Common\Model\Exceptions\QuotaExceededException; |
| 13 | +use Trafiklab\Common\Model\Exceptions\RequestTimedOutException; |
| 14 | +use Trafiklab\Common\Model\Exceptions\ServiceUnavailableException; |
| 15 | +use Trafiklab\Sl\Model\SlTimeTableRequest; |
| 16 | + |
| 17 | +class NextDepartureController extends GoogleHomeController |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Create a new controller instance. |
| 21 | + * |
| 22 | + * @return void |
| 23 | + */ |
| 24 | + public function __construct(Request $request) |
| 25 | + { |
| 26 | + parent::__construct(json_decode($request->getContent(), true)); |
| 27 | + } |
| 28 | + |
| 29 | + public function getNextDeparture() |
| 30 | + { |
| 31 | + $locationName = $this->getDialogFlowPayload()->getParameter('location'); |
| 32 | + $slData = new SlDataRepository(); |
| 33 | + $slData->getStationId($locationName); |
| 34 | + |
| 35 | + $timeTableRequest = new SlTimeTableRequest(); |
| 36 | + |
| 37 | + /** |
| 38 | + * @var $slWrapper SlWrapper |
| 39 | + */ |
| 40 | + $slWrapper = app(SlWrapper::class); |
| 41 | + try { |
| 42 | + $response = $slWrapper->getTimeTable($timeTableRequest); |
| 43 | + // Create a response |
| 44 | + $this->respondWithTextToSpeech("The next {$response->getTimetable()[0]->getTransportType()} |
| 45 | + from {$response->getTimetable()[0]->getStopName()} is |
| 46 | + {$response->getTimetable()[0]->getLineNumber()} {$response->getTimetable()[0]->getLineName()} |
| 47 | + at {$response->getTimetable()[0]->getScheduledStopTime()->format("H:i")}" |
| 48 | + ); |
| 49 | + } catch (InvalidKeyException $e) { |
| 50 | + $this->respondWithTextToSpeech("I would like to answer you, but I don't have the right keys"); |
| 51 | + } catch (InvalidStoplocationException $e) { |
| 52 | + $this->respondWithTextToSpeech("I would like to answer you, but don't know that station"); |
| 53 | + } catch (KeyRequiredException $e) { |
| 54 | + $this->respondWithTextToSpeech("I would like to answer you, but I don't have the right keys"); |
| 55 | + } catch (InvalidRequestException $e) { |
| 56 | + $this->respondWithTextToSpeech("I would like to answer you, but I didn't get all the details"); |
| 57 | + } catch (QuotaExceededException $e) { |
| 58 | + $this->respondWithTextToSpeech("I would like to answer you, but I already talked too much"); |
| 59 | + } catch (RequestTimedOutException $e) { |
| 60 | + $this->respondWithTextToSpeech("I could not obtain this data, it took too long"); |
| 61 | + } catch (ServiceUnavailableException $e) { |
| 62 | + $this->respondWithTextToSpeech("I could not obtain this data, the service is not available"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +} |
0 commit comments