Skip to content

Commit

Permalink
[JSONGEN] Devices section is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
leondavi committed Aug 25, 2023
1 parent 357f59f commit f9001ae
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 56 deletions.
1 change: 1 addition & 0 deletions src_py/nerlPlanner/Definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

KEY_DEVICES_SELECTED_ENTITY_COMBO = '-KEY-DEVICES-SELECTED-ENTITY-COMBO-'
KEY_DEVICES_SELECTED_DEVICE_TEXT = '-KEY-DEVICES-SELECTED-DEVICE-TEXT-'
KEY_DEVICES_ADD_ENTITY_TO_DEVICE = '-KEY-DEVICES-ADD-ENTITY-TO-DEVICE-'

KEY_CLIENTS_WORKERS_LIST_COMBO_BOX = '-KEY-CLIENTS-WORKERS-LIST-COMBO-BOX-'
KEY_CLIENTS_BUTTON_ADD = '-KEY-CLIENTS-BUTTON-ADD-'
Expand Down
86 changes: 49 additions & 37 deletions src_py/nerlPlanner/Handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@


# instances and lists of instances
json_distribtued_config_inst = JsonDistributedConfig()
json_dc_inst = JsonDistributedConfig()

def reset_json_distributed_configuration():
global json_distribtued_config_inst
json_distribtued_config_inst = JsonDistributedConfig()
global json_dc_inst
json_dc_inst = JsonDistributedConfig()

def settings_handler(event, values):
frequency = None
Expand Down Expand Up @@ -47,9 +47,9 @@ def settings_handler(event, values):
if error:
sg.popup_ok(f"Cannot save - Wrong Fields!", keep_on_top=True, title="Input Issue")
else:
json_distribtued_config_inst.add_nerlnet_settings(frequency_inst, batch_size_inst)
json_distribtued_config_inst.add_main_server(main_server_inst)
json_distribtued_config_inst.add_api_server(api_server_inst)
json_dc_inst.add_nerlnet_settings(frequency_inst, batch_size_inst)
json_dc_inst.add_main_server(main_server_inst)
json_dc_inst.add_api_server(api_server_inst)



Expand All @@ -61,8 +61,8 @@ def workers_handler(window, event, values):
global worker_name_selection

if event == KEY_WORKERS_SHOW_WORKER_BUTTON:
if (worker_name_selection in json_distribtued_config_inst.get_workers_dict()):
workers_new_worker = json_distribtued_config_inst.get_workers_dict()[worker_name_selection]
if (worker_name_selection in json_dc_inst.get_workers_dict()):
workers_new_worker = json_dc_inst.get_workers_dict()[worker_name_selection]
if workers_new_worker is not None:
image_path = workers_new_worker.save_graphviz(NERLNET_GRAPHVIZ_OUTPUT_DIR)
sg.popup_ok(f"{workers_new_worker}", title="Worker graph", image=image_path, keep_on_top=True)
Expand All @@ -74,20 +74,20 @@ def workers_handler(window, event, values):
workers_new_worker_dict = json.load(jsonFile)
(workers_new_worker , _, _, _, _, _, _, _, _, _, _, _) = Worker.load_from_dict(workers_new_worker_dict)
window[KEY_WORKERS_INFO_BAR].update(f'Loaded from file: {workers_new_worker}')
window[KEY_WORKERS_LIST_BOX].update(json_distribtued_config_inst.get_workers_names_list())
window[KEY_WORKERS_LIST_BOX].update(json_dc_inst.get_workers_names_list())

if event == KEY_WORKERS_NAME_INPUT:
workers_new_worker_name = values[KEY_WORKERS_NAME_INPUT] if values[KEY_WORKERS_NAME_INPUT] not in json_distribtued_config_inst.get_workers_dict() else None
workers_new_worker_name = values[KEY_WORKERS_NAME_INPUT] if values[KEY_WORKERS_NAME_INPUT] not in json_dc_inst.get_workers_dict() else None

