Added auto refresh
This commit is contained in:
+26
-8
@@ -2,9 +2,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Simple REST Server
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import datetime
|
||||
import werkzeug
|
||||
import tornado.httpserver
|
||||
import tornado.ioloop
|
||||
import tornado.options
|
||||
@@ -12,18 +14,28 @@ import tornado.web
|
||||
|
||||
from tornado.options import define, options
|
||||
define("port", default=8000, help="run on the given port", type=int)
|
||||
define("lines", default=20, help="number of lines displayed", type=int)
|
||||
|
||||
database = []
|
||||
|
||||
class IndexHandler(tornado.web.RequestHandler):
|
||||
def get(self):
|
||||
global database
|
||||
res = ""
|
||||
items = []
|
||||
for data in database:
|
||||
print("{}".format(data))
|
||||
#print("{}".format(len(data[2])))
|
||||
res = res + "{} {:x} {} {}".format(data[0],data[1],data[2],data[3]) + "<br>"
|
||||
self.write(res)
|
||||
#print("{}".format(data))
|
||||
datetime = data[0].split()
|
||||
#print("data={} datetime={}".format(data[0], datetime))
|
||||
|
||||
items.append({
|
||||
"date": datetime[0],
|
||||
"time": datetime[1],
|
||||
"id": data[1],
|
||||
"frame": data[2],
|
||||
"value": data[3]
|
||||
})
|
||||
#print("items={}".format(items))
|
||||
self.render("index.html", lines=options.lines, items=items)
|
||||
|
||||
#curl -X POST -H "Content-Type: application/json" -d '{"canid":101, "frame":"standard" , "data": [1,2,3,4,5,6,7,8]}' http://192.168.10.43:8000/post
|
||||
class PostHandler(tornado.web.RequestHandler):
|
||||
@@ -51,7 +63,9 @@ class PostHandler(tornado.web.RequestHandler):
|
||||
|
||||
global database
|
||||
#print("{} {} {}".format(type(database), len(database), database))
|
||||
if(len(database) >= 20):
|
||||
print("lines={}".format(options.lines))
|
||||
#if(len(database) >= 20):
|
||||
if(len(database) >= options.lines):
|
||||
database.pop(0)
|
||||
database.append(items)
|
||||
|
||||
@@ -59,11 +73,15 @@ class PostHandler(tornado.web.RequestHandler):
|
||||
|
||||
if __name__ == "__main__":
|
||||
tornado.options.parse_command_line()
|
||||
print("port={}".format(options.port))
|
||||
print("lines={}".format(options.lines))
|
||||
app = tornado.web.Application(
|
||||
handlers=[
|
||||
(r"/", IndexHandler),
|
||||
(r"/post", PostHandler),
|
||||
],debug=True
|
||||
],
|
||||
template_path=os.path.join(os.getcwd(), "templates"),
|
||||
debug=True
|
||||
)
|
||||
http_server = tornado.httpserver.HTTPServer(app)
|
||||
http_server.listen(options.port)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<!DOCTYPE html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<meta http-equiv="refresh" content="5;url="/">
|
||||
<title>CAN DATA</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
Display the latest {{ lines }} records.<br>
|
||||
If you want to see more, specify the number of lines at startup.<br>
|
||||
|
||||
<table border="1" cellpadding="2" cellspacing="2">
|
||||
<tbody>
|
||||
<tr style="color:#ffffff;" bgcolor="#800000">
|
||||
<th valign="top">Date</th>
|
||||
<th valign="top">Time</th>
|
||||
<th valign="top">ID</th>
|
||||
<th valign="top">Frame</th>
|
||||
<th valign="top">Value</th>
|
||||
</tr>
|
||||
|
||||
{% for i in items %}
|
||||
<tr>
|
||||
<td>{{ i['date'] }}</td>
|
||||
<td>{{ i['time'] }}</td>
|
||||
<td>{{ i['id'] }}</td>
|
||||
<td>{{ i['frame'] }}</td>
|
||||
<td>{{ i['value'] }}</td>
|
||||
</tr>
|
||||
{% end %}
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user