Order Handler added

This commit is contained in:
caliskan 2023-04-27 19:00:09 +02:00
parent 24268d3039
commit 58bdd595d5
5 changed files with 69 additions and 44 deletions

View File

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.10 (projektarbeit_duelger_waldhauser_caliskan)" jdkType="Python SDK" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -7,41 +7,62 @@ Every function should return json format with the wanted data from the database
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
from plantdatabase import PlantDataBase from plantdatabase import PlantDataBase
from software.defines import Topics from software.defines import Topics
import json
import uuid
# Robot Channel Reactions # Robot Channel Reactions
def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
print("message received") print("message received")
# TODO: Store data in database # TODO: Store data in database
pass str_in = str(message.payload.decode("UTF-8"))
payload = json.loads(str_in)
print("Received data: ", json.dumps(payload))
order_handler.remove(payload['ActionID'])
mydatabase.insert_measurement_data(plant_id=payload['PlantID'],
sensordata_temp=payload['AirTemperature'],
sensordata_humidity=payload['AirHumidity'],
sensordata_soil_moisture=payload['SoilMoisture'],
pest_infestation=0,
light_intensity=payload['Brightness'])
def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: Forward to frontend in json format # TODO: Forward to frontend in json format
pass client.publish(Topics['BACKEND_DATA_POSITION'], message.payload.decode("utf-8"))
def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: Forward to frontend in json format # TODO: Forward to frontend in json format
pass client.publish(Topics['BACKEND_DATA_BATTERY'], message.payload.decode("utf-8"))
# FrontEnd Channel Reactions # FrontEnd Channel Reactions
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list):
# TODO: ROBOT READY CHECK # TODO: ROBOT READY CHECK
client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload).decode("utf-8") if len(order_handler) < 5:
order_handler.append(uuid.uuid4())
else:
# TODO: What to do when no place in order_list left
pass
client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8"))
def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
client.publish(Topics['ROBOT_ACTION_GETPOSITION']) client.publish(Topics['ROBOT_ACTION_GETPOSITION'])
def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
client.publish(Topics['ROBOT_ACTION_GETBATTERY']) client.publish(Topics['ROBOT_ACTION_GETBATTERY'])
def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
# TODO: get data from database # TODO: get data from database
pass alldata = []
for i in range(1, 7):
alldata.append(mydatabase.get_latest_data(plant_id=i))
client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata))

View File

@ -16,47 +16,51 @@ import data_functions
# inits # inits
mydatabase = PlantDataBase() mydatabase = PlantDataBase()
mydatabase.create_table() mydatabase.create_table()
order_handler = [] # will contain UUIDS with Order IDs
def on_connect(client: mqtt.Client, userdata, flags, rc): def on_connect(_client: mqtt.Client, _userdata, _flags, _rc):
""" """
This method gets called, when it connects to a mqtt broker. This method gets called, when it connects to a mqtt broker.
It is used to subscribe to the specific topics It is used to subscribe to the specific topics
:param client: mqtt client object :param _client: mqtt client object
:param userdata: :param _userdata:
:param flags: :param _flags:
:param rc: connection flag :param _rc: connection flag
:return: :return:
""" """
if rc == 0: if _rc == 0:
print("connected") print("connected")
# TOPIC SUBSCRIPTIONS # TOPIC SUBSCRIPTIONS
# From Robot: # From Robot:
client.subscribe(Topics['ROBOT_DATA_SENSORDATA']) _client.subscribe(Topics['ROBOT_DATA_SENSORDATA'])
client.message_callback_add(Topics['ROBOT_DATA_SENSORDATA'], data_functions.data_sensordata) _client.message_callback_add(Topics['ROBOT_DATA_SENSORDATA'], lambda client, userdata, message: data_functions.
data_sensordata(client, userdata, message, mydatabase, order_handler))
client.subscribe(Topics['ROBOT_DATA_POSITION']) _client.subscribe(Topics['ROBOT_DATA_POSITION'])
client.message_callback_add(Topics['ROBOT_DATA_POSITION'], data_functions.data_position) _client.message_callback_add(Topics['ROBOT_DATA_POSITION'], data_functions.data_position)
client.subscribe(Topics['ROBOT_DATA_BATTERY']) _client.subscribe(Topics['ROBOT_DATA_BATTERY'])
client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], data_functions.data_sensordata) _client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], lambda client, userdata, message: data_functions.
data_battery(client, userdata, message, mydatabase))
# client.subscribe('Robot/Data/Picture') # client.subscribe('Robot/Data/Picture')
# From FrontEnd: # From FrontEnd:
client.subscribe(Topics['BACKEND_ACTION_DRIVE']) _client.subscribe(Topics['BACKEND_ACTION_DRIVE'])
client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], data_functions.action_drive) _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_GETPOSITION']) _client.subscribe(Topics['BACKEND_ACTION_GETPOSITION'])
client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition) _client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition)
client.subscribe(Topics['BACKEND_ACTION_GETBATTERY']) _client.subscribe(Topics['BACKEND_ACTION_GETBATTERY'])
client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery) _client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery)
client.subscribe(Topics['BACKEND_ACTION_GETALLDATA']) _client.subscribe(Topics['BACKEND_ACTION_GETALLDATA'])
client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], data_functions.action_getalldata) _client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], data_functions.action_getalldata)
# END TOPIC SUBSCRIPTIONS # END TOPIC SUBSCRIPTIONS
else: else:

View File

@ -33,15 +33,14 @@ Topics = {
} }
# MQTT Messages: # MQTT Messages:
# region Robot -> Backend # region Robot -> Backend
SENSORDATA = { SENSORDATA = {
"AirTemperature": 0.0, "AirTemperature": 0.0,
"AirHumidity" : 0.0, "AirHumidity": 0.0,
"SoilMoisture" : 0.0, "SoilMoisture": 0.0,
"Brightness" : 0, "Brightness": 0,
"PlantID": 0, "PlantID": 0,
"ActionID": 0 "ActionID": 0
} }
@ -74,9 +73,9 @@ DRIVE = {
PLANTDATA = { PLANTDATA = {
"AirTemperature": 0.0, "AirTemperature": 0.0,
"AirHumidity" : 0.0, "AirHumidity": 0.0,
"SoilMoisture" : 0.0, "SoilMoisture": 0.0,
"Brightness" : 0, "Brightness": 0,
"PlantID": 0, "PlantID": 0,
"Timestamp": "", "Timestamp": "",
"MeasurementID": 0 "MeasurementID": 0

View File

@ -1,10 +1,11 @@
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import json import json
def on_message_json(client, userdata, message): def on_message_json(client, userdata, message):
strIn = str(message.payload.decode("UTF-8")) strIn = str(message.payload.decode("UTF-8"))
dataDict = json.loads(strIn) dataDict = json.loads(strIn)
print("Received data: ", json.dumps(dataDict)) print("Received data: ", type(dataDict))
mqttBroker = "mqtt.eclipseprojects.io" mqttBroker = "mqtt.eclipseprojects.io"