-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
93 lines (73 loc) · 3.57 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
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
# Stub out some common entry points to later convert to tests after everything is wired up together
import logging
import time
import os
import yaml
import sys
from model import aircraft_report
from model import report_receiver
from utils import postgres as pg_utils
with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config.yml'), 'r') as yaml_config_file:
config = yaml.load(yaml_config_file)
# log_formatter = logging.Formatter("%(levelname)s: %(asctime)s - %(name)s - %(process)s - %(message)s")
FORMAT = '%(asctime)-15s %(levelname)s: %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger(__name__)
# config vars
aircraft_data_url1 = config['feed1']['url']
receiver1_lat83 = config['receiver1']['lat83']
receiver1_long83 = config['receiver1']['long83']
aircraft_data_url2 = config['feed2']['url']
receiver2_lat83 = config['receiver2']['lat83']
receiver2_long83 = config['receiver2']['long83']
db_hostname = config['database']['hostname']
db_port = config['database']['port']
db_name = config['database']['dbname']
db_user = config['database']['user']
db_pwd = config['database']['pwd']
sleep_time_sec = config['waittimesec']
total_samples_cutoff_val = config['samplescutoff']
postgres_db_connection = pg_utils.database_connection(dbname=db_name,
dbhost=db_hostname,
dbport=db_port,
dbuser=db_user,
dbpasswd=db_pwd)
radio_receiver_1 = report_receiver.RadioReceiver(name='piaware1',
type='raspi',
lat83=receiver1_lat83,
long83=receiver1_long83,
data_access_url='',
location="")
radio_receiver_2 = report_receiver.RadioReceiver(name='piaware2',
type='raspi',
lat83=receiver2_lat83,
long83=receiver2_long83,
data_access_url='',
location="")
def harvest_aircraft_json_from_pi():
logger.info('Aircraft ingest beginning.')
total_samples_count = 0
failure_num = 0
while total_samples_count < total_samples_cutoff_val:
try:
start_time = time.time()
current_reports_list = aircraft_report.get_aircraft_data_from_url(aircraft_data_url1)
if len(current_reports_list) > 0:
aircraft_report.load_aircraft_reports_list_into_db(
aircraft_reports_list=current_reports_list,
radio_receiver=radio_receiver_1,
dbconn=postgres_db_connection)
end_time = time.time()
logger.debug('{} seconds for data pull from Pi'.format((end_time - start_time)))
total_samples_count += 1
time.sleep(sleep_time_sec)
except:
# Workaround for failing connection when pi gets busy
logger.exception('Issue getting data from a receiver {}'.format(radio_receiver_1))
time.sleep(120)
failure_num += 1
if failure_num > 10:
exit(1)
if __name__ == '__main__':
logger.debug('Entry from main.py main started')
harvest_aircraft_json_from_pi()