Skip to content

Commit

Permalink
Merge pull request #48 from devicehive/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
platon authored Jun 18, 2018
2 parents 9948bb1 + 47464b1 commit bb6ffef
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
72 changes: 60 additions & 12 deletions devicehive/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,37 @@ def __init__(self, transport, auth):
self._transport = transport
self._token = Token(self, auth)
self._connected = True
self._subscription_calls = {self.subscribe_insert_commands: set(),
self.subscribe_update_commands: set(),
self.subscribe_notifications: set()}
self.server_timestamp = None

@staticmethod
def _hashable_args(args):
args = list(args)
for i in range(len(args)):
if not isinstance(args[i], list):
continue
args[i] = tuple(args[i])
return tuple(args)

def _set_subscription_call(self, call, args):
args = self._hashable_args(args)
if args in self._subscription_calls[call]:
return
self._subscription_calls[call].add(args)

def remove_subscription_call(self, call, args):
args = self._hashable_args(args)
if args not in self._subscription_calls[call]:
return
self._subscription_calls[call].remove(args)

def apply_subscription_calls(self):
for call in self._subscription_calls:
for args in self._subscription_calls[call]:
call(*args)

@property
def transport(self):
return self._transport
Expand Down Expand Up @@ -127,8 +156,9 @@ def subscribe_insert_commands(self, device_id=None, network_ids=(),
join_names = ','.join(map(str, names))
join_network_ids = ','.join(map(str, network_ids))
join_device_type_ids = ','.join(map(str, device_type_ids))
request_timestamp = None
if not timestamp:
timestamp = self.server_timestamp
request_timestamp = self.server_timestamp
auth_subscription_api_request = AuthSubscriptionApiRequest(self)
auth_subscription_api_request.action(action)
auth_subscription_api_request.url('device/command/poll')
Expand All @@ -137,18 +167,23 @@ def subscribe_insert_commands(self, device_id=None, network_ids=(),
auth_subscription_api_request.param('deviceTypeIds',
join_device_type_ids)
auth_subscription_api_request.param('names', join_names)
auth_subscription_api_request.param('timestamp', timestamp)
auth_subscription_api_request.param('timestamp', request_timestamp)
auth_subscription_api_request.response_key('command')
api_request = ApiRequest(self)
api_request.action('command/subscribe')
api_request.set('deviceId', device_id)
api_request.set('networkIds', network_ids)
api_request.set('deviceTypeIds', device_type_ids)
api_request.set('names', names)
api_request.set('timestamp', timestamp)
api_request.set('timestamp', request_timestamp)
api_request.subscription_request(auth_subscription_api_request)
subscription = api_request.execute('Subscribe insert commands failure.')
return CommandsSubscription(self, subscription)
call = self.subscribe_insert_commands
args = (device_id, network_ids, device_type_ids, names, timestamp)
commands_subscription = CommandsSubscription(self, subscription,
call, args)
self._set_subscription_call(call, args)
return commands_subscription

def subscribe_update_commands(self, device_id=None, network_ids=(),
device_type_ids=(), names=(),
Expand All @@ -157,8 +192,9 @@ def subscribe_update_commands(self, device_id=None, network_ids=(),
join_names = ','.join(map(str, names))
join_network_ids = ','.join(map(str, network_ids))
join_device_type_ids = ','.join(map(str, device_type_ids))
request_timestamp = None
if not timestamp:
timestamp = self.server_timestamp
request_timestamp = self.server_timestamp
auth_subscription_api_request = AuthSubscriptionApiRequest(self)
auth_subscription_api_request.action(action)
auth_subscription_api_request.url('device/command/poll')
Expand All @@ -168,7 +204,7 @@ def subscribe_update_commands(self, device_id=None, network_ids=(),
auth_subscription_api_request.param('deviceTypeIds',
join_device_type_ids)
auth_subscription_api_request.param('names', join_names)
auth_subscription_api_request.param('timestamp', timestamp)
auth_subscription_api_request.param('timestamp', request_timestamp)
auth_subscription_api_request.response_timestamp_key('lastUpdated')
auth_subscription_api_request.response_key('command')
api_request = ApiRequest(self)
Expand All @@ -178,10 +214,15 @@ def subscribe_update_commands(self, device_id=None, network_ids=(),
api_request.set('networkIds', network_ids)
api_request.set('deviceTypeIds', device_type_ids)
api_request.set('names', names)
api_request.set('timestamp', timestamp)
api_request.set('timestamp', request_timestamp)
api_request.subscription_request(auth_subscription_api_request)
subscription = api_request.execute('Subscribe update commands failure.')
return CommandsSubscription(self, subscription)
call = self.subscribe_update_commands
args = (device_id, network_ids, device_type_ids, names, timestamp)
commands_subscription = CommandsSubscription(self, subscription,
call, args)
self._set_subscription_call(call, args)
return commands_subscription

def subscribe_notifications(self, device_id=None, network_ids=(),
device_type_ids=(), names=(),
Expand All @@ -190,8 +231,9 @@ def subscribe_notifications(self, device_id=None, network_ids=(),
join_names = ','.join(map(str, names))
join_network_ids = ','.join(map(str, network_ids))
join_device_type_ids = ','.join(map(str, device_type_ids))
request_timestamp = None
if not timestamp:
timestamp = self.server_timestamp
request_timestamp = self.server_timestamp
auth_subscription_api_request = AuthSubscriptionApiRequest(self)
auth_subscription_api_request.action(action)
auth_subscription_api_request.url('device/notification/poll')
Expand All @@ -200,18 +242,24 @@ def subscribe_notifications(self, device_id=None, network_ids=(),
auth_subscription_api_request.param('deviceTypeIds',
join_device_type_ids)
auth_subscription_api_request.param('names', join_names)
auth_subscription_api_request.param('timestamp', timestamp)
auth_subscription_api_request.param('timestamp', request_timestamp)
auth_subscription_api_request.response_key('notification')
api_request = ApiRequest(self)
api_request.action('notification/subscribe')
api_request.set('deviceId', device_id)
api_request.set('networkIds', network_ids)
api_request.set('deviceTypeIds', device_type_ids)
api_request.set('names', names)
api_request.set('timestamp', timestamp)
api_request.set('timestamp', request_timestamp)
api_request.subscription_request(auth_subscription_api_request)
subscription = api_request.execute('Subscribe notifications failure.')
return NotificationsSubscription(self, subscription)
call = self.subscribe_notifications
args = (device_id, network_ids, device_type_ids, names, timestamp)
notifications_subscription = NotificationsSubscription(self,
subscription,
call, args)
self._set_subscription_call(call, args)
return notifications_subscription

def list_devices(self, name=None, name_pattern=None, network_id=None,
network_name=None, sort_field=None, sort_order=None,
Expand Down
2 changes: 1 addition & 1 deletion devicehive/api_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def handle_connect(self):
if self._api_init:
server_timestamp = self._api.get_info()['server_timestamp']
self._api.server_timestamp = server_timestamp
self._api.apply_subscription_calls()
if not self._handle_connect:
self._handle_connect = True
self._handler.handle_connect()
return

def handle_event(self, event):
api_event = ApiEvent(event)
Expand Down
12 changes: 6 additions & 6 deletions devicehive/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ class BaseSubscription(object):

ID_KEY = 'subscriptionId'

def __init__(self, api, subscription=None):
def __init__(self, api, subscription=None, call=None, args=None):
self._api = api
self._id = None

if subscription:
self._init(subscription)

def _init(self, subscription):
self._id = subscription[self.ID_KEY]
self._id = subscription[self.ID_KEY]
self._call = call
self._args = args

def _ensure_exists(self):
if self._id:
Expand All @@ -55,6 +53,8 @@ def remove(self):
api_request.remove_subscription_request(remove_subscription_api_request)
api_request.execute('Unsubscribe failure.')
self._id = None
if self._call and self._args:
self._api.remove_subscription_call(self._call, self._args)


class CommandsSubscription(BaseSubscription):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


setup(name='devicehive',
version='2.1.4',
version='2.1.5',
author='DataArt (http://dataart.com)',
author_email='[email protected]',
url='https://devicehive.com',
Expand Down

0 comments on commit bb6ffef

Please sign in to comment.