2017-09-02 71 views
0

我正在嘗試構建一個Coap服務器,我可以添加新資源而無需停止服務器,重新編碼它並重新啓動。承載兩種類型的資源,「傳感器(Sens-Me)」和「執行器(Act-Me)」。我想,如果我按A鍵,執行器的一個新實例應該添加到服務器,同樣的,如果我按s的傳感器。下面是我的代碼:動態地將資源添加到具有coapthon庫的python coap服務器

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


class Sensor(Resource): 

    def __init__(self,name="Sensor",coap_server=None): 
    super(Sensor,self).__init__(name,coap_server,visible=True,observable=True,allow_children=True) 
    self.payload = "This is a new sensor" 
    self.resource_type = "rt1" 
    self.content_type = "application/json" 
    self.interface_type = "if1" 
    self.var = 0 

    def render_GET(self,request): 
     self.payload = "new sensor value ::{}".format(str(int(self.var+1))) 
     self.var +=1 
    return self 

class Actuator(Resource): 
def __init__(self,name="Actuator",coap_server=None): 
    super(Actuator,self).__init__(name,coap_server,visible=True,observable=True) 
    self.payload="This is an actuator" 
    self.resource_type="rt1" 
def render_GET(self,request): 
    return self 

class CoAPServer(CoAP): 
    def __init__(self, host, port, multicast=False): 
    CoAP.__init__(self,(host,port),multicast) 
     self.add_resource('sens-Me/',Sensor()) 
     self.add_resource('act-Me/',Actuator()) 
    print "CoAP server started on {}:{}".format(str(host),str(port)) 
    print self.root.dump() 


def main(): 
    ip = "0.0.0.0" 
    port = 5683 
    multicast=False 
    server = CoAPServer(ip,port,multicast) 
    try: 
    server.listen(10) 
      print "executed after listen" 
    except KeyboardInterrupt: 
    server.close() 

if __name__=="__main__": 
main() 

回答

0

我不知道究竟你想做。 只是爲了替換同一路線上的資源或添加新的資源?

替換資源

它根據當前coapthon版本源是不可能的:

https://github.com/Tanganelli/CoAPthon/blob/b6983fbf48399bc5687656be55ac5b9cce4f4718/coapthon/server/coap.py#L279

try: 
    res = self.root[actual_path] 
except KeyError: 
    res = None 
if res is None: 
    if len(paths) != i: 
     return False 
    resource.path = actual_path 
     self.root[actual_path] = resource 

或者,你可以解決它的請求範圍。假設有一個由資源使用的處理程序註冊表,可以在用戶輸入事件中對其進行更改。那麼,你將無法添加新的路線。

如果您絕對需要該功能,您可以向開發人員請求或爲該項目提供幫助。

添加新的資源

我已經延長您的片段一點點。 我有一點Python的經驗,所以我不知道我做了一切正常,但它的工作原理。 有一個單獨的線程輪詢用戶輸入並添加相同的資源。在那裏添加所需的代碼。

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

class Sensor(Resource): 
    def __init__(self,name="Sensor",coap_server=None): 
    super(Sensor,self).__init__(name,coap_server,visible=True,observable=True,allow_children=True) 
    self.payload = "This is a new sensor" 
    self.resource_type = "rt1" 
    self.content_type = "application/json" 
    self.interface_type = "if1" 
    self.var = 0 

    def render_GET(self,request): 
    self.payload = "new sensor value ::{}".format(str(int(self.var+1))) 
    self.var +=1 
    return self 

class Actuator(Resource): 
    def __init__(self,name="Actuator",coap_server=None): 
    super(Actuator,self).__init__(name,coap_server,visible=True,observable=True) 
    self.payload="This is an actuator" 
    self.resource_type="rt1" 
    def render_GET(self,request): 
    return self 

class CoAPServer(CoAP): 
    def __init__(self, host, port, multicast=False): 
    CoAP.__init__(self,(host,port),multicast) 
    self.add_resource('sens-Me/',Sensor()) 
    self.add_resource('act-Me/',Actuator()) 
    print "CoAP server started on {}:{}".format(str(host),str(port)) 
    print self.root.dump() 

def pollUserInput(server): 
    while 1: 
    user_input = raw_input("Some input please: ") 
    print user_input 
    server.add_resource('sens-Me2/', Sensor()) 

def main(): 
    ip = "0.0.0.0" 
    port = 5683 
    multicast=False 

    server = CoAPServer(ip,port,multicast) 
    thread = Thread(target = pollUserInput, args=(server,)) 
    thread.setDaemon(True) 
    thread.start() 

    try: 
    server.listen(10) 
    print "executed after listen" 
    except KeyboardInterrupt: 
    print server.root.dump() 
    server.close() 
    sys.exit() 

if __name__=="__main__": 
    main() 
+0

非常感謝,您的修改(添加資源)是我想要的,我會對其進行進一步更改,以獲得我所需要的。 –

相關問題