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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # file to create a database via python script
  2. import sqlite3
  3. from typing import Optional
  4. import logging
  5. class PlantDataBase:
  6. """
  7. Class to create Makeathon database
  8. """
  9. def __init__(self, database_name: str):
  10. self.db_file = database_name # 'backend_database.db'
  11. self.conn = None
  12. try:
  13. self.conn = sqlite3.connect(self.db_file, check_same_thread=False)
  14. except sqlite3.Error as e:
  15. logging.error("Database init error: " + str(e))
  16. self.cur = self.conn.cursor()
  17. def create_tables(self):
  18. try:
  19. table_config = "CREATE TABLE IF NOT EXISTS plants " \
  20. "(PlantID INTEGER PRIMARY KEY," \
  21. "PlantName TEXT)"
  22. self.cur.execute(table_config)
  23. table_config = "CREATE TABLE IF NOT EXISTS measurement_values " \
  24. "(measurementID INTEGER PRIMARY KEY," \
  25. "Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP," \
  26. "PlantID INTEGER, " \
  27. "AirTemperature REAL," \
  28. "AirHumidity REAL," \
  29. "SoilMoisture REAL," \
  30. "Brightness REAL," \
  31. "FOREIGN KEY (PlantID)" \
  32. "REFERENCES plants (PlantID) )"
  33. self.cur.execute(table_config)
  34. return True
  35. except sqlite3.Warning as e:
  36. logging.error("Could not create tables: " + str(e))
  37. return False
  38. def insert_plant(self, plantname: str, plant_id: int) -> bool:
  39. try:
  40. self.cur.execute("INSERT INTO plants (PlantName, PlantID) VALUES (?,?)", (plantname, plant_id))
  41. self.conn.commit()
  42. return True
  43. except Exception as e:
  44. logging.error("Could not insert plant: " + str(e))
  45. return False
  46. def configure_plant(self, plant_id: int, plantname: str) -> bool:
  47. try:
  48. self.cur.execute("UPDATE plants SET PlantID = ?, PlantName = ? WHERE PlantID= ?",
  49. (plant_id, plantname, plant_id))
  50. self.conn.commit()
  51. return True
  52. except Exception as e:
  53. logging.error("Could not configure plant: " + str(e))
  54. return False
  55. def delete_plant(self, plant_id):
  56. try:
  57. self.cur.execute('DELETE FROM plants WHERE PlantID = ?', (plant_id,))
  58. self.conn.commit()
  59. return True
  60. except Exception as e:
  61. logging.error("Could not delete plant: " + str(e))
  62. return False
  63. def insert_measurement_data(self, plant_id,
  64. sensordata_temp,
  65. sensordata_humidity,
  66. sensordata_soil_moisture,
  67. sensordata_brightness) -> bool:
  68. try:
  69. self.cur.execute(f"INSERT INTO measurement_values (PlantID, AirTemperature, AirHumidity,"
  70. f"SoilMoisture, Brightness) VALUES "
  71. f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_soil_moisture}"
  72. f", {sensordata_brightness})")
  73. self.conn.commit()
  74. return True
  75. except Exception as e:
  76. logging.error("Could not insert measurement data: " + str(e))
  77. return False
  78. def get_latest_data(self, plant_name: Optional[str] = None, plant_id: Optional[int] = None):
  79. """
  80. Gets the newest parameter of specific plant and returns all parameters in json format
  81. :param plant_id:
  82. :param plant_name:
  83. :return:
  84. """
  85. try:
  86. if plant_name is not None and plant_id is None:
  87. self.cur.execute("SELECT PlantID FROM plants where PlantName = ?", (plant_name,))
  88. plant_id = self.cur.fetchone()[0]
  89. elif (plant_id is not None and plant_name is not None) or (plant_id is None and plant_name is None):
  90. raise TypeError("Can't pass plant_id and plant_name to the function. Just one allowed !")
  91. self.cur.execute("SELECT * FROM measurement_values where PlantID = ? ORDER BY Timestamp DESC LIMIT 1",
  92. (plant_id,))
  93. data = self.cur.fetchone()
  94. json_file = {
  95. "MeasurementID": data[0],
  96. "PlantID": data[2],
  97. "Timestamp": data[1],
  98. "AirTemperature": data[3],
  99. "AirHumidity": data[4],
  100. "SoilMoisture": data[5],
  101. "Brightness": data[6],
  102. "PlantName": plant_name
  103. }
  104. return json_file
  105. except Exception as e:
  106. logging.error("Could not get measurement values: " + str(e))
  107. def delete_data(self, table_name):
  108. try:
  109. self.cur.execute(f'DELETE FROM {table_name}')
  110. self.conn.commit()
  111. return True
  112. except Exception as e:
  113. logging.error("Could not delete data: " + str(e))
  114. def plant_count(self) -> int:
  115. """
  116. returns the number of plants registered in the database
  117. :return:
  118. """
  119. try:
  120. self.cur.execute("SELECT COUNT(*) FROM plants")
  121. return self.cur.fetchone()[0]
  122. except Exception as e:
  123. logging.error("Could not count plants: " + str(e))
  124. def get_plant_names(self) -> list:
  125. try:
  126. self.cur.execute("SELECT PlantName FROM plants")
  127. return self.cur.fetchall()
  128. except Exception as e:
  129. logging.error("Could not get plant names: " + str(e))
  130. def get_plant_id(self, plant_name: str) -> int:
  131. try:
  132. self.cur.execute("SELECT PlantID FROM plants WHERE PlantName=?", (plant_name,))
  133. return self.cur.fetchone()[0]
  134. except Exception as e:
  135. logging.error("Could not get plant id: " + str(e))
  136. def __del__(self):
  137. self.conn.close()