Skip to content

Commit

Permalink
hunt core process ok
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguez committed Nov 7, 2023
1 parent 6304141 commit 57df4f3
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 32 deletions.
2 changes: 1 addition & 1 deletion client/panduza/interfaces/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __post_init__(self):

# === CONFIG ===
self.add_attribute(
Attribute( name_ = "device" )
Attribute( name_ = "devices" )
).add_field(
RwField( name_ = "hunting" )
).add_field(
Expand Down
5 changes: 0 additions & 5 deletions platform/panduza_platform/core/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,6 @@ async def __load_tree_task(self):
# Load config
await self.load_config_content_task(new_dtree)

# ---



# ---



Expand Down
26 changes: 23 additions & 3 deletions platform/panduza_platform/core/platform_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ def get_model(self):
def get_manufacturer(self):
return self.get_config_field("manufacturer")

# ---

def get_settings_props(self):
return self.get_config_field("settings_props", [])

# ---

def get_hunted_instances(self):
return self._PZA_DEV_hunt()

# ---

###########################################################################
###########################################################################
#
Expand All @@ -145,12 +157,20 @@ async def _PZA_DEV_mount_interfaces(self):

# ---

@abc.abstractmethod
def _PZA_DEV_hunt(self):
"""
"""
return []

# ---

@abc.abstractmethod
def _PZA_DEV_interfaces_generator(self):
"""Generate interface definitions from device settings
"""
raise Exception("Function Not Implemented !")
# return {}
!!!! DEPRECATED !!!!
"""
raise Exception("Function DEPRECATED !")

# ---

Expand Down
44 changes: 43 additions & 1 deletion platform/panduza_platform/core/platform_device_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ def __init__(self, parent_platform):
# The factory is composed of builders
self.__device_templates = {}

# store
# {
# "manufacturer.model" : {
# "instances: []"
# }
# }
self.__device_store = {}

self.platform = parent_platform
self.__log = self.platform.log

Expand Down Expand Up @@ -57,8 +65,42 @@ def discover(self):
def register_device(self, device_builder):
"""Register a new device model
"""
id = device_builder().get_ref()
builder=device_builder()
id = builder.get_ref()
self.__log.info(f"Register device builder: '{id}'")
self.__device_templates[id] = device_builder
self.__device_store[id] = {
'settings_props': builder.get_settings_props(),
'instances': []
}
# self.__log.info(f"Register device builder: '{id}' {self.__device_store[id]}")

# ---

def get_devices_store(self):
return self.__device_store

# ---

async def hunt_start(self):
keys = self.__device_templates.keys()
max = len(keys)
self.__hunt_iter = iter(self.__device_templates.keys())

# ---

async def hunt_next(self):
try:
d = next(self.__hunt_iter)
self.__log.info(f"{self.__device_templates[d]}")

device_builder = self.__device_templates[d]()
instances = device_builder.get_hunted_instances()
self.__device_store[d]['instances'] = instances

return True
except StopIteration:
return False

# ---

20 changes: 13 additions & 7 deletions platform/panduza_platform/devices/panduza/fake_bps/dev_fake_bps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ def _PZA_DEV_config(self):
return {
"family": "BPS",
"model": "FakeBps",
"manufacturer": "Panduza"
"manufacturer": "Panduza",
"settings_props": [
{
'name': 'number_of_channel',
'type': 'int'
}
]
}

# ---
Expand All @@ -31,10 +37,10 @@ async def _PZA_DEV_mount_interfaces(self):
self.mount_interface(
InterfacePanduzaFakeBpc(name=f":channel_{chan}:_ctrl")
)

am = InterfacePanduzaFakeAmmeter(name=f":channel_{chan}:_am")
self.mount_interface(am)

vm = InterfacePanduzaFakeVoltmeter(name=f":channel_{chan}:_vm")
self.mount_interface(vm)
self.mount_interface(
InterfacePanduzaFakeAmmeter(name=f":channel_{chan}:_am")
)
self.mount_interface(
InterfacePanduzaFakeVoltmeter(name=f":channel_{chan}:_vm")
)

47 changes: 32 additions & 15 deletions platform/panduza_platform/devices/panduza/server/itf_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ class InterfacePanduzaPlatform(PlatformDriver):
"""
"""

###########################################################################
###########################################################################

def _PZA_DRV_config(self):
"""From PlatformDriver
"""
Expand All @@ -18,17 +15,17 @@ def _PZA_DRV_config(self):
}
}

###########################################################################
###########################################################################

async def _PZA_DRV_loop_init(self):
"""From PlatformDriver
"""


self.hunting = False

# Set command handlers
self.__cmd_handlers = {
"dtree": self.__handle_cmds_set_dtree,
"device": self.__handle_cmds_set_device,
"devices": self.__handle_cmds_set_devices,
}

# Append a task to refresh platform data
Expand Down Expand Up @@ -80,15 +77,14 @@ async def __refresh_platform_data_task(self):
})

await self._update_attributes_from_dict({
"device": {
"devices": {
"hunting": False,
"max": 0,
"hunted": 0,
"store": [],
"store": self.platform.device_factory.get_devices_store(),
}
})


# ---

async def __handle_cmds_set_dtree(self, cmd_att):
Expand Down Expand Up @@ -123,14 +119,14 @@ async def __get_config_content(self):

# ---

async def __handle_cmds_set_device(self, cmd_att):
async def __handle_cmds_set_devices(self, cmd_att):
"""
"""
update_obj = {}
self.log.info(cmd_att)

await self._prepare_update(update_obj,
"device", cmd_att,
"devices", cmd_att,
"hunting", [bool]
, self.__set_device_hunting
, self.__get_device_hunting)
Expand All @@ -141,19 +137,40 @@ async def __handle_cmds_set_device(self, cmd_att):
# , self.__set_poll_cycle_enable
# , self.__get_poll_cycle_enable)



await self._update_attributes_from_dict(update_obj)

# ---

async def __set_device_hunting(self, value):
self.log.info(value)
# await self.platform.load_config_content_task(value)
pass

if value == True:
if self.hunting:
pass
# already hunting
else:
self.hunting = True
self.platform.load_task(self.hunt_task())
else:
# stop hunting
self.log.warning("stop huntin not implemented")

# ---

async def __get_device_hunting(self):
return self.platform.dtree
return self.hunting

# ---

async def hunt_task(self):
print("!!!!!!!!!!!!!! HUNT !!!!!!!!!!!!!!!")
await self.platform.device_factory.hunt_start()
while True:
has_next = await self.platform.device_factory.hunt_next()
if not has_next:
break
print(f"!!!!!!!!!!!!!! HUNT !!!!!!!!!!!!!!!")


23 changes: 23 additions & 0 deletions platform/tests/manual/start_hunt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import time
import numpy as np
from panduza import Client, Core, Platform

ADDR="localhost"
PORT=1883

Core.EnableLogging()



c = Client(url=ADDR, port=PORT)
c.connect()
platforms = c.scan_all_platform_interfaces()

platform_topic = next(iter(platforms.keys()))
print(platform_topic)

plat = Platform(addr=ADDR, port=PORT, topic=platform_topic)
plat.devices.hunting.set(True)



0 comments on commit 57df4f3

Please sign in to comment.