You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# The ID can be used to pair the requests with the responses
63
+
print("ID: {}".format(id_param))
64
+
print("Type: {}".format(type_param))
65
+
print("Token: {}".format(token))
66
+
print("Payload: {}".format(payload))
67
+
68
+
# Thread handling the sockets
69
+
defcoap_thread():
70
+
whileTrue:
71
+
# Using Coap.read() without timeout value means that if no new packet arrives to any of the client sessions registered before this is called, then this function will never return
# Request to a Dish Telemetry server, payload is the AMQP message, token is maximum 8 bytes containing the IMEI or IMSI id, no options should be present
# Initialize Coap module, to be used only as a CoAp Client
140
+
Coap.init()
141
+
142
+
# Start a new thread which handles the CoAp, it is safe to start as soon as possible as the Coap.read() has timeout specified
143
+
# After this point it is safe to create new CoAp Client Sessions, it will be processed by the coap_thread correctly as Coap.read() is used with timeout value
144
+
_thread.start_new_thread(coap_thread, ())
145
+
146
+
# Register the response handler for the requests the module initiates as a Coap Client
*`address` is the address where the CoAp module handles communication.
93
-
*`port` is the port where the CoAp module listens. If not set, the default CoAp UDP port is 5683.
94
-
*`service_discovery` is a Boolean argument that enables/disables service discovery. If enabled, the CoAp module will listen on the CoAp multicast address: 224.0.1.187. This is disabled by default.
183
+
*`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.
184
+
*`port` is the port where the CoAp Server listens. If not set, the default CoAp UDP port is 5683.
185
+
*`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.
95
186
96
187
## Methods:
97
188
98
-
#### Coap.socket()
99
-
100
-
Returns with the socket assigned to the given address and port during Coap.init() (= assigned to the CoAp module).
Creates a resource object and adds it to the CoAp module to operate as a server.
191
+
Creates a resource object and adds it to the CoAp Server.
105
192
106
193
*`uri` is the full path of the resource.
107
194
*`media_type` is the media type (CoAp option: Content-Format) of the resource. If not given, no defined media type is associated with the resource.
@@ -131,9 +218,15 @@ Returns with the resource defined by `uri` argument.
131
218
132
219
*`uri` is the full path of the resource to be returned.
133
220
134
-
#### Coap.read()
221
+
#### Coap.read(timeout=0)
135
222
136
-
Must be called when a packet is received on the socket assigned to the CoAp module. This function passes on the incoming request, whilst also composing and sending out the response if needed.
223
+
Must be called peridically to process any incoming CoAp packets.
224
+
225
+
*`timeout` is a timeout value in milliseconds. If not set or value 0 is passed the call will be blocked forever or until a new CoAp packet arrives on the CoAp Server (if any) or on the already registered CoAp Client Sessions (if any). If the value is given it returns after the defined timeout or when a new CoAp packet arrives to the CoAp Server (if any) or to the already registered CoAp Client Sessions (if any).
226
+
227
+
{{% hint style="info" %}}
228
+
Coap.read() is only aware of the CoAp Client Sessions created before the actual Coap.read() is called. If the `timeout` is not specified or 0, it is not safe to create additional CoAp Client Sessions after the first call to Coap.read() as it may happen that no CoAp packets arrives to the CoAp Server or to the already created CoAp Client Sessions thus the newly created CoAp Client Session will not be served as the current Coap.read() will never return.
229
+
{{% /hint %}}
137
230
138
231
#### Coap.register_response_handler(callback)
139
232
@@ -146,28 +239,58 @@ Registers a callback function which will be called when a remote CoAp Server res
146
239
*`token` is the token field from the received message
Creates a new CoAp Client Session which can be used to communicate with an external CoAp Server.
245
+
246
+
*`destination` is the IPv4 Address of the CoAp Server to join.
247
+
*`port` is the port of the CoAp Server to join. If not set, the default CoAp UDP port 5683 is used.
248
+
*`protocol` is the protocol to use to communicate with the CoAp Server. If not set the protocol UDP is used. Currently no other protocols than UDP is supported.
150
249
151
-
Creates and sends a request to a CoAp server.
152
250
153
-
*`uri_host` is the IP address of the server, included in the message as an "URI-HOST" option
*`destination` is the IPv4 Address of the CoAp Server where this CoAp Client Session has joined.
256
+
*`port` is the port of the CoAp Server where this CoAp Client Session has joined. If not set, the default CoAp UDP port 5683 is used.
257
+
*`protocol` is the protocol to use to communicate with the CoAp Server where this CoAp Client Session has joined. If not set the protocol UDP is used. Currently no other protocols than UDP is supported.
258
+
259
+
#### Coap.get_client_sessions()
260
+
261
+
Returns with all the registered CoAp Client Sessions.
262
+
263
+
## Class Client Session
264
+
265
+
The CoAp Client Session class represents a Client Session in the scope of the CoAp module which can be used to communicate with an external CoAp Server. A new CoAp Client Session can only be created with the `Coap.new_client_session` function.
Creates and sends a request to the external CoAp Server defined by the `destination` and `port` parameters when this CoAp Client Session was created.
270
+
154
271
*`method` is the method to be sent to the server, can be: `Coap.REQUEST_GET`, `Coap.REQUEST_PUT`, `Coap.REQUEST_POST`, `Coap.REQUEST_DELETE`
155
-
*`uri_port` is the port of the server, included in the message as an "URI-PORT" option. By default it is 5683
156
272
*`uri_path` is the full path of the resource in the server, included in the message as an "URI-PATH" option. If nothing is given the request will not have URI-PATH option.
157
273
*`content_format` is the Content-Format option of the request, can be: `Coap.MEDIATYPE_TEXT_PLAIN`, `Coap.MEDIATYPE_APP_LINK_FORMAT`, `Coap.MEDIATYPE_APP_XML`, `Coap.MEDIATYPE_APP_OCTET_STREAM`, `Coap.MEDIATYPE_APP_RDF_XML`, `Coap.MEDIATYPE_APP_EXI`, `Coap.MEDIATYPE_APP_JSON`, `Coap.MEDIATYPE_APP_CBOR`. If nothing is given the request will not have Content-Format option.
158
274
*`payload` is the payload of the request. If nothing is given the request will not have payload.
159
275
*`token` is the token field of the request. If nothing is given the request will not have token field.
160
276
*`include_options` decides whether put any options (including the ones above) into the message or not. It can be used to send special requests to servers accepting CoAp formed requests without options, e.g. to a Dish Telemetry server. By default, the options are included.
161
277
162
-
## Class resource
278
+
#### CoapClientSession.get_details()
279
+
280
+
Returns with a list of elements showing internal information about this CoAP Client Session:
281
+
282
+
*`destination` is the IPv4 Address of the CoAp Server where this CoAp Client Session has joined.
283
+
*`port` is the port of the CoAp Server where this CoAp Client Session has joined.
284
+
285
+
## Class CoapResource
163
286
164
-
The resource 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.
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.
165
288
166
289
#### Class methods
167
290
168
291
The following methods are defined in the scope of the `resource` class.
169
292
170
-
#### resource.add_attribute(name, value)
293
+
#### CoapResource.add_attribute(name, value)
171
294
172
295
Adds a new attribute to the resource. Attributes are used to explain the resource during service discovery.
173
296
@@ -184,14 +307,14 @@ coap-client -m get coap://<Coap-Server's address>/.well-known/core
184
307
185
308
{{% /hint %}}
186
309
187
-
#### resource.value(value)
310
+
#### CoapResource.value(value)
188
311
189
312
Updates or fetches the value of the resource.
190
313
191
314
*`value` is the new value to update the current value with.
192
315
If the method is called without a parameter, the current value is returned.
193
316
194
-
#### resource.callback(operation, enable)
317
+
#### CoapResource.callback(operation, enable)
195
318
To enable or disable a specific operation (GET, PUT, POST, DELETE) on the resource.
196
319
197
320
*`operation` is the operation to enable/disable, can be ORED of the followings: `Coap.REQUEST_GET`, `Coap.REQUEST_PUT`, `Coap.REQUEST_POST`, `Coap.REQUEST_DELETE`
0 commit comments