-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfeanta_server.py
304 lines (270 loc) · 11 KB
/
feanta_server.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
"""
STARBURST ACC/FEANTA Middle Server
Author: Lokbondo Kung
Email: [email protected]
"""
# Change-log:
# 2016-01-07 DG
# Added code to implement the "hybrid" stateframe information for
# NOISEDIODESTATUS, HIFREQSTATUS, and LOFREQSTATUS.
# 2016-01-08 DG
# Added handling of RX-SELECT, which translates to two different
# actions.
# 2016-03-01 JV
# Corrected typo in run() so that "RX-SELECT HI" should now work
# and "RX-SELECT" with incorrect args should not crash program
#
import datetime
import socket
import sys
import time
import threading
import gen_fem_sf
import traceback
# Logging information.
TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S'
LOG_FILE = 'bridge_server.log'
# Define all constants:
# Currently hard-coded, will eventually be read from acc.ini
HOST = ''
HOST_PORT = 5676
ACC_HOSTNAME = 'acc.solar.pvt'
ACC_PORT = 5675
VERSION = 1.2 # Version date: 10/6/2015
# region Class Description
"""
Class: ServerDaemon
Description:
Implementation of a daemon server that is intended to run on
the feanta box in order to process commands from the ACC and
direct and execute the commands to the sub-units connected to
the feanta computer.
Arguments:
pidfile: string designating the .pid file to save the pid for
this daemon process to allow for the process to be stopped
by the stop function or to be stopped manually in Linux.
"""
# endregion
class ServerDaemon():
def __init__(self, pidfile):
self.pidfile_path = pidfile
self.pidfile_timeout = 5
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/null'
self.stderr_path = '/dev/null'
self.workers = {}
self.function_map = {}
self.log_file = LOG_FILE
self.acc_ip = socket.gethostbyname(ACC_HOSTNAME)
# ---------------------------------------------------------------
# BASIC ROUTINES:
# ---------------------------------------------------------------
def __get_timestamp(self):
current_time = time.time()
timestamp = datetime.datetime.fromtimestamp(current_time)
timestamp = timestamp.strftime(TIMESTAMP_FMT)
return timestamp
def __log(self, message):
log_message = self.__get_timestamp() + ': ' + str(message) + '\n'
f = open(self.log_file, "a")
f.write(log_message)
f.close()
print log_message
# ---------------------------------------------------------------
# CORE ROUTINES
# ---------------------------------------------------------------
# region Method Description
"""
Method: link_worker
Description:
This method is used to link a worker extending the i_worker
class to this server so that commands associated with the
i_worker can be executed properly through this server.
Arguments:
worker: the target worker to be linked to this server
"""
# endregion
def link_worker(self, worker):
self.workers[worker.name] = worker
for command in worker.get_command_list():
self.function_map[command] = worker
worker.set_logger(self.__log)
# region Method Description
"""
Method: list_workers
Description:
Lists each worker linked to this ServerDaemon.
"""
# endregion
def list_workers(self):
workers_list = ''
for worker in self.workers.keys():
workers_list += worker + '\n'
return workers_list
# region Method Description
"""
Method: set_log_file
Description:
Sets the destination path for the log file. Defaulted to
LOG_FILE.
"""
# endregion
def set_log_file(self, log_file_destination):
self.log_file = log_file_destination
# region Method Description
"""
Method: list_commands
Description:
Lists every command that this server can respond to.
"""
# endregion
def list_commands(self):
return self.function_map.keys()
def make_stateframe_dict(self):
fem_dict = {}
# Handle powerstrip cluster.
worker = self.workers.get('PDU-Worker', None)
if worker is not None:
try:
fem_dict['POWERSTRIP'] = worker.stateframe_query()
except Exception, e:
fem_dict['POWERSTRIP'] = {}
self.__log(traceback.format_exc())
else:
fem_dict['POWERSTRIP'] = {}
# Handle thermal cluster.
worker = self.workers.get('Cryostat-Worker', None)
working_dict = {}
if worker is not None:
try:
working_dict = worker.stateframe_query()
except Exception, s:
self.__log(traceback.format_exc())
worker = self.workers.get('Temp-Worker', None)
if worker is not None:
try:
working_dict['FOCUSBOX'] = worker.stateframe_query()
except Exception, e:
working_dict['FOCUSBOX'] = 0
self.__log(traceback.format_exc())
else:
working_dict['FOCUSBOX'] = 0
fem_dict['THERMAL'] = working_dict
# Handle receiver cluster.
worker = self.workers.get('BB-Worker', None)
working_dict = {}
if worker is not None:
try:
working_dict = worker.stateframe_query()
except Exception, e:
pass
# Handle servo cluster.
worker = self.workers.get('GeoBrick-Worker', None)
if worker is not None:
try:
fem_dict['SERVO'] = worker.stateframe_query()
except Exception, e:
fem_dict['SERVO'] = {}
self.__log(traceback.format_exc())
else:
fem_dict['SERVO'] = {}
# These items require information from multiple sources,
# which can be determined from the stateframe contents
working_dict['NOISESTATUS'] = fem_dict['POWERSTRIP']['STATUS'][7]
# Both selected Rx and RF switch set for low frequency receiver?
if fem_dict['SERVO']['SELECTEDRX'] == 1 and \
fem_dict['POWERSTRIP']['STATUS'][0] == 0:
working_dict['LOFREQSTATUS'] = 1
else:
working_dict['LOFREQSTATUS'] = 0
# Both selected Rx and RF switch set for high frequency receiver?
if fem_dict['SERVO']['SELECTEDRX'] == 2 and \
fem_dict['POWERSTRIP']['STATUS'][0] == 1:
working_dict['HIFREQSTATUS'] = 1
else:
working_dict['HIFREQSTATUS'] = 0
fem_dict['RECEIVER'] = working_dict
# Handle version.
fem_dict['VERSION'] = VERSION
# Handle timestamp
fem_dict['TIMESTAMP'] = time.time() + 2082844800
return {'FEM': fem_dict}
def send_stateframe_dict(self):
try:
fem_dict = self.make_stateframe_dict()
fmt, buf, xml = gen_fem_sf.gen_fem_sf(fem_dict)
packet_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
packet_socket.settimeout(0.3)
packet_socket.connect((self.acc_ip, ACC_PORT))
packet_socket.sendall(buf)
packet_socket.close()
# persec = open('/tmp/persec.txt', 'a')
# persec.write(buf + '\n')
# persec.close()
finally:
threading.Timer(0.3, self.send_stateframe_dict).start()
# region Method Description
"""
Method: run
Description:
Daemon routine for the ServerDaemon between the ACC and the Brick.
"""
# endregion
def run(self):
# Setup listener to this box at HOST_PORT.
acc_listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__log('Attempt to set up listener...')
try:
acc_listener.bind((HOST, HOST_PORT))
except socket.error, msg:
self.__log('Unable to listen at port ' + str(HOST_PORT) +
'. Error Code: ' + str(msg[0]) + '. Message: ' +
str(msg[1]))
sys.exit()
acc_listener.listen(1)
self.__log('Successfully setup listener')
polling_thread = threading.Thread(target=self.send_stateframe_dict)
polling_thread.start()
while True:
# Wait for a connection from ACC.
connection, address = acc_listener.accept()
self.__log('Connection from ' + address[0] +
':' + str(address[1]))
# Read packet sent from ACC, currently capped at 1024 bytes.
acc_command = connection.recv(1024)
self.__log('Command issued from connection: ' + acc_command)
acc_command = acc_command.split()
# Echo command issued back to ACC.
connection.sendall(acc_command[0])
# Verify that the given command exists and execute it with the
# correct worker if it does.
try:
if acc_command[0] == 'RX-SELECT':
# This is the sole command that has to do two things
# at once--so handle it separately. Here are the
# two translations:
# RX-SELECT LO => OUTLET 1 OFF and FRM-RX-SEL LO
# RX-SELECT HI => OUTLET 1 ON and FRM-RX-SEL HI
band = acc_command[1].upper()
outlet_map = {'LO':'OFF','HI':'ON'}
if band == 'LO' or band == 'HI':
outlet_command = ['OUTLET','1',outlet_map[band]]
brick_command = ['FRM-RX-SEL',band]
self.__log('Attempted to send new command: ' +
outlet_command[0] + '.')
worker = self.function_map[outlet_command[0]]
worker.execute(outlet_command)
time.sleep(0.1) #Try giving a little time before the next command
self.__log('Attempted to send new command: ' +
brick_command[0] + '.')
worker = self.function_map[brick_command[0]]
worker.execute(brick_command)
else:
# If acc_command[1] is not LO or HI, print error message to log
self.__log('Command ' + acc_command[0] + ' not executed - requires argument LO or HI')
else:
worker = self.function_map[acc_command[0]]
worker.execute(acc_command)
except KeyError:
self.__log('Unrecognized command received: ' +
acc_command[0] + '.')