Browse Source

DRIVE ALL ADDED

master
caliskanbi 1 year ago
parent
commit
a13b150dfe

BIN
software/backend/backend_database.db View File


+ 53
- 25
software/backend/data_functions.py View File



try: try:
robot.delete_order(drive_data) robot.delete_order(drive_data)
mydatabase.insert_measurement_data(plant_id=payload['PlantID'],
sensordata_temp=payload['AirTemperature'],
sensordata_humidity=payload['AirHumidity'],
sensordata_soil_moisture=payload['SoilMoisture'],
sensordata_brightness=payload['Brightness'])
logging.debug("Inserted to data base: " + json.dumps(payload))
except Exception as e: except Exception as e:
logging.error("Could not delete order: " + str(e)) logging.error("Could not delete order: " + str(e))


mydatabase.insert_measurement_data(plant_id=payload['PlantID'],
sensordata_temp=payload['AirTemperature'],
sensordata_humidity=payload['AirHumidity'],
sensordata_soil_moisture=payload['SoilMoisture'],
sensordata_brightness=payload['Brightness'])
logging.debug("Inserted to data base: " + json.dumps(payload))



def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot): def data_position(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
logging.info("ROBOT_DATA_POSITION Received data: " + json.dumps(message.payload.decode("UTF-8"))) logging.info("ROBOT_DATA_POSITION Received data: " + json.dumps(message.payload.decode("UTF-8")))
client.publish(Topics['BACKEND_DATA_BATTERY'], json.dumps(battery_data)) client.publish(Topics['BACKEND_DATA_BATTERY'], json.dumps(battery_data))




def data_error(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
robot.store_last_error(message.payload.decode("UTF-8"))
logging.error("ROBOT_DATA_ERROR new error received from Robot: " + robot.get_last_error())
client.publish(Topics['BACKEND_DATA_ERROR'], message.payload.decode("UTF-8"))


def data_robotready(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
robot.change_robot_status(message.payload.decode("UTF-8") == 'True')
if robot.get_robot_status() is True and robot.get_order_number() >= 1:
client.publish(Topics['ROBOT_ACTION_DRIVE'], json.dumps(robot.get_next_order()))
logging.info("Waiting Order send to Robot")

logging.info("ROBOT_DATA_ROBOTREADY status updated: " + str(robot.get_robot_status()))
client.publish(Topics['BACKEND_DATA_ROBOTREADY'], message.payload.decode("UTF-8"))


# FrontEnd Channel Reactions # FrontEnd Channel Reactions


def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, def action_drive(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,


def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase, def action_driveall(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase,
robot: Robot): robot: Robot):
plant_names = mydatabase.get_plant_names()
plant_ids = []
print(plant_names)


client.publish(Topics['ROBOT_ACTION_DRIVEALL'])
for names in plant_names:
_id = mydatabase.get_plant_id(names)
plant_ids.append(_id)

action_id = str(uuid.uuid4())
drive_data = {
"PlantID": plant_ids,
"ActionID": action_id
}
print(drive_data)
if robot.get_order_number() < 6 and robot.get_robot_status() is True:
robot.add_order(drive_data)
client.publish(Topics['ROBOT_ACTION_DRIVEALL'], json.dumps(plant_ids))
logging.info("BACKEND_ACTION_DRIVEALL Drive Command published: " + json.dumps(drive_data))
else:
if robot.get_order_number() < 6:
robot.add_order(drive_data)
logging.info("BACKEND_ACTION_DRIVEALL New data added to order list: " + str(drive_data))
elif robot.get_order_number() >= 6:
client.publish(Topics['BACKEND_DATA_ERROR'], "Could not add Order to list. Order discarded")
logging.error("Could not add Order to list. Order discarded")




def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase): def action_getposition(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, mydatabase: PlantDataBase):
def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage): def action_getbattery(client: mqtt.Client, userdata, message: mqtt.MQTTMessage):
client.publish(Topics['ROBOT_ACTION_GETBATTERY']) client.publish(Topics['ROBOT_ACTION_GETBATTERY'])
logging.info("BACKEND_ACTION_GETBATTERY message forwarded to Robot") logging.info("BACKEND_ACTION_GETBATTERY message forwarded to Robot")
battery_data = {
"Battery": 66,
"Timestamp": str(datetime.now())
}
print(battery_data)
client.publish(Topics['BACKEND_DATA_BATTERY'], json.dumps(battery_data))




def action_getalldata(client: mqtt.Client, userdata, message: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase): def action_getalldata(client: mqtt.Client, userdata, message: Union[mqtt.MQTTMessage, list], mydatabase: PlantDataBase):
plant_names = mydatabase.get_plant_names() plant_names = mydatabase.get_plant_names()
alldata = [] alldata = []
for i in plant_names: for i in plant_names:
alldata.append(mydatabase.get_latest_data(plant_name=i[0]))
alldata.append(mydatabase.get_latest_data(plant_name=i))
client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata)) client.publish(Topics['BACKEND_DATA_SENSORDATAALL'], json.dumps(alldata))
logging.info("BACKEND_DATA_SENSORDATAALL got data from database:" + str(alldata)) logging.info("BACKEND_DATA_SENSORDATAALL got data from database:" + str(alldata))


} }
client.publish(Topics["BACKEND_DATA_PLANTCOUNT"], json.dumps(count_payload)) client.publish(Topics["BACKEND_DATA_PLANTCOUNT"], json.dumps(count_payload))
logging.info("BACKEND_DATA_PLANTCOUNT forwarded plant count to FrontEnd: " + str(count_payload)) logging.info("BACKEND_DATA_PLANTCOUNT forwarded plant count to FrontEnd: " + str(count_payload))


