forked from syncope/lavue
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlavuemonitor
executable file
·268 lines (240 loc) · 7.74 KB
/
lavuemonitor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/python
# Copyright (C) 2017 DESY, Notkestr. 85, D-22607 Hamburg
#
# lavue is an image viewing program for photon science imaging detectors.
# Its usual application is as a live viewer using hidra as data source.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation in version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#
# Authors:
# Jan Kotanski <jan.kotanski@desy.de>
# Christoph Rosemann <christoph.rosemann@desy.de>
#
# Socket to talk to server
import sys
import zmq
import time
import json
import math
import argparse
import os
import signal
maxtimegap = 1.
maximumval = 1000.
port = None
topicfilter = "10001"
debug = False
command = ""
rawimage = False
lasttime = time.time()
hostname = "localhost"
context = None
original_sigint = signal.getsignal(signal.SIGINT)
def _onexit(signum, frame):
global context
if context:
try:
context.destroy()
context = None
print("disconnect")
except:
pass
signal.signal(signal.SIGINT, original_sigint)
sys.exit(1)
def stophardware():
if command:
print("-----------------------------------------------------------")
print("execute: '%s'" % str(command))
os.system(command)
print("===========================================================")
def stop(pid, calctime, **args):
stophardware()
print("STOP !!! %s LiveView stopped" % calctime)
def start(pid, calctime, **args):
if debug:
print(" START: %s" % calctime)
def alive(pid, calctime, maxrawval, maxval, minval, scaling, meanval, **args):
global lasttime
if rawimage:
val = float(maxrawval)
else:
val = float(maxval)
if scaling == "sqrt":
val = val * val
elif scaling == "log":
val = math.pow(10, val)
elif scaling != "linear":
raise Exception("Unknown scaling: %s" % scaling)
if debug:
print(" ELIVE !!! %s %s %s %s %s" % (
calctime, scaling, maxval, maxrawval, val))
if val > maximumval:
print("STOP !!! %s maxval = %s > %s" % (calctime, val, maximumval))
stophardware()
lasttime = float(calctime)
def checktime(calctime):
ltime = time.time()
# if debug:
# print("GAP %s %s" % (ltime - calctime, maxtimegap))
if calctime + maxtimegap < ltime:
print("STOP !!! %s Time GAP = %s > %s" % (
calctime, ltime - calctime, maxtimegap))
stophardware()
def main():
global lasttime
global context
signal.signal(signal.SIGINT, _onexit)
context = zmq.Context()
socket = context.socket(zmq.SUB)
conn = "tcp://%s:%s" % (hostname, port)
print("Connecting to: %s" % conn)
socket.connect(conn)
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
receiveloop = True
while receiveloop:
try:
while True:
# check for a message, this will not block
string = socket.recv(flags=zmq.NOBLOCK)
topic, message = string.split(" ", 1)
md = json.loads(message)
cmd = md["command"].lower()
if cmd == 'stop':
stop(**md)
elif cmd == 'alive':
alive(**md)
elif cmd == 'start':
start(**md)
else:
raise Exception("Wrong command")
checktime(float(md["calctime"]))
time.sleep(maxtimegap * 0.001)
except zmq.Again as e:
pass
except Exception as e:
print("STOP !!! Error: %s" % str(e))
stophardware()
checktime(float(lasttime))
time.sleep(maxtimegap * 0.1)
if __name__ == "__main__":
options = None
parser = argparse.ArgumentParser(
description='ZMQ Client for laVue status')
parser.add_argument(
"-i", "--max-intensity",
help="maximal pixel value (default: 1000.)",
dest="maxval", default="1000.")
parser.add_argument(
"-g", "--time-gap",
help="maximal time gap in seconds (default: 1.)",
dest="timegap", default="1.")
parser.add_argument(
"-c", "--stop-command",
help="stop command",
dest="command", default="")
parser.add_argument(
"-r", "--raw", action="store_true",
default=False, dest="raw",
help="check raw image")
parser.add_argument(
"-p", "--port",
help="zmq port (default: automatic)",
dest="port", default=None)
parser.add_argument(
"-z", "--host",
help="zmq host (default: localhost)",
dest="host", default="localhost")
parser.add_argument(
"-t", "--topic",
help="zmq topic (default: 10001)",
dest="topic", default="10001")
parser.add_argument(
"--debug", action="store_true",
default=False, dest="debug",
help="debug mode")
options = parser.parse_args()
try:
port = int(options.port)
except:
try:
import psutil
pids = [process.pid for process in psutil.process_iter()
if process.name() == 'lavue']
ports = []
if len(pids) > 0:
for pd in pids:
proc = psutil.Process(pd)
cnns = proc.connections()
for cn in cnns:
if cn.status == 'LISTEN':
hostname, lport = cn.laddr
ports.append(str(lport))
if len(ports) == 1:
port = ports[0]
elif len(ports) > 1:
sys.stderr.write(
"lavuemonitor: Select one of the ports: %s\n" %
", ".join(ports))
sys.stderr.flush()
parser.print_help()
sys.exit(255)
else:
raise Exception("Cannot find the lavue port")
else:
raise Exception("Cannot find the lavue port")
except Exception as e:
sys.stderr.write(
"lavuemonitor: Invalid --port parameter\n")
sys.stderr.flush()
# print(str(e))
parser.print_help()
sys.exit(255)
try:
maximumval = float(options.maxval)
except:
sys.stderr.write(
"lavuemonitor: Invalid --maxval parameter\n")
sys.stderr.flush()
parser.print_help()
sys.exit(255)
try:
topicfilter = str(options.topic)
except:
sys.stderr.write(
"lavuemonitor: Invalid --topic parameter\n")
sys.stderr.flush()
parser.print_help()
sys.exit(255)
try:
command = str(options.command)
except:
sys.stderr.write(
"lavuemonitor: Invalid --stop-command parameter\n")
sys.stderr.flush()
parser.print_help()
sys.exit(255)
try:
maxtimegap = float(options.timegap)
except:
sys.stderr.write(
"lavuemonitor: Invalid --time-gap parameter\n")
sys.stderr.flush()
parser.print_help()
sys.exit(255)
debug = options.debug
rawimage = options.raw
hostname = options.host
main()