-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
executable file
·52 lines (41 loc) · 1.17 KB
/
main.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
# this program will periodically scan nearby wifi networks and geolocate and print the gps coordinates
import json, os, sys
import geolocation
from time import sleep
# find the directory of the script
dirName = os.path.dirname(os.path.abspath(__file__))
# defines & global vars
DEFAULT_SCAN_INTERVAL = 5
CONFIG_FILE = 'config.json'
config = {}
# read configuration file
def readConfigFile():
global config
filepath = '/'.join([dirName, CONFIG_FILE])
with open( filepath ) as f:
try:
config = json.load(f)
except:
print("ERROR: expecting JSON file")
return False
if 'apiKey' not in config:
print("ERROR: expecting config file to have 'apiKey' member")
return False
if 'scanInterval' not in config:
config['scanInterval'] = DEFAULT_SCAN_INTERVAL
print('> Successfully read config file')
# print(config)
return True
def __main__():
global config
if not readConfigFile():
sys.exit()
while True:
# scan the wifi networks, and get geolocation data from API
data = geolocation.getGeolocation(config['apiKey'])
# display the data
geolocation.displayGeolocation(data)
# sleep
sleep(config['scanInterval'])
if __name__ == '__main__':
__main__()