|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- """
- created by caliskan at 19.04.2023
-
- This file contains the main script for the backend server of smart garden project
- It has the task to be a bridge between the frontend and robot.
- It also contains a database with the current plant data
- Used protocol for interaction: mqtt (paho-mqtt module)
- """
-
- # imports
- import paho.mqtt.client as mqtt
- from defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL
- from plantdatabase import PlantDataBase
- from data_handling_functions import data_handler
-
- # inits
- mydatabase = PlantDataBase()
- 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):
- """
- This method gets called, when it connects to a mqtt broker.
- It is used to subscribe to the specific topics
- :param client: mqtt client object
- :param userdata:
- :param flags:
- :param rc: connection flag
- :return:
- """
- if rc == 0:
- print("connected")
-
- # TOPIC SUBSCRIPTIONS
-
- # From Robot:
- client.subscribe('Robot/Data/SensorData')
- client.subscribe('Robot/Data/Position')
- client.subscribe('Robot/Data/Battery')
- # client.subscribe('Robot/Data/Picture')
-
- # From FrontEnd:
- client.subscribe('BackEnd/Action/Drive')
- client.subscribe('BackEnd/Action/GetPosition')
- client.subscribe('BackEnd/Action/GetBattery')
- client.subscribe('BackEnd/Action/GetAllData')
- # END TOPIC SUBSCRIPTIONS
- else:
- print("connection failed")
-
-
- def main():
- client = mqtt.Client()
- client.on_connect = on_connect
- client.on_message = on_message
- client.connect(MQTT_BROKER_GLOBAL)
- client.loop_forever()
-
-
- if __name__ == "__main__":
- main()
|