Refactor db utility out of main

pull/1/head
Geoff Whittington 2023-04-27 10:24:32 -04:00
rodzic a65639c172
commit 5170348e01
2 zmienionych plików z 44 dodań i 43 usunięć

42
db_utils.py 100644
Wyświetl plik

@ -0,0 +1,42 @@
import sqlite3
# Initialize SQLite database
def initialize_database():
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
cursor.execute(
"CREATE TABLE IF NOT EXISTS longnames (meshtastic_id TEXT PRIMARY KEY, longname TEXT)"
)
conn.commit()
# Get the longname for a given Meshtastic ID
def get_longname(meshtastic_id):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
cursor.execute(
"SELECT longname FROM longnames WHERE meshtastic_id=?", (meshtastic_id,)
)
result = cursor.fetchone()
return result[0] if result else None
def save_longname(meshtastic_id, longname):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
cursor.execute(
"INSERT OR REPLACE INTO longnames (meshtastic_id, longname) VALUES (?, ?)",
(meshtastic_id, longname),
)
conn.commit()
def update_longnames(nodes):
if nodes:
for node in nodes.values():
user = node.get("user")
if user:
meshtastic_id = user["id"]
longname = user.get("longName", "N/A")
save_longname(meshtastic_id, longname)

45
main.py
Wyświetl plik

@ -6,7 +6,6 @@ import asyncio
import time
import logging
import re
import sqlite3
import yaml
import certifi
import ssl
@ -29,6 +28,7 @@ from typing import List, Union
from datetime import datetime
from pathlib import Path
from db_utils import initialize_database, get_longname, update_longnames
bot_start_time = int(
time.time() * 1000
@ -56,47 +56,6 @@ handler.setFormatter(
logger.addHandler(handler)
# Initialize SQLite database
def initialize_database():
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
cursor.execute(
"CREATE TABLE IF NOT EXISTS longnames (meshtastic_id TEXT PRIMARY KEY, longname TEXT)"
)
conn.commit()
# Get the longname for a given Meshtastic ID
def get_longname(meshtastic_id):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
cursor.execute(
"SELECT longname FROM longnames WHERE meshtastic_id=?", (meshtastic_id,)
)
result = cursor.fetchone()
return result[0] if result else None
def save_longname(meshtastic_id, longname):
with sqlite3.connect("meshtastic.sqlite") as conn:
cursor = conn.cursor()
cursor.execute(
"INSERT OR REPLACE INTO longnames (meshtastic_id, longname) VALUES (?, ?)",
(meshtastic_id, longname),
)
conn.commit()
def update_longnames():
if meshtastic_interface.nodes:
for node in meshtastic_interface.nodes.values():
user = node.get("user")
if user:
meshtastic_id = user["id"]
longname = user.get("longName", "N/A")
save_longname(meshtastic_id, longname)
def load_plugins():
plugins = []
plugin_folder = Path("plugins")
@ -392,7 +351,7 @@ async def main():
while True:
try:
# Update longnames
update_longnames()
update_longnames(meshtastic_interface.nodes)
logger.info("Syncing with Matrix server...")
await matrix_client.sync_forever(timeout=30000)