Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hidra
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
steinbac
hidra
Commits
d7375b58
Commit
d7375b58
authored
9 years ago
by
Manuela Kuhn
Browse files
Options
Downloads
Patches
Plain Diff
Added communication with receiver infrastructure to live viewer
parent
9ba0529e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
LiveViewer.py
+166
-118
166 additions, 118 deletions
LiveViewer.py
with
166 additions
and
118 deletions
LiveViewer.py
+
166
−
118
View file @
d7375b58
# -*- coding: utf-8 -*-
import
os
import
time
from
dectris
import
albula
from
PyQt4
import
QtCore
from
PyQt4.QtCore
import
SIGNAL
,
QThread
,
QMutex
class
LiveView
(
QThread
):
FILETYPE_CBF
=
0
FILETYPE_TIF
=
1
FILETYPE_HDF5
=
2
alive
=
False
path
=
""
filetype
=
0
interval
=
0.5
#s
stoptimer
=
-
1.0
viewer
=
None
subframe
=
None
mutex
=
None
def
__init__
(
self
,
path
=
None
,
filetype
=
None
,
interval
=
None
,
parent
=
None
):
QThread
.
__init__
(
self
,
parent
)
if
path
is
not
None
:
self
.
path
=
path
if
filetype
is
not
None
:
self
.
filetype
=
filetype
if
interval
is
not
None
:
self
.
interval
=
interval
self
.
mutex
=
QMutex
()
def
start
(
self
,
path
=
None
,
filetype
=
None
,
interval
=
None
):
if
path
is
not
None
:
self
.
path
=
path
if
filetype
is
not
None
:
self
.
filetype
=
filetype
if
interval
is
not
None
:
self
.
interval
=
interval
QThread
.
start
(
self
)
def
stop
(
self
,
interval
=
0.0
):
if
self
.
stoptimer
<
0.0
and
interval
>
0.0
:
print
"
Live view thread: Stopping in %d seconds
"
%
interval
self
.
stoptimer
=
interval
return
print
"
Live view thread: Stopping thread
"
self
.
alive
=
False
self
.
wait
()
# waits until run stops on his own
def
run
(
self
):
self
.
alive
=
True
print
"
Live view thread: started
"
suffix
=
[
"
.cbf
"
,
"
.tif
"
,
"
.hdf5
"
]
if
self
.
filetype
in
[
LiveView
.
FILETYPE_CBF
,
LiveView
.
FILETYPE_TIF
]:
# open viewer
while
self
.
alive
:
# find latest image
self
.
mutex
.
lock
()
files
=
[(
os
.
path
.
getmtime
(
self
.
path
+
"
/
"
+
fn
),
fn
)
for
fn
in
os
.
listdir
(
self
.
path
)
if
fn
.
lower
().
endswith
(
suffix
[
self
.
filetype
])]
files
.
sort
()
files
.
reverse
()
if
len
(
files
)
>
0
:
# display image
# wait to make sure the image is copied completely before displaying it
time
.
sleep
(
0.1
)
try
:
self
.
subframe
.
loadFile
(
self
.
path
+
"
/
"
+
files
[
0
][
1
])
# viewer or subframe has been closed by the user
except
:
self
.
mutex
.
unlock
()
time
.
sleep
(
0.1
)
try
:
self
.
subframe
=
self
.
viewer
.
openSubFrame
()
except
:
self
.
viewer
=
albula
.
openMainFrame
()
self
.
subframe
=
self
.
viewer
.
openSubFrame
()
continue
self
.
mutex
.
unlock
()
# wait interval
interval
=
0.0
while
interval
<
self
.
interval
and
self
.
alive
:
if
self
.
stoptimer
>
0.0
:
self
.
stoptimer
-=
0.05
if
self
.
stoptimer
<
0.0
:
self
.
stoptimer
=
-
1.0
self
.
alive
=
False
time
.
sleep
(
0.05
)
interval
+=
0.05
elif
self
.
filetype
==
LiveView
.
FILETYPE_HDF5
:
print
"
Live view thread: HDF5 not supported yet
"
print
"
Live view thread: Thread for Live view died
"
self
.
alive
=
False
def
setPath
(
self
,
path
=
None
):
self
.
mutex
.
lock
()
if
path
is
not
None
:
self
.
path
=
path
self
.
mutex
.
unlock
()
def
setFiletype
(
self
,
filetype
=
None
):
restart
=
False
if
self
.
alive
:
restart
=
True
self
.
stop
()
if
filetype
is
not
None
:
self
.
filetype
=
filetype
if
restart
:
self
.
start
()
def
setInterval
(
self
,
interval
=
None
):
if
interval
is
not
None
:
self
.
interval
=
interval
# -*- coding: utf-8 -*-
import
os
import
time
#from dectris import albula
from
PyQt4
import
QtCore
from
PyQt4.QtCore
import
SIGNAL
,
QThread
,
QMutex
import
zmq
class
LiveView
(
QThread
):
FILETYPE_CBF
=
0
FILETYPE_TIF
=
1
FILETYPE_HDF5
=
2
alive
=
False
path
=
""
filetype
=
0
interval
=
0.5
#s
stoptimer
=
-
1.0
viewer
=
None
subframe
=
None
mutex
=
None
zmqIp
=
"
127.0.0.1
"
zmqPort
=
"
6071
"
zmqContext
=
None
zmqSocket
=
None
def
__init__
(
self
,
path
=
None
,
filetype
=
None
,
interval
=
None
,
parent
=
None
):
QThread
.
__init__
(
self
,
parent
)
if
path
is
not
None
:
self
.
path
=
path
if
filetype
is
not
None
:
self
.
filetype
=
filetype
if
interval
is
not
None
:
self
.
interval
=
interval
self
.
zmqContext
,
self
.
zmqSocket
=
createZmqSocket
(
self
.
zmqIp
,
self
.
zmqPort
)
self
.
mutex
=
QMutex
()
def
start
(
self
,
path
=
None
,
filetype
=
None
,
interval
=
None
):
if
path
is
not
None
:
self
.
path
=
path
if
filetype
is
not
None
:
self
.
filetype
=
filetype
if
interval
is
not
None
:
self
.
interval
=
interval
QThread
.
start
(
self
)
def
stop
(
self
,
interval
=
0.0
):
if
self
.
stoptimer
<
0.0
and
interval
>
0.0
:
print
"
Live view thread: Stopping in %d seconds
"
%
interval
self
.
stoptimer
=
interval
return
print
"
Live view thread: Stopping thread
"
self
.
alive
=
False
# close ZeroMQ socket and destroy ZeroMQ context
stopZmq
(
self
.
zmqSocket
,
self
.
zmqContext
)
self
.
wait
()
# waits until run stops on his own
def
run
(
self
):
self
.
alive
=
True
print
"
Live view thread: started
"
suffix
=
[
"
.cbf
"
,
"
.tif
"
,
"
.hdf5
"
]
if
self
.
filetype
in
[
LiveView
.
FILETYPE_CBF
,
LiveView
.
FILETYPE_TIF
]:
# open viewer
while
self
.
alive
:
print
"
self.alive
"
,
self
.
alive
# find latest image
self
.
mutex
.
lock
()
# get latest file from reveiver
try
:
received_file
=
communicateWithReceiver
(
self
.
zmqSocket
)
except
zmq
.
error
.
ZMQError
:
print
"
ZMQError
"
break
# display image
# try:
# self.subframe.loadFile(receiived_file)
# viewer or subframe has been closed by the user
# except:
# self.mutex.unlock()
# time.sleep(0.1)
# try:
# self.subframe = self.viewer.openSubFrame()
# except:
# self.viewer = albula.openMainFrame()
# self.subframe = self.viewer.openSubFrame()
# continue
self
.
mutex
.
unlock
()
# wait interval
interval
=
0.0
while
interval
<
self
.
interval
and
self
.
alive
:
if
self
.
stoptimer
>
0.0
:
self
.
stoptimer
-=
0.05
if
self
.
stoptimer
<
0.0
:
self
.
stoptimer
=
-
1.0
self
.
alive
=
False
time
.
sleep
(
0.05
)
interval
+=
0.05
elif
self
.
filetype
==
LiveView
.
FILETYPE_HDF5
:
print
"
Live view thread: HDF5 not supported yet
"
print
"
Live view thread: Thread for Live view died
"
self
.
alive
=
False
def
setPath
(
self
,
path
=
None
):
self
.
mutex
.
lock
()
if
path
is
not
None
:
self
.
path
=
path
self
.
mutex
.
unlock
()
def
setFiletype
(
self
,
filetype
=
None
):
restart
=
False
if
self
.
alive
:
restart
=
True
self
.
stop
()
if
filetype
is
not
None
:
self
.
filetype
=
filetype
if
restart
:
self
.
start
()
def
setInterval
(
self
,
interval
=
None
):
if
interval
is
not
None
:
self
.
interval
=
interval
def
createZmqSocket
(
zmqIp
,
zmqPort
):
context
=
zmq
.
Context
()
assert
isinstance
(
context
,
zmq
.
sugar
.
context
.
Context
)
socket
=
context
.
socket
(
zmq
.
REQ
)
connectionStrSocket
=
"
tcp://{ip}:{port}
"
.
format
(
ip
=
zmqIp
,
port
=
zmqPort
)
socket
.
connect
(
connectionStrSocket
)
return
context
,
socket
def
communicateWithReceiver
(
socket
):
print
"
Asking for next file
"
socket
.
send
(
"
NextFile
"
)
# Get the reply.
message
=
socket
.
recv
()
print
"
Next file:
"
,
message
def
stopZmq
(
zmqSocket
,
zmqContext
):
try
:
print
"
closing zmqSocket...
"
zmqSocket
.
close
(
linger
=
0
)
print
"
closing zmqSocket...done.
"
except
Exception
as
e
:
print
"
closing zmqSocket...failed.
"
print
e
try
:
print
"
closing zmqContext...
"
zmqContext
.
destroy
()
"
closing zmqContext...done.
"
except
Exception
as
e
:
print
"
closing zmqContext...failed.
"
print
e
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