/
rfid_webapp.py
85 lines (71 loc) · 2.26 KB
/
rfid_webapp.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
import cherrypy
import os
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from ws4py.websocket import WebSocket
from ws4py.messaging import TextMessage
from cherrypy.process import plugins
from rfid_handling import RFIDHandling
WebSocketPlugin(cherrypy.engine).subscribe()
cherrypy.tools.websocket = WebSocketTool()
rfidHandler = RFIDHandling()
def doRFID():
cherrypy.engine.publish("websocket-broadcast", TextMessage(rfidHandler.blockingRead()))
class WebSocketHandler(WebSocket):
def opened(self):
print "Socket Opened------"
def closed(self, code, reason="unknown"):
print "Socket Closed------2"
class rfid(object):
@cherrypy.expose
def index(self):
return open('web/index.html')
@cherrypy.expose
def ws(self):
# you can access the class instance through
handler = cherrypy.request.ws_handler
class rfidGeneratorWebService(object):
exposed = True
@cherrypy.tools.accept(media='text/plain')
def GET(self):
return cherrypy.session['session_id']
if __name__ == '__main__':
rfidapp = rfid()
rfidapp.generator = rfidGeneratorWebService()
file_path = os.getcwd() + '/web'
# This is the configuration and starting of the service
cherrypy.config.update({'server.socket_host' : "0.0.0.0",
'server.socket_port' : 9090})
plugins.BackgroundTask(0.1, doRFID).start()
cherrypy.quickstart(rfid(),'/',
{
'/':
{
'tools.staticdir.root' : file_path,
},
'/logaccess.csv':
{
'tools.staticfile.on' : True,
'tools.staticfile.filename' : file_path + '/logaccess.csv'
},
'/users.txt':
{
'tools.staticfile.on' : True,
'tools.staticfile.filename' : file_path + '/users.txt'
},
'/js':
{
'tools.staticdir.on' : True,
'tools.staticdir.dir' : file_path + '/js'
},
'/static':
{
'tools.staticdir.on' : True,
'tools.staticdir.dir' : file_path + '/static'
},
'/ws':
{
'tools.websocket.on' : True,
'tools.websocket.handler_cls' : WebSocketHandler
}
}
)