-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwxProsilica.py
315 lines (255 loc) · 11.8 KB
/
wxProsilica.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!wxProcilica.py
# wxProsilica
#
# This is a wxPython GUI designed to launch EPICS IOC and MEDM
# scripts on the new RHEL LDAP Detector Pool machines.
#
# Authors: Russell Woods, Matthew Moore
# Date: 05/20/2013
# 08/08/2013
# 03/12/2014
# 03/27/2014
# 07/17/2014
# 09/09/2014
# 12/09/2014 - Updated pv_Prefix construction
# 03/25/2015 - Added save restore menu & updated to newer way of button updateds
# 04/29/2015 - Added Quick Start Guide, removed old wxSaveRestore code
# 06/04/2019 - updated stop_Event and pscheck to work with caQtDM from APSshare
import wx
import commands
import os
import signal
import subprocess
import time
import threading
import DPOStools
import xrd_config
WINDOW_WIDTH = 200
WINDOW_HEIGHT = 1000
DETECTOR = 'Prosilica'
#pv_Prefix = 'prosilica'
pv_Prefix = xrd_config.DP_PV_SECTOR + 'prosilica' + xrd_config.DP_PV_SUFFIX
class ProsilicaFrame(wx.Frame):
'''Prosilica Window'''
# Globals
pid_ioc = -999
pid_medm = -999
pid_imagej = -999
# Define Self Method
def __init__(self, position=(400,500), parent=None, ClosePrompt=False):
wx.Frame.__init__(self, parent, title = 'DP Prosilica Startup', pos = position, size = (WINDOW_WIDTH, WINDOW_HEIGHT), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
#Set PS checking wait time
self.checkCycle=0.1
# Make the panel
self.background = wx.Panel(self, -1, style=wx.SUNKEN_BORDER)
self.closeprompt=ClosePrompt # Add a 'OnClose' Message Prompt
self.Bind(wx.EVT_CLOSE, self.OnClose)
# Make a Menu Bar
self.menubar = wx.MenuBar()
self.helpDocs = wx.Menu() # Make a Menu
self.helpDocs.Append(101, '&Prosilica', '') # Add entry
wx.EVT_MENU(self, 101, self.helpDocs_101_Event) # Bind to Event
self.menubar.Append(self.helpDocs, '&Help Documents') # Append to Menu Bar
self.SetMenuBar(self.menubar) # Set Menu Bar
#--------------------------------------------------------------------------------------
# Prosilica
#--------------------------------------------------------------------------------------
self.title = wx.StaticText(self.background, label="Prosilica CCD")
self.title.SetFont(wx.Font(20, wx.SWISS, wx.NORMAL, wx.BOLD))
# Model Name
self.ModelBox_title = wx.StaticText(self.background, -1, 'DP Prosilica \t\nModel Number:\t')
self.ModelBox_title.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
self.ModelBox = wx.ComboBox(self.background, choices=["GC1380H", "GC2450", "XrayEye", "1BM_manta" ], size=[90,25])
self.ModelBox.SetEditable(False)
# Bit Depth
self.BitDepthBox_title = wx.StaticText(self.background, -1, 'Bit Depth:\t')
self.BitDepthBox_title.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
self.BitDepthBox = wx.ComboBox(self.background, choices=["8", "16"], size=[80,25])
self.BitDepthBox.SetEditable(False)
# Launch and Stop Buttons:
# This Dictionary holds required information for each Process controlled by the GUI
self.processes={
'IOC':{ 'pid': -999,
'running': False,
'search': 'prosilicaApp',
'file': '/local/DPbin/Scripts/start_ioc',
},
'MEDM':{ 'pid': -999,
'running': False,
'search': 'medm -x -macro P='+pv_Prefix +':, R=cam1: prosilica.adl',
'file': ['/local/DPbin/Scripts/start_medm_prosilica', pv_Prefix],
},
'IMAGEJ':{ 'pid': -999,
'running': False,
'search': 'jar ij.jar -run EPICS AD Viewer',
'file': ['/local/DPbin/Scripts/start_imageJ', pv_Prefix],
},
'SAVE-RESTORE MENU':
{ 'pid': -999,
'running': False,
'search': 'medm -x -macro P='+ pv_Prefix +':,CONFIG=setup, configMenu_small.adl',
'file': ['/local/DPbin/Scripts/start_medm_configMenu', pv_Prefix],
},
'caQtDM':
{ 'pid': -999,
'running': False,
'search': 'caQtDM -macro P='+ pv_Prefix + ':, R=cam1: prosilica.ui',
'file': ['/local/DPbin/Scripts/start_caQtDM_prosilica',pv_Prefix,]
},
}
# Start and Stop Buttons:
self.ButtonOrder = [
"IOC",
"MEDM",
'IMAGEJ',
'SAVE-RESTORE MENU',
'caQtDM',
]
self.Buttons = dict.fromkeys(self.ButtonOrder)
# Create Buttons and labels
for Row in self.ButtonOrder:
self.Buttons[Row] = dict.fromkeys(["Title", "Start Button", "Stop Button"])
self.Buttons[Row]["Title"] = wx.StaticText(self.background, label=Row + " \t")
self.Buttons[Row]["Title"].SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
self.Buttons[Row]["Start Button"] = wx.Button(self.background, label='Start', size=[60,25])
self.Buttons[Row]["Stop Button"] = wx.Button(self.background, label='Stop', size=[60,25])
# Bind buttons to functions
self.Buttons['IOC']['Start Button'].Bind(wx.EVT_BUTTON, lambda event: self.start_Event(event, 'IOC'))
self.Buttons['IOC']['Stop Button'].Bind(wx.EVT_BUTTON, lambda event: self.stop_Event(event, 'IOC'))
self.Buttons['MEDM']['Start Button'].Bind(wx.EVT_BUTTON, lambda event: self.start_Event(event, 'MEDM'))
self.Buttons['MEDM']['Stop Button'].Bind(wx.EVT_BUTTON, lambda event: self.stop_Event(event, 'MEDM'))
self.Buttons['IMAGEJ']['Start Button'].Bind(wx.EVT_BUTTON, lambda event: self.start_Event(event, 'IMAGEJ'))
self.Buttons['IMAGEJ']['Stop Button'].Bind(wx.EVT_BUTTON, lambda event: self.stop_Event(event, 'IMAGEJ'))
self.Buttons['SAVE-RESTORE MENU']['Start Button'].Bind(wx.EVT_BUTTON, lambda event: self.start_Event(event, 'SAVE-RESTORE MENU'))
self.Buttons['SAVE-RESTORE MENU']['Stop Button'].Bind(wx.EVT_BUTTON, lambda event: self.stop_Event(event, 'SAVE-RESTORE MENU'))
self.Buttons['caQtDM']['Start Button'].Bind(wx.EVT_BUTTON, lambda event: self.start_Event(event, 'caQtDM'))
self.Buttons['caQtDM']['Stop Button'].Bind(wx.EVT_BUTTON, lambda event: self.stop_Event(event, 'caQtDM'))
# Make Horizontal Box Sizers
self.horizontalBoxes = []
self.horizontalBoxes.append(wx.BoxSizer(wx.HORIZONTAL))
self.horizontalBoxes[-1].Add(self.ModelBox_title, proportion = 1, border = 0,flag=wx.ALIGN_CENTER)
self.horizontalBoxes[-1].Add(self.ModelBox, proportion = 0, border = 0,flag=wx.ALIGN_CENTER)
self.horizontalBoxes.append(wx.BoxSizer(wx.HORIZONTAL))
self.horizontalBoxes[-1].Add(self.BitDepthBox_title, proportion = 1, border = 0,flag=wx.ALIGN_CENTER)
self.horizontalBoxes[-1].Add(self.BitDepthBox, proportion = 0, border = 0,flag=wx.ALIGN_CENTER)
for Rows in self.ButtonOrder:
self.horizontalBoxes.append(wx.BoxSizer(wx.HORIZONTAL))
self.horizontalBoxes[-1].Add(self.Buttons[Rows]['Title'], proportion = 1, border = 0,flag=wx.ALIGN_CENTER)
self.horizontalBoxes[-1].Add(self.Buttons[Rows]['Start Button'], proportion = 0, border = 0,flag=wx.ALIGN_CENTER)
self.horizontalBoxes[-1].Add(self.Buttons[Rows]['Stop Button'], proportion = 0, border = 0,flag=wx.ALIGN_CENTER)
# Make a vertical sizer
self.verticalBox = wx.BoxSizer(wx.VERTICAL)
self.verticalBox.Add(self.title, proportion = 0, border = 1, flag=wx.ALIGN_CENTER)
self.verticalBox.Add(wx.StaticLine(self.background, -1, style=wx.LI_HORIZONTAL), proportion=0, flag=wx.EXPAND)
self.verticalBox.Add(wx.StaticLine(self.background, -1, style=wx.LI_HORIZONTAL), proportion=0, flag=wx.EXPAND)
for Row in self.horizontalBoxes:
self.verticalBox.Add(Row, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)
self.verticalBox.Add(wx.StaticLine(self.background, -1, style=wx.LI_HORIZONTAL), proportion=0, flag=wx.EXPAND)
# PS checking
self.progRunning = True
self.PSthread = threading.Thread(target=self.pscheck)
self.PSthread.start()
# Set the panel size
self.background.SetSizer(self.verticalBox)
self.background.Fit()
self.Fit()
self.Show()
#--------------------------------------------------------------------------------------
# Define Event Methods (button actions)
#--------------------------------------------------------------------------------------
def helpDocs_101_Event(self, event):
tempCommand = ['firefox', 'http://www.aps.anl.gov/Xray_Science_Division/Detectors/Detector_Pool/Detector_Information/Prosilica/index.html']
p_ioc = subprocess.Popen(tempCommand, shell=False, preexec_fn=os.setsid)
def start_Event(self, event, app):
'''Start an App'''
if not(self.processes[app]['running']):
print 'Starting ' + str(app) + '...'
# Start the subprocess
if app == 'IOC':
CCD = self.ModelBox.GetValue()
BitDepth = self.BitDepthBox.GetValue()
if len(CCD)>0 and len(BitDepth)>0:
# Start the IOC
# Start the subprocess
tempFile = [self.processes['IOC']['file'], str(CCD) + "-" + str(BitDepth)]
self.IOCsubprocess = subprocess.Popen(tempFile, preexec_fn=os.setsid)
# Grab the subprocess I.D.
self.processes['IOC']['pid'] = DPOStools.waitforprocess(self.processes['IOC']['search'])
self.processes['IOC']['running'] = True
self.button_status(app, 'on')
print 'process id ' + str(app) + ' = ' + str(self.processes[app]['pid'])
else:
subprocess.Popen(self.processes[app]['file'], preexec_fn=os.setsid)
# Grab the subprocess I.D.
self.processes[app]['pid'] = DPOStools.waitforprocess(self.processes[app]['search'])
self.processes[app]['running']=True
# Set the button state
self.button_status(app, 'on')
print 'process id ' + str(app) + ' = ' + str(self.processes[app]['pid'])
else:
print str(app)+' already running!'
def stop_Event(self, event, app):
'''Stop an App'''
if(self.processes[app]['running']):
print 'Stopping '+str(app)+'...'
# pgrep for all associated PIDs and make into a list
pids = (subprocess.check_output([ 'pgrep', '-f', self.processes[app]['search'] ])).split('\n')
pids = filter(None, pids)
# Loop over all PIDs
for i in pids:
print ' stopping pid: ' + str(i)
os.kill(int(i), signal.SIGKILL)
self.button_status(app, 'off')
else:
print "No process to stop."
def pscheck(self):
'''Track the current state of processes - Runs in a separate thread'''
while(self.progRunning):
for item in self.processes:
try:
tempResult = subprocess.check_output([ 'pgrep', '-f', self.processes[item]['search'] ])
self.processes[item]['pid'] = int(tempResult.split('\n')[0])
if not(self.processes[item]['running']):
self.button_status(item, 'on')
self.processes[item]['running'] = True
except:
if(self.processes[item]['running']):
self.button_status(item, 'off')
time.sleep(self.checkCycle)
def button_status(self, app, switch):
'''Change a button status'''
if switch == 'on':
#print 'setting button colour and label'
wx.CallAfter(self.Buttons[app]['Start Button'].SetBackgroundColour, wx.GREEN)
wx.CallAfter(self.Buttons[app]['Start Button'].SetLabel, "Running")
elif switch == 'off':
if app in self.processes:
print 'switching the button OFF...'
self.processes[app]['pid'] = -999
self.processes[app]['running'] = False
#print 'setting button colour and label'
wx.CallAfter(self.Buttons[app]['Start Button'].SetBackgroundColour, wx.WHITE)
wx.CallAfter(self.Buttons[app]['Start Button'].SetLabel, "Start")
def OnClose(self, event):
'''This method Prompts the User for confimation when closing the Window'''
if self.closeprompt:
dlg = wx.MessageDialog(self,"Close the GUI?","Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
else:
result = wx.ID_OK
if result == wx.ID_OK:
# Clean up PSthread
self.progRunning=False
while (self.PSthread.isAlive()):
time.sleep(self.checkCycle)
self.Hide()
#---------------------------------------------------------------------------------
# Main loop
#---------------------------------------------------------------------------------
# Make an application
#app_2 = wx.App(redirect=False)
# Make an instance of the class - the Topmost GUI element
#russTest = PilatusFrame()
# Let it run!
#app_2.MainLoop()