Cache last value for each topic and only insert into DB on change
Reduces database size
This commit is contained in:
		
							parent
							
								
									adf4e564d9
								
							
						
					
					
						commit
						b88fcb2f04
					
				@ -1,7 +1,7 @@
 | 
			
		||||
{ python3 }:
 | 
			
		||||
with python3.pkgs;
 | 
			
		||||
buildPythonPackage rec {
 | 
			
		||||
  name = "mqtt-config";
 | 
			
		||||
  name = "mqtt-tools";
 | 
			
		||||
  src = ./.;
 | 
			
		||||
  propagatedBuildInputs = [ pyyaml paho-mqtt ];
 | 
			
		||||
  buildInputs = [];
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										12
									
								
								mqtt-collect
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								mqtt-collect
									
									
									
									
									
								
							@ -7,6 +7,7 @@ import paho.mqtt.client
 | 
			
		||||
import sqlite3
 | 
			
		||||
import datetime
 | 
			
		||||
 | 
			
		||||
MAX_VALUE_CACHE_SIZE = 1024
 | 
			
		||||
 | 
			
		||||
def init_db(path, topics):
 | 
			
		||||
    db = sqlite3.connect(path)
 | 
			
		||||
@ -31,6 +32,8 @@ def main():
 | 
			
		||||
    def on_disconnect(client, userdata, rc):
 | 
			
		||||
        client.reconnect()
 | 
			
		||||
 | 
			
		||||
    current_values = {}
 | 
			
		||||
 | 
			
		||||
    def on_message(client, userdata, msg):
 | 
			
		||||
        try:
 | 
			
		||||
            c = db.cursor()
 | 
			
		||||
@ -48,11 +51,18 @@ def main():
 | 
			
		||||
            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))
 | 
			
		||||
            #c.execute(f'SELECT * from "{table}"')
 | 
			
		||||
                db.commit()
 | 
			
		||||
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            print(f'Error writing to database: {e}')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user