-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathngtelcom.py
More file actions
executable file
·294 lines (225 loc) · 7.08 KB
/
ngtelcom.py
File metadata and controls
executable file
·294 lines (225 loc) · 7.08 KB
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
#!/usr/bin/env python
"""
A server for pcTCS to handle socket requests/commands
This is a replacement for the original telcom written by
Dave Harvey.
Author: Scott Swindell
Date: 12/2013
"""
from server import Server, Client
import json
import sys
import threading
#import wiringpi2
import time
import os
import subprocess
import socket
import sys
import time
while 1:#wait for the telescope to come on line
try:
from telescope import telescope;tel = telescope("10.30.5.69", "BIG61")
time.sleep(1.0)
break
except Exception as err:
print err
time.sleep(30)
from astro.angles import RA_angle, Dec_angle
import math
import re
from scottSock import scottSock
#global guided commands
GUIDERA=0.0
GUIDEDEC=0.0
GUIDETIME=0
class ngClient( Client ):
##################################################
""" Client class to handle socket requests
or commands. Each time a socket is opened on
port 5750 the Server class opens this thread
to handle it.
"""
##################################################
def __init__(self,(client,address)):
Client.__init__(self, (client,address))
print "instantiating Client {0} {1}".format( client, address )
self.laddress = address
self.running = 1
def run(self):
self.running = 1
msg_tot = ""
while self.running:
try:
data = self.client.recv(self.size)
print data
except Exception as e:
print "error is", e, 'at', time.ctime()
print "When trying to read from {0}".format(self.laddress)
print "With Messages: ", msg_tot
data = False
if data:
msg_tot+=data
#if cfgDict['hasActivityLight']:
#ACT_LIGHT.blink(0.05)
if data.endswith( '\n' ): data = data[:-1]#Remove trailing new line
inWords = data.split( ' ' )
#Check for Harvey's telcom string strucutre
if inWords[0] == "BIG61" and inWords[1] == "TCS":
try:
refNum = int( inWords[2] )
except(ValueError):
self.client.send("Missing Reference Number")
self.client.close()
self.running = 0
break
else:
self.client.send("BAD")
self.client.close()
self.running = 0
break
#Check for exceptions caused by bad input or bad parse
try:
req_or_com = inWords[3]
except Exception as prob:
self.client.send( 'error=%s value=%s \n'%( prob, data ) )
self.client.close()
self.running = 0
break
#handle requests
if req_or_com == "REQUEST":
self.request( refNum, inWords[4] )
elif req_or_com == "NGREQUEST":
self.ngRequests[inWords[4]]()
#Handle commands
else:
com = ''
self.command( inWords[3:], refNum )
else:
self.client.close()
self.running = 0
#Legacy Request
def request( self, refNum, reqstr ):
####################################################################
"""
Name: request
args" refNum, reqStr
Description: This method handles all the request made
to telcom eg: BOK TCS 123 REQUEST RA
in the above example 123 is refNum and 'REQUEST RA'
is the request string.
"""
####################################################################
#Different programs use different key words. (I think)
extraKeys={"MOTION":"MOT", "AIRMASS":"SECZ", "ROT":"IIS", }
#get rid of any and all new lines or carriage returns
while (reqstr.endswith("\r") or reqstr.endswith('\n')):
reqstr = reqstr[:-1]
if "ALL" in reqstr:
ALL = {}
ALL.update(tel.reqALL())
ALL.update(tel.reqXALL())
ALL.update({'ut':tel.reqTIME()})
ALL["motion"] = int(ALL["motion"])
ALL["ra"] = ALL["ra"].replace(":","")
ALL["dec"] = ALL["dec"].replace(":","")
ALL["dome"] = tel.reqDOME()["az"]
for key in [ "alt", "az", "secz", "jd", "dome" ]:
ALL[key] = float(ALL[key])
for key, val in ALL.iteritems():
try:
ALL[key] = val.strip()
except (ValueError, AttributeError):
pass
#BIG61 TCS 1 0 212629.68 +321422.8 +00:00:00 21:26:31 90.0 180.0 1.00 2000.0 2457275.731741 1 1
resp="BIG61 TCS {refNum} {motion} {ra} {dec} {ha} {lst} {alt:04.1f} {az:05.1f} {secz:05.2f} {epoch} {jd:09.1f} 180.5 {dome:05.1f} 180.0 \r\n".format(refNum=refNum, **ALL)
#print ALL
#print resp
#self.client.send(resp)
elif "MOTION" in reqstr:
#mot = int( tel.request('motion') )
#pretend the telescope isn't moving --scott 5/21/2017
mot = 0
if mot == 4:
resp = "BIG61 TCS {0} 0\r\n".format(refNum )
else:
resp = "BIG61 TCS {0} {1}\r\n".format(refNum, mot )
elif "GUIDE" in reqstr:
resp = "BIG61 TCS {0} {1} {2} {3}\r\n".format(refNum, GUIDETIME, GUIDERA, GUIDEDEC)
else:
print "shit! request was ", reqstr
resp = ""
self.client.send(resp)
#legacy Command
def command(self, comlist, refNum ):
#########################################################
"""
Name: command
args: comlist, refNum
Description: This method handles all the commands made
to telcom eg: BOK TCS 123 MOVELAZ 90.0 180.0
in the above example 123 is refNum and 'MOVELAZ 90.0 180.0'
is the request string.
"""
#########################################################
for ii in range(len(comlist)):
while comlist[ii].endswith('\r') or comlist[ii].endswith('\n'):
comlist[ii] = comlist[ii][:-1]
print comlist
if comlist[0] == 'RADECGUIDE':
dra, ddec = float(comlist[1]), float(comlist[2])
global GUIDERA, GUIDEDEC, GUIDETIME
GUIDERA, GUIDEDEC=dra, ddec
GUIDETIME=time.time()
try:
tel.comSTEPRA(dra/math.cos(tel.reqDEC()) )
tel.comSTEPDEC(ddec )
#soc_guide(dra, ddec)
except Exception as err:
print err
#log_guide( dra, ddec )
elif comlist[0] == 'NEXTRA':
if len( comlist ) == 3:
rapm = float( comlist[2] )
else:
rapm = 0.0
tel.comNEXTPOS( str2ra(comlist[1]), Dec_Angle(tel.reqXDEC()['next']), tel.request("DISEPOCH").strip(), 0.0, 0.0 )
elif comlist[0] == 'NEXTDEC':
if len( comlist ) == 3:
rapm = float( comlist[2] )
else:
rapm = 0.0
tel.comNEXTPOS( RA_Angle(tel.reqXRA()['next']), str2dec(comlist[1]), tel.request("DISEPOCH").strip(), 0.0, 0.0 )
elif comlist[0] == 'MOVNEXT':
tel.comMOVNEXT()
elif comlist[0] == "SHUTDOWN":
self.running = 0
self.client.close()
else:
print "SHIT"
return self.client.send( "{0} {1} {2} {3}\r\n".format("BIG61", "TCS", refNum, "OK") )
def str2ra(angle_str):
return RA_angle("{0}:{1}:{2}".format(angle_str[0:2], angle_str[2:4], angle_str[4:] ) )
def str2dec( angle_str ):
return RA_angle("{0}:{1}:{2}".format(angle_str[0:3], angle_str[3:5], angle_str[5:] ) )
def log_guide( dra, ddec ):
f=open("guide.dat", 'a')
f.write( "{0} {1} {1}\n".format(time.time(), dra, ddec) )
f.close()
def soc_guide(dra, ddec):
try:
s=scottSock( '10.30.1.1', 5749 )
s.talk( "{0} {1}\n".format(dra, ddec) )
print dra, ddec
s.close()
except Exception as err:
pass
return
if __name__ == "__main__":
print "starting server"
try:
s = Server( 5750, handler=ngClient )
s.run()
except(Exception):
print "Closing server"
s.server.close()