chrysn/aiocoap

Using 'd' attribue in endpoint registration

Closed this issue · 8 comments

I run :
Step-1: endpoint registration with resources
aiocoap-client --method POST --content-format 40 --payload '</lamp>;rt="lamp",</light>;rt="sensor"' coap://localhost/resourcedirectory/?ep=lm001&d=bldg1.fl1.off1
Step-2: endpont lookup with d attribute
aiocoap-client --method GET coap://localhost/endpoint-lookup/?d=bldg1*
I got nothing. Did I miss something to discover EP by sector(domain) attribute in the RD.

Thank you so much or the quick response.
Is there any alternative to create an EP with its related sector 'd' in the Resource Directory?

Yes, It works perfectly:
aiocoap-client --method POST --content-format 40 --payload '</lamp>;rt="lamp",</light>;rt="sensor"' 'coap://localhost/resourcedirectory/?ep=lm001&d=bldg1.fl1.off1'

I need to create the same thing but without aiocoap-client tool.
I run this code:

async def main():
    
    context = await Context.create_client_context()
    payload = b'</sensors/temp>;rt="temperature-c";if="sensor"' * 30
    request = Message(code=POST, payload=payload, uri="coap://localhost/resourcedirectory/?ep=ep1&d=bldg1.fl1.off1")

    # create the new resource on the directory
    try:
    	response = await context.request(request).response
    	created_uri =  str(response.payload.decode('utf-8'))
    except Exception as e:
    	print(e)
    else:
    	print('Result: %r'%(created_uri))

if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())

I got this error:

ERROR:coap-server:An exception occurred while rendering a resource: AttributeError("'Message' object has no attribute 'request'")
Traceback (most recent call last):
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/protocol.py", line 383, in _render_to_plumbing_request
    await self._render_to_plumbing_request_inner(plumbing_request,
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/protocol.py", line 512, in _render_to_plumbing_request_inner
    response = await self.serversite.render(request)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/cli/rd.py", line 698, in render
    return await Site.render(self, request)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/resource.py", line 369, in render
    return await child.render(subrequest)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/resource.py", line 117, in render
    response = await m(request)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/cli/rd.py", line 382, in render_post
    links = link_format_from_message(request)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/cli/rd.py", line 353, in link_format_from_message
    certain_format = message.request.opt.accept
AttributeError: 'Message' object has no attribute 'request'

Actually, I need to create hundreds of resources like that in Resource Directory architecture. What is the best method to satisfy this requirement ?
Is it:

  1. using aiocoap-client and sh script.
  2. using a script python (create_client_context ) like above.
  3. Or like server.py example (create_server_context) but the problem it doesn't have not RD inside.

I got this error:

ERROR:coap-server:An exception occurred while rendering a resource: AttributeError("'Message' object has no attribute 'request'")
Traceback (most recent call last):
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/protocol.py", line 383, in _render_to_plumbing_request
    await self._render_to_plumbing_request_inner(plumbing_request,
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/protocol.py", line 512, in _render_to_plumbing_request_inner
    response = await self.serversite.render(request)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/cli/rd.py", line 698, in render
    return await Site.render(self, request)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/resource.py", line 369, in render
    return await child.render(subrequest)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/resource.py", line 117, in render
    response = await m(request)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/cli/rd.py", line 382, in render_post
    links = link_format_from_message(request)
  File "/home/aiocoap/master/thesis/aiocoap/aiocoap/cli/rd.py", line 353, in link_format_from_message
    certain_format = message.request.opt.accept
AttributeError: 'Message' object has no attribute 'request'

I forget to specify the content format. The correct code:

    context = await Context.create_client_context()
    payload = b'</sensors/temp>;rt="temperature-c";if="sensor"'
    request = Message(code=POST, payload=payload, uri="coap://localhost/resourcedirectory/?ep=ep1")
    request.opt.content_format = 40

As for the hundreds, that depends on whether you only need the entries once for testing or for longer testing (including keepalives) or to actually serve the resources. If it's only about creating hundreds once, what you do in #250 looks generally OK.

Is anything else left unresolved from this issue itself?

No. Thank you sir.