repository to manage all files related to the makeathon farm bot project (Software + Documentation).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plantdatabase.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # file to create a database via python script
  2. import sqlite3
  3. class PlantDataBase:
  4. """
  5. Class to create Makeathon database
  6. """
  7. def __init__(self):
  8. self.db_file = 'backend_database.db'
  9. self.conn = None
  10. try:
  11. self.conn = sqlite3.connect(self.db_file, check_same_thread=False)
  12. print(sqlite3.version)
  13. except sqlite3.Error as e:
  14. print(e)
  15. self.cur = self.conn.cursor()
  16. def create_table(self):
  17. table_config = "CREATE TABLE IF NOT EXISTS plants " \
  18. "(plant_ID INTEGER PRIMARY KEY AUTOINCREMENT," \
  19. "plantName TEXT)"
  20. self.cur.execute(table_config)
  21. table_config = "CREATE TABLE IF NOT EXISTS measurement_values " \
  22. "(measurement_id INTEGER PRIMARY KEY AUTOINCREMENT," \
  23. "Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP," \
  24. "plant_ID INTEGER, " \
  25. "sensordata_temp REAL," \
  26. "sensordata_humidity REAL," \
  27. "sensordata_soil_moisture REAL," \
  28. "light_intensity REAL," \
  29. "FOREIGN KEY (plant_ID)" \
  30. "REFERENCES plants (plant_ID) )"
  31. self.cur.execute(table_config)
  32. def insert_plant(self, _gps: str, plantname):
  33. self.cur.execute("INSERT INTO plants (gps, plantName) VALUES (?,?)", (_gps, plantname))
  34. self.conn.commit()
  35. def insert_measurement_data(self, plant_id,
  36. sensordata_temp,
  37. sensordata_humidity,
  38. sensordata_soil_moisture,
  39. light_intensity):
  40. self.cur.execute(f"INSERT INTO measurement_values (plant_ID, sensordata_temp, sensordata_humidity,"
  41. f" sensordata_soil_moisture, light_intensity) VALUES "
  42. f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_soil_moisture}"
  43. f", {light_intensity})")
  44. self.conn.commit()
  45. def get_latest_data(self, plant_id) -> dict:
  46. """
  47. Gets the newest parameter of specific plant and returns all parameters in json format
  48. :param plant_id:
  49. :return:
  50. """
  51. self.cur.execute(f"SELECT * FROM measurement_values where plant_ID = {plant_id} ORDER BY Timestamp DESC LIMIT 1")
  52. data = self.cur.fetchone()
  53. self.cur.execute(f"SELECT plantName FROM plants where plant_ID = {plant_id}")
  54. name = self.cur.fetchone()
  55. print(data)
  56. print(name[0])
  57. json_file = {
  58. "MeasurementID": data[0],
  59. "PlantID": data[2],
  60. "Timestamp": data[1],
  61. "AirTemperature": data[3],
  62. "AirHumidity": data[4],
  63. "SoilMoisture": data[5],
  64. "Brightness": data[6],
  65. "PlantName": name
  66. }
  67. return json_file
  68. def delete_data(self, table_name):
  69. self.cur.execute(f"DELETE FROM {table_name}")
  70. self.conn.commit()
  71. # TODO: Kemals Scheiß implementieren
  72. def delete_plant(self, plant_id):
  73. self.cur.execute('DELETE FROM plants WHERE plant_ID = ?', (plant_id,))
  74. self.conn.commit()