-
Notifications
You must be signed in to change notification settings - Fork 0
/
telcom.bak.bak
executable file
·531 lines (409 loc) · 13.7 KB
/
telcom.bak.bak
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#!/usr/bin/env python
"""
A server for pcTCS to handle socket requests/commands
This is a replacement for a 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 serial
import wiringpi2
import time
import os
import subprocess
from blink import blinker
import sys
#Get config data and initialize a few things
cfgName = sys.argv[1]
cfgDict = json.load( open(cfgName, 'r') )
threadLock = threading.Lock()
if cfgDict['hasIIS']:
wiringpi2.wiringPiSetup()
wiringpi2.pinMode( 1,2 )
if cfgDict['hasActivityLight']:
ACT_LIGHT = blinker(26)
ACT_LIGHT.setState(1)
global outval
outval = []
class legacyClient( 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))
if cfgDict["debug"]: print "instantiating Client {0} {1}".format( client, address )
def run(self):
running = 1
while running:
data = self.client.recv(self.size)
if 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] == cfgDict['OBSID'] and inWords[1] == cfgDict['SYSID']:
try:
refNum = int( inWords[2] )
except(ValueError):
self.client.send("Missing Reference Number")
self.client.close()
running = 0
break
else:
self.client.send("BAD")
self.client.close()
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()
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()
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 reqstr in extraKeys.iterkeys():
reqstr = extraKeys[reqstr]
#Wait for ALL keyword to be populated.
while reqstr == "ALL" and "ALL" not in outval:
#print "waiting"
time.sleep(0.1)
#Build formatted
if reqstr in outval:
outstring = "{0} {1} {2} {3}\n" .format( cfgDict['OBSID'], cfgDict['SYSID'], refNum, outval[reqstr] )
return self.client.send( outstring )
else:
return self.client.send( "Unknown Request: %s \n" %reqstr )
#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.
"""
#########################################################
#construct command string
comstr = ''
for word in comlist:
comstr+=word+' '
#Get rid of whitespace caused by above parsing
comstr = comstr[:-1]
#get rid of any newline or carriage returns
while ( comstr.endswith( "\r" ) or comstr.endswith( '\n' ) ):
comstr = comstr[:-1]
#Write the command to the serial port
try:
ser = serial.Serial( gUSBport, 9600 )
ser.write( comstr+"\r" )
except Exception as e:
if cfgDict['debug']: print e
return self.client.send( "{0} {1} {2} {3}\r\n".format(cfgDict['OBSID'], cfgDict['SYSID'], refNum, "SERIAL_ERROR") )
#Wait half a second and grab the error code from
#the telem stream
time.sleep( 0.5 )
errorCode = outval["ERROR"].upper().strip()
if errorCode != "E": #Command was not understood
returnVal = "BAD"
else:#Command was understood
returnVal = "OK"
return self.client.send( "{0} {1} {2} {3}\r\n".format(cfgDict['OBSID'], cfgDict['SYSID'], refNum, returnVal) )
def ngAll(self):
outstr = ''
for val in outval.itervalues():
outstr+=val+' '
return self.client.send( "{0}\n".format(outstr) )
def findUSBPort(self):
#####################################
"""
Name: findUSBPort
args: None
Returns: device file name for serial-USB converter
Description: Determines which device USB device file
exists (if any). Initially I used lsusb to determine
which if the USB port in question was actually the
serial to USB adapter. But this was slow. If anything else
is plugged into the USB port there could be trouble.
"""
#####################################
#usbs = subprocess.check_output("lsusb", shell=True)
#usbs=usbs.split('\n')
#serialUSBFullPath = False
#for usb in usbs:
#if "USB-Serial" in usb:
#serialUSB = usb.split(' ')
#serialUSBFullPath = "/dev/bus/usb/001/%s" %serialUSB[3][:-1]
if os.path.exists('/dev/ttyUSB0'):
return '/dev/ttyUSB0'
elif os.path.exists('/dev/ttyUSB1'):
return '/dev/ttyUSB1'
else:
print "Could Find USB"
return False
#Class to talk to TCS machine via the
#Serial port
class get_serial ( threading.Thread ):
#####################################################
"""
Class Name: get_serial
Inherits: Threading
Description: Opens a thread to read the pcTCS telem
stream. The stream is parsed into a globalized dictionary
to be read by the client class. If the telem stream
cannot be read for any reason the dictionary
is populated with "SERIAL_ERROR" instead.
"""
###############################################
def __init__ ( self, baude_rate ):
threading.Thread.__init__( self )
self.USBport = self.findUSBPort()
self.baude_rate = baude_rate
print self.USBport
if cfgDict['debug']: print "instantiating serial class"
#convert unicode to ascii
self.data_key = [str(val) for val in cfgDict['keywords']]
print self.data_key
def run ( self ):
running = 1
if cfgDict['debug']: print "running serial loop"
rawStream=''
reading = False
valError = ["SERIAL_ERROR"]*16
global outval #Globalize so client thread can see it.
global gUSBport #Globalize this So client command method doesn't have to find it everytime.
gUSBport=False
outval = dict( zip( self.data_key, valError ) )
serialError = True
lastIIS = 0.0
iisPos = 0.0
iisCount=0
iisMaxCount=10
#Initial check for serial/USB connection
while serialError:
if self.USBport:
gUSBport = self.USBport
self.com = serial.Serial( self.USBport, self.baude_rate, timeout=1.0 )
serialError = False
else:
print( "Could Not Find serial port:%s"%self.USBport )
gUSBport = False
self.USBport = self.findUSBPort()
time.sleep( 0.5 )
i=0
while running:
serialError = True
if cfgDict['debug']: "restarting serial loop"
#Check USB port evertime a telem string char
#is read in. You never know when someone will
#unplug that USB or serial cable
while serialError:
try:
#if DEBUG: print "reading serial port"
gUSBport = self.USBport
inchar = self.com.read()
#if DEBUG: print inchar
if inchar == "":
raise( serial.SerialException )
else:
serialError = False
except( serial.SerialException ):
if cfgDict['debug']: print "serial exception"
threadLock.acquire()
outval = dict( zip( self.data_key,['SERIAL_ERROR']*17 ) )
threadLock.release()
self.USBport = self.findUSBPort()
if self.USBport:
self.com = serial.Serial( self.USBport, self.baude_rate, timeout=1.0 )
serialError = False
if inchar == '\r':#the full telem is read in
if reading:#Make sure we were actually reading
_ALL = rawStream[1:]
reading = False
errorCode = rawStream[cfgDict["errorStart"]:cfgDict["errorEnd"]] #Grab error code from telem stream
if cfgDict['debug']: print "errorCode is {0}".format(errorCode)
rawStream = rawStream[:63] + rawStream[75:] #remove error code
rawStream = rawStream.replace('\n','')
stream = rawStream.split(' ') #turn rawStream into list for zipping into dictionary
stream = [ii for ii in stream if ii != ''] #get rid of empty items in val list
if len( stream ) == 15:#If no command has been given there will be no error
stream.insert(8, '')# add index for error code
if cfgDict['debug']: print "length of stream is {0}".format(str(stream))
#raw stream list is now fully parsed and in list `stream'
if 1:
threadLock.acquire()
outval = dict( zip( self.data_key, stream ) )#zip stream key value pairs into dictionary for easy processing
outval["IIS"] = lastIIS
##############################
#some extra parsing
outval['ERROR'] = errorCode
#The outval["ALL"] contains the entire unparsed telem stream
#It is used by Azcam to populate fits header data rapidly
# Try to get the IIS position
# from Jeff Rill's rabbit board
# Sometimes for unkown reasons
# the information isn't available
# and the socket times out if this
# happens simply use the last iisPos
# recieved from the rabbit board
if cfgDict['hasIIS']:
try:
iisPos = float( get_iis() )
outval['IIS'] = iisPos
lastIIS = iisPos
put_iis( iisPos ) #Use bnc port to display iis on tcs screen
except ValueError:
outval["IIS"] = lastIIS
iisPos = lastIIS
outval["ALL"] = _ALL[:-20]+str(iisPos) + _ALL[-15:]
else:
outval["ALL"] = _ALL[:-20]+str(0.0) + _ALL[-15:]
threadLock.release()
if cfgDict['debug']: print "formatted serial string is {0}".format(outval)
else:
threadLock.acquire()
outval = dict( zip( self.data_key,['BAD_STREAM']*16 ) )
threadLock.release()
rawStream = ""
else: reading = True
else:
if reading:
rawStream+=inchar
def findUSBPort(self):
#####################################
"""
Name: findUSBPort
args: None
Returns: device file name for serial-USB converter
Description: Determines which device USB device file
exists (if any). Initially I used lsusb to determine
which if the USB port in question was actually the
serial to USB adapter. But this was slow. If anything else
is plugged into the USB port there could be trouble.
"""
#####################################
#usbs = subprocess.check_output("lsusb", shell=True)
#usbs=usbs.split('\n')
#serialUSBFullPath = False
#for usb in usbs:
#if "USB-Serial" in usb:
#serialUSB = usb.split(' ')
#serialUSBFullPath = "/dev/bus/usb/001/%s" %serialUSB[3][:-1]
#if serialUSBFullPath:
if os.path.exists( '/dev/ttyUSB0' ):
return '/dev/ttyUSB0'
elif os.path.exists( '/dev/ttyUSB1' ):
return '/dev/ttyUSB1'
else:
return False
print serialUSBFullPath
return serialUSBFullPath
class request_data( threading.Thread ):
def __init__( self ):
threading.Thread.__init__ ( self )
def run ( self ):
while 1:
print outval
time.sleep(5)
def get_iis( hostname="10.30.3.40" ):
########################################
"""
Name: get_iis
args: hostname
Description: Gets the iis position
from networed Jeff Rill's networked
encoder board.
"""
########################################
HOST = socket.gethostbyname( hostname )
PORT= 5750
s=socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.settimeout( 0.1 )
try:
s.connect( ( HOST, PORT ) )
s.send("BOK IIS 123 REQUEST %s\n" %("IIS") )
recvstr = s.recv(100)
s.close()
return recvstr[12:]
except socket.error, msg:
#print "get_iis err", msg
s.close()
return False
def put_iis( pos ):
############################################
"""
Name: put_iis
Args: pos
Descriptiom: Takes a pos in degrees
and outputs a value to the PWM on the board
The PWM is sent to an analogue filter
which converts it to a base voltage to
be interpreted by the ADC (channel 7)
on the TCS machine. To be interpreted
correctly the ADC channel has to be given
the correct slope and intercept in pfedit as it
stands now those values are:
slope => 0.318000
intercept => -652.200
(12/2013)
"""
##########################################
v=float( pos )/1.0814
v=v/100.0
wiringpi2.pwmWrite( 1, int( 1024*v/3.3 ) )
if __name__ == "__main__":
#try:
ser = get_serial( 9600 )
ser.start()
if cfgDict['debug']: print "starting server"
s = Server( 5750, handler=legacyClient )
s.run()
#except Exception as E:
#s=open("/home/pi/telcom.err", 'a')
#s.write("++++++++++++++++++++++++++++++++++++++++++++\n")
#s.write(time.asctime() + "\n")
#s.write( str( E ) )
#s.close()
#ACT_LIGHT.setState( 0 )
#print E
#raise Exception(E)