|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- """
- 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
-
-
- def on_message(client, userdata, message):
- print(f'message received! {message.topic}')
- data_handler(client, message)
-
-
- def on_connect(client, userdata, flags, rc):
- if rc == 0:
- print("connected")
-
- # TOPIC SUBSCRIPTIONS
- client.subscribe("planttest")
- # Robot topics
- client.subscribe("Robot/Data/SensorData")
- client.subscribe("Robot/Data/Position")
- client.subscribe("Robot/Data/Battery")
- client.subscribe("Robot/Data/Picture")
-
- # FrontEnd topics
- client.subscribe("BackEnd/Action/All")
- client.subscribe("BackEnd/Action/GetPosition")
- client.subscribe("BackEnd/Action/GetBattery")
- client.subscribe("BackEnd/Action/GetAllData")
-
- # END TOPIC SUBSCRIPTIONS
- else:
- print("connection failed")
-
-
- def main():
- mydatabase = PlantDataBase()
- mydatabase.create_table()
- 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()
|