-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
183 lines (147 loc) · 5.88 KB
/
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
import astropy.units as u
import socketio
import sqlalchemy as db
import yaml
from aiohttp import web
from astropy.time import Time
from datetime import datetime
from shutil import copyfile
sio = socketio.AsyncServer(async_mode='aiohttp', async_handlers=True,
cors_allowed_origins='*')
app = web.Application()
sio.attach(app)
class db_connection():
def __init__(self):
self.config = yaml.safe_load(open('solar_config.yml', 'r'))
self.nohardware = self.config['no-hardware']
if not self.nohardware:
self.engine = db.create_engine(config['mysql_engine'])
self.metadata = db.MetaData(bind=self.engine)
self.environmentTable = db.Table('environment', self.metadata, autoload=True
)
else:
print("Running Server in no hardware mode")
def get24HEnvironment(self):
if not self.nohardware:
connection = self.engine.connect()
t = (Time(datetime.utcnow()) - 24*u.h).datetime
res = list(connection.execute(self.environmentTable.select().where(self.environmentTable.c.DATEOBS > t)))
connection.close()
time_array = [Time(x[1]).isot for x in res]
t0 = [x[2] for x in res]
t1 = [x[3] for x in res]
t2 = [x[4] for x in res]
h0 = [x[4] for x in res]
return {'time':time_array, 't0':t0, 't1':t1, 't2':t2, 'h0':h0}
else:
return {'time':[], 't0':[], 't1':[], 't2':[], 'h0':[]}
def getLastEntry(self):
if not self.nohardware:
connection = self.engine.connect()
qr = self.environmentTable.select().order_by(self.environmentTable.c.ENVIRONMENTID.desc()).limit(1)
res = list(connection.execute(qr))
connection.close()
time_array = [Time(x[1]).isot for x in res]
t0 = [x[2] for x in res]
t1 = [x[3] for x in res]
t2 = [x[4] for x in res]
h0 = [x[4] for x in res]
return {'time':time_array, 't0':t0[0], 't1':t1[0], 't2':t2[0], 'h0':h0[0]}
else:
return {'time':[], 't0':[], 't1':[], 't2':[], 'h0':[]}
async def index(request):
return web.Response(text='hello', content_type='text/html')
@sio.on('newWebClient')
async def newWebClient(sid, message):
print('New Web Client')
await sio.emit('newWebClient', 'hello')
await sio.emit('environmentData', db_conn.get24HEnvironment())
await sio.emit('updatePlan', {'sun_up': x.sun_up, 'utdate':x.utdate, 'meridian_flip':x.meridian_flip, 'sun_down':x.sun_down})
# await sio.emit('environmentManagerToClient', x.environmentManager)
@sio.on('updatedEnvironment')
async def newWebClient(sid, message):
print("Environment updated - sending new values")
await sio.emit('updateEnvironment', db_conn.getLastEntry())
@sio.on('update')
async def update_variable(sid, data):
for key in data:
x.vars[key] = data
print('Updating {0:s} with value {1:s}'.format(key, data[key]))
await sio.emit('update', data)
@sio.on('webcam')
async def webcamImageReceived(sid, data):
print('new webcam image')
fh = data.split("\\")[-1]
tobs = Time(fh.split('.jpg')[0].replace('_',':'))
copyfile('/Volumes/solar/webcam/{0:s}'.format(fh), './webApp/static/assets/img/webcam_latest.jpg')
await sio.emit('updateWebcam', tobs.iso[0:19])
@sio.on('planToServer')
async def planToServer(sid, data):
print("Plan for new day received")
x.utdate = data['utdate']
x.sun_up = data['sun_up']
x.meridian_flip = data['meridian_flip']
x.sun_down = data['sun_down']
await sio.emit('updatePlan', data)
@sio.on('telescopeStatusToServer')
async def telescopeStatusToServer(sid, message):
print("Updating status of telescope")
x.telescopeStatus = message
await sio.emit('telescopeStatusToClients', message)
@sio.on('environmentManagerToServer')
async def environmentStatusToServer(sid, message):
x.environmentManager = message
await sio.emit('environmentManagerToClient', message)
@sio.on('sunIntensity')
async def update_sun_intensity(sid, data):
print('updating sun intensity with value {0:f}'.format(data))
await sio.emit('sunIntensity', data)
@sio.on('updateEnv')
async def updateEnv(sid, data):
print('updating environment plot')
await sio.emit('updateEnv', data)
@sio.on('guiderUpdate')
async def update_sun_intensity(sid, data):
print('updating guider table')
await sio.emit('guiderUpdate', data)
@sio.on('telescopeStatus')
async def telescopeStatus(sid, data):
print('updating Telescope Status')
await sio.emit('telescopeStatus', data)
@sio.on('clearTables')
async def clearTables(sid, data):
print("Clearing data tables in GUI")
await sio.emit('clearTables', 'hello')
@sio.on("disconnect")
def on_disconnect(sid):
print("Client disconnected")
@sio.on("reconnect")
async def on_reconnect(sid):
print("Client Reconnected")
await sio.emit('newWebClient', 'hello')
@sio.on('guiderStatusToServer')
async def guiderStatusToServer(sid, data):
print("New guider status data received")
print(data['sun_intensity'])
await sio.emit('guiderStatusToClient', data)
@sio.on('NewCalciumImageToServer')
async def NewCalciumImageToServer(sid, data):
print("New Calcium image received")
copyfile(data, './webApp/static/assets/img/calcium_latest.jpg')
await sio.emit('updateCalciumImage', 'hello')
class variables():
def __init__(self):
self.vars = {'sunRA':'',
'sunDec':'',
'sunAlt':'',
'sunAz':'',
'initialize':'',
'observe':'',
'lostT':'',
'lostRH':'',
'environmentManager':'disconnected',
'weather':'cloudy'}
x = variables()
db_conn = db_connection()
app.router.add_get('/', index)
web.run_app(app, port=8081)