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

时间:2017-09-02 06:57:11

标签: python coap

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

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

1 个答案:

答案 0 :(得分:1)

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

替换资源

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