if workers_new_worker_name and (workers_new_worker_name not in json_distribtued_config_inst.get_workers_dict()) and\
if workers_new_worker_name and (workers_new_worker_name not in json_dc_inst.get_workers_dict()) and\
(workers_new_worker is not None):
workers_new_worker.set_name(workers_new_worker_name)

if event == KEY_WORKERS_BUTTON_ADD and (workers_new_worker is not None):
if not workers_new_worker.get_name():
sg.popup_ok(f"Cannot add - Name is missing or exist!", keep_on_top=True, title="Adding New Worker Issue")
elif json_distribtued_config_inst.add_worker(workers_new_worker):
window[KEY_WORKERS_LIST_BOX].update(json_distribtued_config_inst.get_workers_names_list())
elif json_dc_inst.add_worker(workers_new_worker):
window[KEY_WORKERS_LIST_BOX].update(json_dc_inst.get_workers_names_list())
# Clear fields after successful add
window[KEY_WORKERS_INFO_BAR].update(f'{workers_new_worker_name} added, {workers_new_worker}')
workers_new_worker_name = ''
Expand All @@ -104,8 +104,8 @@ def workers_handler(window, event, values):
window[KEY_WORKERS_INFO_BAR].update(f'{worker_name_selection} is selected')

if event == KEY_WORKERS_LOAD_FROM_LIST_WORKER_BUTTON:
if (worker_name_selection in json_distribtued_config_inst.get_workers_dict()) and workers_new_worker_name:
workers_new_worker = json_distribtued_config_inst.get_workers_dict()[worker_name_selection].copy(workers_new_worker_name)
if (worker_name_selection in json_dc_inst.get_workers_dict()) and workers_new_worker_name:
workers_new_worker = json_dc_inst.get_workers_dict()[worker_name_selection].copy(workers_new_worker_name)
window[KEY_WORKERS_INFO_BAR].update(f'{workers_new_worker_name} loaded, {workers_new_worker}')
else:
sg.popup_ok(f"selection or name issue", keep_on_top=True, title="Loading Issue")
Expand All @@ -125,16 +125,28 @@ def devices_handler(window, event, values):
devices_devices_list_box_selection = values[KEY_DEVICES_LIST_BOX_DEVICES][0]
if event == KEY_DEVICES_SELECTED_ENTITY_COMBO:
last_selected_entity = values[KEY_DEVICES_SELECTED_ENTITY_COMBO]
if event == KEY_DEVICES_ONLINE_LIST_COMBO_BOX:
window[KEY_DEVICES_IP_INPUT].update(values[KEY_DEVICES_ONLINE_LIST_COMBO_BOX])
devices_ip_str = values[KEY_DEVICES_ONLINE_LIST_COMBO_BOX]

if event == KEY_DEVICES_BUTTON_ADD:
if devices_name and devices_ip_str:
device_to_add = Device(devices_ip_str, devices_name)
if not device_to_add.error():
json_distribtued_config_inst.add_device(device_to_add)
window[KEY_DEVICES_LIST_BOX_DEVICES].update(json_distribtued_config_inst.get_devices_names())
json_dc_inst.add_device(device_to_add)
window[KEY_DEVICES_LIST_BOX_DEVICES].update(json_dc_inst.get_devices_names())
else:
sg.popup_ok("Ip or Name are wrong or exist!")

if event == KEY_DEVICES_ADD_ENTITY_TO_DEVICE and last_selected_entity and devices_devices_list_box_selection:
res = json_dc_inst.add_entity_to_device(devices_devices_list_box_selection, last_selected_entity)
if not res:
sg.popup_ok(f"Could not add {last_selected_entity} to dev: {devices_devices_list_box_selection}")

if devices_devices_list_box_selection:
device_inst = json_dc_inst.get_device_by_name(devices_devices_list_box_selection)
window[KEY_DEVICES_LIST_BOX_DEVICE_ENTITIES].update(device_inst.get_entities_names())




Expand All @@ -146,7 +158,7 @@ def clients_handler(window, event, values):
global clients_this_client

