@@ -18,6 +18,7 @@ class SimpleHandler(Handler):
1818 def handle_connect (self ):
1919 info = self .api.get_info()
2020 print (info)
21+ self .api.disconnect()
2122```
2223
2324` handle_connect ` is the only one required method. If you want to handle server
@@ -31,9 +32,12 @@ from devicehive import Handler
3132class SimpleHandler (Handler ):
3233
3334 def handle_connect (self ):
34- self .api.subscribe_insert_commands()
35- self .api.subscribe_update_commands()
36- self .api.subscribe_notifications()
35+ device_ids = [' example-device-1' , ' example-device-2' ]
36+ for device_id in device_ids:
37+ self .api.put_device(device_id)
38+ self .api.subscribe_insert_commands(device_ids)
39+ self .api.subscribe_update_commands(device_ids)
40+ self .api.subscribe_notifications(device_ids)
3741
3842 def handle_command_insert (self , command ):
3943 print (command.command)
@@ -58,6 +62,9 @@ from devicehive import DeviceHive
5862class SimpleHandler (Handler ):
5963
6064 def handle_connect (self ):
65+ device_ids = [' example-device-1' , ' example-device-2' ]
66+ for device_id in device_ids:
67+ self .api.put_device(device_id)
6168 self .api.subscribe_insert_commands()
6269 self .api.subscribe_update_commands()
6370 self .api.subscribe_notifications()
@@ -76,7 +83,6 @@ url = 'http://playground.dev.devicehive.com/api/rest'
7683refresh_token = ' SOME_REFRESH_TOKEN'
7784dh = DeviceHive(SimpleHandler)
7885dh.connect(url, refresh_token = refresh_token)
79- dh.join()
8086```
8187
8288### Custom handler args
@@ -98,6 +104,7 @@ class SimpleHandler(Handler):
98104 def handle_connect (self ):
99105 info = self .api.get_info()
100106 print (info)
107+ self .api.disconnect()
101108
102109dh = DeviceHive(SimpleHandler, ' some_arg' , some_kwarg = ' some_kwarg' )
103110```
@@ -143,12 +150,14 @@ custom handler with `self.api`.
143150### Info
144151
145152` self.api.get_info() ` returns ` dict ` with the next fields:
153+
146154* ` api_version `
147155* ` server_timestamp `
148156* ` rest_server_url `
149157* ` websocket_server_url `
150158
151159` self.api.get_cluster_info() ` returns ` dict ` with the next fields:
160+
152161* ` bootstrap.servers `
153162* ` zookeeper.connect `
154163
@@ -164,11 +173,13 @@ class SimpleHandler(Handler):
164173 print (info)
165174 cluster_info = self .api.get_cluster_info()
166175 print (cluster_info)
176+ self .api.disconnect()
167177```
168178
169179### Properties
170180
171181` self.api.get_property(name) ` returns ` dict ` with the next fields:
182+
172183* ` entity_version `
173184* ` name `
174185* ` value `
@@ -191,12 +202,14 @@ class SimpleHandler(Handler):
191202 entity_version = self .api.get_property(name, ' value' )
192203 print (entity_version)
193204 self .api.delete_property(name)
205+ self .api.disconnect()
194206```
195207
196208### Tokens
197209
198210` self.api.create_token(user_id, expiration, actions, network_ids, device_ids) `
199211returns ` dict ` with the next fields:
212+
200213* ` access_token `
201214* ` refresh_token `
202215
@@ -216,6 +229,7 @@ class SimpleHandler(Handler):
216229 print (tokens)
217230 access_token = self .api.refresh_token()
218231 print (access_token)
232+ self .api.disconnect()
219233```
220234
221235### Commands subscription and unsubscription
@@ -300,38 +314,32 @@ Only `device_id` arg is required.
300314#### Device object
301315
302316Properties:
317+
303318* ` id ` (read only)
304319* ` name `
305320* ` data `
306321* ` network_id `
307322* ` is_blocked `
308323
309324Methods:
325+
310326* ` save() ` Does not return anything.
311327* ` remove() ` Does not return anything.
312- * ` subscribe_insert_commands(names, timestamp) ` Does not return anything. All
313- args are optional.
328+ * ` subscribe_insert_commands(names, timestamp) ` Does not return anything. All args are optional.
314329* ` unsubscribe_insert_commands() ` Does not return anything.
315- * ` subscribe_update_commands(names, timestamp) ` Does not return anything.
316- All args are optional.
330+ * ` subscribe_update_commands(names, timestamp) ` Does not return anything. All args are optional.
317331* ` unsubscribe_update_commands() ` Does not return anything.
318- * `list_commands(start, end, command, status, sort_field, sort_order, take,
319- skip)` Returns list of ` Command` objects. All args are
320- optional.
321- * ` send_command(command_name, parameters, lifetime, timestamp, status, result) `
322- Returns ` Command ` object. Only ` command_name ` is required.
323- * ` subscribe_notifications(names, timestamp) ` Does not return anything. All args
324- are optional.
332+ * ` list_commands(start, end, command, status, sort_field, sort_order, take, skip) ` Returns list of ` Command ` objects. All args are optional.
333+ * ` send_command(command_name, parameters, lifetime, timestamp, status, result) ` Returns ` Command ` object. Only ` command_name ` is required.
334+ * ` subscribe_notifications(names, timestamp) ` Does not return anything. All args are optional.
325335* ` unsubscribe_notifications() ` Does not return anything.
326- * `list_notifications(start, end, notification, sort_field, sort_order, take,
327- skip)` Returns list of ` Notification` objects. All args
328- are optional.
329- * ` send_notification(notification_name, parameters, timestamp) ` Returns
330- ` Notification ` object. Only ` notification_name ` is required.
336+ * ` list_notifications(start, end, notification, sort_field, sort_order, take, skip) ` Returns list of ` Notification ` objects. All args are optional.
337+ * ` send_notification(notification_name, parameters, timestamp) ` Returns ` Notification ` object. Only ` notification_name ` is required.
331338
332339#### Command object
333340
334341Properties:
342+
335343* ` id ` (read only)
336344* ` user_id ` (read only)
337345* ` command ` (read only)
@@ -343,11 +351,13 @@ Properties:
343351* ` result `
344352
345353Methods:
354+
346355* ` save() ` Does not return anything.
347356
348357#### Notification object
349358
350359Properties:
360+
351361* ` device_id ` (read only)
352362* ` id ` (read only)
353363* ` notification ` (read only)
@@ -372,6 +382,7 @@ class SimpleHandler(Handler):
372382 print (' Device: %s , name: %s , data: %s ' % (device.id, device.name,
373383 device.data))
374384 device.remove()
385+ self .api.disconnect()
375386```
376387
377388### Networks
@@ -386,15 +397,16 @@ returns list of `Network` objects. All args are optional.
386397#### Network object
387398
388399Properties:
400+
389401* ` id ` (read only)
390402* ` name `
391403* ` description `
392404
393405Methods:
406+
394407* ` save() ` Does not return anything.
395408* ` remove() ` Does not return anything.
396- * ` list_devices(name, name_pattern, sort_field, sort_order, take, skip) `
397- Returns list of ` Device ` objects. All args are optional.
409+ * ` list_devices(name, name_pattern, sort_field, sort_order, take, skip) ` Returns list of ` Device ` objects. All args are optional.
398410
399411Example:
400412``` python
@@ -408,6 +420,7 @@ class SimpleHandler(Handler):
408420 network_description = ' example-description'
409421 network = self .api.create_network(network_name, network_description)
410422 print (network.name)
423+ self .api.disconnect()
411424```
412425
413426### Users
@@ -425,6 +438,7 @@ class SimpleHandler(Handler):
425438#### User object
426439
427440Properties:
441+
428442* ` id ` (read only)
429443* ` login ` (read only)
430444* ` last_login ` (read only)
@@ -434,6 +448,7 @@ Properties:
434448* ` data `
435449
436450Methods:
451+
437452* ` save() ` Does not return anything.
438453* ` update_password(password) ` Does not return anything.
439454* ` remove() ` Does not return anything.
@@ -456,6 +471,7 @@ class SimpleHandler(Handler):
456471 data = {' key' : ' value' }
457472 user = self .api.create_user(login, password, role, data)
458473 print (user.login)
474+ self .api.disconnect()
459475```
460476
461477## Extended example:
@@ -497,8 +513,6 @@ url = 'ws://playground.dev.devicehive.com/api/websocket'
497513refresh_token = ' SOME_REFRESH_TOKEN'
498514dh = DeviceHive(ReceiverHandler)
499515dh.connect(url, refresh_token = refresh_token)
500- dh.join()
501- dh.print_exception()
502516```
503517
504518On the next step we will create ` sender.py `
@@ -524,6 +538,7 @@ class SenderHandler(Handler):
524538 notification = ' %s -notification' % num_notification
525539 self ._device.send_notification(notification)
526540 print (' Sending notification "%s "' % notification)
541+ self .api.disconnect()
527542
528543 def handle_connect (self ):
529544 self ._device = self .api.get_device(self ._device_id)
@@ -541,8 +556,6 @@ url = 'http://playground.dev.devicehive.com/api/rest'
541556refresh_token = ' SOME_REFRESH_TOKEN'
542557dh = DeviceHive(SenderHandler)
543558dh.connect(url, refresh_token = refresh_token)
544- dh.join()
545- dh.print_exception()
546559```
547560
548561Run ` python receiver.py ` in the first terminal. And ` python sender.py ` in the
0 commit comments