1 | #!/usr/bin/pythonw
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 |
|
---|
4 | from AppKit import NSApplication, NSBeep, NSSystemDefined, NSURL, NSWorkspace
|
---|
5 | from Foundation import NSDistributedNotificationCenter, NSLog
|
---|
6 | from PyObjCTools import AppHelper
|
---|
7 | from Carbon.CarbonEvt import RegisterEventHotKey, GetApplicationEventTarget
|
---|
8 | from SystemConfiguration import *
|
---|
9 | from appscript import app
|
---|
10 | import re
|
---|
11 | import sets
|
---|
12 |
|
---|
13 | import action
|
---|
14 |
|
---|
15 | GROWL_APP_NAME = 'LocationDo'
|
---|
16 | NOTIFICATION_LOCATION = 'Location'
|
---|
17 | NOTIFICATION_ARRIVE = 'Arrive'
|
---|
18 | NOTIFICATION_DEPART = 'Depart'
|
---|
19 | NOTIFICATION_CURRENT = 'Current location'
|
---|
20 | NOTIFICATION_UNKNOWN = 'Unknown location'
|
---|
21 | NOTIFICATION_READY = 'Ready'
|
---|
22 | NOTIFICATIONS_ALL = [NOTIFICATION_LOCATION, NOTIFICATION_ARRIVE,
|
---|
23 | NOTIFICATION_DEPART, NOTIFICATION_CURRENT,
|
---|
24 | NOTIFICATION_UNKNOWN, NOTIFICATION_READY]
|
---|
25 |
|
---|
26 | DEBUG = True
|
---|
27 |
|
---|
28 | kEventHotKeyPressedSubtype = 6
|
---|
29 | kEventHotKeyReleasedSubtype = 9
|
---|
30 |
|
---|
31 | RE_AIRPORT_STATUS = '/'.join([kSCDynamicStoreDomainState, kSCCompNetwork, kSCCompInterface, kSCCompAnyRegex, kSCEntNetAirPort])
|
---|
32 |
|
---|
33 | # XXX switch to Cocoa interface for Growl
|
---|
34 | growl = app('GrowlHelperApp')
|
---|
35 |
|
---|
36 | growl.register(
|
---|
37 | as_application=GROWL_APP_NAME,
|
---|
38 | all_notifications=NOTIFICATIONS_ALL,
|
---|
39 | default_notifications=NOTIFICATIONS_ALL)
|
---|
40 |
|
---|
41 | def growlNotify(name, title, description, **kw):
|
---|
42 | params = dict(with_name=name,
|
---|
43 | title=title,
|
---|
44 | description=unicode(description),
|
---|
45 | application_name=GROWL_APP_NAME,
|
---|
46 | image_from_location='/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericNetworkIcon.icns')
|
---|
47 | params.update(kw)
|
---|
48 | if not params.get('sticky', False):
|
---|
49 | params['identifier'] = name
|
---|
50 | if DEBUG:
|
---|
51 | NSLog("%s: %s" % (title, description))
|
---|
52 | growl.notify(**params)
|
---|
53 |
|
---|
54 | class Location(sets.Set):
|
---|
55 | def __str__(self):
|
---|
56 | if not self: return '(unknown)'
|
---|
57 | return ', '.join(self)
|
---|
58 |
|
---|
59 | currentLocation = Location()
|
---|
60 |
|
---|
61 | # XXX attach actions to location tag; something like:
|
---|
62 | # locations['Siebel'].onArrive.ensureKerberosPrincipalsValid(...)
|
---|
63 |
|
---|
64 | def arriveAtLocation(location):
|
---|
65 | growlNotify(NOTIFICATION_ARRIVE, 'Arriving at location', location)
|
---|
66 | if 'Siebel' in location:
|
---|
67 | action.ensureKerberosPrincipalsValid(['njriley@ACM.UIUC.EDU',
|
---|
68 | 'njriley@AD.UIUC.EDU'])
|
---|
69 | action.setVolumePercent(0.15)
|
---|
70 | if '1115 SC' in location:
|
---|
71 | action.openInBackground('~/Documents/CS 423/CS 423 to do.oo3')
|
---|
72 | action.setDisplayBrightnessPercent(0.2)
|
---|
73 | action.setAdiumStatus('Working in 1115 SC')
|
---|
74 | if '1404 SC' in location:
|
---|
75 | action.setDisplayBrightnessPercent(0.4)
|
---|
76 | action.setVolumePercent(0)
|
---|
77 | action.setAdiumStatus('In 1404 SC')
|
---|
78 | if '4111 SC' in location:
|
---|
79 | action.openInBackground('~/Documents/Research/Research progress.oo3')
|
---|
80 | # XXX complain that NX has no creator nor bundle ID
|
---|
81 | action.openApplicationWithName('NX Client for OSX')
|
---|
82 | # XXX make these changes dependent on screen resolution
|
---|
83 | action.moveWindow('iTunes', 'iTunes', (2123, 1534), ifSizeMatches=(129, 65))
|
---|
84 | action.moveWindow('Adium', 'Contacts', (1280, 1553))
|
---|
85 | action.moveWindow('Adium', 'AOL System Msg', (1448, 1320))
|
---|
86 | # XXX need better way to do this
|
---|
87 | action.moveWindow('OmniOutliner Pro', 'Research progress', (1634, 847))
|
---|
88 | action.moveWindow('OmniOutliner Pro', '◇ Research progress', (1634, 847))
|
---|
89 | # XXX wait for yt to be accessible and Kerberos, then open the terminal window
|
---|
90 | # action.terminalDo('kswitch -p njriley@ACM.UIUC.EDU;(sleep 10;kswitch -p njriley@AD.UIUC.EDU)&! ;clear;ssh yt')
|
---|
91 | action.setDisplayBrightnessPercent(0.9)
|
---|
92 | action.setAdiumStatus('Working in 4111 SC')
|
---|
93 | if 'ACM office' in location:
|
---|
94 | action.setDisplayBrightnessPercent(0.2)
|
---|
95 | action.setVolumePercent(0.5)
|
---|
96 | action.setAdiumStatus('At ACM')
|
---|
97 | if 'Champaign' in location:
|
---|
98 | action.ensureKerberosPrincipalsValid(['njriley@ACM.UIUC.EDU',
|
---|
99 | 'njriley@AD.UIUC.EDU'])
|
---|
100 | action.openInBackground('~/Documents/To do.oo3')
|
---|
101 | action.setDisplayBrightnessPercent(0.2)
|
---|
102 | action.setVolumePercent(0.5)
|
---|
103 | action.setAdiumStatus('At home')
|
---|
104 | if '13[12]0 DCL' in location:
|
---|
105 | action.setDisplayBrightnessPercent(0.2)
|
---|
106 | action.setVolumePercent(0)
|
---|
107 | action.setAdiumStatus('In 13[12]0 DCL')
|
---|
108 | if 'UIUCnet' in location:
|
---|
109 | action.startVPNC('UIUCnet-oncampus')
|
---|
110 | if 'CSL' in location:
|
---|
111 | action.setDisplayBrightnessPercent(0.1)
|
---|
112 | action.setVolumePercent(0)
|
---|
113 | action.setAdiumStatus('At CSL')
|
---|
114 | action.openURL('http://wireless.csl.uiuc.edu/authenticate.html')
|
---|
115 | growlNotify(NOTIFICATION_ARRIVE, 'Arrived at location',
|
---|
116 | location)
|
---|
117 |
|
---|
118 | def departLocation(location):
|
---|
119 | growlNotify(NOTIFICATION_DEPART, 'Departing location', location)
|
---|
120 | if 'UIUCnet' in location:
|
---|
121 | # XXX should handle this more symmetrically
|
---|
122 | action.stopVPNC()
|
---|
123 | growlNotify(NOTIFICATION_DEPART, 'Departed location', location)
|
---|
124 |
|
---|
125 | def locationChanged(location):
|
---|
126 | if not location: return # unknown
|
---|
127 | global currentLocation
|
---|
128 | oldLocation = currentLocation - location
|
---|
129 | newLocation = location - currentLocation
|
---|
130 | if DEBUG:
|
---|
131 | NSLog("new %s | was %s | departing %s | arriving %s" % \
|
---|
132 | (location, currentLocation, oldLocation, newLocation))
|
---|
133 | if oldLocation:
|
---|
134 | departLocation(oldLocation)
|
---|
135 | currentLocation = location
|
---|
136 | if newLocation:
|
---|
137 | currentLocation = location
|
---|
138 | arriveAtLocation(newLocation)
|
---|
139 |
|
---|
140 | def formatMACAddress(data):
|
---|
141 | return ':'.join(re.findall('..', data.bytes()[:].encode('hex')))
|
---|
142 |
|
---|
143 | # XXX watch battery status, too, for display brightness
|
---|
144 | # XXX look at display configuration to tell difference between 4111 and 4124
|
---|
145 | # XXX need concept of ambiguity, ask user which location they're in (ACM/1115)
|
---|
146 |
|
---|
147 | airPortStatus = {'BSSID': None, 'SSID': None}
|
---|
148 |
|
---|
149 | class SCWatcher(NSObject):
|
---|
150 | def keysChanged_inDynamicStore_(self, changed, store):
|
---|
151 | location = Location()
|
---|
152 | for key in changed:
|
---|
153 | if re.match(RE_AIRPORT_STATUS, key):
|
---|
154 | st = store.valueForKey_(key)
|
---|
155 | bssid = formatMACAddress(st['BSSID'])
|
---|
156 | if airPortStatus['BSSID'] != bssid:
|
---|
157 | airPortStatus['BSSID'] = bssid
|
---|
158 | if bssid in ['00:0e:83:05:77:22', '00:0e:83:05:77:20',
|
---|
159 | '00:0e:83:05:75:d2']:
|
---|
160 | location.add('4111 SC')
|
---|
161 | elif bssid in ['00:0e:83:05:75:a2', '00:0e:83:05:75:a0']:
|
---|
162 | location.add('1404 SC')
|
---|
163 | elif bssid in ['00:0e:83:05:76:82', '00:0e:83:05:76:80']:
|
---|
164 | location.add('ACM office')
|
---|
165 | elif bssid == '00:11:24:0f:23:69':
|
---|
166 | location.add('Champaign')
|
---|
167 | elif bssid == '00:13:c4:ce:66:a0':
|
---|
168 | location.add('13[12]0 DCL')
|
---|
169 | elif bssid in ['00:0e:83:05:78:02', '00:0e:83:05:77:42']:
|
---|
170 | location.add('1304 SC')
|
---|
171 | elif bssid == '00:0e:83:05:78:a2':
|
---|
172 | location.add('3124 SC')
|
---|
173 | elif bssid == '00:0e:83:05:76:b2':
|
---|
174 | location.add('Siebel 2nd floor atrium')
|
---|
175 | elif bssid in ['00:14:1c:ad:5c:80', '00:14:1c:ad:64:50']:
|
---|
176 | location.add('MEB')
|
---|
177 | elif bssid == '00:02:2d:2c:cb:34':
|
---|
178 | location.add('B02 CSL')
|
---|
179 | elif bssid == '00:11:5c:fe:31:00':
|
---|
180 | location.add('CAB')
|
---|
181 | elif bssid == '00:13:c4:78:e1:a0':
|
---|
182 | location.add('Engineering Hall')
|
---|
183 | elif bssid in ['00:0c:30:d4:b7:2d', '00:0c:30:d4:b6:70']:
|
---|
184 | location.add('Illini Union')
|
---|
185 | elif bssid == '00:0d:93:ed:e0:bc':
|
---|
186 | location.add('340 Marlborough St.')
|
---|
187 | elif bssid == '00:40:96:5a:5e:0f':
|
---|
188 | location.add('BMI baggage claim')
|
---|
189 | elif bssid == '00:12:0e:10:1f:83':
|
---|
190 | location.add('Waterville Valley')
|
---|
191 | elif bssid == '44:44:44:44:44:44':
|
---|
192 | location.add('No AirPort network')
|
---|
193 | else:
|
---|
194 | growlNotify(NOTIFICATION_UNKNOWN, 'Unknown BSSID', bssid,
|
---|
195 | sticky=True)
|
---|
196 | NSLog('Unknown BSSID %s' % bssid)
|
---|
197 | ssid = st['SSID']
|
---|
198 | if airPortStatus['SSID'] != ssid:
|
---|
199 | airPortStatus['SSID'] = ssid
|
---|
200 | if ssid == '':
|
---|
201 | pass
|
---|
202 | elif ssid == 'dcs-wpa':
|
---|
203 | location.add('Siebel')
|
---|
204 | elif ssid == 'DCSnet':
|
---|
205 | location.add('Siebel')
|
---|
206 | location.add('UIUCnet')
|
---|
207 | elif ssid == 'UIUCnet':
|
---|
208 | location.add('UIUCnet')
|
---|
209 | elif ssid == 'ACME Acres':
|
---|
210 | location.add('Champaign')
|
---|
211 | elif ssid == 'CSL Wireless':
|
---|
212 | location.add('CSL')
|
---|
213 | elif ssid == 'Wackyland':
|
---|
214 | location.add('340 Marlborough St.')
|
---|
215 | elif ssid == '05B408706826':
|
---|
216 | location.add('Waterville Valley')
|
---|
217 | elif ssid == 'CIRA':
|
---|
218 | location.add('BMI')
|
---|
219 | else:
|
---|
220 | growlNotify(NOTIFICATION_UNKNOWN, 'Unknown SSID', ssid,
|
---|
221 | sticky=True)
|
---|
222 | locationChanged(location)
|
---|
223 |
|
---|
224 | class LocationDo(NSApplication):
|
---|
225 |
|
---|
226 | hotKeyRef = None
|
---|
227 |
|
---|
228 | def displayLocation(self):
|
---|
229 | growlNotify(NOTIFICATION_CURRENT, 'Current location', currentLocation)
|
---|
230 |
|
---|
231 | def finishLaunching(self):
|
---|
232 | super(LocationDo, self).finishLaunching()
|
---|
233 | hotKeyRef = RegisterEventHotKey(98, 0, (0, 0),
|
---|
234 | GetApplicationEventTarget(), 0) # F7
|
---|
235 | growlNotify(NOTIFICATION_READY, 'LocationDo ready', '')
|
---|
236 |
|
---|
237 | def sendEvent_(self, theEvent):
|
---|
238 | if theEvent.type() == NSSystemDefined and \
|
---|
239 | theEvent.subtype() == kEventHotKeyPressedSubtype:
|
---|
240 | self.displayLocation()
|
---|
241 | super(LocationDo, self).sendEvent_(theEvent)
|
---|
242 |
|
---|
243 | store = None
|
---|
244 |
|
---|
245 | if __name__ == "__main__":
|
---|
246 | store = UNSystemConfigurationDynamicStore.alloc().initWithName_('LocationDo')
|
---|
247 | watcher = SCWatcher.alloc().init()
|
---|
248 | store.setDelegate_(watcher)
|
---|
249 | store.notifyValuesForKeys_matchingPatterns_(None, [RE_AIRPORT_STATUS])
|
---|
250 | store.addToCurrentRunLoop()
|
---|
251 | AppHelper.runEventLoop()
|
---|