diff --git a/EXEC.bat b/EXEC.bat new file mode 100644 index 0000000..bf31763 --- /dev/null +++ b/EXEC.bat @@ -0,0 +1,8 @@ +@echo off +cd /d %~dp0 +echo Starting OPC UA Robot Control Dashboard... +echo. + +REM Use conda run instead of activate (more reliable) +conda run -n opcua_com streamlit run app.py --server.port 8505 + diff --git a/EXEC_DASHBOARD.bat b/EXEC_DASHBOARD.bat deleted file mode 100644 index a562816..0000000 --- a/EXEC_DASHBOARD.bat +++ /dev/null @@ -1,29 +0,0 @@ -@echo off -cd /d %~dp0 -echo Starting OPC UA Robot Control Dashboard... -echo. - -REM Check if conda is available -where conda >nul 2>nul -if %ERRORLEVEL% NEQ 0 ( - echo ERROR: Conda is not installed or not in PATH - echo Please install Anaconda or Miniconda first - pause - exit /b 1 -) - -echo Setting up conda environment... - -REM Try to create environment, if it exists, update it -conda env create -f environment.yml 2>nul -if %ERRORLEVEL% NEQ 0 ( - echo Environment already exists, updating... - conda env update -f environment.yml -) - -echo. -echo Starting Streamlit app on port 8505 using conda run... -conda run -n opcua_com --no-capture-output streamlit run app.py --server.port 8505 -echo. -echo Streamlit exited with code %ERRORLEVEL%. -pause diff --git a/INSTALL.bat b/INSTALL.bat new file mode 100644 index 0000000..08aa7d7 --- /dev/null +++ b/INSTALL.bat @@ -0,0 +1,44 @@ +@echo off +cd /d %~dp0 +echo Installing OPC UA Robot Control Dashboard... +echo. + +REM Check if conda is available +where conda >nul 2>nul +if %ERRORLEVEL% NEQ 0 ( + echo ERROR: Conda is not installed or not in PATH + echo Please install Anaconda or Miniconda first + pause + exit /b 1 +) + +echo Checking if environment 'opcua_com' exists... + +REM Check if environment exists using conda info --envs +conda info --envs | findstr "opcua_com" >nul 2>&1 +if %ERRORLEVEL% EQU 0 ( + echo Environment 'opcua_com' found, updating... + conda env update -f environment.yml + if %ERRORLEVEL% NEQ 0 ( + echo WARNING: Failed to update environment, but continuing... + ) else ( + echo Environment updated successfully! + ) +) else ( + echo Environment 'opcua_com' not found, creating... + conda env create -f environment.yml + if %ERRORLEVEL% NEQ 0 ( + echo ERROR: Failed to create environment + echo Please check the environment.yml file + pause + exit /b 1 + ) else ( + echo Environment created successfully! + ) +) + +echo. +echo Environment 'opcua_com' is ready! +echo You can now run EXEC.bat to start the dashboard. +echo. +pause \ No newline at end of file diff --git a/app.py b/app.py index 633472a..817ed79 100644 --- a/app.py +++ b/app.py @@ -103,8 +103,7 @@ def is_opcua_connected(OPCcon) -> bool: try: if OPCcon is None: return False - client = OPCcon.opcuaclient - return hasattr(client, 'uaclient') and client.uaclient and getattr(client.uaclient, 'session', None) is not None + return OPCcon.check_connection() == 1 except Exception: return False @@ -119,35 +118,6 @@ def add_log(message): """No-op logger (logs disabled for minimal UI).""" return None -def check_opcua_server(OPCcon): - """Check if OPC UA server is reachable""" - try: - # Check if already connected - if hasattr(OPCcon.opcuaclient, 'uaclient') and OPCcon.opcuaclient.uaclient: - add_log("✅ OPC UA server already connected") - return True - - # Try to connect - OPCcon.opcuaclient.connect() - add_log("✅ OPC UA server is reachable") - return True - except Exception as e: - add_log(f"❌ Error connecting to OPC UA server: {e}") - return False - -def establish_connection(OPCcon): - """Establish connection to OPC UA server""" - try: - if OPCcon.opcuaclient.uaclient: - add_log("✅ OPC UA connection established") - return True - else: - add_log("❌ Error establishing connection") - return False - except Exception as e: - add_log(f"❌ Error establishing connection: {e}") - return False - def push_button(OPCcon, btn_name): """Push a button on the OPC UA server""" try: @@ -165,17 +135,16 @@ def push_button(OPCcon, btn_name): def initialize_connection(): """Initialize OPC UA connection""" try: - config = yaml.safe_load(open('./cfg.yaml')) + config = yaml.safe_load(_APP_CFG) OPCcon = opcua_connector(config) - # Check server - if check_opcua_server(OPCcon): - # Establish connection - if establish_connection(OPCcon): - st.session_state.opcua_connector = OPCcon - st.session_state.connection_status = "Connected" - add_log("🎉 Connection initialized successfully") - return True + # Connect and check status + OPCcon.opcuaclient.connect() + if OPCcon.check_connection() == 1: + st.session_state.opcua_connector = OPCcon + st.session_state.connection_status = "Connected" + add_log("🎉 Connection initialized successfully") + return True st.session_state.connection_status = "Disconnected" return False @@ -216,10 +185,9 @@ with st.sidebar: st.session_state.opcua_connector.influxclient.close() add_log("📊 InfluxDB connection closed") - # Disconnect OPC UA - if hasattr(st.session_state.opcua_connector, 'opcuaclient'): - st.session_state.opcua_connector.opcuaclient.disconnect() - add_log("🔌 Disconnected from OPC UA server") + # Disconnect OPC UA using the new method + st.session_state.opcua_connector.disconnect() + add_log("🔌 Disconnected from OPC UA server") except Exception as e: add_log(f"⚠️ Warning during disconnect: {e}") diff --git a/opcua_connector.py b/opcua_connector.py index a1565d5..8796de9 100644 --- a/opcua_connector.py +++ b/opcua_connector.py @@ -22,6 +22,33 @@ class opcua_connector: self.influx_bucket = config['cred']["influxdb"]["bucket"] self.varlist_dict = self.read_varlist(config['data']["cfg_varlist"]) + + + def check_connection(self): + ''' + check if the connection is established + ''' + try: + if self.opcuaclient: + print("ROBOT-OPCUA connection established.") + return 1 + else: + print("ROBOT-OPCUA connection not established.") + return 0 + except Exception as e: + print(f"ROBOT-OPCUA Error establishing connection") + return 0 + + def disconnect(self): + ''' + disconnect from the robot + ''' + try: + self.opcuaclient.disconnect() + except Exception as e: + raise e + + def press_btn(self, btn_name): var = self.varlist_dict[btn_name] print(var) @@ -40,15 +67,15 @@ class opcua_connector: object_node = self.opcuaclient.get_node("ns=2;s=/Methods") object_node.call_method(method_node, self.config['cred']["robot"]["username"], access_rights) -def _get_opcua_data(self, var_list): - """ Retrieve OPC UA node values based on the list of variable nodes. """ - return [self.opcuaclient.get_node(var).get_value() for var in var_list] + def _get_opcua_data(self, var_list): + """ Retrieve OPC UA node values based on the list of variable nodes. """ + return [self.opcuaclient.get_node(var).get_value() for var in var_list] -def _sent_opcua_data_impuls(self, var): - node = self.opcuaclient.get_node(var) - node.set_value(1) # Set node to 1 - # time.sleep(1) # Wait for 1 second - # node.set_value(0) # Set node back to 0 + def _sent_opcua_data_impuls(self, var): + node = self.opcuaclient.get_node(var) + node.set_value(1) # Set node to 1 + # time.sleep(1) # Wait for 1 second + # node.set_value(0) # Set node back to 0