meshtastic-matrix-relay/db_utils.py

43 wiersze
1.3 KiB
Python
Czysty Zwykły widok Historia

2023-04-27 14:24:32 +00:00
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)