v1.0 .
This commit is contained in:
parent
9ee5ab82c5
commit
24d382e94a
95
app.py
95
app.py
@ -4,38 +4,9 @@ import time
|
|||||||
import os
|
import os
|
||||||
from opcua_connector import opcua_connector
|
from opcua_connector import opcua_connector
|
||||||
|
|
||||||
class app:
|
|
||||||
def __init__(self):
|
|
||||||
self.app_cfg = yaml.safe_load(open('./cfg.yaml')) or {}
|
|
||||||
self.OPCon = opcua_connector(self.app_cfg)
|
|
||||||
|
|
||||||
def initialize_connection(self):
|
|
||||||
"""Initialize OPC UA connection"""
|
|
||||||
try:
|
|
||||||
self.OPCon.connect()
|
|
||||||
if self.OPCon.check_connection() == 1:
|
|
||||||
st.session_state.connection_status = "Connected"
|
|
||||||
print("🎉 Connection initialized successfully")
|
|
||||||
return True
|
|
||||||
st.session_state.connection_status = "Disconnected"
|
|
||||||
return False
|
|
||||||
except Exception as e:
|
|
||||||
print(f"❌ Failed to initialize connection: {e}")
|
|
||||||
st.session_state.connection_status = "Disconnected"
|
|
||||||
return False
|
|
||||||
|
|
||||||
def execute_button_action(self, button_name):
|
|
||||||
"""Execute button action with proper error handling"""
|
|
||||||
if button_name == 'INIT':
|
|
||||||
self.OPCon.adapt_access_rights()
|
|
||||||
print(f"🔧 INIT button pressed - Access rights adapted")
|
|
||||||
else:
|
|
||||||
self.OPCon.press_btn(button_name)
|
|
||||||
print(f"🔘 {button_name} button pressed")
|
|
||||||
return True
|
|
||||||
|
|
||||||
# Load config early for password if present
|
# Load config early for password if present
|
||||||
app = app()
|
app_cfg = yaml.safe_load(open('./cfg.yaml')) or {}
|
||||||
|
|
||||||
|
|
||||||
# Page configuration
|
# Page configuration
|
||||||
st.set_page_config(
|
st.set_page_config(
|
||||||
@ -49,11 +20,19 @@ st.set_page_config(
|
|||||||
with open('styles.css') as f:
|
with open('styles.css') as f:
|
||||||
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
||||||
|
|
||||||
# Initialize session state
|
|
||||||
if 'connection_status' not in st.session_state:
|
def execute_button_action(button_name):
|
||||||
st.session_state.connection_status = "Disconnected"
|
"""Execute button action with proper error handling"""
|
||||||
if 'authenticated' not in st.session_state:
|
if st.session_state.opcon is None:
|
||||||
st.session_state.authenticated = False
|
return False
|
||||||
|
|
||||||
|
if button_name == 'INIT':
|
||||||
|
st.session_state.opcon.adapt_access_rights()
|
||||||
|
print(f"🔧 INIT button pressed - Access rights adapted")
|
||||||
|
else:
|
||||||
|
st.session_state.opcon.press_btn(button_name)
|
||||||
|
print(f"🔘 {button_name} button pressed")
|
||||||
|
return True
|
||||||
|
|
||||||
# Re-validate connection status each render (do this before rendering UI)
|
# Re-validate connection status each render (do this before rendering UI)
|
||||||
def is_opcua_connected(conn) -> bool:
|
def is_opcua_connected(conn) -> bool:
|
||||||
@ -61,10 +40,21 @@ def is_opcua_connected(conn) -> bool:
|
|||||||
return bool(conn and conn.check_connection() == 1)
|
return bool(conn and conn.check_connection() == 1)
|
||||||
except Exception:
|
except Exception:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
# Initialize session state
|
||||||
|
if 'opcon' not in st.session_state:
|
||||||
|
st.session_state.opcon = None
|
||||||
|
if 'connection_status' not in st.session_state:
|
||||||
|
st.session_state.connection_status = "Disconnected"
|
||||||
|
|
||||||
|
# Update connection status on each render
|
||||||
st.session_state.connection_status = (
|
st.session_state.connection_status = (
|
||||||
"Connected" if is_opcua_connected(app.OPCon) else "Disconnected"
|
"Connected" if is_opcua_connected(st.session_state.opcon) else "Disconnected"
|
||||||
)
|
)
|
||||||
|
if 'authenticated' not in st.session_state:
|
||||||
|
st.session_state.authenticated = False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -75,16 +65,31 @@ with st.sidebar:
|
|||||||
# Initialize button
|
# Initialize button
|
||||||
if st.button("🔌 Initialize Connection", use_container_width=True):
|
if st.button("🔌 Initialize Connection", use_container_width=True):
|
||||||
with st.spinner("Initializing connection..."):
|
with st.spinner("Initializing connection..."):
|
||||||
app.initialize_connection()
|
try:
|
||||||
|
if st.session_state.opcon is None:
|
||||||
|
st.session_state.opcon = opcua_connector(app_cfg)
|
||||||
|
|
||||||
|
st.session_state.opcon.connect()
|
||||||
|
if st.session_state.opcon.check_connection() == 1:
|
||||||
|
st.session_state.connection_status = "Connected"
|
||||||
|
print("🎉 Connection initialized successfully")
|
||||||
|
else:
|
||||||
|
st.session_state.connection_status = "Disconnected"
|
||||||
|
print("❌ Connection failed")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"❌ Failed to initialize connection: {e}")
|
||||||
|
st.session_state.connection_status = "Disconnected"
|
||||||
st.rerun()
|
st.rerun()
|
||||||
|
|
||||||
# Disconnect button
|
# Disconnect button
|
||||||
if st.button("🔌 Disconnect", use_container_width=True):
|
if st.button("🔌 Disconnect", use_container_width=True):
|
||||||
try:
|
try:
|
||||||
app.OPCon.disconnect()
|
if st.session_state.opcon:
|
||||||
print("🔌 Disconnected from OPC UA server")
|
st.session_state.opcon.disconnect()
|
||||||
|
print("🔌 Disconnected from OPC UA server")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"⚠️ Warning during disconnect: {e}")
|
print(f"⚠️ Warning during disconnect: {e}")
|
||||||
|
st.session_state.opcon = None
|
||||||
st.session_state.connection_status = "Disconnected"
|
st.session_state.connection_status = "Disconnected"
|
||||||
print("🔌 Connection reset")
|
print("🔌 Connection reset")
|
||||||
st.rerun()
|
st.rerun()
|
||||||
@ -96,7 +101,7 @@ with st.sidebar:
|
|||||||
# Password gate
|
# Password gate
|
||||||
# Prefer cfg.yaml cred.dashboard.password, else env DASHBOARD_PASSWORD, else 'admin'
|
# Prefer cfg.yaml cred.dashboard.password, else env DASHBOARD_PASSWORD, else 'admin'
|
||||||
expected_password = (
|
expected_password = (
|
||||||
(app.app_cfg.get('cred', {}).get('dashboard', {}) or {}).get('password')
|
(app_cfg.get('cred', {}).get('dashboard', {}) or {}).get('password')
|
||||||
or 'admin'
|
or 'admin'
|
||||||
)
|
)
|
||||||
ui_locked = not st.session_state.authenticated
|
ui_locked = not st.session_state.authenticated
|
||||||
@ -120,19 +125,19 @@ if not ui_locked:
|
|||||||
|
|
||||||
with btn_col1:
|
with btn_col1:
|
||||||
if st.button("🔧\n\nINIT", key="init_btn", use_container_width=True, disabled=buttons_disabled):
|
if st.button("🔧\n\nINIT", key="init_btn", use_container_width=True, disabled=buttons_disabled):
|
||||||
app.execute_button_action('INIT')
|
execute_button_action('INIT')
|
||||||
|
|
||||||
with btn_col2:
|
with btn_col2:
|
||||||
if st.button("▶️\n\nSTART", key="start_btn", use_container_width=True, disabled=buttons_disabled):
|
if st.button("▶️\n\nSTART", key="start_btn", use_container_width=True, disabled=buttons_disabled):
|
||||||
app.execute_button_action('BTN_START')
|
execute_button_action('BTN_START')
|
||||||
|
|
||||||
with btn_col3:
|
with btn_col3:
|
||||||
if st.button("⏹️\n\nSTOP", key="stop_btn", use_container_width=True, disabled=buttons_disabled):
|
if st.button("⏹️\n\nSTOP", key="stop_btn", use_container_width=True, disabled=buttons_disabled):
|
||||||
app.execute_button_action('BTN_STOP')
|
execute_button_action('BTN_STOP')
|
||||||
|
|
||||||
with btn_col4:
|
with btn_col4:
|
||||||
if st.button("🔄\n\nRESET", key="reset_btn", use_container_width=True, disabled=buttons_disabled):
|
if st.button("🔄\n\nRESET", key="reset_btn", use_container_width=True, disabled=buttons_disabled):
|
||||||
app.execute_button_action('BTN_RESET')
|
execute_button_action('BTN_RESET')
|
||||||
|
|
||||||
st.markdown('</div>', unsafe_allow_html=True)
|
st.markdown('</div>', unsafe_allow_html=True)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user