-
Notifications
You must be signed in to change notification settings - Fork 13
/
headless.py
219 lines (185 loc) · 8.04 KB
/
headless.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
#!/usr/bin/python3
"""
n1mm_view_headless
create images from contest data
non-interactive version. This creates files on the disk and updates them periodically.
"""
import gc
import logging
import os
import re
import sqlite3
import sys
import time
import config
import dataaccess
import graphics
__author__ = 'Jeffrey B. Otterson, N1KDO'
__copyright__ = 'Copyright 2017 Jeffrey B. Otterson'
__license__ = 'Simplified BSD'
logging.basicConfig(format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG) #.LOG_LEVEL)
logging.Formatter.converter = time.gmtime
def makePNGTitle(image_dir, title):
if image_dir is None:
image_dir = './images'
title = title.replace(' ', '_')
return f'{image_dir}/{title}.png'
# return ''.join([image_dir, '/', re.sub('[^\w\-_]', '_', title), '.png'])
def create_images(size, image_dir, last_qso_timestamp):
"""
load data from the database tables
"""
logging.debug('load data')
qso_operators = []
qso_stations = []
qso_band_modes = []
operator_qso_rates = []
qsos_per_hour = []
qsos_by_section = {}
qso_classes = []
db = None
data_updated = False
try:
logging.debug('connecting to database')
db = sqlite3.connect(config.DATABASE_FILENAME)
cursor = db.cursor()
logging.debug('database connected')
# Handy routine to dump the database to help debug strange problems
#if logging.getLogger().isEnabledFor(logging.DEBUG):
# cursor.execute('SELECT timestamp, callsign, section, operator_id, operator.name FROM qso_log join operator WHERE operator.id = operator_id')
# for row in cursor:
# logging.debug('QSO: %s\t%s\t%s\t%s\t%s' % (row[0], row[1], row[2], row[3], row[4]))
# get timestamp from the last record in the database
last_qso_time, message = dataaccess.get_last_qso(cursor)
logging.debug('old_timestamp = %d, timestamp = %d', last_qso_timestamp, last_qso_time)
if last_qso_time != last_qso_timestamp:
# last_qso_time is passed as the result and updated in call to this function.
logging.debug('data updated!')
data_updated = True
# load qso_operators
qso_operators = dataaccess.get_operators_by_qsos(cursor)
# load qso_stations -- maybe useless chartjunk
qso_stations = dataaccess.get_station_qsos(cursor)
# get something else.
qso_band_modes = dataaccess.get_qso_band_modes(cursor)
# load QSOs per Hour by Operator
operator_qso_rates = dataaccess.get_qsos_per_hour_per_operator(cursor, last_qso_time)
# load QSO rates per Hour by Band
qsos_per_hour, qsos_per_band = dataaccess.get_qsos_per_hour_per_band(cursor)
# load qso exchange data: what class are the other stations?
qso_classes = dataaccess.get_qso_classes(cursor)
# load QSOs by Section
qsos_by_section = dataaccess.get_qsos_by_section(cursor)
logging.debug('load data done')
except sqlite3.OperationalError as error:
logging.exception(error)
return
finally:
if db is not None:
logging.debug('Closing DB')
cursor.close()
db.close()
db = None
if data_updated:
try:
image_data, image_size = graphics.qso_summary_table(size, qso_band_modes)
filename = makePNGTitle(image_dir, 'qso_summary_table')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_rates_table(size, operator_qso_rates)
filename = makePNGTitle(image_dir, 'qso_rates_table')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_operators_graph(size, qso_operators)
filename = makePNGTitle(image_dir, 'qso_operators_graph')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_operators_table(size, qso_operators)
filename = makePNGTitle(image_dir, 'qso_operators_table')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_operators_table_all(size, qso_operators)
filename = makePNGTitle(image_dir, 'qso_operators_table_all')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_stations_graph(size, qso_stations)
filename = makePNGTitle(image_dir, 'qso_stations_graph')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_bands_graph(size, qso_band_modes)
filename = makePNGTitle(image_dir, 'qso_bands_graph')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_modes_graph(size, qso_band_modes)
filename = makePNGTitle(image_dir, 'qso_modes_graph')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_classes_graph(size, qso_classes)
filename = makePNGTitle(image_dir, 'qso_classes_graph')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
try:
image_data, image_size = graphics.qso_rates_graph(size, qsos_per_hour)
filename = makePNGTitle(image_dir, 'qso_rates_graph')
graphics.save_image(image_data, image_size, filename)
except Exception as e:
logging.exception(e)
# map gets updated every time so grey line moves
try:
# There is a memory leak in the next code -- is there?
image_data, image_size = graphics.draw_map(size, qsos_by_section)
filename = makePNGTitle(image_dir, 'sections_worked_map')
graphics.save_image(image_data, image_size, filename)
gc.collect()
except Exception as e:
logging.exception(e)
#if data_updated: # Data is always updated since the sections map is always updated. Let rsync command handle this.
if config.POST_FILE_COMMAND is not None:
os.system(config.POST_FILE_COMMAND)
return last_qso_time
def main():
logging.info('headless startup...')
size = (1280, 1024)
image_dir = config.IMAGE_DIR
logging.debug("Checking for IMAGE_DIR")
logging.info("IMAGE_DIR set to %s - checking if exists" % config.IMAGE_DIR)
# Check if the dir given exists and create if necessary
if config.IMAGE_DIR is not None:
if not os.path.exists(config.IMAGE_DIR):
logging.error("%s did not exist - creating..." % config.IMAGE_DIR)
os.makedirs(config.IMAGE_DIR)
if not os.path.exists(config.IMAGE_DIR):
sys.exit('Image %s directory could not be created' % config.IMAGE_DIR)
logging.info('creating world...')
# base_map = graphics.create_map()
run = True
last_qso_timestamp = ''
logging.info('headless running...')
while run:
try:
last_qso_timestamp = create_images(size, image_dir, last_qso_timestamp)
time.sleep(config.DATA_DWELL_TIME)
except KeyboardInterrupt:
logging.info('Keyboard interrupt, shutting down...')
run = False
logging.info('headless shutdown...')
if __name__ == '__main__':
main()