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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. " plant_type 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_ground_humidity 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, plant_type: str):
  35. self.cur.execute(f"INSERT INTO plants (gps, plant_type) VALUES ({gps}, {plant_type})")
  36. self.conn.commit()
  37. def insert_measurement_data(self, plant_id,
  38. sensordata_temp,
  39. sensordata_humidity,
  40. sensordata_ground_humidity,
  41. pest_infestation,
  42. light_intensity):
  43. self.cur.execute(f"INSERT INTO measurement_values (plant_ID, sensordata_temp, sensordata_humidity,"
  44. f" sensordata_ground_humidity, pest_infestation, light_intensity) VALUES "
  45. f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_ground_humidity}, {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. json_file = {
  57. "measurement_id": data[0],
  58. "plant_id": data[2],
  59. "timestamp": data[1],
  60. "sensordata_temp": data[3],
  61. "sensordata_humidity": data[4],
  62. "sensordata_ground_humidity": data[5],
  63. "pest_infestation": data[6],
  64. "light_intensity": data[7]
  65. }
  66. return json_file
  67. def delete_data(self, table_name):
  68. self.cur.execute(f"DELETE FROM {table_name}")
  69. self.conn.commit()