-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtelescope.py
370 lines (308 loc) · 12.1 KB
/
telescope.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""
The Telescope module executes telescope commands via an SSH connection to the aster server (the SEO telescope host).
"""
import logging
import paramiko
import subprocess
import re
from astropy.coordinates import EarthLocation
import astropy.units as u
class SSH:
def __init__(self, ixchel):
self.logger = logging.getLogger('SSH')
self.ixchel = ixchel
self.config = ixchel.config
self.lock = ixchel.lock
self.server = self.config.get('ssh', 'server')
self.username = self.config.get('ssh', 'username')
self.key_path = self.config.get('ssh', 'key_path')
# init Paramiko
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def connect(self):
try:
self.ixchel.slack.send_message(
'Connecting to the telescope. Please wait...')
self.ssh.connect(self.server, username=self.username,
key_filename=self.key_path)
self.command('echo its alive', False) # test the connection
self.ixchel.slack.send_message('Connected to the telescope!')
return True
except Exception as e:
self.ixchel.slack.send_message(
'Failed to connect to the telescope!')
self.logger.error(
'SSH initialization failed. Exception (%s).' % e)
return False
def command(self, command, is_background):
if is_background:
return self.command_background(command)
else:
return self.command_foreground(command)
def command_background(self, command):
if not self.is_connected():
self.logger.error(
'Background command (%s) failed. SSH client is not connected.' % command)
return False
# run command
result = {
'response': None,
'stdout': [],
'stderr': [],
'pid': None
}
try:
self.logger.info('Running background command: %s' % command)
stdin, stdout, stderr = self.ssh.exec_command('%s &' % command)
stdout.channel.recv_exit_status()
result['stdout'] = stdout.readlines()
result['stderr'] = stderr.readlines()
self.logger.debug(result['stdout'])
self.logger.debug(result['stderr'])
if len(result['stdout']) > 0:
result['response'] = result['stdout'][0]
# get the pid
match = re.search(r'([0-9]+)$', result['response'])
if match:
result['pid'] = int(match.group(1))
else:
self.logger.error(
'Command (%s) did not return a pid.' % command)
elif len(result['stderr']) > 0:
result['response'] = result['stderr'][0]
self.logger.error('Command (%s) returned error (%s).' % (
command, result['response']))
else:
result['response'] = ''
self.logger.warning(
'Command (%s) returned no response.' % (command))
except Exception as e:
self.logger.error(
'SSH command failed. Exception (%s).' % e)
return result
def command_foreground(self, command):
self.logger.info(command)
if not self.is_connected():
self.logger.error(
'Foreground command (%s) failed. SSH client is not connected.' % command)
return False
# run command
result = {
'response': None,
'stdout': [],
'stderr': [],
'pid': None
}
try:
self.logger.info('Running foreground command: %s' % command)
stdin, stdout, stderr = self.ssh.exec_command(command)
stdout.channel.recv_exit_status()
result['stdout'] = stdout.readlines()
result['stderr'] = stderr.readlines()
self.logger.debug(result['stdout'])
self.logger.debug(result['stderr'])
if len(result['stdout']) > 0:
result['response'] = result['stdout']
elif len(result['stderr']) > 0:
result['response'] = result['stderr']
self.logger.error('Command (%s) returned error (%s).' % (
command, result['response']))
else:
result['response'] = ['']
self.logger.warning(
'Command (%s) returned no response.' % (command))
except Exception as e:
self.logger.error(
'SSH command failed. Exception (%s).' % e)
return result
def get_file(self, remote_path, local_path):
if not self.is_connected():
self.logger.error('SFTP failed. SSH client is not connected.')
return False
try:
sftp = self.ssh.open_sftp()
sftp.get(remote_path, local_path)
sftp.close()
except Exception as e:
self.logger.error('SFTP failed. Exception (%s).' % e)
return False
return True
def is_connected(self):
try:
self.ssh.exec_command('echo its alive') # test the connection
return True
except Exception as e: # try to reconnect
self.logger.warning(
'SSH command failed. Exception (%s). Reconnecting...' % e)
return self.connect()
class Telescope:
ssh = None
def __init__(self, ixchel):
self.logger = logging.getLogger('Telescope')
self.ixchel = ixchel
self.config = ixchel.config
self.use_ssh = self.config.get('telescope', 'use_ssh', False)
self.latitude = self.config.get('telescope', 'latitude')
self.longitude = self.config.get('telescope', 'longitude')
self.elevation = self.config.get('telescope', 'elevation')
self.image_dir = self.config.get('telescope', 'image_dir')
self.earthLocation = EarthLocation(lat=float(
self.latitude)*u.deg, lon=float(self.longitude)*u.deg, height=float(self.elevation)*u.m)
if self.use_ssh:
self.ssh = SSH(ixchel)
self.ssh.connect()
def init_ssh(self):
server = self.config.get('ssh', 'server')
username = self.config.get('ssh', 'username')
key_path = self.config.get('ssh', 'key_path')
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(server, username=username, key_filename=key_path)
try:
ssh.exec_command('echo its alive') # test the connection
return ssh
except Exception as e:
self.logger.error(
'SSH initialization failed. Exception (%s).' % e)
return None
def command(self, command, is_background, use_communicate=True, timeout=0):
result = {
'stdout': [],
'stderr': []
}
# add a timeout to this command
if timeout > 0:
command = 'timeout %f ' % timeout + command
# use ssh
if self.use_ssh:
if self.ssh == None: # not connected?
self.logger.warn(
'SSH is not connected. Reconnecting...')
self.ssh.connect()
else: # need to reconnect?
try:
self.ssh.command('echo its alive', is_background)
except Exception as e:
self.logger.warn(
'SSH is not connected. Reconnecting...')
self.ssh.connect()
try:
return self.ssh.command(command, is_background)
except Exception as e:
self.logger.error(
'Command (%s) via SSH failed. Exception (%s).')
return result
else: # use local
command_array = command.split()
try:
sp = subprocess.Popen(
command_array, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
sp.wait()
if use_communicate:
output, error = sp.communicate(b'\n\n')
result['stdout'] = output.splitlines()
result['stderr'] = error.splitlines()
except Exception as e:
self.logger.error(
'Command (%s) failed. Exception (%s).')
return result
def get_file(self, remote_path, local_path):
return self.ssh.get_file(remote_path, local_path)
# Generic getter is the standard for all SEO get commands
# To support future telescope interfaces,
# these will be called by the *explicit* getter
def getter(self, interface):
results = self.command(interface.get_command(),
interface.is_background())
result = results['response']
# parse the result and assign values to output valuse
interface.assign_outputs(result)
# Generic setter is the standard for all SEO get commands
# To support future telescope interfaces,
# these will be called by the *explicit* setter
def setter(self, interface):
command = interface.assign_inputs()
results = self.command(command, interface.is_background())
result = results['response']
# parse the result and assign values to output valuse
interface.assign_outputs(result)
def get_domecam(self, interface):
self.setter(interface)
def get_skycam(self, interface):
self.setter(interface)
def to_stars(self, interface):
self.setter(interface)
def get_image(self, interface):
self.setter(interface)
def get_psf(self, interface):
self.setter(interface)
def convert_fits_to_jpg(self, interface):
self.setter(interface)
def get_precipitation(self, interface):
self.getter(interface)
def get_sun(self, interface):
self.getter(interface)
def get_dome(self, interface):
self.getter(interface)
def center_dome(self, interface):
self.setter(interface)
def home_domer(self, interface):
self.getter(interface)
def home_domel(self, interface):
self.getter(interface)
def get_slit(self, interface):
self.getter(interface)
def set_slit(self, interface):
self.setter(interface)
def get_mirror(self, interface):
self.getter(interface)
def set_mirror(self, interface):
self.setter(interface)
def get_lights(self, interface):
self.getter(interface)
def set_lights(self, interface):
self.setter(interface)
def get_ccd(self, interface):
self.getter(interface)
def set_ccd(self, interface):
self.setter(interface)
def get_moon(self, interface):
self.getter(interface)
def get_filter(self, interface):
self.getter(interface)
def set_filter(self, interface):
self.setter(interface)
def get_focus(self, interface):
self.getter(interface)
def set_focus(self, interface):
self.setter(interface)
def get_lock(self, interface):
self.getter(interface)
def set_lock(self, interface):
self.setter(interface)
def unlock(self, interface):
self.setter(interface)
def keepopen(self, interface):
self.setter(interface)
def open_observatory(self, interface):
self.setter(interface)
def close_observatory(self, interface):
self.setter(interface)
def clear_lock(self, interface):
self.setter(interface)
def get_where(self, interface):
self.getter(interface)
def point(self, interface):
self.setter(interface)
def track(self, interface):
self.setter(interface)
def get_track(self, interface):
self.setter(interface)
def pinpoint(self, interface):
self.setter(interface)
def sextractor(self, interface):
self.setter(interface)
def psfex(self, interface):
self.setter(interface)
def offset(self, interface):
self.setter(interface)