Clean Version

This commit is contained in:
Atilla
2024-02-16 11:05:07 +01:00
14 changed files with 574 additions and 151 deletions

View File

@@ -1,60 +0,0 @@
import requests
import time
import mysql.connector
def database_connect():
return mysql.connector.connect(
host="localhost",
user="root",
password="",
database="goodgarden"
)
def fetch_battery_voltage_events():
url = "https://garden.inajar.nl/api/battery_voltage_events/?format=json"
headers = {
"Authorization": "Token 33bb3b42452306c58ecedc3c86cfae28ba22329c"
}
while True:
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
load_data(data)
print("Wachten voor de volgende ophaalactie...")
time.sleep(300) # De tijd hier is in seconden.
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
time.sleep(300)
def load_data(data):
mydb = database_connect()
if mydb.is_connected():
mycursor = mydb.cursor()
insert_query = """
INSERT INTO goodgarden.battery_voltage_events (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']:
timestamp = record['timestamp']
gateway_receive_time = record['gateway_receive_time']
device = record['device']
value = record['value']
print(data)
mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value))
mydb.commit()
mycursor.close()
mydb.close()
print("Data ingevoegd in de database.")
if __name__ == "__main__":
fetch_battery_voltage_events()

View File

@@ -1,16 +1,16 @@
DROP DATABASE IF EXISTS goodgarden;
CREATE DATABASE goodgarden;
-- DROP DATABASE IF EXISTS goodgarden;
-- CREATE DATABASE goodgarden;
CREATE TABLE goodgarden.sensor_data (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
timestamp INT,
gateway_receive_time VARCHAR(50),
device INT,
value DECIMAL(10, 5),
PRIMARY KEY (id)
);
-- CREATE TABLE goodgarden.sensor_data (
-- id INT UNSIGNED NOT NULL AUTO_INCREMENT,
-- timestamp INT,
-- gateway_receive_time VARCHAR(50),
-- device INT,
-- value DECIMAL(10, 5),
-- PRIMARY KEY (id)
-- );
Invoegen van gegevens in de 'sensor_data'-tabel
INSERT INTO goodgarden.sensor_data (timestamp, gateway_receive_time, device, value)
VALUES (1707295162, '2024-02-07T08:39:22Z', 256, 4.107448101043701),
(1707261284, '2024-02-06T23:14:44Z', 322, 4.111111164093018);
-- Invoegen van gegevens in de 'sensor_data'-tabel
-- INSERT INTO goodgarden.sensor_data (timestamp, gateway_receive_time, device, value)
-- VALUES (1707295162, '2024-02-07T08:39:22Z', 256, 4.107448101043701),
-- (1707261284, '2024-02-06T23:14:44Z', 322, 4.111111164093018);

View File

@@ -1,67 +0,0 @@
import mysql.connector
import requests
import time
def database_connect():
return mysql.connector.connect(
host="localhost",
user="root",
password="",
database="goodgarden"
)
def fetch_data():
url = "https://garden.inajar.nl/api/battery_voltage_events/?format=json"
headers = {
"Authorization": "Token 33bb3b42452306c58ecedc3c86cfae28ba22329c"
}
while True:
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
load_data(data)
# Wacht voor een bepaalde tijd (bijvoorbeeld 60 seconden) voordat je de volgende oproep doet
print("Wachten voor de volgende ophaalactie...")
time.sleep(60) # De tijd hier is in seconden.
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")
# Wacht ook hier bij een fout, om niet in een snelle foutloop te komen
time.sleep(300)
def load_data(data):
mydb = database_connect()
if mydb.is_connected():
mycursor = mydb.cursor()
# Hier moet je de juiste kolomnamen en dataformaten aanpassen op basis van de API-respons
insert_query = """
INSERT INTO goodgarden.sensor_data (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']: # Pas dit aan op basis van de werkelijke structuur van de JSON
timestamp = record['timestamp']
gateway_receive_time = record['gateway_receive_time']
device = record['device']
value = record['value']
print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") # Print de data die wordt ingevoegd
# Voer de query uit
mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value))
# Commit de wijzigingen
mydb.commit()
# Sluit cursor en verbinding
mycursor.close()
mydb.close()
print("Data ingevoegd in de database.")
if __name__ == "__main__":
fetch_data()

View File

