Added auto refresh
This commit is contained in:
+28
-9
@@ -2,10 +2,12 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Simple REST Server
|
# Simple REST Server
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
from flask import Flask, request
|
import argparse
|
||||||
|
from flask import Flask, request, render_template
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
database = []
|
database = []
|
||||||
@@ -13,13 +15,21 @@ database = []
|
|||||||
@app.route("/")
|
@app.route("/")
|
||||||
def root():
|
def root():
|
||||||
global database
|
global database
|
||||||
res = ""
|
items = []
|
||||||
for data in database:
|
for data in database:
|
||||||
print("{}".format(data))
|
#print("{}".format(data))
|
||||||
#print("{}".format(len(data[2])))
|
datetime = data[0].split()
|
||||||
res = res + "{} {:x} {} {}".format(data[0],data[1],data[2],data[3]) + "<br>"
|
#print("data={} datetime={}".format(data[0], datetime))
|
||||||
#return "Hello World!"
|
|
||||||
return res
|
items.append({
|
||||||
|
"date": datetime[0],
|
||||||
|
"time": datetime[1],
|
||||||
|
"id": data[1],
|
||||||
|
"frame": data[2],
|
||||||
|
"value": data[3]
|
||||||
|
})
|
||||||
|
#print("items={}".format(items))
|
||||||
|
return render_template("index.html", lines=app.config['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
|
#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
|
||||||
@app.route("/post", methods=["POST"])
|
@app.route("/post", methods=["POST"])
|
||||||
@@ -48,7 +58,9 @@ def post():
|
|||||||
|
|
||||||
global database
|
global database
|
||||||
#print("{} {} {}".format(type(database), len(database), database))
|
#print("{} {} {}".format(type(database), len(database), database))
|
||||||
if(len(database) >= 20):
|
print("lines={}".format(app.config['lines']))
|
||||||
|
#if(len(database) >= 20):
|
||||||
|
if(len(database) >= app.config['lines']):
|
||||||
database.pop(0)
|
database.pop(0)
|
||||||
database.append(items)
|
database.append(items)
|
||||||
|
|
||||||
@@ -56,6 +68,13 @@ def post():
|
|||||||
return data
|
return data
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('-port', type=int, default=8000)
|
||||||
|
parser.add_argument('-lines', type=int, default=20)
|
||||||
|
args = parser.parse_args()
|
||||||
|
print("port={}".format(args.port))
|
||||||
|
print("lines={}".format(args.lines))
|
||||||
|
app.config['lines'] = args.lines
|
||||||
#app.run()
|
#app.run()
|
||||||
app.run(host='0.0.0.0', port=8000, debug=True)
|
app.run(host='0.0.0.0', port=args.port, debug=True)
|
||||||
|
|
||||||
|
|||||||
@@ -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>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
+26
-8
@@ -2,9 +2,11 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Simple REST Server
|
# Simple REST Server
|
||||||
|
|
||||||
import json
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
|
import werkzeug
|
||||||
import tornado.httpserver
|
import tornado.httpserver
|
||||||
import tornado.ioloop
|
import tornado.ioloop
|
||||||
import tornado.options
|
import tornado.options
|
||||||
@@ -12,18 +14,28 @@ import tornado.web
|
|||||||
|
|
||||||
from tornado.options import define, options
|
from tornado.options import define, options
|
||||||
define("port", default=8000, help="run on the given port", type=int)
|
define("port", default=8000, help="run on the given port", type=int)
|
||||||
|
define("lines", default=20, help="number of lines displayed", type=int)
|
||||||
|
|
||||||
database = []
|
database = []
|
||||||
|
|
||||||
class IndexHandler(tornado.web.RequestHandler):
|
class IndexHandler(tornado.web.RequestHandler):
|
||||||
def get(self):
|
def get(self):
|
||||||
global database
|
global database
|
||||||
res = ""
|
items = []
|
||||||
for data in database:
|
for data in database:
|
||||||
print("{}".format(data))
|
#print("{}".format(data))
|
||||||
#print("{}".format(len(data[2])))
|
datetime = data[0].split()
|
||||||
res = res + "{} {:x} {} {}".format(data[0],data[1],data[2],data[3]) + "<br>"
|
#print("data={} datetime={}".format(data[0], datetime))
|
||||||
self.write(res)
|
|
||||||
|
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
|
#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):
|
class PostHandler(tornado.web.RequestHandler):
|
||||||
@@ -51,7 +63,9 @@ class PostHandler(tornado.web.RequestHandler):
|
|||||||
|
|
||||||
global database
|
global database
|
||||||
#print("{} {} {}".format(type(database), len(database), 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.pop(0)
|
||||||
database.append(items)
|
database.append(items)
|
||||||
|
|
||||||
@@ -59,11 +73,15 @@ class PostHandler(tornado.web.RequestHandler):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
tornado.options.parse_command_line()
|
tornado.options.parse_command_line()
|
||||||
|
print("port={}".format(options.port))
|
||||||
|
print("lines={}".format(options.lines))
|
||||||
app = tornado.web.Application(
|
app = tornado.web.Application(
|
||||||
handlers=[
|
handlers=[
|
||||||
(r"/", IndexHandler),
|
(r"/", IndexHandler),
|
||||||
(r"/post", PostHandler),
|
(r"/post", PostHandler),
|
||||||
],debug=True
|
],
|
||||||
|
template_path=os.path.join(os.getcwd(), "templates"),
|
||||||
|
debug=True
|
||||||
)
|
)
|
||||||
http_server = tornado.httpserver.HTTPServer(app)
|
http_server = tornado.httpserver.HTTPServer(app)
|
||||||
http_server.listen(options.port)
|
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