# update worker with list
window[KEY_CLIENTS_WORKERS_LIST_COMBO_BOX].update(values[KEY_CLIENTS_WORKERS_LIST_COMBO_BOX],values=list(json_distribtued_config_inst.get_workers_names_list()))
window[KEY_CLIENTS_WORKERS_LIST_COMBO_BOX].update(values[KEY_CLIENTS_WORKERS_LIST_COMBO_BOX],values=list(json_dc_inst.get_workers_names_list()))

if event == KEY_CLIENTS_NAME_INPUT:
clients_this_client_name = values[KEY_CLIENTS_NAME_INPUT]
Expand All @@ -158,11 +170,11 @@ def clients_handler(window, event, values):
clients_combo_box_worker_selection = values[KEY_CLIENTS_WORKERS_LIST_COMBO_BOX]

if (event == KEY_CLIENTS_WORKERS_LIST_ADD_WORKER) and clients_combo_box_worker_selection:
owned_workers_dict = json_distribtued_config_inst.get_owned_workers_by_clients_dict()
owned_workers_dict = json_dc_inst.get_owned_workers_by_clients_dict()
if clients_combo_box_worker_selection in owned_workers_dict:
sg.popup_ok(f"worker {clients_combo_box_worker_selection} already belongs to client {owned_workers_dict[clients_combo_box_worker_selection]}", title='Adding a worker failed')
elif clients_this_client is not None:
worker_sha = json_distribtued_config_inst.get_workers_dict()[clients_combo_box_worker_selection].get_sha()
worker_sha = json_dc_inst.get_workers_dict()[clients_combo_box_worker_selection].get_sha()
clients_this_client.add_worker(clients_combo_box_worker_selection, worker_sha)
window[KEY_CLIENTS_STATUS_BAR].update(f"Updated client {clients_this_client_name}: {clients_this_client}")
window[KEY_CLIENTS_WORKERS_LIST_BOX_CLIENT_FOCUS].update(clients_this_client.get_workers_names())
Expand All @@ -173,7 +185,7 @@ def clients_handler(window, event, values):
if event == KEY_CLIENTS_BUTTON_LOAD:
clients_this_client_name = values[KEY_ENTITIES_CLIENTS_LISTBOX][0] if values[KEY_ENTITIES_CLIENTS_LISTBOX] else None # protects from bypassing load with selection from KEY_DEVICES_SELECTED_ENTITY_COMBO
if clients_this_client_name:
clients_this_client = json_distribtued_config_inst.get_client(clients_this_client_name)
clients_this_client = json_dc_inst.get_client(clients_this_client_name)
clients_this_client_port = clients_this_client.get_port().get_value()
window[KEY_CLIENTS_NAME_INPUT].update(clients_this_client.get_name())
window[KEY_CLIENTS_PORT_INPUT].update(f"{clients_this_client_port}")
Expand All @@ -182,12 +194,12 @@ def clients_handler(window, event, values):

if event == KEY_CLIENTS_BUTTON_ADD:
if clients_this_client_name:
clients_this_client = json_distribtued_config_inst.get_client(clients_this_client_name)
clients_this_client = json_dc_inst.get_client(clients_this_client_name)
if clients_this_client is not None:
pass # update the client parameters
elif clients_this_client_port: # create a new client
clients_this_client = Client(clients_this_client_name, clients_this_client_port)
json_distribtued_config_inst.add_client(clients_this_client)
json_dc_inst.add_client(clients_this_client)
window[KEY_CLIENTS_STATUS_BAR].update(f"Added client {clients_this_client_name}: {clients_this_client}")
clients_this_client_name = ''
clients_this_client_port = ''
Expand Down Expand Up @@ -231,12 +243,12 @@ def routers_handler(window,event,values):


