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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. " gps TEXT," \
  20. "plantName TEXT)"
  21. self.cur.execute(table_config)
  22. table_config = "CREATE TABLE IF NOT EXISTS measurement_values " \
  23. "(measurement_id INTEGER PRIMARY KEY AUTOINCREMENT," \
  24. "Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP," \
  25. "plant_ID INTEGER, " \
  26. "sensordata_temp REAL," \
  27. "sensordata_humidity REAL," \
  28. "sensordata_soil_moisture REAL," \
  29. "pest_infestation INTEGER," \
  30. "light_intensity REAL," \
  31. "FOREIGN KEY (plant_ID)" \
  32. " REFERENCES plants (plant_ID) )"
  33. self.cur.execute(table_config)
  34. def insert_plant(self, _gps: str, plantname):
  35. self.cur.execute("INSERT INTO plants (gps, plantName) VALUES (?,?)", (_gps, plantname))
  36. self.conn.commit()
  37. def insert_measurement_data(self, plant_id,
  38. sensordata_temp,
  39. sensordata_humidity,
  40. sensordata_soil_moisture,
  41. pest_infestation,
  42. light_intensity):
  43. self.cur.execute(f"INSERT INTO measurement_values (plant_ID, sensordata_temp, sensordata_humidity,"
  44. f" sensordata_soil_moisture, pest_infestation, light_intensity) VALUES "
  45. f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_soil_moisture}, {pest_infestation}"
  46. f", {light_intensity})")
  47. self.conn.commit()
  48. def get_latest_data(self, plant_id) -> dict:
  49. """
  50. Gets the newest parameter of specific plant and returns all parameters in json format
  51. :param plant_id:
  52. :return:
  53. """
  54. self.cur.execute(f"SELECT * FROM measurement_values where plant_ID = {plant_id} ORDER BY Timestamp DESC LIMIT 1")
  55. data = self.cur.fetchone()
  56. self.cur.execute(f"SELECT plantName FROM plants where plant_ID = {plant_id}")
  57. name = self.cur.fetchone()
  58. print(data)
  59. print(name[0])
  60. json_file = {
  61. "MeasurementID": data[0],
  62. "PlantID": data[2],
  63. "Timestamp": data[1],
  64. "AirTemperature": data[3],
  65. "AirHumidity": data[4],
  66. "SoilMoisture": data[5],
  67. "Brightness": data[7],
  68. "PlantName": name
  69. }
  70. return json_file
  71. def delete_data(self, table_name):
  72. self.cur.execute(f"DELETE FROM {table_name}")
  73. self.conn.commit()