def data_error(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
robot.store_last_error(message.payload.decode("UTF-8"))
logging.error("ROBOT_DATA_ERROR new error received from Robot: " + robot.get_last_error())
client.publish(Topics['BACKEND_DATA_ERROR'], message.payload.decode("UTF-8"))


def data_robotready(client: mqtt.Client, userdata, message: mqtt.MQTTMessage, robot: Robot):
robot.change_robot_status(message.payload.decode("UTF-8") == 'True')
if robot.get_robot_status() is True and robot.get_order_number() >= 1:
client.publish(Topics['ROBOT_ACTION_DRIVE'], json.dumps(robot.get_next_order()))
logging.info("Waiting Order send to Robot")

logging.info("ROBOT_DATA_ROBOTREADY status updated: " + str(robot.get_robot_status()))
client.publish(Topics['BACKEND_DATA_ROBOTREADY'], message.payload.decode("UTF-8"))

+ 4
- 4
software/backend/dev_test_examples/mqtt_publisher.py View File

import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt


import software.backend.defines import software.backend.defines
from software.backend.defines import MQTT_BROKER_LOCAL
from software.backend.defines import MQTT_BROKER_GLOBAL
from random import randrange, uniform from random import randrange, uniform
import time import time
import json import json
client = mqtt.Client() client = mqtt.Client()
client.on_connect = on_connect client.on_connect = on_connect


client.connect("mqtt://192.168.137.197", 1883)
client.connect(MQTT_BROKER_GLOBAL, 1883)


plantdata = { plantdata = {
"Battery": 10
"PlantName": "Ipne"
} }




print(type(PLANTDATA)) print(type(PLANTDATA))
client.publish(Topics['ROBOT_DATA_BATTERY'], json.dumps(plantdata))
client.publish(Topics['BACKEND_ACTION_DRIVE'], json.dumps(plantdata))
print(json.dumps(plantdata)) print(json.dumps(plantdata))
time.sleep(2) time.sleep(2)

+ 8
- 9
software/backend/main.py View File

_client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], lambda client, userdata, message: data_functions. _client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], lambda client, userdata, message: data_functions.
data_battery(client, userdata, message, _robot)) data_battery(client, userdata, message, _robot))


# client.subscribe('Robot/Data/Picture')
_client.subscribe(Topics['ROBOT_DATA_ERROR'])
_client.message_callback_add(Topics['ROBOT_DATA_ERROR'], lambda client, userdata, message: data_functions.
data_error(client, userdata, message, _robot))

_client.subscribe(Topics['ROBOT_DATA_ROBOTREADY'])
_client.message_callback_add(Topics['ROBOT_DATA_ROBOTREADY'], lambda client, userdata, message: data_functions.
data_robotready(client, userdata, message, _robot))


# From FrontEnd: # From FrontEnd:
_client.subscribe(Topics['BACKEND_ACTION_DRIVE']) _client.subscribe(Topics['BACKEND_ACTION_DRIVE'])
_client.message_callback_add(Topics['BACKEND_ACTION_PLANTCOUNT'], lambda client, userdata, message: data_functions. _client.message_callback_add(Topics['BACKEND_ACTION_PLANTCOUNT'], lambda client, userdata, message: data_functions.
action_countplants(client, userdata, message, _mydatabase)) action_countplants(client, userdata, message, _mydatabase))


_client.subscribe(Topics['ROBOT_DATA_ERROR'])
_client.message_callback_add(Topics['ROBOT_DATA_ERROR'], lambda client, userdata, message: data_functions.
data_error(client, userdata, message, _robot))

_client.subscribe(Topics['ROBOT_DATA_ROBOTREADY'])
_client.message_callback_add(Topics['ROBOT_DATA_ROBOTREADY'], lambda client, userdata, message: data_functions.
data_robotready(client, userdata, message, _robot))
# END TOPIC SUBSCRIPTIONS # END TOPIC SUBSCRIPTIONS
else: else:
print("connection failed") print("connection failed")
robot = Robot() robot = Robot()
my_database = PlantDataBase(database_name=DATABASE_NAME) my_database = PlantDataBase(database_name=DATABASE_NAME)
my_database.create_tables() my_database.create_tables()
mqttclient = mqtt.Client(BACKEND_CLIENT_ID, transport="websockets")
mqttclient = mqtt.Client(BACKEND_CLIENT_ID)
mqttclient.on_connect = lambda client, userdata, flags, rc: on_connect(_client=client, mqttclient.on_connect = lambda client, userdata, flags, rc: on_connect(_client=client,
_userdata=userdata, _userdata=userdata,
_flags=flags, _flags=flags,

+ 2
- 1
software/backend/plantdatabase.py View File

import sqlite3 import sqlite3
from typing import Optional from typing import Optional
import logging import logging
from itertools import chain




class PlantDataBase: class PlantDataBase:
def get_plant_names(self) -> list: def get_plant_names(self) -> list:
try: try:
self.cur.execute("SELECT PlantName FROM plants") self.cur.execute("SELECT PlantName FROM plants")
return self.cur.fetchall()
return [row[0] for row in self.cur.fetchall()]
except Exception as e: except Exception as e:
logging.error("Could not get plant names: " + str(e)) logging.error("Could not get plant names: " + str(e))



Loading…
Cancel
Save