Browse Source

FIXED Backend functions

master
caliskan 1 year ago
parent
commit
c35028f354

BIN
software/backend/backend_database.db View File


+ 18
- 9
software/backend/data_functions.py View File

from software.defines import Topics, MAX_PLANT_COUNT from software.defines import Topics, MAX_PLANT_COUNT
import json import json
import uuid import uuid
from typing import Union




# Robot Channel Reactions # Robot Channel Reactions
client.publish(Topics['ROBOT_ACTION_GETBATTERY']) client.publish(Topics['ROBOT_ACTION_GETBATTERY'])




def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
plant_names = json.loads(message.payload.decode("UTF-8"))
print(plant_names)
def action_getalldata(client: mqtt.Client, userdata, message: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase):
plant_names = mydatabase.get_plant_names()
print(type(plant_names))
alldata = [] alldata = []
for i in plant_names: for i in plant_names:
alldata.append(mydatabase.get_latest_data(plant_name=i))
print("I Type: " + str(type(i)))
print("I: " + i[0])
alldata.append(mydatabase.get_latest_data(plant_name=i[0]))
client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata))
print("BACKEND_DATA_SENSORDATAALL SEND DATA:" + str(alldata))




def action_newplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): def action_newplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
sensordata_soil_moisture=plant_data["SoilMoisture"], sensordata_soil_moisture=plant_data["SoilMoisture"],
sensordata_brightness=plant_data["Brightness"]) sensordata_brightness=plant_data["Brightness"])
print("BACKEND_ACTION_NEWPLANT RECEIVED DATA: " + str(plant_data)) print("BACKEND_ACTION_NEWPLANT RECEIVED DATA: " + str(plant_data))
print(mydatabase.get_plant_names())
action_getalldata(client, userdata, message, mydatabase)




def action_configureplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): def action_configureplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
plant_data = json.loads(message.payload.decode("UTF-8")) plant_data = json.loads(message.payload.decode("UTF-8"))
mydatabase.configure_plant(plant_id=plant_data["plant_ID"], plantname=plant_data["PlantName"])
mydatabase.configure_plant(plant_id=plant_data["PlantID"], plantname=plant_data["PlantName"])
mydatabase.insert_measurement_data(plant_id=plant_data["PlantID"], mydatabase.insert_measurement_data(plant_id=plant_data["PlantID"],
sensordata_temp=plant_data["AirTemperature"], sensordata_temp=plant_data["AirTemperature"],
sensordata_humidity=plant_data["AirHumidity"], sensordata_humidity=plant_data["AirHumidity"],
sensordata_soil_moisture=plant_data["SoilMoisture"], sensordata_soil_moisture=plant_data["SoilMoisture"],
sensordata_brightness=plant_data["Brightness"]) sensordata_brightness=plant_data["Brightness"])
print("BACKEND_ACTION_CONFIGUREPLANT RECEIVED DATA: " + str(plant_data)) print("BACKEND_ACTION_CONFIGUREPLANT RECEIVED DATA: " + str(plant_data))
action_getalldata(client, userdata, message, mydatabase)




def action_deleteplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): def action_deleteplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
delete_plant = json.loads(message.payload.decode("UTF-8")) delete_plant = json.loads(message.payload.decode("UTF-8"))
mydatabase.delete_plant(plant_id=delete_plant["PlantID"])
mydatabase.delete_plant(plant_id=delete_plant)
print("BACKEND_ACTION_DELETEPLANT RECEIVED DATA: " + str(delete_plant)) print("BACKEND_ACTION_DELETEPLANT RECEIVED DATA: " + str(delete_plant))
action_getalldata(client, userdata, message, mydatabase)




def action_countplants(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): def action_countplants(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
count = mydatabase.plant_count()
count_payload = { count_payload = {
"CurrentCount": mydatabase.plant_count(),
"CurrentCount": count,
"MaxCount": MAX_PLANT_COUNT "MaxCount": MAX_PLANT_COUNT
} }
print(type(json.dumps(count_payload)))
client.publish(Topics["BACKEND_ACTION_PLANTCOUNT"], json.dumps(count_payload))
client.publish(Topics["BACKEND_DATA_PLANTCOUNT"], json.dumps(count_payload))
print("BACKEND_DATA_PLANTCOUNT SEND DATA: " + str(count_payload))

+ 4
- 0
software/backend/plantdatabase.py View File

self.cur.execute("SELECT COUNT(*) FROM plants") self.cur.execute("SELECT COUNT(*) FROM plants")
return self.cur.fetchone()[0] return self.cur.fetchone()[0]


def get_plant_names(self) -> list:
self.cur.execute("SELECT PlantName FROM plants")
return self.cur.fetchall()

def __del__(self): def __del__(self):
self.conn.close() self.conn.close()

Loading…
Cancel
Save