Read from database for API.

This commit is contained in:
W1CDN
2023-04-15 13:27:00 -05:00
parent 63963c0ade
commit 6e6693bfbc
3 changed files with 124 additions and 31 deletions

View File

@ -6,6 +6,7 @@ import csv
import ast
import glob
import json
import sqlite3
api_app = Flask(__name__)
api = Api(api_app)
@ -42,16 +43,41 @@ def read_logs(log_folder):
return(json_array)
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def get_db_connection():
conn = sqlite3.connect('database.db')
#conn.row_factory = sqlite3.Row
conn.row_factory = dict_factory
return conn
def select_all_frames(conn):
"""
Query all rows in the frames table
:param conn: the Connection object
:return:
"""
cur = conn.cursor()
cur.execute("SELECT * FROM frames")
rows = cur.fetchall()
return rows
class Packets(Resource):
def get(self):
data = read_logs(log_folder)
#data = read_logs(log_folder)
conn = get_db_connection()
data = select_all_frames(conn)
return {'data': data}, 200 # return data and 200 OK code
# 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)
#data = read_logs(log_folder)
# Start subprocess to watch KISS connection
import subprocess