From 4446d112299cb3d601c021e47af2a0e3dbef98ae Mon Sep 17 00:00:00 2001 From: Burak <6028083@mborijnland.nl> Date: Tue, 13 Feb 2024 14:23:30 +0100 Subject: [PATCH 1/2] Fect Aanpassen --- fetch.py | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ goodGarden.sql | 28 +++++++-------- index.py | 67 ---------------------------------- 3 files changed, 111 insertions(+), 81 deletions(-) create mode 100644 fetch.py diff --git a/fetch.py b/fetch.py new file mode 100644 index 0000000..822facd --- /dev/null +++ b/fetch.py @@ -0,0 +1,97 @@ +import mysql.connector +import requests +import time + +def database_connect(): + return mysql.connector.connect( + host="localhost", + user="root", + password="", + database="goodgarden" + ) + +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) diff --git a/goodGarden.sql b/goodGarden.sql index de6d9d1..1319274 100644 --- a/goodGarden.sql +++ b/goodGarden.sql @@ -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); \ No newline at end of file diff --git a/index.py b/index.py index 0bc2059..e69de29 100644 --- a/index.py +++ b/index.py @@ -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() \ No newline at end of file From 54139a803e50054ed17288e82d4177d963eca823 Mon Sep 17 00:00:00 2001 From: Burak <6028083@mborijnland.nl> Date: Wed, 14 Feb 2024 11:20:30 +0100 Subject: [PATCH 2/2] Update voor API --- battery_voltage_events.py | 72 +++++++++++++++++++++++++++ devices.py | 73 ++++++++++++++++++++++++++++ par_events.py | 71 +++++++++++++++++++++++++++ relative_humidity_events.py | 71 +++++++++++++++++++++++++++ soil_electric_conductivity_events.py | 72 +++++++++++++++++++++++++++ soil_relative_permittivity_events.py | 72 +++++++++++++++++++++++++++ soil_temperature_events.py | 72 +++++++++++++++++++++++++++ 7 files changed, 503 insertions(+) create mode 100644 battery_voltage_events.py create mode 100644 devices.py create mode 100644 par_events.py create mode 100644 relative_humidity_events.py create mode 100644 soil_electric_conductivity_events.py create mode 100644 soil_relative_permittivity_events.py create mode 100644 soil_temperature_events.py diff --git a/battery_voltage_events.py b/battery_voltage_events.py new file mode 100644 index 0000000..3dee033 --- /dev/null +++ b/battery_voltage_events.py @@ -0,0 +1,72 @@ +import mysql.connector +import requests +import time + +def database_connect(): + return mysql.connector.connect( + host="localhost", + user="root", + password="", + database="goodgarden" + ) + +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.battery_voltage_events (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", + + ] + + access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang dit met jouw echte toegangstoken + + fetch_and_display_all(urls, access_token) diff --git a/devices.py b/devices.py new file mode 100644 index 0000000..83465c8 --- /dev/null +++ b/devices.py @@ -0,0 +1,73 @@ +import mysql.connector +import requests +import time + +def database_connect(): + return mysql.connector.connect( + host="localhost", + user="root", + password="", + database="goodgarden" + ) + +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.devices (serial_number, name, label, last_seen, last_battery_voltage) + VALUES (%s, %s, %s, %s, %s ) + """ + for record in data['results']: # Adjust this based on the actual structure of the JSON + 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}") # Print the data being inserted + + # 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" # Vervang dit met jouw echte toegangstoken + + fetch_and_display_all(urls, access_token) diff --git a/par_events.py b/par_events.py new file mode 100644 index 0000000..c5fa84b --- /dev/null +++ b/par_events.py @@ -0,0 +1,71 @@ +import mysql.connector +import requests +import time + +def database_connect(): + return mysql.connector.connect( + host="localhost", + user="root", + password="", + database="goodgarden" + ) + +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.par_events (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/par_events/?format=json" + ] + + access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang dit met jouw echte toegangstoken + + fetch_and_display_all(urls, access_token) diff --git a/relative_humidity_events.py b/relative_humidity_events.py new file mode 100644 index 0000000..08a3803 --- /dev/null +++ b/relative_humidity_events.py @@ -0,0 +1,71 @@ +import mysql.connector +import requests +import time + +def database_connect(): + return mysql.connector.connect( + host="localhost", + user="root", + password="", + database="goodgarden" + ) + +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.relative_humidity_events (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/relative_humidity_events/?format=json" + ] + + access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang dit met jouw echte toegangstoken + + fetch_and_display_all(urls, access_token) diff --git a/soil_electric_conductivity_events.py b/soil_electric_conductivity_events.py new file mode 100644 index 0000000..af30f78 --- /dev/null +++ b/soil_electric_conductivity_events.py @@ -0,0 +1,72 @@ +import mysql.connector +import requests +import time + +def database_connect(): + return mysql.connector.connect( + host="localhost", + user="root", + password="", + database="goodgarden" + ) + +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.soil_electric_conductivity_events (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/soil_electric_conductivity_events/?format=json" + ] + + access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang dit met jouw echte toegangstoken + + fetch_and_display_all(urls, access_token) diff --git a/soil_relative_permittivity_events.py b/soil_relative_permittivity_events.py new file mode 100644 index 0000000..a7bed71 --- /dev/null +++ b/soil_relative_permittivity_events.py @@ -0,0 +1,72 @@ +import mysql.connector +import requests +import time + +def database_connect(): + return mysql.connector.connect( + host="localhost", + user="root", + password="", + database="goodgarden" + ) + +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.soil_relative_permittivity_events (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/soil_relative_permittivity_events/?format=json" + ] + + access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang dit met jouw echte toegangstoken + + fetch_and_display_all(urls, access_token) diff --git a/soil_temperature_events.py b/soil_temperature_events.py new file mode 100644 index 0000000..78a3dae --- /dev/null +++ b/soil_temperature_events.py @@ -0,0 +1,72 @@ +import mysql.connector +import requests +import time + +def database_connect(): + return mysql.connector.connect( + host="localhost", + user="root", + password="", + database="goodgarden" + ) + +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.soil_temperature_events (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/soil_temperature_events/?format=json" + ] + + access_token = "33bb3b42452306c58ecedc3c86cfae28ba22329c" # Vervang dit met jouw echte toegangstoken + + fetch_and_display_all(urls, access_token)