Refactor to remove sqlite3 & pass flake8

This commit is contained in:
Starbeamrainbowlabs 2018-11-15 19:33:42 +00:00
parent 6f6dec8b41
commit 2266532db4
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 28 additions and 44 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
*.db *.db
*.sqlite3
# Created by https://www.gitignore.io/api/python,git # Created by https://www.gitignore.io/api/python,git
# Edit at https://www.gitignore.io/?templates=python,git # Edit at https://www.gitignore.io/?templates=python,git

View file

@ -5,7 +5,6 @@ import json
import logging import logging
from random import randint from random import randint
import socket import socket
import sqlite3
from board_config import LoraBoardDraguino from board_config import LoraBoardDraguino
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
@ -20,46 +19,19 @@ GATEWAY_PORT = 1700
class LoRaPacketsDB(object): class LoRaPacketsDB(object):
def __init__(self): def __init__(self):
self.conn = sqlite3.connect('gateway_packages.db') # we only need the db at runtime pass
self.c = self.conn.cursor()
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): def log_pkg(self, board, packet):
query = """INSERT INTO `PACKETS` ( print("[{}] [receive] freq: {}, crc: {}, signal-noise-ratio: {}, signal strength: {} (packet: {})".format(
`DATETIME`, payload['datetime'].isoformat(),
`DATA`, board.frequency,
`FREQUENCY`, payload.get('crc'),
`IS_CRC_OK`, payload.get('pkt_snr'),
`PKT_SNR`, payload.get('rssi'),
`PKT_RSSI`, payload.get('pkt_rssi'),
`RSSI` ))
) VALUES (?, ?, ?, ?, ?, ?, ?)
"""
try:
self.c.execute(query, (payload['datetime'].isoformat(),
payload.get('payload'),
board.frequency,
payload.get('crc'),
payload.get('pkt_snr'),
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): def construct_semtec_udp(board, payload):
@ -67,7 +39,10 @@ def construct_semtec_udp(board, payload):
frame = bytearray() frame = bytearray()
frame.append(2) # Protocol version = 2 frame.append(2) # Protocol version = 2
frame.extend([randint(0, 255), randint(0, 255)]) # Random numbers 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 = { data = {
"rxpk": "rxpk":
@ -81,7 +56,8 @@ def construct_semtec_udp(board, payload):
"rssi": payload['pkt_rssi'], "rssi": payload['pkt_rssi'],
"lsnr": payload['pkt_snr'], "lsnr": payload['pkt_snr'],
"size": len(payload['payload']), "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: with LoraBoardDraguino(433300000, 7) as board:
board.set_mode_rx() 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: while True:
if GPIO.input(board._pin_dio0) == 1: if GPIO.input(board._pin_dio0) == 1:
@ -108,6 +87,10 @@ if __name__ == "__main__":
# logging.info(semtec_udp) # logging.info(semtec_udp)
# logging.info("Received: {}".format(payload)) # 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) db.log_pkg(board, payload)