IoT - Kubus
/
Kubus
JNP3 16/17
server/coapserver.py@68:e88f3fd2e9cf, 2017-01-23 (annotated)
- Committer:
- pannaanna
- Date:
- Mon Jan 23 14:18:23 2017 +0100
- Revision:
- 68:e88f3fd2e9cf
- Parent:
- 16:1569130916f1
port changed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Kopa?ski | 16:1569130916f1 | 1 | from coapthon.server.coap import CoAP |
Jan Kopa?ski | 16:1569130916f1 | 2 | from coapthon.resources.resource import Resource |
Jan Kopa?ski | 16:1569130916f1 | 3 | from threading import Thread |
Jan Kopa?ski | 16:1569130916f1 | 4 | |
Jan Kopa?ski | 16:1569130916f1 | 5 | |
Jan Kopa?ski | 16:1569130916f1 | 6 | class DatabaseResource(Resource): |
Jan Kopa?ski | 16:1569130916f1 | 7 | def __init__(self, table, connector, name='DatabaseResource', coap_server=None): |
Jan Kopa?ski | 16:1569130916f1 | 8 | super(DatabaseResource, self).__init__(name, coap_server, visible=True, observable=True, allow_children=True) |
Jan Kopa?ski | 16:1569130916f1 | 9 | self.table = table |
Jan Kopa?ski | 16:1569130916f1 | 10 | self.connector = connector |
Jan Kopa?ski | 16:1569130916f1 | 11 | self.payload = 'GET is not implemented' |
Jan Kopa?ski | 16:1569130916f1 | 12 | |
Jan Kopa?ski | 16:1569130916f1 | 13 | def render_GET(self, request): |
Jan Kopa?ski | 16:1569130916f1 | 14 | return self |
Jan Kopa?ski | 16:1569130916f1 | 15 | |
Jan Kopa?ski | 16:1569130916f1 | 16 | def render_POST(self, request): |
Jan Kopa?ski | 16:1569130916f1 | 17 | ret = self.connector.write(self.table, request.payload) |
Jan Kopa?ski | 16:1569130916f1 | 18 | if ret: |
Jan Kopa?ski | 16:1569130916f1 | 19 | print 'CoAP server error: ' + ret |
Jan Kopa?ski | 16:1569130916f1 | 20 | return self |
Jan Kopa?ski | 16:1569130916f1 | 21 | |
Jan Kopa?ski | 16:1569130916f1 | 22 | def render_PUT(self, request): |
Jan Kopa?ski | 16:1569130916f1 | 23 | return self |
Jan Kopa?ski | 16:1569130916f1 | 24 | |
Jan Kopa?ski | 16:1569130916f1 | 25 | def render_DELETE(self, request): |
Jan Kopa?ski | 16:1569130916f1 | 26 | return self |
Jan Kopa?ski | 16:1569130916f1 | 27 | |
Jan Kopa?ski | 16:1569130916f1 | 28 | |
Jan Kopa?ski | 16:1569130916f1 | 29 | class CoAPServer(CoAP, Thread): |
Jan Kopa?ski | 16:1569130916f1 | 30 | def __init__(self, host, port, routes, connector): |
Jan Kopa?ski | 16:1569130916f1 | 31 | Thread.__init__(self) |
Jan Kopa?ski | 16:1569130916f1 | 32 | CoAP.__init__(self, (host, port)) |
Jan Kopa?ski | 16:1569130916f1 | 33 | for t in routes: |
Jan Kopa?ski | 16:1569130916f1 | 34 | self.add_resource(t + '/', DatabaseResource(t, connector)) |
Jan Kopa?ski | 16:1569130916f1 | 35 | print 'CoAP server starts on ' + host + ':' + str(port) |
Jan Kopa?ski | 16:1569130916f1 | 36 | print self.root.dump() |
Jan Kopa?ski | 16:1569130916f1 | 37 | |
Jan Kopa?ski | 16:1569130916f1 | 38 | def run(self): |
Jan Kopa?ski | 16:1569130916f1 | 39 | self.listen(10) |