This commit is contained in:
Eduard Gerlitz 2025-09-05 15:10:30 +02:00
parent 27734995b6
commit 512b5714d1
5 changed files with 99 additions and 81 deletions

8
EXEC.bat Normal file
View File

@ -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

View File

@ -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

44
INSTALL.bat Normal file
View File

@ -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

46
app.py
View File

@ -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,13 +135,12 @@ 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):
# 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")
@ -216,9 +185,8 @@ 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()
# Disconnect OPC UA using the new method
st.session_state.opcua_connector.disconnect()
add_log("🔌 Disconnected from OPC UA server")
except Exception as e:

View File

@ -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)