Merge branch 'main' into mohammed
This commit is contained in:
11
app.js
11
app.js
@@ -1,11 +0,0 @@
|
|||||||
function closeWindow() {
|
|
||||||
window.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
document.getElementById("close").addEventListener("click", closeWindow);
|
|
||||||
|
|
||||||
function minimizeWindow() {
|
|
||||||
remote.getCurrentWindow().minimize();
|
|
||||||
}
|
|
||||||
// werkt niet!!!
|
|
||||||
document.getElementById("minimize").addEventListener("click", minimizeWindow);
|
|
||||||
@@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS goodgarden.sensor_data (
|
|||||||
PRIMARY KEY (id)
|
PRIMARY KEY (id)
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Invoegen van gegevens in de 'sensor_data'-tabel
|
Invoegen van gegevens in de 'sensor_data'-tabel
|
||||||
INSERT INTO goodgarden.sensor_data (timestamp, gateway_receive_time, device, value)
|
INSERT INTO goodgarden.sensor_data (timestamp, gateway_receive_time, device, value)
|
||||||
VALUES
|
VALUES
|
||||||
(1707295162, '2024-02-07T08:39:22Z', 256, 4.107448101043701),
|
(1707295162, '2024-02-07T08:39:22Z', 256, 4.107448101043701),
|
||||||
|
|||||||
101
index.py
101
index.py
@@ -2,65 +2,68 @@ import mysql.connector
|
|||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
# API-verzoek
|
|
||||||
url = "https://garden.inajar.nl/api/battery_voltage_events/?format=json"
|
|
||||||
headers = {
|
|
||||||
"Authorization": "Token 33bb3b42452306c58ecedc3c86cfae28ba22329c"
|
|
||||||
}
|
|
||||||
|
|
||||||
response = requests.get(url, headers=headers)
|
|
||||||
response.raise_for_status()
|
|
||||||
|
|
||||||
data = response.json().get('results', [])
|
def database_connect():
|
||||||
|
return mysql.connector.connect(
|
||||||
|
host="localhost",
|
||||||
|
user="root",
|
||||||
|
password="",
|
||||||
|
database="goodgarden"
|
||||||
|
)
|
||||||
|
|
||||||
print("API-reactie:")
|
def fetch_data():
|
||||||
print(data)
|
url = "https://garden.inajar.nl/api/battery_voltage_events/?format=json"
|
||||||
|
headers = {
|
||||||
|
"Authorization": "Token 33bb3b42452306c58ecedc3c86cfae28ba22329c"
|
||||||
|
}
|
||||||
|
|
||||||
if not isinstance(data, list):
|
while True:
|
||||||
raise ValueError("De API-reactie wordt niet herkend als een lijst van dictionaries.")
|
try:
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
# Verbinding maken met de database
|
data = response.json()
|
||||||
connection = mysql.connector.connect(
|
load_data(data)
|
||||||
host="localhost",
|
|
||||||
user="root",
|
|
||||||
password="",
|
|
||||||
database="goodgarden"
|
|
||||||
)
|
|
||||||
|
|
||||||
if connection.is_connected():
|
# Wacht voor een bepaalde tijd (bijvoorbeeld 60 seconden) voordat je de volgende oproep doet
|
||||||
cursor = connection.cursor()
|
print("Wachten voor de volgende ophaalactie...")
|
||||||
|
time.sleep(60) # De tijd hier is in seconden.
|
||||||
|
|
||||||
# Database bijwerken met API-gegevens
|
except requests.exceptions.RequestException as e:
|
||||||
for entry in data:
|
print(f"Error fetching data: {e}")
|
||||||
timestamp = entry.get("timestamp")
|
# Wacht ook hier bij een fout, om niet in een snelle foutloop te komen
|
||||||
gateway_receive_time = entry.get("gateway_receive_time")
|
time.sleep(300)
|
||||||
device = entry.get("device")
|
|
||||||
value = entry.get("value")
|
|
||||||
|
|
||||||
# Gebruik van prepared statements om SQL-injectie te voorkomen
|
def load_data(data):
|
||||||
sql_update_query = (
|
mydb = database_connect()
|
||||||
"UPDATE goodgarden.sensor_data "
|
if mydb.is_connected():
|
||||||
"SET timestamp=%s, gateway_receive_time=%s, device=%s "
|
mycursor = mydb.cursor()
|
||||||
"WHERE value=%s"
|
|
||||||
)
|
|
||||||
cursor.execute(sql_update_query, (timestamp, gateway_receive_time, device, value))
|
|
||||||
connection.commit()
|
|
||||||
|
|
||||||
print("Database succesvol bijgewerkt")
|
# 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']
|
||||||
|
|
||||||
except Exception as e:
|
print(f"Inserting data: timestamp={timestamp}, gateway_receive_time={gateway_receive_time}, device={device}, value={value}") # Print de data die wordt ingevoegd
|
||||||
print(f"Fout bijwerken database: {e}")
|
|
||||||
|
|
||||||
finally:
|
# Voer de query uit
|
||||||
if connection.is_connected():
|
mycursor.execute(insert_query, (timestamp, gateway_receive_time, device, value))
|
||||||
cursor.close()
|
|
||||||
connection.close()
|
|
||||||
print("MySQL-verbinding is gesloten")
|
|
||||||
|
|
||||||
# Voeg deze regel toe binnen de while-loop
|
# Commit de wijzigingen
|
||||||
print("Aantal gegevens uit de API:", len(data))
|
mydb.commit()
|
||||||
|
|
||||||
# Voeg een pauze toe van 10 minuten voordat de lus opnieuw wordt uitgevoerd
|
# Sluit cursor en verbinding
|
||||||
time.sleep(600)
|
mycursor.close()
|
||||||
|
mydb.close()
|
||||||
|
|
||||||
|
print("Data ingevoegd in de database.")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
fetch_data()
|
||||||
|
|||||||
51
main.js
51
main.js
@@ -1,46 +1,39 @@
|
|||||||
const { app, BrowserWindow, nativeImage, shell } = require("electron");
|
const { app, BrowserWindow, nativeImage, shell } = require("electron");
|
||||||
const screen = require("electron").screen;
|
// const screen = require("electron").screen;
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
|
|
||||||
let mainWindow;
|
let mainWindow;
|
||||||
|
|
||||||
function createWindow() {
|
function createWindow()
|
||||||
const iconPath = path.join(__dirname, "images/logo.png");
|
{
|
||||||
const icon = nativeImage.createFromPath(iconPath);
|
// const iconPath = path.join(__dirname, "images/logo.png");
|
||||||
|
// const icon = nativeImage.createFromPath(iconPath);
|
||||||
|
const urlElectron = path.join(__dirname, "src/main.html");
|
||||||
|
|
||||||
mainWindow = new BrowserWindow({
|
mainWindow = new BrowserWindow(
|
||||||
width: 450,
|
{
|
||||||
height: 500, //moet 500 zijn als frame false is
|
width: 1000,
|
||||||
webPreferences: {
|
height: 650,
|
||||||
|
webPreferences:
|
||||||
|
{
|
||||||
nodeIntegration: true,
|
nodeIntegration: true,
|
||||||
},
|
},
|
||||||
resizable: false,
|
resizable: false,
|
||||||
frame: false,
|
frame: true,
|
||||||
icon: icon,
|
// icon: icon,
|
||||||
alwaysOnTop: true,
|
alwaysOnTop: true,
|
||||||
backgroundColor: "red"
|
// backgroundColor: "green"
|
||||||
});
|
});
|
||||||
|
|
||||||
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
|
// const { width, height } = screen.getPrimaryDisplay().workAreaSize;
|
||||||
mainWindow.setPosition(width - 450, height - 500);
|
// mainWindow.setPosition(width - 450, height - 500);
|
||||||
|
|
||||||
mainWindow.loadURL("C:/Users/oomen/OneDrive - mboRijnland/Schooljaar 23-24/P3/ALA/GoodGarden/index.html");
|
mainWindow.loadURL(urlElectron);
|
||||||
|
|
||||||
mainWindow.on("closed", function () {
|
// mainWindow.on("closed", function ()
|
||||||
mainWindow = null;
|
// {
|
||||||
});
|
// mainWindow = null;
|
||||||
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
app.on("ready", createWindow);
|
app.on("ready", createWindow);
|
||||||
|
|
||||||
app.on("window-all-closed", function () {
|
|
||||||
if (process.platform !== "darwin") {
|
|
||||||
app.quit();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.on("activate", function () {
|
|
||||||
if (mainWindow === null) {
|
|
||||||
createWindow();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|||||||
265
package-lock.json
generated
265
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"name": "app",
|
"name": "goodgarden",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "main.js",
|
"main": "main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@@ -9,6 +9,9 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"electron": "^23.1.1"
|
"electron": "^23.3.13"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"elctron": "^0.0.1-security"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,6 @@
|
|||||||
<title>Document</title>
|
<title>Document</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>HALLO WERELD!!!!!</h1>
|
<h1>sdgdfsgjindgfvadsiuuiadsiuhdgnhaewgiadsnugaeuigndfogjdfsgdff</h1>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user