Compare commits
1 Commits
master
...
6c14800a02
| Author | SHA1 | Date | |
|---|---|---|---|
| 6c14800a02 |
@@ -1,12 +1,10 @@
|
|||||||
{ python3 }:
|
{ python3 }:
|
||||||
with python3.pkgs;
|
with python3.pkgs;
|
||||||
buildPythonPackage rec {
|
buildPythonPackage rec {
|
||||||
name = "mqtt-tools";
|
name = "mqtt-config";
|
||||||
src = ./.;
|
src = ./.;
|
||||||
propagatedBuildInputs = [ pyyaml paho-mqtt ];
|
propagatedBuildInputs = [ pyyaml paho-mqtt ];
|
||||||
buildInputs = [ ];
|
buildInputs = [];
|
||||||
doCheck = false;
|
doCheck = false;
|
||||||
shellHook = "";
|
shellHook = "";
|
||||||
pyproject = true;
|
|
||||||
build-system = [ setuptools ];
|
|
||||||
}
|
}
|
||||||
|
|||||||
39
mqtt-collect
39
mqtt-collect
@@ -7,10 +7,17 @@ import paho.mqtt.client
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
MAX_VALUE_CACHE_SIZE = 1024
|
|
||||||
|
|
||||||
def init_db(path, topics):
|
def init_db(path, topics):
|
||||||
db = sqlite3.connect(path)
|
db = sqlite3.connect(path)
|
||||||
|
c = db.cursor()
|
||||||
|
for topic in topics:
|
||||||
|
table = topic.replace('/', '_')
|
||||||
|
c.execute(
|
||||||
|
f'CREATE TABLE IF NOT EXISTS {table} (timestamp timestamp, value real)'
|
||||||
|
)
|
||||||
|
|
||||||
|
db.commit()
|
||||||
return db
|
return db
|
||||||
|
|
||||||
|
|
||||||
@@ -32,37 +39,15 @@ def main():
|
|||||||
def on_disconnect(client, userdata, rc):
|
def on_disconnect(client, userdata, rc):
|
||||||
client.reconnect()
|
client.reconnect()
|
||||||
|
|
||||||
current_values = {}
|
|
||||||
|
|
||||||
def on_message(client, userdata, msg):
|
def on_message(client, userdata, msg):
|
||||||
try:
|
try:
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
table = msg.topic.replace("/", "_")
|
table = msg.topic.replace("/", "_")
|
||||||
ts = datetime.datetime.now()
|
ts = datetime.datetime.now()
|
||||||
textual = {
|
value = float(msg.payload)
|
||||||
b"on": 1.,
|
c.execute(f'INSERT INTO {table} VALUES (?, ?)', (ts, value))
|
||||||
b"off": 0.,
|
c.execute(f'SELECT * from {table}')
|
||||||
b"yes": 1.,
|
db.commit()
|
||||||
b"no": 0.
|
|
||||||
}
|
|
||||||
|
|
||||||
if msg.payload.lower() in textual:
|
|
||||||
value = textual[msg.payload.lower()]
|
|
||||||
else:
|
|
||||||
value = float(msg.payload)
|
|
||||||
print(f"{table}: {value} ")
|
|
||||||
|
|
||||||
if current_values.get(table, None) != value:
|
|
||||||
current_values[table] = value
|
|
||||||
if len(current_values) > MAX_VALUE_CACHE_SIZE:
|
|
||||||
first_entry = current_values.keys().next()
|
|
||||||
del current_values[first_entry]
|
|
||||||
|
|
||||||
c.execute(f'CREATE TABLE IF NOT EXISTS "{table}" '
|
|
||||||
'(timestamp timestamp, value real)')
|
|
||||||
c.execute(f'INSERT INTO "{table}" VALUES (?, ?)', (ts, value))
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Error writing to database: {e}')
|
print(f'Error writing to database: {e}')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user