Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nerlnetplanner #262

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 89 additions & 15 deletions src_py/nerlPlanner/Handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def settings_handler(window, event, values):
device_by_main_server = json_dc_inst.get_device_by_entity(main_server_inst.NAME)
device_by_api_server = json_dc_inst.get_device_by_entity(api_server_inst.NAME)
window[KEY_SETTINGS_MAIN_SERVER_STATUS_BAR].update(f"Main Server, {main_server_inst}, {device_by_main_server}")
window[KEY_SETTINGS_API_SERVER_STATUS_BAR].update(f"Api Server, {api_server_inst}, {device_by_main_server}")
window[KEY_SETTINGS_API_SERVER_STATUS_BAR].update(f"Api Server, {api_server_inst}, {device_by_api_server}")

if event == KEY_SETTINGS_SPECIAL_ENTITIES_CLEAR:
main_server_port = values[KEY_SETTINGS_MAINSERVER_PORT_INPUT] if values[KEY_SETTINGS_MAINSERVER_PORT_INPUT] else None
Expand Down Expand Up @@ -147,46 +147,93 @@ def devices_reset_inputs_ui(window):
window[KEY_DEVICES_IP_INPUT].update('x.x.x.x')

def devices_handler(window, event, values):
global devices_name
global devices_ip_str
global devices_this_device_name
global devices_devices_list_box_selection
global last_selected_entity
global devices_this_device
global devices_this_device_ip_str

# inputs
if event == KEY_DEVICES_NAME_INPUT:
devices_name = values[KEY_DEVICES_NAME_INPUT]
devices_this_device_name = values[KEY_DEVICES_NAME_INPUT]
if event == KEY_DEVICES_IP_INPUT:
devices_ip_str = values[KEY_DEVICES_IP_INPUT]
devices_this_device_ip_str = values[KEY_DEVICES_IP_INPUT]
if event == KEY_DEVICES_LIST_BOX_DEVICES:
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]
devices_this_device_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_dc_inst.add_device(device_to_add)
if devices_this_device_name and devices_this_device_ip_str:
devices_this_device = Device(devices_this_device_ip_str, devices_this_device_name)
if not devices_this_device.error():
json_dc_inst.add_device(devices_this_device)
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_BUTTON_LOAD:
devices_this_device_name = values[KEY_DEVICES_LIST_BOX_DEVICES][0] if values[KEY_DEVICES_LIST_BOX_DEVICES] else None # protects from bypassing load with selection from KEY_DEVICES_SELECTED_ENTITY_COMBO
if devices_this_device_name:
devices_this_device = json_dc_inst.get_device_by_name(devices_this_device_name)
devices_this_device_ip_str = devices_this_device.get_ip().get_address()
window[KEY_DEVICES_NAME_INPUT].update(devices_this_device_name)
window[KEY_DEVICES_IP_INPUT].update(devices_this_device_ip_str)

if event == KEY_DEVICES_BUTTON_SAVE:
if devices_this_device and (devices_this_device.get_name() in json_dc_inst.get_devices_names()):
devices_this_device_name = devices_this_device_name if devices_this_device_name else devices_this_device.get_name()
devices_this_device_ip_str = devices_this_device_ip_str if devices_this_device_ip_str else devices_this_device.get_ip()
device_to_remove = devices_this_device
device_to_remove_name = device_to_remove.get_name()
entities_names = device_to_remove.get_entities_names()
devices_without_remove_candidate = json_dc_inst.get_devices_names()
devices_without_remove_candidate.remove(devices_this_device.get_name())
if devices_this_device_name not in devices_without_remove_candidate: # if apperas in devices_without_remove_candidate then operation is forbidden
json_dc_inst.remove_device(device_to_remove_name)
new_device = Device(devices_this_device_ip_str, devices_this_device_name)
if not new_device.error():
json_dc_inst.add_device(new_device)
window[KEY_DEVICES_LIST_BOX_DEVICES].update(json_dc_inst.get_devices_names())
else:
sg.popup_ok("Ip or Name are wrong or exist!")
# declare on varibales
main_server_inst = json_dc_inst.get_main_server() if json_dc_inst.get_main_server() else None
api_server_inst = json_dc_inst.get_api_server() if json_dc_inst.get_api_server() else None
main_server_name = main_server_inst.get_name() if main_server_inst else None
api_server_name = api_server_inst.get_name() if api_server_inst else None
# build device entity list
for entity_name in entities_names:
entity_inst = json_dc_inst.get_entity(entity_name)
new_device.add_entity(entity_inst)
# update special entities status bar
if main_server_name in new_device.get_entities_names():
window[KEY_SETTINGS_MAIN_SERVER_STATUS_BAR].update(f"Main Server, {main_server_inst}, {new_device}")
if api_server_name in new_device.get_entities_names():
window[KEY_SETTINGS_API_SERVER_STATUS_BAR].update(f"Api Server, {api_server_inst}, {new_device}")
window[KEY_DEVICES_LIST_BOX_DEVICE_ENTITIES].update(new_device.get_entities_names())
devices_devices_list_box_selection = "" # prevent update entites box for a device that doesn't exist
else:
sg.popup_ok(f"Name {devices_this_device_name} already exists", keep_on_top=True, title="Chance device issue")

