MQTT wkerned, files worden opgeschoond

This commit is contained in:
Atilla
2024-03-12 12:12:38 +01:00
parent 6b8c3846c1
commit 8e31537b57
2 changed files with 96 additions and 127 deletions

View File

@@ -1,71 +1,101 @@
import requests # import requests
import time # import time
from db_connect import database_connect # from db_connect import database_connect
def fetch_and_display_all(url, access_token, repeat_count=5): # def fetch_and_display_all(url, access_token):
for _ in range(repeat_count): # # for _ in range(repeat_count):
try: # try:
headers = { # headers = {
"Authorization": f"Token {access_token}" # "Authorization": f"Token {access_token}"
} # }
response = requests.get(url, headers=headers) # response = requests.get(url, headers=headers)
response.raise_for_status() # response.raise_for_status()
# data = response.json()
# print(f"Data from {url}:")
# print(data)
# load_data(data)
# except requests.exceptions.RequestException as e:
# print(f"Error fetching data from {url}: {e}")
# print("Waiting for the next retrieval action...")
# # time.sleep(300) # Time here is in seconds.
# time.sleep(1) # Time here is in seconds.
data = response.json() # def load_data(data):
print(f"Data from {url}:") # mydb = database_connect()
print(data) # if mydb.is_connected():
load_data(data) # mycursor = mydb.cursor()
except requests.exceptions.RequestException as e: # # Here you need to adjust the correct column names and data formats based on the API response
print(f"Error fetching data from {url}: {e}") # insert_query = """
# INSERT INTO goodgarden.devices (serial_number, name, label, last_seen, last_battery_voltage)
# VALUES (%s, %s, %s, %s, %s )
# """
# for record in data['results']:
# serial_number = record.get('serial_number', '')
# name = record.get('name', '')
# label = record.get('label', '')
# last_seen = record.get('last_seen', '')
# last_battery_voltage = record.get('last_battery_voltage', '')
print("Waiting for the next retrieval action...") # print(f"Inserting data: serial_number={serial_number}, name={name}, label={label}, last_seen={last_seen}, last_battery_voltage={last_battery_voltage}")
# time.sleep(300) # Time here is in seconds. # # Execute the query
# mycursor.execute(insert_query, (serial_number, name, label, last_seen, last_battery_voltage))
time.sleep(1) # Time here is in seconds. # # Commit the changes
# mydb.commit()
def load_data(data): # # Close cursor and connection
mydb = database_connect() # mycursor.close()
if mydb.is_connected(): # mydb.close()
mycursor = mydb.cursor()
# Here you need to adjust the correct column names and data formats based on the API response # print("Data inserted into the database.")
insert_query = """
INSERT INTO goodgarden.devices (serial_number, name, label, last_seen, last_battery_voltage)
VALUES (%s, %s, %s, %s, %s )
"""
for record in data['results']:
serial_number = record.get('serial_number', '')
name = record.get('name', '')
label = record.get('label', '')
last_seen = record.get('last_seen', '')
last_battery_voltage = record.get('last_battery_voltage', '')
print(f"Inserting data: serial_number={serial_number}, name={name}, label={label}, last_seen={last_seen}, last_battery_voltage={last_battery_voltage}") # if __name__ == "__main__":
# url = "https://garden.inajar.nl/api/devices/?format=json"
# access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Replace this with your actual access token
# Execute the query
mycursor.execute(insert_query, (serial_number, name, label, last_seen, last_battery_voltage))
# Commit the changes # # access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
mydb.commit()
# Close cursor and connection # # You can change the repeat_count to control how many times you want to repeat the process
mycursor.close() # # repeat_count = 10
mydb.close()
print("Data inserted into the database.")
# fetch_and_display_all(url, access_token)
import sys
from os.path import dirname, abspath, join
# Voeg het pad naar de 'root' directory toe aan sys.path
root_dir = dirname(dirname(abspath(__file__)))
sys.path.append(root_dir)
# Nu kan je de mqtt_client importeren
from mqtt.mqtt_client import create_client, start_loop
# Je kunt nu de create_client en start_loop functies gebruiken
# Lijst waarop je je wil subscriben
mqtt_topics = [
"goodgarden/devices"
]
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Abonneer op alle topics in de mqtt_topics lijst
for topic in mqtt_topics:
client.subscribe(topic)
print(f"Subscribed to {topic}")
def on_message(client, userdata, msg):
# Decodeer de payload van bytes naar string
message = msg.payload.decode()
print(f"Message received on topic {msg.topic}: {message}")
# Hier kun je code toevoegen om iets te doen met het ontvangen bericht
if __name__ == "__main__": if __name__ == "__main__":
url = "https://garden.inajar.nl/api/devices/?format=json" client = create_client("subscriber1", on_connect, on_message) # Zorg voor een unieke client ID
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Replace this with your actual access token start_loop(client)
# access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
# You can change the repeat_count to control how many times you want to repeat the process
repeat_count = 10
fetch_and_display_all(url, access_token, repeat_count)

View File

@@ -1,73 +1,12 @@
import requests import paho.mqtt.client as mqtt
import time
from db_connect import database_connect def on_message(client, userdata, msg):
topic = msg.topic
# Establish a database connection payload = str(msg.payload.decode("utf-8"))
connection = database_connect() print(f"Message received on topic {topic}: {payload}")
if topic == "goodgarden/temperature":
def fetch_and_display_all(urls, access_token): # Verwerk temperatuurdata
for url in urls: # elif topic == "goodgarden/humidity":
try: print(f"Message received on topic {topic}: {payload}")
headers = { # Verwerk vochtigheidsdata
"Authorization": f"Token {access_token}" # Voeg meer condities toe voor andere subtopics
}
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
print(f"Data from {url}:")
print(data)
load_data(data)
except requests.exceptions.RequestException as e:
print(f"Error fetching data from {url}: {e}")
# Wait for a certain time (e.g., 60 seconds) before making the next call
print("Waiting for the next retrieval action...")
time.sleep(10) # Time here is in seconds.
def load_data(data):
mydb = database_connect()
if mydb.is_connected():
mycursor = mydb.cursor()
# Here you need to adjust the correct column names and data formats based on the API response
insert_query = """
INSERT INTO goodgarden.fetch (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']: # Adjust this based on the actual structure of the JSON
timestamp = record.get('timestamp', '')
gateway_receive_time = record.get('gateway_receive_time', '')
device = record.get('device', '')
value = record.get('value', '')
print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") # Print the data being inserted
# Execute the query
mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value))
# Commit the changes
mydb.commit()
# Close cursor and connection
mycursor.close()
mydb.close()
print("Data inserted into the database.")
if __name__ == "__main__":
urls = [
"https://garden.inajar.nl/api/battery_voltage_events/?format=json",
"https://garden.inajar.nl/api/devices/?format=json",
"https://garden.inajar.nl/api/par_events/?format=json",
"https://garden.inajar.nl/api/relative_humidity_events/?format=json",
"https://garden.inajar.nl/api/soil_electric_conductivity_events/?format=json",
"https://garden.inajar.nl/api/soil_relative_permittivity_events/?format=json",
"https://garden.inajar.nl/api/soil_temperature_events/?format=json"
]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang dit met jouw echte toegangstoken
fetch_and_display_all(urls, access_token)