Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
tango
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LOFAR2.0
tango
Commits
409ab032
Commit
409ab032
authored
4 years ago
by
Jan David Mol
Browse files
Options
Downloads
Patches
Plain Diff
Split off helper functions into separate files
parent
96ed3b95
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
RCUSCC/RCUSCC/RCUSCC.py
+2
-110
2 additions, 110 deletions
RCUSCC/RCUSCC/RCUSCC.py
RCUSCC/RCUSCC/opcua_connection.py
+80
-0
80 additions, 0 deletions
RCUSCC/RCUSCC/opcua_connection.py
RCUSCC/RCUSCC/wrappers.py
+37
-0
37 additions, 0 deletions
RCUSCC/RCUSCC/wrappers.py
with
119 additions
and
110 deletions
RCUSCC/RCUSCC/RCUSCC.py
+
2
−
110
View file @
409ab032
...
...
@@ -25,121 +25,13 @@ import sys
import
opcua
import
traceback
import
numpy
import
time
from
threading
import
Thread
from
functools
import
wraps
import
socket
from
wrappers
import
only_when_on
,
fault_on_error
from
opcua_connection
import
OPCUAConnection
__all__
=
[
"
RCUSCC
"
,
"
main
"
]
def
only_when_on
(
func
):
"""
Wrapper to return None when the device isn
'
t in ON state.
If in ON state, calls & returns the wrapped function.
"""
@wraps
(
func
)
def
when_on_wrapper
(
self
,
*
args
,
**
kwargs
):
if
self
.
get_state
()
!=
DevState
.
ON
:
return
None
return
func
(
self
,
*
args
,
**
kwargs
)
return
when_on_wrapper
def
fault_on_error
(
func
):
"""
Wrapper to catch exceptions. Sets the device in a FAULT state if any occurs.
"""
@wraps
(
func
)
def
error_wrapper
(
self
,
*
args
,
**
kwargs
):
try
:
return
func
(
self
,
*
args
,
**
kwargs
)
except
Exception
as
e
:
self
.
error_stream
(
"
Function failed. Trace: %s
"
,
traceback
.
format_exc
())
self
.
Fault
()
return
None
return
error_wrapper
class
OPCUAConnection
(
Thread
):
"""
Connects to OPC-UA in the foreground or background, and sends HELLO
messages to keep a check on the connection. On connection failure, reconnects once.
"""
def
__init__
(
self
,
client
,
on_func
,
fault_func
,
streams
,
try_interval
=
2
):
super
().
__init__
(
daemon
=
True
)
self
.
client
=
client
self
.
on_func
=
on_func
self
.
fault_func
=
fault_func
self
.
try_interval
=
try_interval
self
.
streams
=
streams
self
.
stopping
=
False
self
.
connected
=
False
def
_servername
(
self
):
return
self
.
client
.
server_url
.
geturl
()
def
connect
(
self
):
try
:
self
.
streams
.
debug_stream
(
"
Connecting to server %s
"
,
self
.
_servername
())
self
.
client
.
connect
()
self
.
connected
=
True
self
.
streams
.
debug_stream
(
"
Connected to server. Initialising.
"
)
return
True
except
socket
.
error
as
e
:
self
.
streams
.
error_stream
(
"
Could not connect to server %s: %s
"
,
self
.
_servername
(),
e
)
return
False
def
disconnect
(
self
):
self
.
connected
=
False
# always force a reconnect, regardless of a successful disconnect
try
:
self
.
client
.
disconnect
()
except
Exception
as
e
:
self
.
streams
.
error_stream
(
"
Disconnect from OPC-UA server %s failed: %s
"
,
self
.
_servername
(),
e
)
def
run
(
self
):
while
not
self
.
stopping
:
# keep trying to connect
if
not
self
.
connected
:
if
self
.
connect
():
self
.
on_func
()
else
:
# we retry only once, to catch exotic network issues. if the infra or hardware is down,
# our device cannot help, and must be reinitialised after the infra or hardware is fixed.
self
.
fault_func
()
return
# keep checking if the connection is still alive
try
:
while
not
self
.
stopping
:
self
.
client
.
send_hello
()
time
.
sleep
(
self
.
try_interval
)
except
Exception
as
e
:
self
.
streams
.
error_stream
(
"
Lost connection to server %s: %s
"
,
self
.
_servername
(),
e
)
# technically, we may not have dropped the connection, but encounter a different error. so explicitly disconnect.
self
.
disconnect
()
# signal that we're disconnected
self
.
fault_func
()
def
stop
(
self
):
"""
Stop connecting & disconnect. Can take a few seconds for the timeouts to hit.
"""
self
.
stopping
=
True
self
.
join
()
self
.
disconnect
()
class
RCUSCC
(
Device
):
"""
...
...
This diff is collapsed.
Click to expand it.
RCUSCC/RCUSCC/opcua_connection.py
0 → 100644
+
80
−
0
View file @
409ab032
from
threading
import
Thread
import
socket
import
time
__all__
=
[
"
OPCUAConnection
"
]
class
OPCUAConnection
(
Thread
):
"""
Connects to OPC-UA in the foreground or background, and sends HELLO
messages to keep a check on the connection. On connection failure, reconnects once.
"""
def
__init__
(
self
,
client
,
on_func
,
fault_func
,
streams
,
try_interval
=
2
):
super
().
__init__
(
daemon
=
True
)
self
.
client
=
client
self
.
on_func
=
on_func
self
.
fault_func
=
fault_func
self
.
try_interval
=
try_interval
self
.
streams
=
streams
self
.
stopping
=
False
self
.
connected
=
False
def
_servername
(
self
):
return
self
.
client
.
server_url
.
geturl
()
def
connect
(
self
):
try
:
self
.
streams
.
debug_stream
(
"
Connecting to server %s
"
,
self
.
_servername
())
self
.
client
.
connect
()
self
.
connected
=
True
self
.
streams
.
debug_stream
(
"
Connected to server. Initialising.
"
)
return
True
except
socket
.
error
as
e
:
self
.
streams
.
error_stream
(
"
Could not connect to server %s: %s
"
,
self
.
_servername
(),
e
)
return
False
def
disconnect
(
self
):
self
.
connected
=
False
# always force a reconnect, regardless of a successful disconnect
try
:
self
.
client
.
disconnect
()
except
Exception
as
e
:
self
.
streams
.
error_stream
(
"
Disconnect from OPC-UA server %s failed: %s
"
,
self
.
_servername
(),
e
)
def
run
(
self
):
while
not
self
.
stopping
:
# keep trying to connect
if
not
self
.
connected
:
if
self
.
connect
():
self
.
on_func
()
else
:
# we retry only once, to catch exotic network issues. if the infra or hardware is down,
# our device cannot help, and must be reinitialised after the infra or hardware is fixed.
self
.
fault_func
()
return
# keep checking if the connection is still alive
try
:
while
not
self
.
stopping
:
self
.
client
.
send_hello
()
time
.
sleep
(
self
.
try_interval
)
except
Exception
as
e
:
self
.
streams
.
error_stream
(
"
Lost connection to server %s: %s
"
,
self
.
_servername
(),
e
)
# technically, we may not have dropped the connection, but encounter a different error. so explicitly disconnect.
self
.
disconnect
()
# signal that we're disconnected
self
.
fault_func
()
def
stop
(
self
):
"""
Stop connecting & disconnect. Can take a few seconds for the timeouts to hit.
"""
self
.
stopping
=
True
self
.
join
()
self
.
disconnect
()
This diff is collapsed.
Click to expand it.
RCUSCC/RCUSCC/wrappers.py
0 → 100644
+
37
−
0
View file @
409ab032
from
tango
import
DevState
from
functools
import
wraps
import
traceback
__all__
=
[
"
only_when_on
"
,
"
fault_on_error
"
]
def
only_when_on
(
func
):
"""
Wrapper to return None when the device isn
'
t in ON state.
If in ON state, calls & returns the wrapped function.
"""
@wraps
(
func
)
def
when_on_wrapper
(
self
,
*
args
,
**
kwargs
):
if
self
.
get_state
()
!=
DevState
.
ON
:
return
None
return
func
(
self
,
*
args
,
**
kwargs
)
return
when_on_wrapper
def
fault_on_error
(
func
):
"""
Wrapper to catch exceptions. Sets the device in a FAULT state if any occurs.
"""
@wraps
(
func
)
def
error_wrapper
(
self
,
*
args
,
**
kwargs
):
try
:
return
func
(
self
,
*
args
,
**
kwargs
)
except
Exception
as
e
:
self
.
error_stream
(
"
Function failed. Trace: %s
"
,
traceback
.
format_exc
())
self
.
Fault
()
return
None
return
error_wrapper
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment