forked from richhowley/mbta-bus-tracking-skill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
957 lines (663 loc) · 29.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
from adapt.intent import IntentBuilder
from mycroft import MycroftSkill, intent_file_handler
from mycroft.util.parse import match_one
from mycroft.util.parse import fuzzy_match
from mycroft.util.log import getLogger
import mycroft.util
from mycroft.audio import wait_while_speaking
from mycroft import intent_handler
import requests
import datetime
from pytz import timezone
import pickle
import re
import copy
LOGGER = getLogger(__name__)
class MBTA():
################ API DISCLAIMER ################
#
# The Massachusetts Department of Transportation (MassDOT)
# is the provider of MBTA Data.
# The author is not nn agent or partner of
# MassDOT or its agencies and authorities
def __init__(self, apiKey, trackCount):
self.routeInfo = None; # dictionary with info on all bus routes
# error flag valid after API is called
self.serverError = False
# save API key
self.apiKey = apiKey;
self.currentRoute = None # entry from routInfo for selected route
self.currentDirections = None # direction options for current route - list of tuples
self.currentDirection = "" # used in queries, index into current route directions as string
self.stopId = "" # internal ID of selected bus stop, used to get predicitons
self.stopName ="" # text name of bus stop
self.busStops = dict() # dictionary of stop names, ids in slected direction
self.predTimes = dict() # dictionary of trip ids, predictated arrival times
self.maxTrackCnt = int(trackCount) # max # of busses to track
self.lastTrack = "" # last trip to track - stop when no longer in predictions
# settings have been changed on Home
def updateSettings(self, apiKey, trackCount):
self.apiKey = apiKey
self.maxTrackCnt = int(trackCount) # max # of busses to track
# reset class, call when stopping tracking
def reset(self):
self.currentRoute = None
self.currentDirection = ""
self.stopId = ""
# get data from MBTA API at given endpoint with passed arguments
def _getData(self,endPoint, args=None):
retVal = None;
# clear error flag
self.serverError = False
# base url
api_url = "https://api-v3.mbta.com/{}".format(endPoint)
# if we are using an api key and have args
if self.apiKey != None and args != None:
# url?key%args
api_url = "{}?api_key={}&{}".format(api_url,self.apiKey,args)
elif self.apiKey != None:
# url?key
api_url = "{}?api_key={}".format(api_url,self.apiKey)
elif args != None :
# url?args
api_url = "{}?{}".format(api_url,args)
try:
# get requested data
r = requests.get(api_url)
# check if we got any data before setting return value
retVal = r.json()['data'] if len(r.json()['data']) > 0 else None
except:
# set error flag
self.serverError = True
return retVal
# API calls die silently, return true if last call
# resulted in an error
def callError(self):
return self.serverError
# information on all MBTA bus routes is read from server
# not all information is relevant to skill, we build a
# dictionary with the info we need
# this API call is necessary before getting any predictions
# and will only be done once
def readRoutes(self):
# if route info has not been read yet
if( self.routeInfo == None ):
# create empty dicitonary
self.routeInfo = dict()
# get info on all bus routes - only called once
routes = self._getData('routes',"filter[type]=3&sort=sort_order")
if routes != None:
# build dictionary with the info we need on each route
# key is short name
for rt in routes:
self.routeInfo[rt['attributes']['short_name']] = {
'id': rt['id'], # id
'short_name' : rt['attributes']['short_name'],# short name
'long_name' : rt['attributes']['long_name'], # long name
'dirs' : rt['attributes']['direction_names'], # directions
'dest' : rt['attributes']['direction_destinations'] # terminus
}
# set current route based on passed name
# return route name or None
def setRoute(self, routeName):
self.currentRoute = None;
# make certain routes are loaded
self.readRoutes()
# look in the dictionary
rt = self.routeInfo.get(routeName)
# if we got a valid route
if rt != None:
self.currentRoute = self.routeInfo[routeName]
return(None if not rt else self.currentRoute['short_name'])
# return object that can be saved to settings
# to remember current route and direction
def getRouteSettings(self):
# current route basic info
currRoute = self.currentRoute
# add current direction and stop id
currRoute["direction"] = self.currentDirection
currRoute["stopid"] = self.stopId
currRoute["stopName"] = self.stopName
return(currRoute)
# restore a root saved in settings
# return route name
def restoreRoute(self, rt):
# current direction, stop id and stop name are not in route dict
self.currentDirection = rt.pop('direction', None)
self.stopId = rt.pop('stopid', None);
self.stopName = rt.pop('stopName')
# now route is reduced to proper contents of current route
self.currentRoute = rt
# fill current directions array
self.getDirections()
return self.currentRoute['short_name'];
# getters for info on restored route
def getStopName(self):
return self.stopName
def getDirDest(self):
return(self.currentDirections[self.currentDirection])
# return list of directions and destinations
# each element in list is direction, destination tuple
# directions are usually Inbound and Outbound
def getDirections(self):
self.currentDirections = []
# append tuple for each direction
for idx, d in enumerate(self.currentRoute["dirs"]):
self.currentDirections.append((self.currentRoute["dirs"][idx],self.currentRoute["dest"][idx]))
return(self.currentDirections)
# pass string for direction - could be inbound, outboud or terminus
# a tuple of (direction name, destination name) for best match is returned
def setDirection(self, str):
# build list of directions and destinations as strings
dirList = [' '.join(x) for x in self.currentDirections]
# select one destinaiton
dirKey, dirConfidence = match_one(str, dirList)
# set direction id to index of selected destination
self.currentDirection = dirList.index(dirKey)
return(self.currentDirections[self.currentDirection])
# format stop name to (hopefully) match spoken version
def formatStopName(self, str):
# Street for St
str = re.sub(r'St ', 'Street ', str)
# opposite for opp
str = re.sub(r'opp ', 'opposite ', str)
return(str)
# build dictionary of bus stops for current route in selected direction
# key is name, to compare with utterance
# value is id, to use for API calls
def getStops(self):
# ask API for stops
routeStops = self._getData('stops',
"filter[direction_id]={}&filter[route]={}"
.format(self.currentDirection, self.currentRoute["id"]))
# empty dictionary
self.busStops = dict()
# create entry in dictionary for each bus stop
for stop in routeStops:
stopKey = self.formatStopName(stop['attributes']['name'])
stopKey = stopKey.lower()
self.busStops[stopKey] = stop['id']
# a bus stop name is passed, match to stop on route and set stop id
# return name found
def setStop(self, stopName):
# build dictionay of stops if necessary
self.getStops()
# find closest match on route
theStop = key, confidence = match_one(stopName, list(self.busStops))
self.stopId = self.busStops.get(key);
# record stop name
self.stopName = theStop[0]
return(self.stopName)
# get arrival predictions for current route in the selected direcation at the chosen stop
# return (possibly empty) list of arrival time, trip id tuples
def getPredictions(self):
predList = []
# ask API for predictions
predictions = self._getData('predictions',
"filter[direction_id]={}&filter[route]={}&filter[stop]={}"
.format(self.currentDirection,self.currentRoute["id"],self.stopId))
# if we got valid predictions
if predictions != None:
# build list of arrival time, trip id tuples
predList = list(map(lambda x: (x['attributes']['arrival_time'],
x['relationships']['trip']['data']['id']), predictions))
# remove tuples where arrival time is none
predList = [p for p in predList if p[0] != None ]
return(predList)
# return arrival predictions as list
# predictions are datatime objects with time zone
def getArrivals(self):
# get predictions
self.predTimes = self.getPredictions()
# only return prediction times
return(None if len(self.predTimes) == 0 else [x[0] for x in self.predTimes])
# begin tracking busses, return list of arrival predictions
def startTracking(self):
# get predictions
self.predTimes = self.getPredictions()
# if we got any predictions
if len(self.predTimes) > 0:
# record last trip we will track
self.lastTrack = self.predTimes[min(len(self.predTimes),self.maxTrackCnt)-1][1]
# only return prediction times
# predictions are datatime objects with time zone
return(None if len(self.predTimes) == 0 else [x[0] for x in self.predTimes][:self.maxTrackCnt])
# call periodically for current predictions of bus being tracked
# startTracking must be called first to record the last trip tracked
# return list of arrival predictions
def updateTracking(self):
idx = -1 # assume we won't find the last tracked bus
# get predictions
self.predTimes = self.getPredictions()
if self.predTimes != None:
# get index of last tracked bus
try:
# build list with trip id of last tracked bus then find its index in prediction list
idx = self.predTimes.index([x for x in self.predTimes if x[1] == self.lastTrack][0])
except:
self.reset()
# return prediction times up to last tracked bus
# if we got valid predictions and last
# tracked trip is still in list
return(None if idx < 0
else [x[0] for x in self.predTimes][:idx+1])
# call when done tracking or stopped by voice command
def stopTracking(self):
self.reset()
TZ_STR_IDX = len('-05:00') * (-1) # time zone string, used to strip tz from api dates
ROUTE_FILE = 'savedroutes' # file for saving route information
class MbtaBusTracking(MycroftSkill):
def __init__(self):
MycroftSkill.__init__(self)
super(MbtaBusTracking, self).__init__(name="MbtaBusTracking")
self.apiKey = None
# using api key?
self.useownkey = self.settings.get('useownkey')
# yes, read it from settings
if self.useownkey:
self.apiKey = self.settings.get('api_key')
# create MBTA object to handle api calls
self.t = MBTA(self.apiKey,self.settings.get('maxTrack', 3))
self.routeName = None # bus route
self.requestTracking = False # True => last request was for tracking, not arrivals
self.directions = None # direction name, terminus tuple for route
self.stopName = None # bus stop
self.dirName = None # direction of travel
self.destName = None # terminus for direction
self.savedRoutes = dict() # routes save to disk
self.trackingInterval = max(30, (self.settings.get('trackingUpateFreq', 30))) # enforce min tracking updates
# watch for changes on HOME
self.settings.set_changed_callback(self.on_websettings_changed)
def initialize(self):
# try to read saved routes
try:
with self.file_system.open(ROUTE_FILE , 'rb') as f:
self.savedRoutes = pickle.load(f)
# make a vocabulary from saved routes
if self.savedRoutes:
for s in self.savedRoutes:
self.register_vocabulary(s, 'SavedRouteNames')
except:
pass
# handle change of setting on home
def on_websettings_changed(self):
# using api key?
self.useownkey = self.settings.get('useownkey')
# yes, read it from settings
if self.useownkey:
self.apiKey = self.settings.get('api_key')
LOGGER.info('MBTA skill API key set to ' + self.apiKey)
else:
self.apiKey = None
LOGGER.info('MBTA skill not use an API key')
# update MBTA object with new settings
self.t.updateSettings(self.apiKey,self.settings.get('maxTrack', 3))
# get tracking interval
self.trackingInterval = max(30, (self.settings.get('trackingUpateFreq', 30))) # enforce min tracking updates
# speak list of passed arrival times
def announceArrivals(self,eta):
# get current datetime for east coast without timecode
currentTime = datetime.datetime.now((timezone('America/New_York'))).replace(tzinfo=None)
# build datetime objets from strings in predection list
# strip timezone first since we cannot count on fromisoformat() being available (3.7+)
eta = [datetime.datetime.strptime(x[0:TZ_STR_IDX], '%Y-%m-%dT%H:%M:%S') for x in eta]
# calculate waiting times
wt = [x-currentTime for x in eta if x > currentTime]
arrivalCount = len(wt)
# speak prefix if necessary
if arrivalCount > 0:
# add stop to prefix string
prefix = {'stop': self.stopName}
# might be updating arrivals while Mycroft is speaking, so wait
wait_while_speaking()
self.speak_dialog("Bus.Arrival.Prefix", prefix)
for waitTime in wt:
# wait for previous arrival announcement to finish
wait_while_speaking()
# calculate hour and minutes from datetime timedelta seconds
arrival = {
'hour': waitTime.seconds // 3600,
'minutes' : (waitTime.seconds % 3600) // 60
}
# one hour or longer
if (arrival['hour'] > 0):
# round down to one hour if only one minute over
if(arrival['minutes'] < 2 ):
self.speak_dialog("Arriving.Hour", arrival )
else:
# arrives in 1 hour and some minutes
self.speak_dialog("Arriving.Hour.Minutes", arrival )
elif arrival['minutes'] > 1:
# arrives in more than one minute
self.speak_dialog("Arriving.Minutes", arrival )
elif arrival['minutes'] == 1:
# arrives in one minute
self.speak_dialog("Arriving.Minute", arrival )
else:
# arrives in less than a minute
self.speak_dialog("Arriving.Now")
return arrivalCount
# call to stop tracking
def endTracking(self):
# stop updates
self.cancel_scheduled_event('BusTracker')
# tell T object that we are no longer tracking
self.t.stopTracking()
# calllback for tracking updates
def updateTracking(self):
# get predictions
eta = self.t.updateTracking()
# if any arrivals predicted for our stop
if eta != None:
# speak times
self.announceArrivals(eta)
else:
# last tracked bus has passed the stop, end updates
self.endTracking()
# call to start tracking arrivals
# afer route, direction and stop have been set
def startTracking(self):
# get predictions
eta = self.t.startTracking()
# if any arrivals predicted for our stop
if eta != None:
# speak times
self.announceArrivals(eta)
# schedule updates
self.schedule_repeating_event(self.updateTracking,
None,
self.trackingInterval,
name='BusTracker')
else:
# no busses running
stopInfo = {'name' : self.stopName}
self.speak_dialog("No.Busses.Found",stopInfo)
# call when an arrival route, direction and
# stop have been set
def getArrivals(self):
# ask API for arrival times
eta = self.t.getArrivals()
# begin arrival announcments
announcement = {
'route': self.routeName,
'dest': self.destName
}
self.speak_dialog("Service.Announcement",announcement)
# speak arrival times if we got any
if eta != None:
self.announceArrivals(eta)
else:
stopInfo = {'name' : self.stopName}
self.speak_dialog("No.Busses.Found",stopInfo)
# write route to file
def writeRoutes(self):
# open file in /home/pi/.mycroft/skills
with self.file_system.open(ROUTE_FILE , 'wb') as f:
# serialize dictionary
pickle.dump(self.savedRoutes, f, pickle.HIGHEST_PROTOCOL)
# save the current route as a shortcut
def saveRoute(self, name):
# add current route to saved routes dictionary
self.savedRoutes[name] = self.t.getRouteSettings()
# wrtie it to disk
self.writeRoutes()
# add to vocabulary
self.register_vocabulary(name, 'SavedRouteNames')
# remove saved route from file
def removeRoute(self, name):
# if route is in dict, remove and save
if self.savedRoutes.pop(name, None) != None:
self.writeRoutes()
# try to restore route with passed name, return True if successful
def restoreRoute(self, name):
retVal = False
# look up route associated with this name
restoredRoute = self.savedRoutes.get(name)
# if we got one
if restoredRoute != None:
# set up API class with copy of route object
self.routeName = self.t.restoreRoute(copy.deepcopy(restoredRoute))
# set class variables
self.stopName = self.t.getStopName()
self.dirName, self.destName = self.t.getDirDest()
retVal = True
return retVal
# set proper route name based on utterance
# and get directions for route
def setRouteAndDirection(self, routeName):
# Silver Line and Crosstown must be abreviated for API calls
routeName = routeName.replace('crosstown ','CT')
routeName = routeName.replace('silverline ','SL')
# quirks fround in testing
routeName = routeName.replace('to', '2')
routeName = routeName.replace('for', '4')
# convert any alpha characters to uppercase
routeName = routeName.upper()
# tell API which route we are riding
self.routeName = self.t.setRoute(routeName)
# read directions for this route
if self.routeName:
self.directions = self.t.getDirections()
return self.routeName
# prompt for direction and set context
def setDirectionContext(self):
# possible directions have already been set
dirChoices = {
'dir1': self.directions[0][0],
'dest1': self.directions[0][1],
'dir2': self.directions[1][0],
'dest2': self.directions[1][1]
}
# prompt and set context
self.speak_dialog('Which.Direction',dirChoices,expect_response=True)
self.set_context('DirectionNeededContex')
# prompt for direction and set context
def setStopContext(self):
self.speak_dialog('Which.Stop', expect_response=True)
self.set_context('StopNeededContext')
# remove all contexts we may have set
def removeContexts(self):
self.remove_context('RouteNeededContex')
self.remove_context('DirectionNeededContex')
self.remove_context('StopNeededContex')
# process request for arrivals or tracking
def processRequest(self, message, tracking):
# may have set context in previous call
self.removeContexts()
# may already be tracking
self.endTracking()
# init class variables
self.routeName = None
self.dirName = None
self.destName = None
self.stopName = None
self.requestTracking = tracking
# get fields from utterance
routeName = message.data.get("Route.Name", None)
direction = message.data.get("Direction", None)
stop = message.data.get("Stop", None)
# set up for API call
if routeName:
# set route name
routeName = self.setRouteAndDirection(routeName)
# check for error
if self.t.callError() is True:
# server error
self.speak_dialog("Error.Calling.Server")
# clear route name
routeName = None;
# only accept direction if we have a route
if routeName and direction:
# set direction from utterance
self.dirName, self.destName = self.t.setDirection(direction)
#print('Direction set to {}'.format(self.dirName))
# only accept stop name if we have route and direction
if routeName and direction and stop:
self.stopName = self.t.setStop(stop)
#print('Direction set to {} toward {} at {}'.format(self.dirName,self.destName,self.stopName))
# if we got all the info needed,get arrivals
if( self.routeName and self.dirName and self.stopName ):
# good to go - list arrivals or start tracking
if self.requestTracking:
self.startTracking()
else:
self.getArrivals()
elif( self.routeName and self.dirName):
# got route and direction, need stop
self.setStopContext()
#print('got route {} and direction {}'.format(self.routeName, self.dirName))
elif self.routeName:
# got route,need direction
#print('got route {} '.format(self.routeName))
# now we nedd a direction
self.setDirectionContext()
else:
# if we are here it is because no route was in the
# utterance, the route in the utterance was not valid
# or there was an error calling the API
#
# set context to get a valid route as long as there was
# not an error calling the API
#
if self.t.callError() is False:
# need route name
self.speak_dialog('Which.Route',expect_response=True)
self.set_context('RouteNeededContex')
# set route based on context
@intent_handler(IntentBuilder('')
.require('RouteNeededContex')
.optionally('Route').require('Route.Name').build())
def handle_route_context_intent(self, message):
# done with this context
self.remove_context('RouteNeededContex')
# pull route from message
routeName = message.data.get("Route.Name", None)
# set route name and direction
routeName = self.setRouteAndDirection(routeName)
# check for server error
if self.t.callError() is True:
# server error
self.speak_dialog("Error.Calling.Server")
else:
# now we nedd a direction
self.setDirectionContext()
# set direction based on context
@intent_handler(IntentBuilder('')
.require('DirectionNeededContex').build())
def handle_direction_context_intent(self, message):
# done with this context
self.remove_context('DirectionNeededContex')
# set direction from utterance
self.dirName, self.destName = self.t.setDirection(message.data.get('utterance'))
#print('Direction set to {}'.format(self.dirName))
# now we need a stop
self.setStopContext()
# set stop based on context
@intent_handler(IntentBuilder('')
.require('StopNeededContext').build())
def handle_stop_context_intent(self, message):
# done with this context
self.remove_context('StopNeededContext')
# set stop from utterance
self.stopName = self.t.setStop(message.data.get('utterance'))
# good to go - list arrivals or start tracking
if self.requestTracking:
self.startTracking()
else:
self.getArrivals()
# arrivals for bus route
@intent_handler(IntentBuilder('')
.require('T.Bus')
.require('Arrivals')
.optionally('Route')
.optionally('Route.Name')
.optionally('Direction')
.optionally('Stop')
.build())
def handle_arrivals_intent(self, message):
# process arrivals request
self.processRequest(message, False)
# tracking bus route
@intent_handler(IntentBuilder('')
.require('T.Bus')
.require('Tracking')
.optionally('Route')
.optionally('Route.Name')
.optionally('Direction')
.optionally('Stop').build())
def handle_tracking_intent(self, message):
# process tracking request
self.processRequest(message, True)
# save shortcut
@intent_handler(IntentBuilder('')
.require('Save').require('T.Bus').require('Shortcut').build())
def handle_save_route_intent(self, message):
# need full route information to save
if( self.routeName and self.dirName and self.stopName ):
# get name for shortcut
shortcutName = self.get_response("Shortcut.Prompt");
if shortcutName:
# save route under passed name
self.saveRoute(shortcutName)
shortcutInfo = {'shortcut': shortcutName}
self.speak_dialog("Save.Complete", shortcutInfo)
else:
# don't have complete info
self.speak_dialog("Not.Enough.Info")
# remove shortcut
@intent_handler(IntentBuilder('')
.require('Remove').require('T.Bus').optionally('Shortcut').require('SavedRouteNames').build())
def handle_remove_route_intent(self, message):
# extract short cut
shortcutNmae = message.data.get("SavedRouteNames", None)
# remove it
self.removeRoute(shortcutNmae)
shortcutInfo = {'shortcut': shortcutNmae}
self.speak_dialog("Delete.Complete", shortcutInfo)
# list shortcuts
@intent_handler(IntentBuilder('')
.require('List').require('T.Bus').require('Shortcuts').build())
def handle_list_saved_route_intent(self, message):
# build list of rotue names
routeList = [s for s in self.savedRoutes]
if len(routeList) > 0:
# speak the names
routes = {'routes': ' '.join(routeList)}
self.speak_dialog('List.Saved', routes)
else:
self.speak_dialog('No.Saved.Routes')
# tracking for a saved route
@intent_handler(IntentBuilder('')
.require('T.Bus').require('Tracking').optionally('Route').require('SavedRouteNames').build())
def handle_saved_tracking_intent(self, message):
# may already be tracking
self.endTracking()
# restore named route and start tracking
routeName = message.data.get("SavedRouteNames", None)
self.restoreRoute(routeName)
self.startTracking()
# arrivals for a saved route
@intent_handler(IntentBuilder('')
.require('T.Bus').require('Arrivals').optionally('Route').require('SavedRouteNames').build())
def handle_saved_arrivals_intent(self, message):
# pull shortcut name from intent
shortCut = message.data.get("SavedRouteNames", None)
# if shortcut has been deleted it will still be
# in vocablulary until restart
if self.savedRoutes.get(shortCut, None):
# may already be tracking
self.endTracking()
# restore route and list arrivals
self.restoreRoute(shortCut)
self.getArrivals()
# stop tracking
@intent_handler(IntentBuilder('')
.require('T.Bus').require('Shutdown').build())
def handle_shutdown_intent(self, message):
# stop tracking
self.endTracking()
# reset contexts
self.removeContexts()
self.speak_dialog('Shutdown.Message')
def create_skill():
return MbtaBusTracking()