Refactor to remove sqlite3 & pass flake8
This commit is contained in:
parent
6f6dec8b41
commit
2266532db4
2 changed files with 28 additions and 44 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
|||
*.db
|
||||
*.sqlite3
|
||||
# Created by https://www.gitignore.io/api/python,git
|
||||
# Edit at https://www.gitignore.io/?templates=python,git
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ import json
|
|||
import logging
|
||||
from random import randint
|
||||
import socket
|
||||
import sqlite3
|
||||
|
||||
from board_config import LoraBoardDraguino
|
||||
import RPi.GPIO as GPIO
|
||||
|
@ -20,46 +19,19 @@ GATEWAY_PORT = 1700
|
|||
|
||||
class LoRaPacketsDB(object):
|
||||
def __init__(self):
|
||||
self.conn = sqlite3.connect('gateway_packages.db') # we only need the db at runtime
|
||||
self.c = self.conn.cursor()
|
||||
pass
|
||||
|
||||
self._init_db()
|
||||
|
||||
def _init_db(self):
|
||||
create_table = """CREATE TABLE IF NOT EXISTS `PACKETS` (
|
||||
`ID` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
`DATETIME` TEXT NOT NULL,
|
||||
`DATA` BLOB,
|
||||
`FREQUENCY` NUMERIC,
|
||||
`IS_CRC_OK` INTEGER,
|
||||
`PKT_SNR` NUMERIC,
|
||||
`PKT_RSSI` NUMERIC,
|
||||
`RSSI` NUMERIC
|
||||
)"""
|
||||
self.conn.execute(create_table)
|
||||
|
||||
def log_pkg(self, board, packet):
|
||||
query = """INSERT INTO `PACKETS` (
|
||||
`DATETIME`,
|
||||
`DATA`,
|
||||
`FREQUENCY`,
|
||||
`IS_CRC_OK`,
|
||||
`PKT_SNR`,
|
||||
`PKT_RSSI`,
|
||||
`RSSI`
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
"""
|
||||
try:
|
||||
self.c.execute(query, (payload['datetime'].isoformat(),
|
||||
payload.get('payload'),
|
||||
print("[{}] [receive] freq: {}, crc: {}, signal-noise-ratio: {}, signal strength: {} (packet: {})".format(
|
||||
payload['datetime'].isoformat(),
|
||||
board.frequency,
|
||||
payload.get('crc'),
|
||||
payload.get('pkt_snr'),
|
||||
payload.get('rssi'),
|
||||
payload.get('pkt_rssi'),
|
||||
payload.get('rssi')))
|
||||
self.conn.commit()
|
||||
except sqlite3.IntegrityError as e:
|
||||
logging.exception("SQL ERROR: ", e)
|
||||
))
|
||||
|
||||
|
||||
|
||||
def construct_semtec_udp(board, payload):
|
||||
|
@ -67,7 +39,10 @@ def construct_semtec_udp(board, payload):
|
|||
frame = bytearray()
|
||||
frame.append(2) # Protocol version = 2
|
||||
frame.extend([randint(0, 255), randint(0, 255)]) # Random numbers
|
||||
frame.extend([0x80, 0xFA, 0x5C, 0xFF, 0xFF, 0x69, 0x33, 0xBB]) # TODO: construct from hardware
|
||||
frame.extend([
|
||||
0x80, 0xFA, 0x5C, 0xFF,
|
||||
0xFF, 0x69, 0x33, 0xBB
|
||||
]) # TODO: construct from hardware
|
||||
|
||||
data = {
|
||||
"rxpk":
|
||||
|
@ -81,7 +56,8 @@ def construct_semtec_udp(board, payload):
|
|||
"rssi": payload['pkt_rssi'],
|
||||
"lsnr": payload['pkt_snr'],
|
||||
"size": len(payload['payload']),
|
||||
"data": base64.standard_b64encode(payload['payload']).decode("utf-8")
|
||||
"data": base64.standard_b64encode(payload['payload'])
|
||||
.decode("utf-8")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +73,10 @@ if __name__ == "__main__":
|
|||
|
||||
with LoraBoardDraguino(433300000, 7) as board:
|
||||
board.set_mode_rx()
|
||||
logging.info("Listening at SF{} on {} MHz".format(board.sf, board.frequency/1000000))
|
||||
print("Listening at SF{} on {} MHz".format(
|
||||
board.sf,
|
||||
board.frequency / 1000000
|
||||
))
|
||||
|
||||
while True:
|
||||
if GPIO.input(board._pin_dio0) == 1:
|
||||
|
@ -108,6 +87,10 @@ if __name__ == "__main__":
|
|||
# logging.info(semtec_udp)
|
||||
|
||||
# logging.info("Received: {}".format(payload))
|
||||
logging.info("Received at {}: {} = {}".format(payload['datetime'].isoformat(), payload['payload'].hex(), payload['payload'][4:]))
|
||||
print("Received at {}: {} = {}".format(
|
||||
payload['datetime'].isoformat(),
|
||||
payload['payload'].hex(),
|
||||
payload['payload'][4:]
|
||||
))
|
||||
|
||||
db.log_pkg(board, payload)
|
||||
|
|
Reference in a new issue