|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- """
- 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 software.defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL, Topics, BACKEND_CLIENT_ID
- from plantdatabase import PlantDataBase
- import data_functions
-
- # inits
- mydatabase = PlantDataBase()
- mydatabase.create_table()
- order_handler = [] # will contain UUIDS with Order IDs
-
-
- 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(Topics['ROBOT_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.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'], lambda client, userdata, message: data_functions.
- data_battery(client, userdata, message, mydatabase))
-
- # client.subscribe('Robot/Data/Picture')
-
- # From FrontEnd:
- _client.subscribe(Topics['BACKEND_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.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
- else:
- print("connection failed")
-
-
- def main():
- client = mqtt.Client(BACKEND_CLIENT_ID)
- client.on_connect = on_connect
- client.connect(MQTT_BROKER_GLOBAL)
- client.loop_forever()
-
-
- if __name__ == "__main__":
- main()
|