JNP3 16/17

Dependencies:   mbed nRF24L01P

server/coapserver.py

Committer:
pannaanna
Date:
2017-01-23
Revision:
68:e88f3fd2e9cf
Parent:
16:1569130916f1

File content as of revision 68:e88f3fd2e9cf:

from coapthon.server.coap import CoAP
from coapthon.resources.resource import Resource
from threading import Thread


class DatabaseResource(Resource):
    def __init__(self, table, connector, name='DatabaseResource', coap_server=None):
        super(DatabaseResource, self).__init__(name, coap_server, visible=True, observable=True, allow_children=True)
        self.table = table
        self.connector = connector
        self.payload = 'GET is not implemented'

    def render_GET(self, request):
        return self

    def render_POST(self, request):
        ret = self.connector.write(self.table, request.payload)
        if ret:
            print 'CoAP server error: ' + ret
        return self

    def render_PUT(self, request):
        return self

    def render_DELETE(self, request):
        return self


class CoAPServer(CoAP, Thread):
    def __init__(self, host, port, routes, connector):
        Thread.__init__(self)
        CoAP.__init__(self, (host, port))
        for t in routes:
            self.add_resource(t + '/', DatabaseResource(t, connector))
        print 'CoAP server starts on ' + host + ':' + str(port)
        print self.root.dump()

    def run(self):
        self.listen(10)