Skip to content

Commit

Permalink
mn_manager (track_avail_cu missing)
Browse files Browse the repository at this point in the history
  • Loading branch information
javibu13 committed Nov 14, 2023
1 parent b01b20b commit 32b90e4
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion mn_manager/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ system-logger-tool
system-shared-tool
wattrex-battery-cycler-datatypes
wattrex-driver-mqtt
wattrex-driver-db
wattrex-driver-db>=0.0.16
1 change: 0 additions & 1 deletion mn_manager/src/wattrex_mn_manager/mn_broker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def process_det_dev(self, raw_data : bytearray) -> None:
Args:
raw_data (bytearray): [description]
'''
# TODO: add cu_id to the message
devices : List[CommDataDeviceC] = loads(raw_data)
if len(devices) > 0:
cu_id = devices[0].cu_id
Expand Down
38 changes: 38 additions & 0 deletions mn_manager/src/wattrex_mn_manager/mn_db_facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,43 @@ def get_available_cus(self) -> List[int]:
return cus


def is_cu_registered(self, cu_info : CommDataCuC) -> bool:
'''Check if a CU is already registered.
Args:
cu_info (CommDataCuC): [description]
Returns:
bool: [description]
'''
result = False
stmt = select(DrvDbComputationalUnitC)\
.where(DrvDbComputationalUnitC.MAC == cu_info.mac)
db_result = self.database.session.execute(stmt).first()
if db_result is not None:
result = True
return result


def get_cu_by_mac(self, mac : int) -> int|None:
'''Returns the CU_ID from database for the given mac address.
Returns None if not found.
Args:
mac (int): [description]
Returns:
result (int|None): CU_ID retrieved from database. None if not found
'''
result : int = None
stmt = select(DrvDbComputationalUnitC)\
.where(DrvDbComputationalUnitC.MAC == mac)
db_result = self.database.session.execute(stmt).first()
if db_result is not None:
result = DrvDbComputationalUnitC(db_result[0]).CUID
return result


def register_cu(self, cu_info : CommDataCuC) -> None:
'''Register a CU data unit.
Expand All @@ -82,6 +119,7 @@ def register_cu(self, cu_info : CommDataCuC) -> None:
self.last_cu_id += 1
cu_db = DrvDbComputationalUnitC()
cu_db.CUID = self.last_cu_id
cu_db.MAC = cu_info.mac
cu_db.HostName = cu_info.hostname
cu_db.User = cu_info.user
cu_db.IP = cu_info.ip
Expand Down
41 changes: 27 additions & 14 deletions mn_manager/src/wattrex_mn_manager/mn_manager_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,37 @@ def register_cb(self, cu_info : CommDataCuC) -> None:
cu_info (CommDataCuC): [description]
'''
if cu_info.msg_type is CommDataRegisterTypeE.DISCOVER:
new_cu_id = self.db_facha.get_last_cu_id()
# TODO: Check if the same device with the same MAC is already registered
cu_info.cu_id = new_cu_id + 1
cu_info.msg_type = CommDataRegisterTypeE.OFFER
log.info(f"{cu_info.msg_type.name}s cu_id {cu_info.cu_id} for device with MAC:"
+ f"{cu_info.mac}")
found_cu = self.db_facha.get_cu_by_mac(cu_info.mac)
if found_cu is not None:
cu_info.cu_id = found_cu
cu_info.msg_type = CommDataRegisterTypeE.OFFER
log.info(f"{cu_info.msg_type.name}s cu_id {cu_info.cu_id} for device already "
+ "found in database with MAC:"
+ f"{cu_info.mac}")
else:
new_cu_id = self.db_facha.get_last_cu_id()
cu_info.cu_id = new_cu_id + 1
cu_info.msg_type = CommDataRegisterTypeE.OFFER
log.info(f"{cu_info.msg_type.name}s cu_id {cu_info.cu_id} for device with MAC:"
+ f"{cu_info.mac}")
self.client_mqtt.publish_inform(cu_info)
elif cu_info.msg_type is CommDataRegisterTypeE.REQUEST:
self.db_facha.register_cu(cu_info)
try:
self.db_facha.commit()
except Exception as err:
log.error(f"Error on commiting new CU: {err}")
else:
if self.db_facha.is_cu_registered(cu_info):
cu_info.msg_type = CommDataRegisterTypeE.ACK
log.info(f"Send {cu_info.msg_type.name}. Registered new CU: {cu_info.cu_id} "
+ f"with MAC: {cu_info.mac}")
log.info(f"Send {cu_info.msg_type.name}. Already registered CU: {cu_info.cu_id} "
+ f"with MAC: {cu_info.mac}")
self.client_mqtt.publish_inform(cu_info)
else:
self.db_facha.register_cu(cu_info)
try:
self.db_facha.commit()
except Exception as err:
log.error(f"Error on commiting new CU: {err}")
else:
cu_info.msg_type = CommDataRegisterTypeE.ACK
log.info(f"Send {cu_info.msg_type.name}. Registered new CU: {cu_info.cu_id} "
+ f"with MAC: {cu_info.mac}")
self.client_mqtt.publish_inform(cu_info)
else:
log.debug(f"Inconsistent register message: {cu_info.msg_type}. Ignore it.")

Expand Down

0 comments on commit 32b90e4

Please sign in to comment.