Skip to content

Commit 9948bb1

Browse files
authored
Merge pull request #46 from devicehive/develop
New release
2 parents 50c94e3 + ce37dfb commit 9948bb1

16 files changed

Lines changed: 367 additions & 228 deletions

README.md

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
![build status](https://travis-ci.org/devicehive/devicehive-python.svg?branch=stable)
1+
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
2+
[![PyPI](https://img.shields.io/pypi/v/devicehive.svg)](https://pypi.python.org/pypi/devicehive)
3+
[![Build Status](https://travis-ci.org/devicehive/devicehive-python.svg?branch=master)](https://travis-ci.org/devicehive/devicehive-python)
24

35
# Devicehive
46

@@ -18,7 +20,7 @@ Example:
1820
from devicehive import DeviceHiveApi
1921

2022

21-
url = 'http://playground-dev.devicehive.com/api/rest'
23+
url = 'http://playground.devicehive.com/api/rest'
2224
refresh_token = 'SOME_REFRESH_TOKEN'
2325
device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
2426
```
@@ -28,7 +30,7 @@ device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
2830
If you want to use `Websocket` protocol you need only to specify the url:
2931

3032
```python
31-
url = 'ws://playground-dev.devicehive.com/api/websocket'
33+
url = 'ws://playground.devicehive.com/api/websocket'
3234
```
3335

3436
### Authentication
@@ -45,23 +47,23 @@ Examples:
4547
from devicehive import DeviceHiveApi
4648

4749

48-
url = 'ws://playground-dev.devicehive.com/api/websocket'
50+
url = 'ws://playground.devicehive.com/api/websocket'
4951
device_hive_api = DeviceHiveApi(url, refresh_token='SOME_REFRESH_TOKEN')
5052
```
5153

5254
```python
5355
from devicehive import DeviceHiveApi
5456

5557

56-
url = 'ws://playground-dev.devicehive.com/api/websocket'
58+
url = 'ws://playground.devicehive.com/api/websocket'
5759
device_hive_api = DeviceHiveApi(url, access_token='SOME_ACCESS_TOKEN')
5860
```
5961

6062
```python
6163
from devicehive import DeviceHiveApi
6264

6365

64-
url = 'ws://playground-dev.devicehive.com/api/websocket'
66+
url = 'ws://playground.devicehive.com/api/websocket'
6567
device_hive_api = DeviceHiveApi(url, login='SOME_LOGIN', password='SOME_PASSWORD')
6668
```
6769

@@ -85,7 +87,7 @@ Example:
8587
from devicehive import DeviceHiveApi
8688

8789

88-
url = 'http://playground-dev.devicehive.com/api/rest'
90+
url = 'http://playground.devicehive.com/api/rest'
8991
refresh_token = 'SOME_REFRESH_TOKEN'
9092
device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
9193
info = device_hive_api.get_info()
@@ -112,7 +114,7 @@ Example:
112114
from devicehive import DeviceHiveApi
113115

114116

115-
url = 'http://playground-dev.devicehive.com/api/rest'
117+
url = 'http://playground.devicehive.com/api/rest'
116118
refresh_token = 'SOME_REFRESH_TOKEN'
117119
device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
118120
name = 'user.login.lastTimeoutSent'
@@ -141,7 +143,7 @@ Example:
141143
from devicehive import DeviceHiveApi
142144

143145

144-
url = 'http://playground-dev.devicehive.com/api/rest'
146+
url = 'http://playground.devicehive.com/api/rest'
145147
refresh_token = 'SOME_REFRESH_TOKEN'
146148
device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
147149
tokens = device_hive_api.create_token(1)
@@ -159,6 +161,18 @@ objects. All args are optional.
159161

160162
`put_device(device_id, name, data, network_id, is_blocked)` method returns `Device` object. Only `device_id` is required.
161163

164+
`list_commands(device_id, start, end, command, status, sort_field, sort_order, take, skip)` method returns list of `Command`
165+
objects. Only `device_id` is required.
166+
167+
`send_command(device_id, command_name, parameters, lifetime, timestamp, status, result)` method returns `Command` object.
168+
Only `device_id` and `command_name` are required.
169+
170+
`list_notifications(device_id, start, end, notification, sort_field, sort_order, take, skip)` method returns list of `Notification`
171+
object. Only `device_id` is required.
172+
173+
`send_notification(device_id, notification_name, parameters, timestamp)` method returns `Notification` object.
174+
Only `device_id` and `notification_name` are required.
175+
162176
#### Device object
163177

164178
Properties:
@@ -213,7 +227,7 @@ Example:
213227
from devicehive import DeviceHiveApi
214228

215229

216-
url = 'http://playground-dev.devicehive.com/api/rest'
230+
url = 'http://playground.devicehive.com/api/rest'
217231
refresh_token = 'SOME_REFRESH_TOKEN'
218232
device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
219233
device_id = 'example-device'
@@ -247,15 +261,15 @@ Properties:
247261
Methods:
248262

249263
* `save()` method does not return anything.
250-
* `remove()` method does not return anything.
264+
* `remove(force)` method does not return anything. All args are optional.
251265

252266
Example:
253267

254268
```python
255269
from devicehive import DeviceHiveApi
256270

257271

258-
url = 'http://playground-dev.devicehive.com/api/rest'
272+
url = 'http://playground.devicehive.com/api/rest'
259273
refresh_token = 'SOME_REFRESH_TOKEN'
260274
device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
261275
network_name = 'example-name'
@@ -283,15 +297,15 @@ Properties:
283297
Methods:
284298

285299
* `save()` method does not return anything.
286-
* `remove()` method does not return anything.
300+
* `remove(force)` method does not return anything. All args are optional.
287301

288302
Example:
289303

290304
```python
291305
from devicehive import DeviceHiveApi
292306

293307

294-
url = 'http://playground-dev.devicehive.com/api/rest'
308+
url = 'http://playground.devicehive.com/api/rest'
295309
refresh_token = 'SOME_REFRESH_TOKEN'
296310
device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
297311
device_type_name = 'example-name'
@@ -345,7 +359,7 @@ from devicehive import DeviceHiveApi
345359
from devicehive.user import User
346360

347361

348-
url = 'http://playground-dev.devicehive.com/api/rest'
362+
url = 'http://playground.devicehive.com/api/rest'
349363
refresh_token = 'SOME_REFRESH_TOKEN'
350364
device_hive_api = DeviceHiveApi(url, refresh_token=refresh_token)
351365
login = 'example-login'
@@ -434,7 +448,7 @@ class SimpleHandler(Handler):
434448
print(notification.notification)
435449

436450

437-
url = 'http://playground-dev.devicehive.com/api/rest'
451+
url = 'http://playground.devicehive.com/api/rest'
438452
refresh_token = 'SOME_REFRESH_TOKEN'
439453
dh = DeviceHive(SimpleHandler)
440454
dh.connect(url, refresh_token=refresh_token)
@@ -469,7 +483,7 @@ device_hive = DeviceHive(SimpleHandler, 'some_arg', some_kwarg='some_kwarg')
469483
If you want to use `Websocket` protocol you need only to specify the url:
470484

471485
```python
472-
url = 'ws://playground-dev.devicehive.com/api/websocket'
486+
url = 'ws://playground.devicehive.com/api/websocket'
473487
```
474488

475489
### Authentication
@@ -483,17 +497,17 @@ There are three ways of initial authentication:
483497
Examples:
484498

485499
```python
486-
url = 'ws://playground-dev.devicehive.com/api/websocket'
500+
url = 'ws://playground.devicehive.com/api/websocket'
487501
device_hive.connect(url, refresh_token='SOME_REFRESH_TOKEN')
488502
```
489503

490504
```python
491-
url = 'ws://playground-dev.devicehive.com/api/websocket'
505+
url = 'ws://playground.devicehive.com/api/websocket'
492506
device_hive.connect(url, access_token='SOME_ACCESS_TOKEN')
493507
```
494508

495509
```python
496-
url = 'ws://playground-dev.devicehive.com/api/websocket'
510+
url = 'ws://playground.devicehive.com/api/websocket'
497511
device_hive.connect(url, login='SOME_LOGIN', password='SOME_PASSWORD')
498512
```
499513

@@ -671,6 +685,18 @@ class SimpleHandler(Handler):
671685

672686
`self.api.put_device(device_id, name, data, network_id, device_type_id, is_blocked)` method does not return anything. `put_device` method of `DeviceHiveApi` class is the wrapper on top of this call.
673687

688+
`self.api.list_commands(device_id, start, end, command, status, sort_field, sort_order, take, skip)` method returns list of `Command`
689+
objects. `list_commands` method of `DeviceHiveApi` class is the wrapper on top of this call.
690+
691+
`self.api.send_command(device_id, command_name, parameters, lifetime, timestamp, status, result)` method returns `Command` object.
692+
`send_command` method of `DeviceHiveApi` class is the wrapper on top of this call.
693+
694+
`self.api.list_notifications(device_id, start, end, notification, sort_field, sort_order, take, skip)` method returns list of `Notification`
695+
objects. `list_notifications` method of `DeviceHiveApi` class is the wrapper on top of this call.
696+
697+
`self.api.send_notification(device_id, notification_name, parameters, timestamp)` method returns `Notification` object.
698+
`send_notification` method of `DeviceHiveApi` class is the wrapper on top of this call.
699+
674700
See the description of `DeviceHiveApi` [device](#devices) methods for more details.
675701

676702
#### API device object
@@ -864,7 +890,7 @@ class ReceiverHandler(Handler):
864890
print('Notification "%s" received' % notification.notification)
865891

866892

867-
url = 'ws://playground-dev.devicehive.com/api/websocket'
893+
url = 'ws://playground.devicehive.com/api/websocket'
868894
refresh_token = 'SOME_REFRESH_TOKEN'
869895
dh = DeviceHive(ReceiverHandler)
870896
dh.connect(url, refresh_token=refresh_token)
@@ -907,7 +933,7 @@ class SenderHandler(Handler):
907933
self._send_notifications()
908934

909935

910-
url = 'http://playground-dev.devicehive.com/api/rest'
936+
url = 'http://playground.devicehive.com/api/rest'
911937
refresh_token = 'SOME_REFRESH_TOKEN'
912938
dh = DeviceHive(SenderHandler)
913939
dh.connect(url, refresh_token=refresh_token)

devicehive/api.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from devicehive.api_request import AuthApiRequest
2020
from devicehive.api_request import AuthSubscriptionApiRequest
2121
from devicehive.device import Device
22+
from devicehive.command import Command
23+
from devicehive.notification import Notification
2224
from devicehive.subscription import CommandsSubscription, \
2325
NotificationsSubscription
2426
from devicehive.network import Network
@@ -249,6 +251,90 @@ def put_device(self, device_id, name=None, data=None, network_id=None,
249251
device.get(device_id)
250252
return device
251253

254+
def list_commands(self, device_id, start=None, end=None, command=None,
255+
status=None, sort_field=None, sort_order=None, take=None,
256+
skip=None):
257+
auth_api_request = AuthApiRequest(self)
258+
auth_api_request.url('device/{deviceId}/command', deviceId=device_id)
259+
auth_api_request.action('command/list')
260+
auth_api_request.param('start', start)
261+
auth_api_request.param('end', end)
262+
auth_api_request.param('command', command)
263+
auth_api_request.param('status', status)
264+
auth_api_request.param('sortField', sort_field)
265+
auth_api_request.param('sortOrder', sort_order)
266+
auth_api_request.param('take', take)
267+
auth_api_request.param('skip', skip)
268+
auth_api_request.response_key('commands')
269+
commands = auth_api_request.execute('List commands failure.')
270+
return [Command(self, command) for command in commands]
271+
272+
def send_command(self, device_id, command_name, parameters=None,
273+
lifetime=None, timestamp=None, status=None, result=None):
274+
command = {Command.COMMAND_KEY: command_name}
275+
if parameters:
276+
command[Command.PARAMETERS_KEY] = parameters
277+
if lifetime:
278+
command[Command.LIFETIME_KEY] = lifetime
279+
if timestamp:
280+
command[Command.TIMESTAMP_KEY] = timestamp
281+
if status:
282+
command[Command.STATUS_KEY] = status
283+
if result:
284+
command[Command.RESULT_KEY] = result
285+
auth_api_request = AuthApiRequest(self)
286+
auth_api_request.method('POST')
287+
auth_api_request.url('device/{deviceId}/command', deviceId=device_id)
288+
auth_api_request.action('command/insert')
289+
auth_api_request.set('command', command, True)
290+
auth_api_request.response_key('command')
291+
command = auth_api_request.execute('Command send failure.')
292+
command[Command.DEVICE_ID_KEY] = device_id
293+
command[Command.COMMAND_KEY] = command_name
294+
command[Command.PARAMETERS_KEY] = parameters
295+
command[Command.LIFETIME_KEY] = lifetime
296+
command[Command.STATUS_KEY] = status
297+
command[Command.RESULT_KEY] = result
298+
return Command(self, command)
299+
300+
def list_notifications(self, device_id, start=None, end=None,
301+
notification=None, sort_field=None, sort_order=None,
302+
take=None, skip=None):
303+
auth_api_request = AuthApiRequest(self)
304+
auth_api_request.url('device/{deviceId}/notification',
305+
deviceId=device_id)
306+
auth_api_request.action('notification/list')
307+
auth_api_request.param('start', start)
308+
auth_api_request.param('end', end)
309+
auth_api_request.param('notification', notification)
310+
auth_api_request.param('sortField', sort_field)
311+
auth_api_request.param('sortOrder', sort_order)
312+
auth_api_request.param('take', take)
313+
auth_api_request.param('skip', skip)
314+
auth_api_request.response_key('notifications')
315+
notifications = auth_api_request.execute('List notifications failure.')
316+
return [Notification(notification) for notification in notifications]
317+
318+
def send_notification(self, device_id, notification_name, parameters=None,
319+
timestamp=None):
320+
notification = {'notification': notification_name}
321+
if parameters:
322+
notification['parameters'] = parameters
323+
if timestamp:
324+
notification['timestamp'] = timestamp
325+
auth_api_request = AuthApiRequest(self)
326+
auth_api_request.method('POST')
327+
auth_api_request.url('device/{deviceId}/notification',
328+
deviceId=device_id)
329+
auth_api_request.action('notification/insert')
330+
auth_api_request.set('notification', notification, True)
331+
auth_api_request.response_key('notification')
332+
notification = auth_api_request.execute('Notification send failure.')
333+
notification[Notification.DEVICE_ID_KEY] = device_id
334+
notification[Notification.NOTIFICATION_KEY] = notification_name
335+
notification[Notification.PARAMETERS_KEY] = parameters
336+
return Notification(notification)
337+
252338
def list_networks(self, name=None, name_pattern=None, sort_field=None,
253339
sort_order=None, take=None, skip=None):
254340
auth_api_request = AuthApiRequest(self)

0 commit comments

Comments
 (0)