使用 coapthon 库将资源动态添加到 python coap 服务器

dynamically adding a resource to a python coap server with coapthon library

我正在尝试构建一个 coap 服务器,我可以在其中添加新资源而无需停止服务器、重新编码并重新启动。我的服务器应该托管两种类型的资源,"sensors(Sens-Me)" 和 "Actuators(Act-Me)" 。我希望如果我按下 A 键,则应将一个新的执行器实例添加到服务器,同样如果我为传感器按下 S。下面是我的代码:

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()

我不确定你到底想做什么。 只是更换同一条路线上的资源还是增加一个新资源?

替换资源

根据目前的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()