if event == KEY_DEVICES_BUTTON_REMOVE and devices_devices_list_box_selection:
# Update settings status bar
device_inst = json_dc_inst.get_device_by_name(devices_devices_list_box_selection)
devices_this_device = json_dc_inst.get_device_by_name(devices_devices_list_box_selection)
main_server_inst = json_dc_inst.get_main_server()
api_server_inst = json_dc_inst.get_api_server()
if main_server_inst and main_server_inst.NAME in device_inst.get_entities_names():
if main_server_inst and main_server_inst.NAME in devices_this_device.get_entities_names():
window[KEY_SETTINGS_MAIN_SERVER_STATUS_BAR].update(f"Main Server, {main_server_inst}, None")
if api_server_inst and api_server_inst.NAME in device_inst.get_entities_names():
if api_server_inst and api_server_inst.NAME in devices_this_device.get_entities_names():
window[KEY_SETTINGS_API_SERVER_STATUS_BAR].update(f"Api Server, {api_server_inst}, None")

# Remove device and update GUI
json_dc_inst.remove_device(devices_devices_list_box_selection)
devices_devices_list_box_selection = ""
last_entities_list_state_not_occupied = [x for x in last_entities_list_state if x not in json_dc_inst.get_devices_entities()]
window[KEY_DEVICES_SELECTED_ENTITY_COMBO].update(last_selected_entity, last_entities_list_state_not_occupied)
window[KEY_DEVICES_LIST_BOX_DEVICE_ENTITIES].update("")
window[KEY_DEVICES_LIST_BOX_DEVICES].update(json_dc_inst.get_devices_names())
devices_reset_inputs_ui(window)
Expand All @@ -209,9 +256,15 @@ def devices_handler(window, event, values):
window[KEY_SETTINGS_API_SERVER_STATUS_BAR].update(f"Api Server, {api_server_inst}, {device_selected_inst}")

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())
devices_this_device = json_dc_inst.get_device_by_name(devices_devices_list_box_selection)
window[KEY_DEVICES_LIST_BOX_DEVICE_ENTITIES].update(devices_this_device.get_entities_names())

def clients_reset_inputs_ui(window):
global clients_this_client_name
global clients_this_client_port
window[KEY_CLIENTS_NAME_INPUT].update('')
window[KEY_CLIENTS_PORT_INPUT].update('')
window[KEY_CLIENTS_STATUS_BAR].update('')

def clients_handler(window, event, values):
global clients_combo_box_worker_selection
Expand Down Expand Up @@ -243,6 +296,14 @@ def clients_handler(window, event, values):
else:
sg.popup_ok(f"Add this client before adding workers", keep_on_top=True, title="Add workers issue")

if event == KEY_CLIENTS_WORKERS_LIST_REMOVE_WORKER:
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
worker_name = values[KEY_CLIENTS_WORKERS_LIST_BOX_CLIENT_FOCUS][0] if values[KEY_CLIENTS_WORKERS_LIST_BOX_CLIENT_FOCUS] else None
if clients_this_client_name and worker_name:
clients_this_client = json_dc_inst.get_client(clients_this_client_name)
clients_this_client.remove_worker(worker_name)
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())

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
Expand Down Expand Up @@ -287,6 +348,12 @@ def clients_handler(window, event, values):
window[KEY_CLIENTS_STATUS_BAR].update(f"Client {client_to_remove_name} replaced by Client: {new_client}")
else:
sg.popup_ok(f"Name {clients_this_client_name} already exists", keep_on_top=True, title="Chance client issue")

if event == KEY_CLIENTS_BUTTON_REMOVE:
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:
json_dc_inst.remove_client(clients_this_client_name)
clients_reset_inputs_ui(window)