@@ -0,0 +1,64 @@
import requests
import time
from db_connect import database_connect
def fetch_and_display_all(urls, access_token):
for url in urls:
try:
headers = {
"Authorization": f"Token {access_token}"
}
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}")
print("Waiting for the next retrieval action...")
time.sleep(300) # Time here is in seconds.
def load_data(data):
mydb = database_connect()
if mydb.is_connected():
mycursor = mydb.cursor()
insert_query = """
INSERT INTO goodgarden.battery_voltage_events (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']:
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}")
# 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",
]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
fetch_and_display_all(urls, access_token)

19
script/db_connect.py Normal file
View File

@@ -0,0 +1,19 @@
import mysql.connector
from mysql.connector import Error
def database_connect():
try:
connection = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="goodgarden"
)
if connection.is_connected():
# print("Connection gelukt!")
return connection
except Error as e:
print(f"Connection NIET gelukt! ${e}")
return None
# database_connect()

64
script/devices.py Normal file
View File

@@ -0,0 +1,64 @@
import requests
import time
from db_connect import database_connect
def fetch_and_display_all(urls, access_token):
for url in urls:
try:
headers = {
"Authorization": f"Token {access_token}"
}
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}")
print("Waiting for the next retrieval action...")
time.sleep(300) # 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.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}")
# Execute the query
mycursor.execute(insert_query, (serial_number, name, label, last_seen, last_battery_voltage))
# 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/devices/?format=json"
]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
fetch_and_display_all(urls, access_token)

92
script/fetch.py Normal file
View File

@@ -0,0 +1,92 @@
import requests
import time
from db_connect import database_connect
# Establish a database connection
connection = database_connect()
def fetch_and_display_all(urls, access_token):
for url in urls:
try:
headers = {
"Authorization": f"Token {access_token}"
}
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))
# insert_query = """ Voor deze code werktengid te krijgen moet je in de datebase een ''id, serial_number, name, label, last_seen, last_battery_voltage)' aanmaken en dan werkt het.
# INSERT INTO goodgarden.fetch (id, serial_number, name, label, last_seen, last_battery_voltage) Hier de tabel naam veranderen
# VALUES (%s, %s, %s, %s, %s, %s)
# """
# for record in data['results']: # Adjust this based on the actual structure of the JSON
# id = record.get('id', '')
# 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: id={id}, serial_number={serial_number}, name={name}, label={label}, last_seen={last_seen}, last_battery_voltage={last_battery_voltage}") # Print the data being inserted
# # Execute the query
# mycursor.execute(insert_query, (id, serial_number, name, label, last_seen, last_battery_voltage))
# 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)

View File

@@ -1,14 +1,7 @@
import requests
import time
import mysql.connector
def database_connect():
return mysql.connector.connect(
host="localhost",
user="root",
password="",
database="goodgarden"
)
from db_connect import database_connect
def fetch_battery_voltage_events():
url = "https://garden.inajar.nl/api/battery_voltage_events/?format=json"
@@ -24,9 +17,8 @@ def fetch_battery_voltage_events():
data = response.json()
load_data(data)
# Wacht voor een bepaalde tijd (bijvoorbeeld 60 seconden) voordat je de volgende oproep doet
print("Wachten voor de volgende ophaalactie...")
time.sleep(60) # De tijd hier is in seconden.
time.sleep(300) # De tijd hier is in seconden.
except requests.exceptions.RequestException as e:
print(f"Error fetching data: {e}")

63
script/par_events.py Normal file
View File

@@ -0,0 +1,63 @@
import requests
import time
from db_connect import database_connect
def fetch_and_display_all(urls, access_token):
for url in urls:
try:
headers = {
"Authorization": f"Token {access_token}"
}
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}")
print("Waiting for the next retrieval action...")
time.sleep(300) # 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.par_events (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']:
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}")
# 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/par_events/?format=json"
]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
fetch_and_display_all(urls, access_token)

View File

@@ -0,0 +1,63 @@
import requests
import time
from db_connect import database_connect
def fetch_and_display_all(urls, access_token):
for url in urls:
try:
headers = {
"Authorization": f"Token {access_token}"
}
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}")
print("Waiting for the next retrieval action...")
time.sleep(300) # 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.relative_humidity_events (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']:
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}")
# 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/relative_humidity_events/?format=json"
]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
fetch_and_display_all(urls, access_token)

View File

@@ -0,0 +1,63 @@
import requests
import time
from db_connect import database_connect
def fetch_and_display_all(urls, access_token):
for url in urls:
try:
headers = {
"Authorization": f"Token {access_token}"
}
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}")
print("Waiting for the next retrieval action...")
time.sleep(300) # 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.soil_electric_conductivity_events (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']:
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}")
# 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/soil_electric_conductivity_events/?format=json"
]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
fetch_and_display_all(urls, access_token)

View File

@@ -0,0 +1,65 @@
import requests
import time
from db_connect import database_connect
# connection = database_connect()
def fetch_and_display_all(urls, access_token):
for url in urls:
try:
headers = {
"Authorization": f"Token {access_token}"
}
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}")
print("Waiting for the next retrieval action...")
time.sleep(300) # 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.soil_relative_permittivity_events (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']:
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}")
# 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/soil_relative_permittivity_events/?format=json"
]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
fetch_and_display_all(urls, access_token)

View File

@@ -0,0 +1,65 @@
import requests
import time
from db_connect import database_connect
# connection = database_connect()
def fetch_and_display_all(urls, access_token):
for url in urls:
try:
headers = {
"Authorization": f"Token {access_token}"
}
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}")
print("Waiting for the next retrieval action...")
time.sleep(300) # 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.soil_temperature_events (timestamp, gateway_receive_time, device, value)
VALUES (%s, %s, %s, %s)
"""
for record in data['results']:
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}")
# 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/soil_temperature_events/?format=json"
]
access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c"
fetch_and_display_all(urls, access_token)