-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgammac.py
executable file
·134 lines (106 loc) · 4.38 KB
/
gammac.py
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
#!/usr/bin/env python2
#
# Terminal client for gamma measurements
# Copyright (C) 2016 Norwegain Radiation Protection Authority
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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: Dag Robole,
from __future__ import print_function
import sys, signal, datetime, socket, argparse, json
exit_dump = False
def signalHandler(signal, frame):
global exit_dump
exit_dump = True
def handleOneResponse(skt, timeout, bufsiz):
skt.settimeout(timeout)
try:
data, server = skt.recvfrom(bufsiz)
print("received %s" % json.loads(data.decode("utf-8")))
except socket.timeout:
print("Timeout waiting for response")
except socket.error as err:
print("Interrupted")
def handleResponses(skt, timeout, bufsiz):
global exit_dump
skt.settimeout(timeout)
while not exit_dump:
try:
data, server = skt.recvfrom(bufsiz)
print("received %s" % json.loads(data.decode("utf-8")))
except socket.error as err:
pass
def main():
signal.signal(signal.SIGINT, signalHandler)
parser = argparse.ArgumentParser()
parser.add_argument('mode', help = "Possible values are: config, start, stop, dump")
parser.add_argument('--session', help = "Name of session to operate on")
parser.add_argument('--ip', default = '127.0.0.1:9999', help = "IP address and port of remote peer. Default 127.0.0.1:9999")
parser.add_argument('--timeout', type = int, default = 5, help = "Receive timeout for responses in seconds. Default 5")
parser.add_argument('--buffersize', type = int, default = 8192, help = "Size of response buffer in bytes. Default 8192")
args = parser.parse_args()
ip, sep, port = args.ip.partition(':')
port = 9999 if not port else int(port)
address = (ip, port)
msg = None
if args.mode == 'config':
msg = {
'command': "detector_config",
'detector_type': "osprey",
'voltage': 775,
'coarse_gain': 1.0,
'fine_gain': 1.375,
'num_channels': 1024,
'lld': 3,
'uld': 110
}
args.timeout = 30
responseFunc = handleOneResponse
elif args.mode == 'start':
session_name = '{:%d%m%Y_%H%M%S}'.format(datetime.datetime.now())
msg = {
'command': "start_session",
'ip': ip,
'session_name': session_name,
'comment': "gammac",
'livetime': 2,
'detector_data': '{"TypeName":"NaI-2x2","CurrentHV":691,"CurrentNumChannels":1024,"Serialnumber":"CP-932","CurrentCoarseGain":1.0,"CurrentFineGain":2.647,"CurrentLivetime":2,"CurrentLLD":3,"CurrentULD":110,"EnergyCurveCoefficients":[-16.322600665547952,1.5798230485869882,2.5686852946056607E-07,-2.0953782940494292E-07]}',
'detector_type_data': '{"Name":"NaI-2x2","MaxNumChannels":2048,"MinHV":1,"MaxHV":1300,"GEScript":"Nai-2tom.py"}'
}
responseFunc = handleOneResponse
elif args.mode == 'stop':
if args.session is None:
print('Missing session argument')
else:
msg = { 'command': "stop_session", 'session_name': args.session }
responseFunc = handleOneResponse
elif args.mode == 'dump':
msg = { 'command': "dump_session" }
args.timeout = 30
responseFunc = handleResponses
elif args.mode == 'status':
msg = { 'command': "get_status" }
responseFunc = handleOneResponse
else:
print("Invalid options")
os.exit(1)
try:
skt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
nbytes = skt.sendto(bytes(json.dumps(msg)), address)
responseFunc(skt, args.timeout, args.buffersize)
finally:
skt.close()
if __name__ == "__main__":
main()