""" | |||||
created by caliskan at 19.04.2023 | |||||
This file contains all functions, which handle the different cases. | |||||
Every function should return json format with the wanted data from the database | |||||
""" | |||||
import paho.mqtt.client as mqtt | |||||
from plantdatabase import PlantDataBase | |||||
from software.defines import Topics | |||||
# Robot Channel Reactions | |||||
def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): | |||||
print("message received") | |||||
# TODO: Store data in database | |||||
pass | |||||
def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): | |||||
# TODO: Publish as json | |||||
pass | |||||
def data_battery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): | |||||
# TODO: Publish as json | |||||
pass | |||||
# FrontEnd Channel Reactions | |||||
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): | |||||
client.publish('Robot/Action/Drive', message.payload).decode("utf-8") | |||||
def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): | |||||
client.publish('Robot/Action/GetPosition') | |||||
def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): | |||||
client.publish(Topics.drive) | |||||
def action_getalldata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): | |||||
# TODO: get data from database | |||||
pass |
""" | |||||
created by caliskan at 19.04.2023 | |||||
This file contains all functions, which handle the different cases. | |||||
Every function should return json format with the wanted data from the database | |||||
""" | |||||
import paho.mqtt.client as mqtt | |||||
from plantdatabase import PlantDataBase | |||||
def data_handler(client: mqtt.Client, message: mqtt.MQTTMessage, mydatabase: PlantDataBase) -> None: | |||||
""" | |||||
main entrypoint for a message handling method | |||||
:param mydatabase: database with plant data | |||||
:param client: mqtt client | |||||
:param message: received message | |||||
:return: None | |||||
""" | |||||
if message.topic.startswith('Robot'): | |||||
if message.topic == 'Robot/Action/SensorData': | |||||
data_sensordata(client, message) | |||||
elif message.topic == 'Robot/Action/Position': | |||||
data_position(client, message) | |||||
elif message.topic == 'Robot/Action/Battery': | |||||
data_battery(client, message) | |||||
elif message.topic.startswith('BackEnd'): | |||||
if message.topic == 'BackEnd/Action/Drive': | |||||
action_drive(client, message) | |||||
elif message.topic == 'BackEnd/Action/GetPosition': | |||||
action_getposition(client) | |||||
elif message.topic == 'BackEnd/Action/GetBattery': | |||||
action_getbattery(client) | |||||
elif message.topic == 'BackEnd/Action/GetAllData': | |||||
action_getalldata(client) | |||||
# Robot Channel Reactions | |||||
def data_sensordata(client: mqtt.Client, message: mqtt.MQTTMessage): | |||||
# TODO: Store data in database | |||||
pass | |||||
def data_position(client: mqtt.Client, message: mqtt.MQTTMessage): | |||||
# TODO: Publish as json | |||||
pass | |||||
def data_battery(client: mqtt.Client, message: mqtt.MQTTMessage): | |||||
# TODO: Publish as json | |||||
pass | |||||
# FrontEnd Channel Reactions | |||||
def action_drive(client: mqtt.Client, message: mqtt.MQTTMessage): | |||||
client.publish('Robot/Action/Drive', message.payload).decode("utf-8") | |||||
def action_getposition(client: mqtt.Client): | |||||
client.publish('Robot/Action/GetPosition') | |||||
def action_getbattery(client: mqtt.Client): | |||||
client.publish('Robot/Action/GetBattery') | |||||
def action_getalldata(client: mqtt.Client): | |||||
# TODO: get data from database | |||||
pass |
""" | |||||
created by caliskan at 19.04.2023 | |||||
contains all constants for the backend architecture of the smart garden project | |||||
""" | |||||
MQTT_BROKER_LOCAL = "lorem ipsum" | |||||
MQTT_BROKER_GLOBAL = "mqtt.eclipseprojects.io" | |||||
RASPI_CLIENT_ID = "lorem ipsum" | |||||
BACKEND_CLIENT_ID = "lorem ipsum" |
client.connect(mqttBroker) | client.connect(mqttBroker) | ||||
while True: | while True: | ||||
randNumber = uniform(20.0, 21.0) | |||||
client.publish("Robot/Data/Battery") | |||||
print("Just published " + str(randNumber) + " to topic TEMPERATURE") | |||||
client.publish("ROBOT/ACTION/DRIVE") | |||||
print("Just published to topic TEMPERATURE") | |||||
time.sleep(0.5) | time.sleep(0.5) | ||||
# imports | # imports | ||||
import paho.mqtt.client as mqtt | import paho.mqtt.client as mqtt | ||||
from defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL | |||||
from software.defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL, Topics | |||||
from plantdatabase import PlantDataBase | from plantdatabase import PlantDataBase | ||||
from data_handling_functions import data_handler | |||||
import data_functions | |||||
# inits | # inits | ||||
mydatabase = PlantDataBase() | mydatabase = PlantDataBase() | ||||
mydatabase.create_table() | mydatabase.create_table() | ||||
def on_message(client: mqtt.Client, userdata, message): | |||||
""" | |||||
This method gets called, if a subscribed channel gets a new message. | |||||
Message gets forwarded to the data handler | |||||
:param client: mqtt client object | |||||
:param userdata: | |||||
:param message: received message object | |||||
:return: None | |||||
""" | |||||
print(type(message)) | |||||
print(f'message received! {message.topic}') | |||||
data_handler(client, message, mydatabase) | |||||
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. | ||||
# TOPIC SUBSCRIPTIONS | # TOPIC SUBSCRIPTIONS | ||||
# From Robot: | # From Robot: | ||||
client.subscribe('Robot/Data/SensorData') | |||||
client.subscribe('Robot/Data/Position') | |||||
client.subscribe('Robot/Data/Battery') | |||||
client.subscribe(Topics['robot_data_sensordata']) | |||||
client.message_callback_add(Topics['robot_data_sensordata'], data_functions.data_sensordata) | |||||
client.subscribe(Topics['robot_data_position']) | |||||
client.message_callback_add(Topics['robot_data_position'], data_functions.data_position) | |||||
client.subscribe(Topics['robot_data_battery']) | |||||
client.message_callback_add(Topics['robot_data_battery'], data_functions.data_sensordata) | |||||
# client.subscribe('Robot/Data/Picture') | # client.subscribe('Robot/Data/Picture') | ||||
# From FrontEnd: | # From FrontEnd: | ||||
client.subscribe('BackEnd/Action/Drive') | |||||
client.subscribe('BackEnd/Action/GetPosition') | |||||
client.subscribe('BackEnd/Action/GetBattery') | |||||
client.subscribe('BackEnd/Action/GetAllData') | |||||
client.subscribe(Topics['backend_action_drive']) | |||||
client.message_callback_add(Topics['backend_action_drive'], data_functions.action_drive) | |||||
client.subscribe(Topics['backend_action_getposition']) | |||||
client.message_callback_add(Topics['backend_action_getposition'], data_functions.action_getposition) | |||||
client.subscribe(Topics['backend_action_getbattery']) | |||||
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'], data_functions.action_getalldata) | |||||
# END TOPIC SUBSCRIPTIONS | # END TOPIC SUBSCRIPTIONS | ||||
else: | else: | ||||
print("connection failed") | print("connection failed") | ||||
def main(): | def main(): | ||||
client = mqtt.Client() | client = mqtt.Client() | ||||
client.on_connect = on_connect | client.on_connect = on_connect | ||||
client.on_message = on_message | |||||
client.connect(MQTT_BROKER_GLOBAL) | client.connect(MQTT_BROKER_GLOBAL) | ||||
client.loop_forever() | client.loop_forever() | ||||
""" | |||||
created by caliskan at 19.04.2023 | |||||
contains all constants for the backend architecture of the smart garden project | |||||
""" | |||||
MQTT_BROKER_LOCAL = "lorem ipsum" | |||||
MQTT_BROKER_GLOBAL = "mqtt.eclipseprojects.io" | |||||
RASPI_CLIENT_ID = "lorem ipsum" | |||||
BACKEND_CLIENT_ID = "lorem ipsum" | |||||
# Topics: | |||||
Topics = { | |||||
"robot_action_drive": "ROBOT/ACTION/DRIVE", | |||||
"robot_action_getposition": "ROBOT/ACTION/GETPOSITION", | |||||
"robot_action_getbattery": "ROBOT/ACTION/GETBATTERY", | |||||
"robot_data_sensordata": "ROBOT/DATA/SENSORDATA", | |||||
"robot_data_battery": "ROBOT/DATA/BATTERY", | |||||
"robot_data_position": "ROBOT/DATA/POSITION", | |||||
"robot_data_picture": "ROBOT/DATA/PICTURE", | |||||
"backend_action_drive": "BACKEND/ACTION/DRIVE", | |||||
"backend_action_getposition": "BACKEND/ACTION/GETPOSITION", | |||||
"backend_action_getbattery": "BACKEND/ACTION/GETBATTERY", | |||||
"backend_action_getalldata": "BACKEND/ACTION/GETALLDATA", | |||||
"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" | |||||
} |