Browse Source

new topics added

master
caliskanbi 1 year ago
parent
commit
0dc80b8981

BIN
documentation/Definitions.docx View File


+ 24
- 2
software/backend/data_functions.py View File

@@ -13,7 +13,8 @@ import uuid

# Robot Channel Reactions

def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
order_handler: list):
print("message received")
# TODO: Store data in database
str_in = str(message.payload.decode("UTF-8"))
@@ -42,7 +43,8 @@ def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydat

# FrontEnd Channel Reactions

def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
order_handler: list):
# TODO: ROBOT READY CHECK
if len(order_handler) < 5:
order_handler.append(uuid.uuid4())
@@ -52,6 +54,11 @@ def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydat
client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8"))


def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: Implement here
pass


def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
client.publish(Topics['ROBOT_ACTION_GETPOSITION'])

@@ -66,3 +73,18 @@ def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage,
for i in range(1, 7):
alldata.append(mydatabase.get_latest_data(plant_id=i))
client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata))


def action_newplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: insert new plant to database
pass


def action_configureplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: configure plant
pass


def action_deleteplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: delete plant from database (from ID)
pass

+ 2
- 2
software/backend/dev_test_examples/mqtt_publisher.py View File

@@ -4,7 +4,7 @@ from random import randrange, uniform
import time
import json
from software.defines import Topics, PLANTDATA
mqttBroker = "mqtt.eclipseprojects.io"
mqttBroker = "192.168.178.182"


def on_connect(client, userdata, flags, rc):
@@ -31,6 +31,6 @@ plantdata = {

print(type(PLANTDATA))
while True:
client.publish(Topics["BACKEND_DATA_SENSORDATA"], json.dumps(plantdata))
client.publish("TEST", json.dumps(plantdata))
print(json.dumps(plantdata))
time.sleep(2)

+ 3
- 6
software/backend/dev_test_examples/mqtt_subscriber.py View File

@@ -14,15 +14,12 @@ def on_connect(client, userdata, flags, rc):
print("Connection failed")


mqttBroker = "192.168.0.199"
mqttBroker = "192.168.178.182"

client = mqtt.Client()
client.connect(mqttBroker, 1883)

client.loop_start()
client.on_message = on_message

client.subscribe(Topics["BACKEND_DATA_SENSORDATA"])
client.subscribe("Bilal")
time.sleep(30)
client.loop_stop()
client.subscribe("TEST")
client.loop_forever()

+ 20
- 1
software/backend/main.py View File

@@ -53,6 +53,10 @@ def on_connect(_client: mqtt.Client, _userdata, _flags, _rc):
_client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions.
action_drive(client, userdata, message, mydatabase, order_handler))

_client.subscribe(Topics['BACKEND_ACTION_DRIVEALL'])
_client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions.
action_driveall(client, userdata, message, mydatabase))

_client.subscribe(Topics['BACKEND_ACTION_GETPOSITION'])
_client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition)

@@ -60,7 +64,22 @@ def on_connect(_client: mqtt.Client, _userdata, _flags, _rc):
_client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery)

_client.subscribe(Topics['BACKEND_ACTION_GETALLDATA'])
_client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], lambda client, userdata, message: data_functions.action_getalldata(client, userdata, message, mydatabase))
_client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'],
lambda client, userdata, message: data_functions.
action_getalldata(client, userdata, message, mydatabase))

_client.subscribe(Topics['BACKEND_ACTION_NEWPLANT'])
_client.message_callback_add(Topics['BACKEND_ACTION_NEWPLANT'], lambda client, userdata, message: data_functions.
action_newplant(client, userdata, message, mydatabase))

_client.subscribe(Topics['BACKEND_ACTION_CONFIGUREPLANT'])
_client.message_callback_add(Topics['BACKEND_ACTION_CONFIGUREPLANT'], lambda client, userdata, message: data_functions.
action_configureplant(client, userdata, message, mydatabase))

_client.subscribe(Topics['BACKEND_ACTION_DELETEPLANT'])
_client.message_callback_add(Topics['BACKEND_ACTION_DELETEPLANT'],
lambda client, userdata, message: data_functions.
action_deleteplant(client, userdata, message, mydatabase))

# END TOPIC SUBSCRIPTIONS
else:

+ 10
- 7
software/backend/plantdatabase.py View File

@@ -19,7 +19,6 @@ class PlantDataBase:
def create_table(self):
table_config = "CREATE TABLE IF NOT EXISTS plants " \
"(plant_ID INTEGER PRIMARY KEY AUTOINCREMENT," \
" gps TEXT," \
"plantName TEXT)"
self.cur.execute(table_config)

@@ -30,10 +29,9 @@ class PlantDataBase:
"sensordata_temp REAL," \
"sensordata_humidity REAL," \
"sensordata_soil_moisture REAL," \
"pest_infestation INTEGER," \
"light_intensity REAL," \
"FOREIGN KEY (plant_ID)" \
" REFERENCES plants (plant_ID) )"
"REFERENCES plants (plant_ID) )"
self.cur.execute(table_config)

def insert_plant(self, _gps: str, plantname):
@@ -44,11 +42,10 @@ class PlantDataBase:
sensordata_temp,
sensordata_humidity,
sensordata_soil_moisture,
pest_infestation,
light_intensity):
self.cur.execute(f"INSERT INTO measurement_values (plant_ID, sensordata_temp, sensordata_humidity,"
f" sensordata_soil_moisture, pest_infestation, light_intensity) VALUES "
f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_soil_moisture}, {pest_infestation}"
f" sensordata_soil_moisture, light_intensity) VALUES "
f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_soil_moisture}"
f", {light_intensity})")
self.conn.commit()

@@ -71,7 +68,7 @@ class PlantDataBase:
"AirTemperature": data[3],
"AirHumidity": data[4],
"SoilMoisture": data[5],
"Brightness": data[7],
"Brightness": data[6],
"PlantName": name
}
return json_file
@@ -79,3 +76,9 @@ class PlantDataBase:
def delete_data(self, table_name):
self.cur.execute(f"DELETE FROM {table_name}")
self.conn.commit()

# TODO: Kemals Scheiß implementieren

def delete_plant(self, plant_id):
self.cur.execute('DELETE FROM plants WHERE plant_ID = ?', (plant_id,))
self.conn.commit()

+ 24
- 2
software/defines.py View File

@@ -21,15 +21,22 @@ Topics = {
"ROBOT_DATA_PICTURE": "ROBOT/DATA/PICTURE",

"BACKEND_ACTION_DRIVE": "BACKEND/ACTION/DRIVE",
"BACKEND_ACTION_DRIVEPALL": "BACKEND/ACTION/DRIVEALL",
"BACKEND_ACTION_GETPOSITION": "BACKEND/ACTION/GETPOSITION",
"BACKEND_ACTION_GETBATTERY": "BACKEND/ACTION/GETBATTERY",
"BACKEND_ACTION_GETALLDATA": "BACKEND/ACTION/GETALLDATA",
"BACKEND_ACTION_NEWPLANT": "BACKEND/ACTION/NEWPLANT",
"BACKEND_ACTION_CONFIGUREPLANT": "BACKEND/ACTION/CONFIGUREPLANT",
"BACKEND_ACTION_DELETEPLANT": "BACKEND/ACTION/DELETEPLANT",
"BACKEND_ACTION_PLANTCOUNT": "BACKEND/ACTION/PLANTCOUNT",

"BACKEND_DATA_SENSORDATA": "BACKEND/DATA/SENSORDATA",
"BACKEND_DATA_SENSORDATAALL": "BACKEND/DATA/SENSORDATA_ALL",
"BACKEND_DATA_POSITION": "BACKEND/DATA/POSITION",
"BACKEND_DATA_BATTERY": "BACKEND/DATA/BATTERY",
"BACKEND_DATA_PICTURE": "BACKEND/DATA/PICTURE"
"BACKEND_DATA_PICTURE": "BACKEND/DATA/PICTURE",
"BACKEND_DATA_PLANTCOUNT": "BACKEND/DATA/PLANTCOUNT",


}

@@ -102,14 +109,29 @@ BATTERY = {
"Timestamp": ""
}

PLANTCOUNT = {
"CurrenCount": 0,
"maxCount": 0
}

# endregion

# region Frontend -> Backend

DRIVE = {
"PlantID": 0
"PlantName": ""
}

NEWPLANT = PLANTDATA

CONFIGUREPLANT = PLANTDATA

DELETEPLANT = {
"PlantID": ""
}

# DRIVEALL -> no message needed

# GETPOSITION -> no message needed

# GETBATTERY -> no message needed

Loading…
Cancel
Save