-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAllunaToolKit.py
230 lines (189 loc) · 7.86 KB
/
AllunaToolKit.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
"""
A pyhton script to control the GUI application named "TCS" developed by Alluna.
TCS supports the ASCOM interface to provide a kind of APIs to handle itself,
however, ASCOM does not have dustcover capability,
there is no way to control dust cover from CUI.
That is the reason to develop this script.
2014.5.8 Yousuke Utsumi
"""
from pywinauto import application, controls
import time
import config
import threading
import logging
pathtoapp = "C:\Program Files\ALLUNA Optics\Telescope Control System\TCS.exe"
windowname = u'TCS V11.0T'
logging.basicConfig(format=config.FORMAT, level=config.loglevel)
logger=logging.getLogger(__name__)
class AlarmException(Exception):
def __init__(self,msg):
Exception.__init__(self,msg)
def timerhandler():
raise AlarmException("Maybe connection is lost")
class Telescope:
def __init__(self):
pass
def __del__(self):
self.app.kill_()
def __checkconnection(self):
if u"Connect" in self.buttonconnect.Texts():
logger.info("Connecting to the telescope...")
self.buttonconnect.Click()
else:
logger.debug("Already connected to the telescope...")
def Connect(self):
"""Try to connect to the TCS software"""
try:
self.app = application.Application()
self.app.connect_(path=pathtoapp)
logger.info("TCS is already running... Try to fetch the handle.")
except application.ProcessNotFoundError:
logger.info("Try to run TCS application")
self.app = application.Application.start(pathtoapp)
self.app_form = self.app[windowname]
self.buttonconnect = controls.win32_controls.ButtonWrapper(self.app_form[u"Connect"])
self.__checkconnection()
self._WaitCompletion() # wait a completion of the initialization
logger.info("Get tab content to handle tabs")
self.tabcontrol=controls.common_controls.TabControlWrapper(self.app_form[u"TTabControl"])
self.tabdict = dict( \
[ (self.tabcontrol.GetTabText(i), i) for i in range(self.tabcontrol.TabCount())])
logger.info("Get tab content in Settings to handle tabs")
self._MoveTab("Settings") # need to move to get labels before do it
self.settingstabcontrol=controls.common_controls.TabControlWrapper(self.app_form[u"TTabControl2"])
self.settingstabdict = dict( \
[ (self.settingstabcontrol.GetTabText(i), i) for i in range(self.settingstabcontrol.TabCount())])
self._WaitCompletion()
def _MoveTab(self,dst):
"""Internal method to handle the top-layere tabs"""
if self.tabcontrol.GetSelectedTab() == self.tabdict[u"%s" % dst]:
return
logger.info("Then move to %s control tab" % dst)
self.tabcontrol.Select(self.tabdict[u"%s" % dst])
def _MoveSettingTab(self,dst):
"""Internal method to handle the tabs in the setting tab"""
if self.settingstabcontrol.GetSelectedTab() \
== self.settingstabdict[u"%s" % dst]:
return
logger.info("Then move to %s control tab" % dst)
self.settingstabcontrol.Select(self.settingstabdict[u"%s" % dst])
def _DustcoverControl(self,cmd):
"""Internal method to control the mirror cover"""
self._MoveTab("Dustcover")
logger.info("Try to %s dust cover" % cmd)
if cmd in self.DustcoverStatus():
logger.info("Now %sing" % cmd)
if self.app_form["Button2"].IsEnabled():
self.app_form["Button2"].Click()
while "Wait ..." in self.DustcoverStatus():
logger.debug("Wait completion for 1 seconds.")
time.sleep(1)
else:
logger.error("Seems lost the handle to the telescope")
else:
logger.info("Seems already %sed" % cmd)
def DustcoverStatus(self):
"""Try to retrieve the mirror cover"""
self._MoveTab("Dustcover")
self._WaitCompletion()
return self.app_form["Button2"].Texts()
def DustcoverOpen(self):
"""Try to open the mirror cover"""
self._DustcoverControl("Open")
def DustcoverClose(self):
"""Try to close the mirror cover"""
self._DustcoverControl("Close")
def _WaitCompletion(self):
"""Waits to completion of something to do"""
try:
t=threading.Timer(config.apptimeout,timerhandler)
t.start()
nth=threading.activeCount()
logger.debug("a number of thread is %d" % nth)
while self.buttonconnect.IsEnabled() != True:
logger.debug("wait for 1 seconds")
time.sleep(1)
if nth!=threading.activeCount():
raise AlarmException("Timeout")
t.cancel()
except AlarmException:
logger.error("Application may be not respond. Try to restart")
self.app.kill_()
time.sleep(3)
self.Connect()
finally:
del t
def FocusingTargetPosition(self,target):
"""Try to make the focuser to be at desired position in terms of the counter"""
self._MoveTab("Focus")
zpos=controls.win32_controls.EditWrapper(self.app_form["TJvSpinEdit17"])
# zpos.SetText("%d" % int(target/config.focusconv))
zpos.SetText("%d" % target)
gotobutton=controls.win32_controls.EditWrapper(self.app_form["GoToButton2"])
gotobutton.Click()
self._WaitCompletion()
def FocusingHomePosition(self):
"""Try to make the focuser to be at the home position"""
self._MoveTab("Focus")
self.app_form["GoToStatic"]
nominalbutton = controls.win32_controls.ButtonWrapper(self.app_form["GoToStatic"])
nominalbutton.Click(coords=(26,0))
self._WaitCompletion()
# There was no key to describe "Homerun" button.
# So I decited to use a "coords" extraoption to shift mouse click.
def FocusingPosition(self):
"""Returns the current focuser's position"""
self._MoveTab("Settings")
self._MoveSettingTab("Focuser")
# return float(self.app_form["TJvSpinEdit4"].GetProperties()["Texts"][0])/1000.
return float(self.app_form["TJvSpinEdit4"].GetProperties()["Texts"][0])*config.focusconv
def InspectClass(self):
# self._MoveTab("Focus")
self._MoveTab("Climate")
for i, child in enumerate(self.app_form.Children()):
if child.IsVisible() is not True:
continue
child.CaptureAsImage().save("%s%d.jpg" \
% (child.FriendlyClassName(),i) )
def CheckAppStatus(self):
"""ControlNotEnalbed or ControlNotVisible"""
try:
t=threading.Timer(config.apptimeout,timerhandler)
t.start()
nth=threading.activeCount()
logger.debug("a number of thread is %d" % nth)
while self.buttonconnect.IsVisible() != True:
self.app_form.TypeKeys("\e")
time.sleep(1)
if nth!=threading.activeCount():
raise AlarmException("Timeout")
t.cancel()
self.__checkconnection()
except AlarmException as e:
self.app_form.TypeKeys("\e")
self.app.kill_()
time.sleep(3)
self.Connect()
raise e
except controls.HwndWrapper.ControlNotEnabled as e :
raise e
except controls.HwndWrapper.ControlNotVisible as e :
raise e
finally:
del t
if __name__ == "__main__":
import time, sys, traceback
try:
telescope = Telescope()
telescope.Connect()
telescope.InspectClass()
# print telescope.DustcoverStatus()
# telescope.DustcoverOpen()
# print telescope.DustcoverStatus()
# telescope.DustcoverClose()
# telescope.FocusingTargetPosition(9884)
# print telescope.FocusingPosition()
except:
traceback.print_exc(file=sys.stdout)
pass
# del telescope