# Robot Channel Reactions | # Robot Channel Reactions | ||||
def data_sensordata(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list): | |||||
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 | ||||
str_in = str(message.payload.decode("UTF-8")) | str_in = str(message.payload.decode("UTF-8")) | ||||
# FrontEnd Channel Reactions | # FrontEnd Channel Reactions | ||||
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, order_handler: list): | |||||
def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, | |||||
order_handler: list): | |||||
# TODO: ROBOT READY CHECK | # TODO: ROBOT READY CHECK | ||||
if len(order_handler) < 5: | if len(order_handler) < 5: | ||||
order_handler.append(uuid.uuid4()) | order_handler.append(uuid.uuid4()) | ||||
client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8")) | client.publish(Topics['ROBOT_ACTION_DRIVE'], message.payload.decode("utf-8")) | ||||
def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): | |||||
# TODO: Implement here | |||||
pass | |||||
def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): | def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): | ||||
client.publish(Topics['ROBOT_ACTION_GETPOSITION']) | client.publish(Topics['ROBOT_ACTION_GETPOSITION']) | ||||
for i in range(1, 7): | for i in range(1, 7): | ||||
alldata.append(mydatabase.get_latest_data(plant_id=i)) | alldata.append(mydatabase.get_latest_data(plant_id=i)) | ||||
client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) | client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) | ||||
def action_newplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): | |||||
# TODO: insert new plant to database | |||||
pass | |||||
def action_configureplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): | |||||
# TODO: configure plant | |||||
pass | |||||
def action_deleteplant(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): | |||||
# TODO: delete plant from database (from ID) | |||||
pass |
import time | import time | ||||
import json | import json | ||||
from software.defines import Topics, PLANTDATA | from software.defines import Topics, PLANTDATA | ||||
mqttBroker = "mqtt.eclipseprojects.io" | |||||
mqttBroker = "192.168.178.182" | |||||
def on_connect(client, userdata, flags, rc): | def on_connect(client, userdata, flags, rc): | ||||
print(type(PLANTDATA)) | print(type(PLANTDATA)) | ||||
while True: | while True: | ||||
client.publish(Topics["BACKEND_DATA_SENSORDATA"], json.dumps(plantdata)) | |||||
client.publish("TEST", json.dumps(plantdata)) | |||||
print(json.dumps(plantdata)) | print(json.dumps(plantdata)) | ||||
time.sleep(2) | time.sleep(2) |
print("Connection failed") | print("Connection failed") | ||||
mqttBroker = "192.168.0.199" | |||||
mqttBroker = "192.168.178.182" | |||||
client = mqtt.Client() | client = mqtt.Client() | ||||
client.connect(mqttBroker, 1883) | client.connect(mqttBroker, 1883) | ||||
client.loop_start() | |||||
client.on_message = on_message | client.on_message = on_message | ||||
client.subscribe(Topics["BACKEND_DATA_SENSORDATA"]) | |||||
client.subscribe("Bilal") | |||||
time.sleep(30) | |||||
client.loop_stop() | |||||
client.subscribe("TEST") | |||||
client.loop_forever() |
_client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions. | _client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions. | ||||
action_drive(client, userdata, message, mydatabase, order_handler)) | action_drive(client, userdata, message, mydatabase, order_handler)) | ||||
_client.subscribe(Topics['BACKEND_ACTION_DRIVEALL']) | |||||
_client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], lambda client, userdata, message: data_functions. | |||||
action_driveall(client, userdata, message, mydatabase)) | |||||
_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.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'], lambda client, userdata, message: data_functions.action_getalldata(client, userdata, message, mydatabase)) | |||||
_client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], | |||||
lambda client, userdata, message: data_functions. | |||||
action_getalldata(client, userdata, message, mydatabase)) | |||||
_client.subscribe(Topics['BACKEND_ACTION_NEWPLANT']) | |||||
_client.message_callback_add(Topics['BACKEND_ACTION_NEWPLANT'], lambda client, userdata, message: data_functions. | |||||
action_newplant(client, userdata, message, mydatabase)) | |||||
_client.subscribe(Topics['BACKEND_ACTION_CONFIGUREPLANT']) | |||||
_client.message_callback_add(Topics['BACKEND_ACTION_CONFIGUREPLANT'], lambda client, userdata, message: data_functions. | |||||
action_configureplant(client, userdata, message, mydatabase)) | |||||
_client.subscribe(Topics['BACKEND_ACTION_DELETEPLANT']) | |||||
_client.message_callback_add(Topics['BACKEND_ACTION_DELETEPLANT'], | |||||
lambda client, userdata, message: data_functions. | |||||
action_deleteplant(client, userdata, message, mydatabase)) | |||||
# END TOPIC SUBSCRIPTIONS | # END TOPIC SUBSCRIPTIONS | ||||
else: | else: |
def create_table(self): | def create_table(self): | ||||
table_config = "CREATE TABLE IF NOT EXISTS plants " \ | table_config = "CREATE TABLE IF NOT EXISTS plants " \ | ||||
"(plant_ID INTEGER PRIMARY KEY AUTOINCREMENT," \ | "(plant_ID INTEGER PRIMARY KEY AUTOINCREMENT," \ | ||||
" gps TEXT," \ | |||||
"plantName TEXT)" | "plantName TEXT)" | ||||
self.cur.execute(table_config) | self.cur.execute(table_config) | ||||
"sensordata_temp REAL," \ | "sensordata_temp REAL," \ | ||||
"sensordata_humidity REAL," \ | "sensordata_humidity REAL," \ | ||||
"sensordata_soil_moisture REAL," \ | "sensordata_soil_moisture REAL," \ | ||||
"pest_infestation INTEGER," \ | |||||
"light_intensity REAL," \ | "light_intensity REAL," \ | ||||
"FOREIGN KEY (plant_ID)" \ | "FOREIGN KEY (plant_ID)" \ | ||||
" REFERENCES plants (plant_ID) )" | |||||
"REFERENCES plants (plant_ID) )" | |||||
self.cur.execute(table_config) | self.cur.execute(table_config) | ||||
def insert_plant(self, _gps: str, plantname): | def insert_plant(self, _gps: str, plantname): | ||||
sensordata_temp, | sensordata_temp, | ||||
sensordata_humidity, | sensordata_humidity, | ||||
sensordata_soil_moisture, | sensordata_soil_moisture, | ||||
pest_infestation, | |||||
light_intensity): | light_intensity): | ||||
self.cur.execute(f"INSERT INTO measurement_values (plant_ID, sensordata_temp, sensordata_humidity," | self.cur.execute(f"INSERT INTO measurement_values (plant_ID, sensordata_temp, sensordata_humidity," | ||||
f" sensordata_soil_moisture, pest_infestation, light_intensity) VALUES " | |||||
f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_soil_moisture}, {pest_infestation}" | |||||
f" sensordata_soil_moisture, light_intensity) VALUES " | |||||
f"({plant_id}, {sensordata_temp}, {sensordata_humidity}, {sensordata_soil_moisture}" | |||||
f", {light_intensity})") | f", {light_intensity})") | ||||
self.conn.commit() | self.conn.commit() | ||||
"AirTemperature": data[3], | "AirTemperature": data[3], | ||||
"AirHumidity": data[4], | "AirHumidity": data[4], | ||||
"SoilMoisture": data[5], | "SoilMoisture": data[5], | ||||
"Brightness": data[7], | |||||
"Brightness": data[6], | |||||
"PlantName": name | "PlantName": name | ||||
} | } | ||||
return json_file | return json_file | ||||
def delete_data(self, table_name): | def delete_data(self, table_name): | ||||
self.cur.execute(f"DELETE FROM {table_name}") | self.cur.execute(f"DELETE FROM {table_name}") | ||||
self.conn.commit() | self.conn.commit() | ||||
# TODO: Kemals Scheiß implementieren | |||||
def delete_plant(self, plant_id): | |||||
self.cur.execute('DELETE FROM plants WHERE plant_ID = ?', (plant_id,)) | |||||
self.conn.commit() |
"ROBOT_DATA_PICTURE": "ROBOT/DATA/PICTURE", | "ROBOT_DATA_PICTURE": "ROBOT/DATA/PICTURE", | ||||
"BACKEND_ACTION_DRIVE": "BACKEND/ACTION/DRIVE", | "BACKEND_ACTION_DRIVE": "BACKEND/ACTION/DRIVE", | ||||
"BACKEND_ACTION_DRIVEPALL": "BACKEND/ACTION/DRIVEALL", | |||||
"BACKEND_ACTION_GETPOSITION": "BACKEND/ACTION/GETPOSITION", | "BACKEND_ACTION_GETPOSITION": "BACKEND/ACTION/GETPOSITION", | ||||
"BACKEND_ACTION_GETBATTERY": "BACKEND/ACTION/GETBATTERY", | "BACKEND_ACTION_GETBATTERY": "BACKEND/ACTION/GETBATTERY", | ||||
"BACKEND_ACTION_GETALLDATA": "BACKEND/ACTION/GETALLDATA", | "BACKEND_ACTION_GETALLDATA": "BACKEND/ACTION/GETALLDATA", | ||||
"BACKEND_ACTION_NEWPLANT": "BACKEND/ACTION/NEWPLANT", | |||||
"BACKEND_ACTION_CONFIGUREPLANT": "BACKEND/ACTION/CONFIGUREPLANT", | |||||
"BACKEND_ACTION_DELETEPLANT": "BACKEND/ACTION/DELETEPLANT", | |||||
"BACKEND_ACTION_PLANTCOUNT": "BACKEND/ACTION/PLANTCOUNT", | |||||
"BACKEND_DATA_SENSORDATA": "BACKEND/DATA/SENSORDATA", | "BACKEND_DATA_SENSORDATA": "BACKEND/DATA/SENSORDATA", | ||||
"BACKEND_DATA_SENSORDATAALL": "BACKEND/DATA/SENSORDATA_ALL", | "BACKEND_DATA_SENSORDATAALL": "BACKEND/DATA/SENSORDATA_ALL", | ||||
"BACKEND_DATA_POSITION": "BACKEND/DATA/POSITION", | "BACKEND_DATA_POSITION": "BACKEND/DATA/POSITION", | ||||
"BACKEND_DATA_BATTERY": "BACKEND/DATA/BATTERY", | "BACKEND_DATA_BATTERY": "BACKEND/DATA/BATTERY", | ||||
"BACKEND_DATA_PICTURE": "BACKEND/DATA/PICTURE" | |||||
"BACKEND_DATA_PICTURE": "BACKEND/DATA/PICTURE", | |||||
"BACKEND_DATA_PLANTCOUNT": "BACKEND/DATA/PLANTCOUNT", | |||||
} | } | ||||
"Timestamp": "" | "Timestamp": "" | ||||
} | } | ||||
PLANTCOUNT = { | |||||
"CurrenCount": 0, | |||||
"maxCount": 0 | |||||
} | |||||
# endregion | # endregion | ||||
# region Frontend -> Backend | # region Frontend -> Backend | ||||
DRIVE = { | DRIVE = { | ||||
"PlantID": 0 | |||||
"PlantName": "" | |||||
} | } | ||||
NEWPLANT = PLANTDATA | |||||
CONFIGUREPLANT = PLANTDATA | |||||
DELETEPLANT = { | |||||
"PlantID": "" | |||||
} | |||||
# DRIVEALL -> no message needed | |||||
# GETPOSITION -> no message needed | # GETPOSITION -> no message needed | ||||
# GETBATTERY -> no message needed | # GETBATTERY -> no message needed |