-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy.py
86 lines (69 loc) · 2.58 KB
/
proxy.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
from twisted.web import server, resource
from twisted.internet import reactor
from twisted.python import log
import sys
import txthings.coap as coap
import txthings.resource as res
class MySite(server.Site):
def getResourceFor(self, request):
request.setHeader("Content-Type", "application/ld+json")
request.setHeader("Connection","close")
return server.Site.getResourceFor(self, request)
class HttpServer(resource.Resource):
isLeaf = True
def __init__ (self, arduinoAgent):
self.arduinoAgent = arduinoAgent
def render_GET(self, request):
#send the requests to the arduino
self.arduinoAgent.requestResource(request, self)
#store the request in the scope to finish the response
self.request = request
return server.NOT_DONE_YET
def render_POST(self, request):
self.arduinoAgent.postResource(request, self)
self.request = request
return server.NOT_DONE_YET
def render_PUT(self, request):
self.arduinoAgent.putResource(request, self)
self.request = request
return server.NOT_DONE_YET
#callback function when a CoAP message is received
def _delayedResponse(self, result):
self.request.write(result.payload)
self.request.finish()
class CoapAgent():
def __init__(self, protocol):
self.protocol = protocol
def requestResource(self, httpRequest, httpServer):
request = coap.Message(code=coap.GET)
request.opt.uri_path = (httpRequest.path[1:],)
request.remote = ("10.0.0.2", coap.COAP_PORT)
d = coapProtocol.request(request)
d.addCallback(httpServer._delayedResponse)
d.addErrback(self.noResponse)
def postResource(self, httpRequest, httpServer):
request = coap.Message(code=coap.POST, payload=httpRequest.content.getvalue())
request.opt.uri_path = (httpRequest.path[1:],)
request.remote = ('10.0.0.2', coap.COAP_PORT)
d = coapProtocol.request(request)
d.addCallback(httpServer._delayedResponse)
d.addErrback(self.noResponse)
def putResource(self, httpRequest, httpServer):
request = coap.Message(code=coap.PUT, payload=httpRequest.content.getvalue())
request.opt.uri_path = (httpRequest.path[1:],)
request.remote = ('10.0.0.2', coap.COAP_PORT)
d = coapProtocol.request(request)
d.addCallback(httpServer._delayedResponse)
d.addErrback(self.noResponse)
def noResponse(self, failure):
print 'Failed to fetch resource:'
print failure
reactor.stop()
log.startLogging(sys.stdout)
endpoint = res.Endpoint(None)
coapProtocol = coap.Coap(endpoint)
arduinoAgent = CoapAgent(coapProtocol)
reactor.listenUDP(61616, coapProtocol)
entryPoint = MySite(HttpServer(arduinoAgent))
reactor.listenTCP(8080, entryPoint)
reactor.run()