Read from database for API.
This commit is contained in:
30
api_app.py
30
api_app.py
@ -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
|
||||
|
Reference in New Issue
Block a user