2023-04-05 16:28:39 -05:00
|
|
|
from flask import Flask
|
|
|
|
from flask_restful import Resource, Api, reqparse
|
2023-04-05 20:36:19 -05:00
|
|
|
from datetime import date, timedelta
|
|
|
|
import configparser
|
2023-04-06 14:34:59 -05:00
|
|
|
import csv
|
2023-04-05 16:28:39 -05:00
|
|
|
import ast
|
2023-04-05 18:21:37 -05:00
|
|
|
import glob
|
2023-04-05 22:43:35 -05:00
|
|
|
import json
|
2023-04-05 16:28:39 -05:00
|
|
|
app = Flask(__name__)
|
2023-04-05 16:58:35 -05:00
|
|
|
api = Api(app)
|
|
|
|
|
2023-04-05 20:36:19 -05:00
|
|
|
def read_config():
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read('config.ini')
|
|
|
|
return config
|
|
|
|
|
|
|
|
def read_logs(log_folder):
|
|
|
|
# Read some log files
|
2023-04-06 20:45:31 -05:00
|
|
|
# UTC time, so let's look at tomorrow, today, and yesterday.
|
2023-04-05 20:36:19 -05:00
|
|
|
# TODO Load new files into a database on a schedule?
|
|
|
|
today = date.today()
|
|
|
|
yesterday = today - timedelta(days = 1)
|
|
|
|
tomorrow = today + timedelta(days = 1)
|
|
|
|
file_list = glob.glob(log_folder+str(yesterday)+"*") + \
|
|
|
|
glob.glob(log_folder+str(today)+"*") + \
|
|
|
|
glob.glob(log_folder+str(tomorrow)+"*")
|
2023-04-06 14:34:59 -05:00
|
|
|
|
|
|
|
# https://stackoverflow.com/a/66071962
|
|
|
|
json_array = []
|
2023-04-05 20:36:19 -05:00
|
|
|
for file in file_list:
|
2023-04-06 14:34:59 -05:00
|
|
|
with open(file, encoding='utf-8') as csvf:
|
|
|
|
csvReader = csv.DictReader(csvf)
|
|
|
|
for row in csvReader:
|
|
|
|
#add this python dict to json array
|
|
|
|
json_array.append(row)
|
2023-04-05 22:43:35 -05:00
|
|
|
|
2023-04-06 20:45:31 -05:00
|
|
|
# Add the call and location of this station to the packet info
|
|
|
|
config = read_config()
|
|
|
|
for item in json_array:
|
|
|
|
item['station_name'] = config['Settings']['station_call']
|
|
|
|
item['station_lat'] = config['Settings']['station_lat']
|
|
|
|
item['station_lon'] = config['Settings']['station_lon']
|
2023-04-05 20:36:19 -05:00
|
|
|
|
2023-04-06 20:45:31 -05:00
|
|
|
return(json_array)
|
2023-04-05 17:36:16 -05:00
|
|
|
|
2023-04-06 20:45:31 -05:00
|
|
|
# TODO need a function that adds the most recent location for any 'name'
|
|
|
|
# into a simple database. Then if it needs to be mapped, it can be, even
|
2023-04-08 15:36:54 -05:00
|
|
|
# if it hasn't been recently heard directly.
|
2023-04-05 17:36:16 -05:00
|
|
|
|
2023-04-05 18:21:37 -05:00
|
|
|
class Packets(Resource):
|
|
|
|
def get(self):
|
2023-04-06 20:45:31 -05:00
|
|
|
# TODO neither path nor actual path taken are included here--how to
|
|
|
|
# include them? I am not sure whether multiple stations would be listed
|
|
|
|
# in 'source' or not;
|
|
|
|
# "source": "KF4ME-7",
|
|
|
|
# "heard": "K0UND-2",
|
|
|
|
# TODO need to be able to pass arguments in URL to filter
|
2023-04-06 14:34:59 -05:00
|
|
|
|
2023-04-05 18:21:37 -05:00
|
|
|
return {'data': data}, 200 # return data and 200 OK code
|
|
|
|
|
2023-04-05 20:36:19 -05:00
|
|
|
# Read config
|
|
|
|
config = read_config()
|
|
|
|
log_folder = config['Settings']['log_folder']
|
|
|
|
# Load logs first (just to check for errors before page loads)
|
|
|
|
data = read_logs(log_folder)
|
|
|
|
|
2023-04-05 18:21:37 -05:00
|
|
|
api.add_resource(Packets, '/packets') # and '/locations' is our entry point for Locations
|
2023-04-05 16:58:35 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-04-06 20:45:31 -05:00
|
|
|
app.run(debug=True, host='0.0.0.0') # run our Flask app
|