Skip to content

Commit 5716ff4

Browse files
committed
Update documentation for feature which enables new resource creation via PUT
1 parent 4f595ee commit 5716ff4

File tree

1 file changed

+53
-8
lines changed
  • content/firmwareapi/pycom/network

1 file changed

+53
-8
lines changed

content/firmwareapi/pycom/network/coap.md

+53-8
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,34 @@ from network import WLAN
1515
from network import Coap
1616
import _thread
1717

18+
# Callback to be called when a new resource has been added via PUT
19+
def new_resource_callback(new_resource):
20+
details = new_resource.get_details()
21+
print("New resource has been created!")
22+
print("URI: {}".format(details[0]))
23+
print("Mediatype: {}".format(details[1]))
24+
print("Max Age: {}".format(details[2]))
25+
print("ETAG: {}".format(details[3]))
26+
print("ETAG value: {}".format(details[4]))
27+
print("Value: {}".format(new_resource.value()))
28+
# Configure the properties of the new resource
29+
new_resource.set_details(mediatype=Coap.MEDIATYPE_TEXT_PLAIN, max_age=132, etag=True)
30+
1831
# Thread handling the CoAp Server
1932
def coap_thread():
2033
while True:
21-
# Call Coap.read() without specyfing timeout value, in this way it handled all requests sent to the CoAp Server silently
34+
# Call Coap.read() without specyfing timeout value, in this way it handles all requests sent to the CoAp Server silently
2235
Coap.read()
2336

2437

2538
# Connect to the network
2639
wlan = WLAN(mode=WLAN.STA)
2740
wlan.connect('your-ssid', auth=(WLAN.WPA2, 'your-key'))
2841

29-
# Initialize Coap module as CoAp Server
30-
Coap.init(str(wlan.ifconfig()[0]), service_discovery=True)
42+
# Initialize Coap module as CoAp Server, enable new resources to be added via PUT
43+
Coap.init(str(wlan.ifconfig()[0]), service_discovery=True, dynamic_resources=True)
44+
# Register callback which will be called when new resource is added via PUT
45+
Coap.register_new_resource_handler(new_resource_callback)
3146

3247
# Add a resource with default value and plain text content format
3348
r = Coap.add_resource("resource1", media_type=Coap.MEDIATYPE_TEXT_PLAIN, value="default_value")
@@ -174,7 +189,7 @@ print(id)
174189

175190
## Initialization
176191

177-
#### Coap.init(address, *, port=5683, service_discovery=False)
192+
#### Coap.init(address, *, port=5683, service_discovery=False, dynamic_resources=False)
178193

179194
Initialize the CoAp module.
180195

@@ -183,6 +198,10 @@ The arguments are:
183198
* `address` is the address where the CoAp module handles communication when it is a Server. If not set, the module can be used as a CoAp Client only.
184199
* `port` is the port where the CoAp Server listens. If not set, the default CoAp UDP port is 5683.
185200
* `service_discovery` is a Boolean argument that enables/disables service discovery. If enabled, the CoAp Server will listen on the CoAp multicast address: 224.0.1.187. This is disabled by default.
201+
* `dynamic_resources` is a Boolean argument that enables/disables new resource creation via PUT operations. This is disabled by default.
202+
203+
When `dynamic_resources` is TRUE new resources can be created via PUT reqeusts, if the resource with the given URI does not already exist.
204+
The new resource is created with default properties (no mediatype, no Max-Age and no ETAG is enabled) and with default value received in the PUT request. On the new resource by default all operations (GET, PUT, POST and DELETE) are enabled.
186205

187206
## Methods:
188207

@@ -239,6 +258,13 @@ Registers a callback function which will be called when a remote CoAp Server res
239258
* `token` is the token field from the received message
240259
* `payload` is the payload of the received message
241260

261+
#### Coap.register_new_resource_handler(callback)
262+
263+
Registers a callback function which will be called when a a new resource has been created via PUT operation.
264+
265+
* `callback` is the callback to be registered. It must have the following arguments:
266+
* `resource` is the new resource which has been created
267+
242268
#### Coap.new_client_session(destination, port=5683, protocol=UDP)
243269

244270
Creates a new CoAp Client Session which can be used to communicate with an external CoAp Server.
@@ -284,7 +310,7 @@ Returns with a list of elements showing internal information about this CoAP Cli
284310

285311
## Class CoapResource
286312

287-
The CoapResource class represents a resource in the scope of the CoAp module when acting as a server. A new resource can only be created with the `Coap.add_resource` function.
313+
The CoapResource class represents a resource in the scope of the CoAp module when acting as a server.
288314

289315
#### Class methods
290316

@@ -307,6 +333,25 @@ coap-client -m get coap://<Coap-Server's address>/.well-known/core
307333

308334
{{% /hint %}}
309335

336+
#### CoapResource.get_details()
337+
338+
Returns with the main details of this resource in a form of list:
339+
* `Item 0:` is the URI of the resource.
340+
* `Item 1:` is the mediatype (content format) configured for this resource. -1 means no mediatype is configured.
341+
* `Item 2:` is the Max-Age configured for this resource. -1 means no Max-Age is configured.
342+
* `Item 3:` is a boolean showing whether ETAG is enabled on this resource.
343+
* `Item 4:` is the current ETAG value.
344+
345+
#### CoapResource.set_details(*, mediatype, max_age, etag)
346+
347+
Configures the main details of this resource:
348+
* `mediatype` is the mediatype (content format) os the resource. Value -1 means no mediatype is defined for this resource.
349+
* `max_age` is max-age value to be used on the resource. Value -1 means no Max-Age is defined for this resource.
350+
* `etag` is a Boolean which enables or disables ETAG on the resource.
351+
352+
This function leaves untouched any property of the resource not given as a parameter.
353+
354+
310355
#### CoapResource.value(value)
311356

312357
Updates or fetches the value of the resource.
@@ -322,15 +367,15 @@ To enable or disable a specific operation (GET, PUT, POST, DELETE) on the resour
322367

323368

324369
{{% hint style="info" %}}
325-
During a GET request, only the first occurrence of an ETAG or Accept option is passed on and interpreted; others of the same type are dropped (if any).
370+
During a GET request, only the first occurrence of an ETAG or Accept option is interpreted; others of the same type are dropped (if any).
326371
{{% /hint %}}
327372

328373
{{% hint style="info" %}}
329-
During a PUT request, only the first occurrence of an If-Match option is passed on and interpreted; others of the same type are dropped (if any).
374+
During a PUT request, only the first occurrence of an If-Match and If-None-Match option is interpreted; others of the same type are dropped (if any).
330375
{{% /hint %}}
331376

332377
{{% hint style="danger" %}}
333-
Due to limitations of the underlying ESP-IDF/libcoap library, new resources cannot be added via PUT or POST requests.
378+
Due to limitations of the underlying ESP-IDF/libcoap library, new resources cannot be added via POST request.
334379
{{% /hint %}}
335380

336381
## Constants

0 commit comments

Comments
 (0)