def routers_reset_inputs_ui(window):
Expand Down Expand Up @@ -482,6 +549,13 @@ def sync_fields_with_json_dc_inst(window, values):
window[KEY_SETTINGS_MAINSERVER_ARGS_INPUT].update(json_dc_inst.get_main_server().get_args().get_value())
window[KEY_SETTINGS_APISERVER_PORT_INPUT].update(json_dc_inst.get_api_server().get_port().get_value_str())
window[KEY_SETTINGS_APISERVER_ARGS_INPUT].update(json_dc_inst.get_main_server().get_args().get_value())
# special entities status bar
main_server_inst = json_dc_inst.get_main_server()
api_server_inst = json_dc_inst.get_api_server()
device_by_main_server = json_dc_inst.get_device_by_entity(main_server_inst.NAME)
device_by_api_server = json_dc_inst.get_device_by_entity(api_server_inst.NAME)
window[KEY_SETTINGS_MAIN_SERVER_STATUS_BAR].update(f"Main Server, {main_server_inst}, {device_by_main_server}")
window[KEY_SETTINGS_API_SERVER_STATUS_BAR].update(f"Api Server, {api_server_inst}, {device_by_api_server}")
# settings
window[KEY_SETTINGS_FREQUENCY_INPUT].update(json_dc_inst.get_frequency().get_value_str()) if json_dc_inst.get_frequency() is not None else None
window[KEY_SETTINGS_BATCH_SIZE_INPUT].update(json_dc_inst.get_batch_size().get_value_str()) if json_dc_inst.get_batch_size() is not None else None
Expand Down
15 changes: 14 additions & 1 deletion src_py/nerlPlanner/JsonDistributedConfig.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import PySimpleGUI as sg
from JsonElements import *
from collections import OrderedDict
from JsonElementWorker import *
Expand Down Expand Up @@ -149,17 +150,29 @@ def add_entity_to_device(self, device_name : str , entity_name : str):
Input device and entity names that exist 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():
for dev_name in self.get_devices_names(): # check if the entities is used
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)
entity_inst = self.get_entity(entity_name)
if device_inst.duplicated_ports_validator(entity_inst.get_port()): # check if the port is used by device
if not self.suggest_alternative_port(entity_inst, device_inst):
return False # don't switch the port
device_inst.add_entity(entity_inst)
else:
return False
return True

def suggest_alternative_port(self, entity_inst, device_inst : Device):
ch = sg.popup_yes_no("The port of the entity you selected is in use, would you like to change it to a random port?", title="suggest alternative port")
if ch == "Yes":
new_port = device_inst.generate_random_port()
entity_inst.set_port(new_port)
return True
else:
return False

def remove_entity_from_device(self, entity_name : str):
for dev_name in self.get_devices_names():
dev_inst = self.main_dict[KEY_DEVICES][dev_name]
Expand Down
29 changes: 21 additions & 8 deletions src_py/nerlPlanner/JsonElements.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def __init__(self, value):

def get_value(self):
return self.value

def get_value_str(self):
return str(self.value)

Expand All @@ -219,7 +219,10 @@ def __format__(self, __format_spec: str) -> str:

def get_port(self):
return self.port


def set_port(self, new_port : Port):
self.port = new_port

def get_args(self) -> str:
return self.args

Expand Down Expand Up @@ -248,6 +251,9 @@ def __format__(self, __format_spec: str) -> str:
def get_port(self):
return self.port

def set_port(self, new_port : Port):
self.port = new_port

def get_args(self):
return self.args

Expand Down Expand Up @@ -275,12 +281,13 @@ def __format__(self, __format_spec: str) -> str:
def get_entities_names(self):
return list(self.entities_dict.keys())

def duplicated_ports_validator(self):
def duplicated_ports_validator(self, new_entity_port : Port):
'''
Checks if there is at least a single occurance of duplicated ports.
If there is returns True
If there is returns True = duplicate
'''
ports_set = set()
ports_set.add(new_entity_port.get_value())
for _ , entity in self.entities_dict.items():
port_val = entity.get_port().get_value()
if port_val in ports_set:
Expand Down Expand Up @@ -332,6 +339,9 @@ def communication_elem(self):
def get_port(self):
return self.port

def set_port(self, new_port : Port):
self.port = new_port

def get_policy(self):
return self.policy

Expand Down Expand Up @@ -363,6 +373,9 @@ def source_type_error(self):
def get_port(self):
return self.port

def set_port(self, new_port : Port):
self.port = new_port

def get_frequency(self):
return self.frequency

Expand Down Expand Up @@ -404,10 +417,10 @@ def __format__(self, __format_spec: str) -> str:

def get_port(self):
return self.port
def set_port(self, port):
self.port = Port(port)

def set_port(self, new_port : Port):
self.port = new_port

def get_workers_names(self):
return list(self.workers_dict.keys())

Expand Down