-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpower_server.py
273 lines (242 loc) · 9.39 KB
/
power_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
import time
import traceback
from concurrent.futures import ThreadPoolExecutor
import tornado.ioloop
import tornado.options
import tornado.web
from tornado import gen
from tornado.concurrent import run_on_executor
import globals
import init
from util import logger
power_cache = {}
cache_ttl = 10
class api_v1_power(tornado.web.RequestHandler):
if len(globals.COM_SHARD) <= 0:
executor = ThreadPoolExecutor(1)
else:
executor = ThreadPoolExecutor(len(globals.COM_SHARD) * 2)
@run_on_executor
def read_power_by_IP(self, ip):
if ip in globals.METERS_IP_MAP:
SN = globals.METERS_IP_MAP[ip][0]
SER = globals.METERS_IP_MAP[ip][1]
return globals.COM_SHARD[SER].read_power(SN)
else:
logger.error("IP %s record not found" % ip)
@gen.coroutine
def get(self):
start_point = time.time()
if "ip" in self.request.arguments:
ipaddr = self.get_argument("ip")
else:
ipaddr = self.request.remote_ip
e = None
if ipaddr not in globals.METERS_IP_MAP:
elapsed = round(time.time() - start_point, 3)
self.write({'time': time.time(),
"version": "0.0",
"power": -1,
"error": True,
"msg": "IP Not Found",
"ipaddr": ipaddr,
"elapsed": elapsed,
})
else:
try:
power = yield self.read_power_by_IP(ipaddr)
elapsed = round(time.time() - start_point, 3)
if power is None:
self.write({'time': time.time(),
"version": "0.0",
"power": -1,
"error": True,
"msg": "broken data",
"ipaddr": ipaddr,
"elapsed": elapsed,
})
else:
self.write({
'time': time.time(),
"version": "0.0",
"power": power,
"error": False,
"msg": "success",
"ipaddr": ipaddr,
"elapsed": elapsed,
})
power_cache[ipaddr] = [power, time.time()]
logger.debug("Request %s , cost %sms." % (ipaddr, round(elapsed * 1000, 3)))
except Exception as excepts:
traceback.print_exc()
e = excepts
pass
if e:
elapsed = round(time.time() - start_point, 3)
logger.exception("Meters read error:%s" % traceback.format_exc())
self.write({'time': time.time(),
"version": "0.0",
"power": -1,
"error": True,
"msg": e,
"ipaddr": ipaddr,
"elapsed": elapsed,
})
self.finish()
class api_v1_power_cached(tornado.web.RequestHandler):
if len(globals.COM_SHARD) <= 0:
executor = ThreadPoolExecutor(1)
else:
executor = ThreadPoolExecutor(len(globals.COM_SHARD) * 2)
@run_on_executor
def read_power_by_IP(self, ip):
if ip in globals.METERS_IP_MAP:
SN = globals.METERS_IP_MAP[ip][0]
SER = globals.METERS_IP_MAP[ip][1]
return globals.COM_SHARD[SER].read_power(SN)
else:
logger.error("IP %s record not found" % ip)
@gen.coroutine
def get(self):
start_point = time.time()
if "ip" in self.request.arguments:
ipaddr = self.get_argument("ip")
else:
ipaddr = self.request.remote_ip
e = None
if ipaddr not in globals.METERS_IP_MAP:
elapsed = round(time.time() - start_point, 3)
self.write({'time': time.time(),
"version": "0.0",
"power": -1,
"error": True,
"msg": "IP Not Found",
"ipaddr": ipaddr,
"elapsed": elapsed,
})
else:
try:
if ipaddr in power_cache:
if time.time() < power_cache[ipaddr][1] + cache_ttl:
power = power_cache[ipaddr][0]
else:
power = yield self.read_power_by_IP(ipaddr)
else:
power = yield self.read_power_by_IP(ipaddr)
elapsed = round(time.time() - start_point, 3)
if power is None:
self.write({'time': time.time(),
"version": "0.0",
"power": -1,
"error": True,
"msg": "broken data",
"ipaddr": ipaddr,
"elapsed": elapsed,
})
else:
self.write({
'time': time.time(),
"version": "0.0",
"power": power,
"error": False,
"msg": "success",
"ipaddr": ipaddr,
"elapsed": elapsed,
})
power_cache[ipaddr] = [power, time.time()]
logger.debug("Request %s , cost %sms." % (ipaddr, round(elapsed * 1000, 3)))
except Exception as excepts:
traceback.print_exc()
e = excepts
pass
if e:
elapsed = round(time.time() - start_point, 3)
logger.exception("Meters read error:%s" % traceback.format_exc())
self.write({'time': time.time(),
"version": "0.0",
"power": -1,
"error": True,
"msg": e,
"ipaddr": ipaddr,
"elapsed": elapsed,
})
self.finish()
class api_v1_environment(tornado.web.RequestHandler):
if len(globals.COM_SHARD) <= 0:
executor = ThreadPoolExecutor(1)
else:
executor = ThreadPoolExecutor(len(globals.COM_SHARD) * 2)
@run_on_executor
def read_temp_by_name(self, name):
addr = globals.TEMP_MAP[name][0]
SER = globals.TEMP_MAP[name][1]
return globals.COM_SHARD[SER].read_env(addr)
@gen.coroutine
def get(self):
start_point = time.time()
if "name" in self.request.arguments:
name = self.get_argument("name")
else:
self.write({'time': time.time(),
"version": "0.0",
"temperature": "NULL",
"humidity": "NULL",
"error": True,
"msg": "too few arguments",
"elapsed": 0.00,
})
self.finish()
return
if name not in globals.TEMP_MAP:
elapsed = round(time.time() - start_point, 3)
self.write({'time': time.time(),
"version": "0.0",
"temperature": "NULL",
"humidity": "NULL",
"error": True,
"msg": "Temper Not Found",
"elapsed": elapsed,
})
else:
try:
temp,humi = yield self.read_temp_by_name(name)
elapsed = round(time.time() - start_point, 3)
if temp == humi is None:
self.write({'time': time.time(),
"version": "0.0",
"temperature": "NULL",
"humidity": "NULL",
"error": True,
"msg": "Temper Read Fail!",
"elapsed": elapsed,
})
else:
self.write({'time': time.time(),
"version": "0.0",
"temperature": temp,
"humidity": humi,
"error": False,
"msg": "",
"elapsed": elapsed,
})
logger.debug("Request temper %s , cost %sms." % (name, round(elapsed * 1000, 3)))
except Exception as excepts:
traceback.print_exc()
pass
self.finish()
class ra9(tornado.web.RequestHandler):
def get(self):
self.write("I am alive")
self.finish()
def make_app():
return tornado.web.Application([
(r"/api/v1/power", api_v1_power),
(r"/api/v1/power_cached", api_v1_power_cached),
(r"/api/v1/environment", api_v1_environment),
(r"/ra9", ra9),
], autoreload=False)
def run():
app = make_app()
logger.info("Server listening on %s:%s" % (init.LISTEN_IP, init.LISTEN_PORT,))
app.listen(init.LISTEN_PORT, address=init.LISTEN_IP)
tornado.ioloop.IOLoop.current().start()