cleaned up
This commit is contained in:
@@ -1,46 +0,0 @@
|
|||||||
import csv
|
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
import shutil
|
|
||||||
|
|
||||||
class DataBaseCsv:
|
|
||||||
def __init__(self, filename: str):
|
|
||||||
self.filename = filename
|
|
||||||
|
|
||||||
def store_data(self, data: dict):
|
|
||||||
new_fields = list(data.keys())
|
|
||||||
|
|
||||||
# If file does not exist or is empty → create new file with header
|
|
||||||
if not os.path.exists(self.filename) or os.path.getsize(self.filename) == 0:
|
|
||||||
with open(self.filename, mode='w', newline='') as csv_file:
|
|
||||||
writer = csv.DictWriter(csv_file, fieldnames=new_fields)
|
|
||||||
writer.writeheader()
|
|
||||||
writer.writerow(data)
|
|
||||||
return
|
|
||||||
|
|
||||||
# If file exists → read existing header and modbus_registers
|
|
||||||
with open(self.filename, mode='r', newline='') as csv_file:
|
|
||||||
reader = csv.DictReader(csv_file)
|
|
||||||
existing_fields = reader.fieldnames
|
|
||||||
existing_data = list(reader)
|
|
||||||
|
|
||||||
# Merge old and new fields (keep original order, add new ones)
|
|
||||||
all_fields = existing_fields.copy()
|
|
||||||
for field in new_fields:
|
|
||||||
if field not in all_fields:
|
|
||||||
all_fields.append(field)
|
|
||||||
|
|
||||||
# Write to a temporary file with updated header
|
|
||||||
with tempfile.NamedTemporaryFile(mode='w', delete=False, newline='', encoding='utf-8') as tmp_file:
|
|
||||||
writer = csv.DictWriter(tmp_file, fieldnames=all_fields)
|
|
||||||
writer.writeheader()
|
|
||||||
|
|
||||||
# Write old rows with updated field list
|
|
||||||
for row in existing_data:
|
|
||||||
writer.writerow({field: row.get(field, '') for field in all_fields})
|
|
||||||
|
|
||||||
# Write new modbus_registers row
|
|
||||||
writer.writerow({field: data.get(field, '') for field in all_fields})
|
|
||||||
|
|
||||||
# Replace original file with updated temporary file
|
|
||||||
shutil.move(tmp_file.name, self.filename)
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
from sshtunnel import SSHTunnelForwarder
|
|
||||||
|
|
||||||
# ---- KONFIG ----
|
|
||||||
SSH_HOST = "192.168.1.146" # Raspberry Pi im 192.168.1.x Netz
|
|
||||||
SSH_PORT = 22
|
|
||||||
SSH_USER = "pi"
|
|
||||||
PASSWORD = 'raspberry' # oder Passwort als String
|
|
||||||
|
|
||||||
|
|
||||||
REMOTE_IP = "10.0.0.10" # Wärmepumpe im 10.0.0.x Netz
|
|
||||||
REMOTE_PORT = 502 # Modbus/TCP Port
|
|
||||||
|
|
||||||
def make_tunnel(port):
|
|
||||||
tunnel = SSHTunnelForwarder(
|
|
||||||
(SSH_HOST, SSH_PORT),
|
|
||||||
ssh_username=SSH_USER,
|
|
||||||
ssh_password=PASSWORD,
|
|
||||||
remote_bind_address=(REMOTE_IP, REMOTE_PORT),
|
|
||||||
local_bind_address=("127.0.0.1", port),
|
|
||||||
)
|
|
||||||
tunnel.start()
|
|
||||||
return tunnel
|
|
||||||
Reference in New Issue
Block a user