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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. 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. "pest_infestation INTEGER," \
  29. "light_intensity REAL," \
  30. "FOREIGN KEY (plant_ID)" \
  31. " REFERENCES plants (plant_ID) )"
  32. self.cur.execute(table_config)
  33. def insert_plant(self, gps: str, plant_type: str):
  34. self.cur.execute(f"INSERT INTO plants (gps, plant_type) VALUES ({gps}, {plant_type})")
  35. self.conn.commit()
  36. def insert_measurement_data(self, plant_id,
  37. sensordata_temp,
  38. sensordata_humidity,
  39. sensordata_soil_moisture,
  40. pest_infestation,
  41. light_intensity):
  42. self.cur.execute(f"INSERT INTO measurement_values (plant_ID, sensordata_temp, sensordata_humidity,"
  43. f" sensordata_soil_moisture, pest_infestation, light_intensity) VALUES "
  44. f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_soil_moisture}, {pest_infestation}"
  45. f", {light_intensity})")
  46. self.conn.commit()
  47. def get_latest_data(self, plant_id) -> dict:
  48. """
  49. Gets the newest parameter of specific plant and returns all parameters in json format
  50. :param plant_id:
  51. :return:
  52. """
  53. self.cur.execute(f"SELECT * FROM measurement_values where plant_ID = {plant_id} ORDER BY Timestamp DESC LIMIT 1")
  54. data = self.cur.fetchone()
  55. json_file = {
  56. "measurement_id": data[0],
  57. "plant_id": data[2],
  58. "timestamp": data[1],
  59. "sensordata_temp": data[3],
  60. "sensordata_humidity": data[4],
  61. "sensordata_soil_moisture": data[5],
  62. "pest_infestation": data[6],
  63. "light_intensity": data[7]
  64. }
  65. return json_file
  66. def delete_data(self, table_name):
  67. self.cur.execute(f"DELETE FROM {table_name}")
  68. self.conn.commit()