if event == KEY_ROUTERS_BUTTON_ADD and routers_this_router_name and routers_this_router_port and (routers_this_router_policy is not None):
routers_this_router = json_distribtued_config_inst.get_router(routers_this_router_name)
routers_this_router = json_dc_inst.get_router(routers_this_router_name)
if routers_this_router is None:
# there is no such router - create a new one
routers_this_router = Router(routers_this_router_name, routers_this_router_port, routers_this_router_policy)
if not routers_this_router.error():
json_distribtued_config_inst.add_router(routers_this_router)
json_dc_inst.add_router(routers_this_router)
routers_this_router = None
routers_this_router_name = None
routers_this_router_port = None
Expand All @@ -248,7 +260,7 @@ def routers_handler(window,event,values):
if event == KEY_ROUTERS_BUTTON_LOAD:
routers_this_router_name = values[KEY_ENTITIES_ROUTERS_LISTBOX][0] if values[KEY_ENTITIES_ROUTERS_LISTBOX] else None # protects from bypassing load with selection from KEY_DEVICES_SELECTED_ENTITY_COMBO
if routers_this_router_name:
routers_this_router = json_distribtued_config_inst.get_router(routers_this_router_name)
routers_this_router = json_dc_inst.get_router(routers_this_router_name)
routers_this_router_port = routers_this_router.get_port().get_value()
routers_this_router_policy = routers_this_router.get_policy().get_policy_name()
routers_update_inputs_ui(window)
Expand All @@ -267,13 +279,13 @@ def sources_handler(window, event, values):

if (event == KEY_SOURCES_BUTTON_ADD):
sources_this_source_name = values[KEY_SOURCES_NAME_INPUT]
sources_this_source = json_distribtued_config_inst.get_source(sources_this_source_name)
sources_this_source = json_dc_inst.get_source(sources_this_source_name)
#frequency handling:
frequency = values[KEY_SOURCES_FREQUENCY_INPUT]
epochs = Epochs(values[KEY_SOURCES_EPOCHS_INPUT]) if values[KEY_SOURCES_EPOCHS_INPUT] else None
checkbox_val = values[KEY_SOURCES_FREQUENCY_DEFAULT_CHECKBOX]
if checkbox_val:
frequency = json_distribtued_config_inst.get_frequency()
frequency = json_dc_inst.get_frequency()
if frequency is None:
sg.popup_ok(f"Default frequency was selected but never set!", title='Adding Source Failed')
else:
Expand All @@ -286,7 +298,7 @@ def sources_handler(window, event, values):
sources_this_source_policy = values[KEY_SOURCES_POLICY_COMBO_BOX]
sources_this_source_type = values[KEY_SOURCES_TYPE_COMBO_BOX]
sources_this_source = Source(sources_this_source_name, sources_this_source_port, sources_this_source_frequency, sources_this_source_policy, sources_this_source_epochs, sources_this_source_type)
json_distribtued_config_inst.add_source(sources_this_source)
json_dc_inst.add_source(sources_this_source)
else:
sg.popup_ok(f"Source {sources_this_source_name} is already exist", title='Adding Source Failed')
else:
Expand All @@ -311,8 +323,8 @@ def entities_handler(window, event, values):
if last_selected_entity in last_entities_list_state:
window[KEY_DEVICES_SELECTED_ENTITY_COMBO].update(last_selected_entity, last_entities_list_state)

if last_entities_list_state != json_distribtued_config_inst.get_entities():
last_entities_list_state = json_distribtued_config_inst.get_entities()
if last_entities_list_state != json_dc_inst.get_entities():
last_entities_list_state = json_dc_inst.get_entities()
window[KEY_DEVICES_SELECTED_ENTITY_COMBO].update(last_selected_entity, last_entities_list_state)

if event == KEY_ENTITIES_CLIENTS_LISTBOX:
Expand All @@ -328,16 +340,16 @@ def entities_handler(window, event, values):
window[KEY_DEVICES_SELECTED_ENTITY_COMBO].update(last_selected_entity)

# entities update lists
if json_distribtued_config_inst.get_clients_names() != entities_clients_names_list:
entities_clients_names_list = json_distribtued_config_inst.get_clients_names()
if json_dc_inst.get_clients_names() != entities_clients_names_list:
entities_clients_names_list = json_dc_inst.get_clients_names()
window[KEY_ENTITIES_CLIENTS_LISTBOX].update(entities_clients_names_list)

if json_distribtued_config_inst.get_routers_names() != entities_routers_names_list:
entities_routers_names_list = json_distribtued_config_inst.get_routers_names()
if json_dc_inst.get_routers_names() != entities_routers_names_list:
entities_routers_names_list = json_dc_inst.get_routers_names()
window[KEY_ENTITIES_ROUTERS_LISTBOX].update(entities_routers_names_list)

if json_distribtued_config_inst.get_sources_names() != entities_sources_names_list:
entities_sources_names_list = json_distribtued_config_inst.get_sources_names()
if json_dc_inst.get_sources_names() != entities_sources_names_list:
entities_sources_names_list = json_dc_inst.get_sources_names()
window[KEY_ENTITIES_SOURCES_LISTBOX].update(entities_sources_names_list)


Expand Down
28 changes: 20 additions & 8 deletions src_py/nerlPlanner/JsonDistributedConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def __init__(self):
self.default_frequency = None

def init_dictionary(self):
self.main_dict[KEY_NERLNET_SETTINGS] = ""
self.main_dict[MainServer.NAME] = ""
self.main_dict[ApiServer.NAME] = ""
self.main_dict[KEY_NERLNET_SETTINGS] = None
self.main_dict[MainServer.NAME] = None
self.main_dict[ApiServer.NAME] = None
self.main_dict[KEY_DEVICES] = OrderedDict()
self.main_dict[KEY_ROUTERS] = OrderedDict()
self.main_dict[KEY_SOURCES] = OrderedDict()
Expand All @@ -37,7 +37,7 @@ def clear(self):
self.init_dictionary()

def get_entities(self):
special_entities = [x for x in list(self.reserved_names_set) if x in self.main_dict]
special_entities = [x for x in list(self.reserved_names_set) if (x in self.main_dict) and self.main_dict [x]]
return self.get_clients_names() + self.get_routers_names() + self.get_sources_names() + special_entities

def add_nerlnet_settings(self, frequency : Frequency, batchsize : BatchSize):
Expand All @@ -49,10 +49,10 @@ def get_frequency(self):
return self.default_frequency # returns Frequency or None

def add_main_server(self, main_server : MainServer):
self.main_dict[MainServer.NAME] = main_server.get_as_dict()
self.main_dict[MainServer.NAME] = main_server

def add_api_server(self, api_server : ApiServer):
self.main_dict[ApiServer.NAME] = api_server.get_as_dict()
self.main_dict[ApiServer.NAME] = api_server

def get_devices_names(self):
return list(self.main_dict[KEY_DEVICES].keys())
Expand All @@ -62,6 +62,7 @@ def get_device_by_name(self, name : str) -> Device:

def get_devices_ips(self):
return list( dev.get_ip().get_address() for _ , dev in self.main_dict[KEY_DEVICES].items())


DEVICE_ADD_ISSUE_WITH_IP = -1
DEVICE_ADD_ISSUE_WITH_NAME = -2
Expand All @@ -76,11 +77,20 @@ def add_device(self, device : Device) -> int:
return self.DEVICE_ADD_SUCCESS

def add_entity_to_device(self, device_name : str , entity_name : str):
'''
Input device and entity names that exists in json DC database
'''
if (device_name in self.main_dict[KEY_DEVICES]) and (entity_name in self.get_entities()):
for dev_name in self.get_devices_names():
dev_inst = self.main_dict[KEY_DEVICES][dev_name]
if entity_name in dev_inst.get_entities_names():
return False
device_inst = self.get_device_by_name(device_name)
device_inst.add_entity()
entity_inst = self.get_entity(entity_name)
device_inst.add_entity(entity_inst)
else:
raise "issue with given device or entity"
return False
return True



Expand Down Expand Up @@ -114,6 +124,8 @@ def get_entity(self, entity_name : str):
for entity_type in [KEY_ROUTERS, KEY_CLIENTS, KEY_SOURCES]:
if entity_name in self.main_dict[entity_type]:
return self.main_dict[entity_type][entity_name]
if entity_name in self.special_entities_list:
return self.main_dict[entity_name]
return None

def get_entity_with_type(self, entity_name : str, entity_type):
Expand Down
Loading

0 comments on commit f9001ae

Please sign in to comment.