def get(self, device_id): auth_api_request = AuthApiRequest(self._api) auth_api_request.url('device/{deviceId}', deviceId=device_id) auth_api_request.action('device/get') auth_api_request.response_key('device') device = auth_api_request.execute('Device get failure.') self._init(device)
def get(self, user_id): auth_api_request = AuthApiRequest(self._api) auth_api_request.url('user/{userId}', userId=user_id) auth_api_request.action('user/get') auth_api_request.response_key('user') user = auth_api_request.execute('User get failure.') self._init(user)
def get_current(self): auth_api_request = AuthApiRequest(self._api) auth_api_request.url('user/current') auth_api_request.action('user/getCurrent') auth_api_request.response_key('current') user = auth_api_request.execute('Current user get failure.') self._init(user)
def get(self, network_id): auth_api_request = AuthApiRequest(self._api) auth_api_request.url('network/{id}', id=network_id) auth_api_request.action('network/get') auth_api_request.response_key('network') network = auth_api_request.execute('Network get failure.') self._init(network)
def send_command(self, device_id, command_name, parameters=None, lifetime=None, timestamp=None, status=None, result=None): command = {Command.COMMAND_KEY: command_name} if parameters: command[Command.PARAMETERS_KEY] = parameters if lifetime: command[Command.LIFETIME_KEY] = lifetime if timestamp: command[Command.TIMESTAMP_KEY] = timestamp if status: command[Command.STATUS_KEY] = status if result: command[Command.RESULT_KEY] = result auth_api_request = AuthApiRequest(self) auth_api_request.method('POST') auth_api_request.url('device/{deviceId}/command', deviceId=device_id) auth_api_request.action('command/insert') auth_api_request.set('command', command, True) auth_api_request.response_key('command') command = auth_api_request.execute('Command send failure.') command[Command.DEVICE_ID_KEY] = device_id command[Command.COMMAND_KEY] = command_name command[Command.PARAMETERS_KEY] = parameters command[Command.LIFETIME_KEY] = lifetime command[Command.STATUS_KEY] = status command[Command.RESULT_KEY] = result return Command(self, command)
def create_token(self, user_id, expiration=None, actions=None, network_ids=None, device_type_ids=None, device_ids=None): payload = {'userId': user_id} if expiration: payload['expiration'] = expiration if actions: payload['actions'] = actions if network_ids: payload['networkIds'] = network_ids if device_type_ids: payload['deviceTypeIds'] = device_type_ids if device_ids: payload['deviceIds'] = device_ids auth_api_request = AuthApiRequest(self) auth_api_request.method('POST') auth_api_request.url('token/create') auth_api_request.action('token/create') auth_api_request.set('payload', payload, True) tokens = auth_api_request.execute('Token refresh failure.') return { 'refresh_token': tokens['refreshToken'], 'access_token': tokens['accessToken'] }
def create_user(self, login, password, role, data, all_device_types_available=True): status = User.ACTIVE_STATUS user = { User.LOGIN_KEY: login, User.ROLE_KEY: role, User.STATUS_KEY: status, User.DATA_KEY: data, User.PASSWORD_KEY: password, User.ALL_DEVICE_TYPES_KEY: all_device_types_available } auth_api_request = AuthApiRequest(self) auth_api_request.method('POST') auth_api_request.url('user') auth_api_request.action('user/insert') auth_api_request.set('user', user, True) auth_api_request.response_key('user') user = auth_api_request.execute('User create failure.') user[User.LOGIN_KEY] = login user[User.ROLE_KEY] = role user[User.STATUS_KEY] = status user[User.DATA_KEY] = data return User(self, user)
def disallow_all_device_types(self): self._ensure_exists() self._ensure_all_device_types_available() auth_api_request = AuthApiRequest(self._api) auth_api_request.method('DELETE') auth_api_request.url('user/{userId}/devicetype/all', userId=self._id) auth_api_request.action('user/disallowAllDeviceTypes') auth_api_request.execute('Unassign device type failure.') self._all_device_types_available = False
def set_property(self, name, value): auth_api_request = AuthApiRequest(self) auth_api_request.method('PUT') auth_api_request.url('configuration/{name}', name=name) auth_api_request.action('configuration/put') auth_api_request.set('value', value) auth_api_request.response_key('configuration') configuration = auth_api_request.execute('Set property failure.') return {'entity_version': configuration['entityVersion']}
def update_password(self, password): self._ensure_exists() user = {self.PASSWORD_KEY: password} auth_api_request = AuthApiRequest(self._api) auth_api_request.method('PUT') auth_api_request.url('user/{userId}', userId=self._id) auth_api_request.action('user/update') auth_api_request.set('user', user, True) auth_api_request.execute('User password update failure.')
def allow_all_device_types(self): self._ensure_exists() self._ensure_not_all_device_types_available() auth_api_request = AuthApiRequest(self._api) auth_api_request.method('PUT') auth_api_request.url('user/{userId}/devicetype/all', userId=self._id) auth_api_request.action('user/allowAllDeviceTypes') auth_api_request.execute('Assign all device types failure.') self._all_device_types_available = True
def unassign_network(self, network_id): self._ensure_exists() auth_api_request = AuthApiRequest(self._api) auth_api_request.method('DELETE') auth_api_request.url('user/{userId}/network/{networkId}', userId=self._id, networkId=network_id) auth_api_request.action('user/unassignNetwork') auth_api_request.execute('Unassign network failure.')
def remove(self): self._ensure_exists() auth_api_request = AuthApiRequest(self._api) auth_api_request.method('DELETE') auth_api_request.url('network/{id}', id=self._id) auth_api_request.action('network/delete') auth_api_request.execute('Network remove failure.') self._id = None self.name = None self.description = None
def unassign_device_type(self, device_type_id): self._ensure_exists() self._ensure_not_all_device_types_available() auth_api_request = AuthApiRequest(self._api) auth_api_request.method('DELETE') auth_api_request.url('user/{userId}/devicetype/{deviceTypeId}', userId=self._id, deviceTypeId=device_type_id) auth_api_request.action('user/unassignDeviceType') auth_api_request.execute('Unassign device type failure.')
def remove(self): self._ensure_exists() auth_api_request = AuthApiRequest(self._api) auth_api_request.method('DELETE') auth_api_request.url('devicetype/{deviceTypeId}', deviceTypeId=self._id) auth_api_request.action('devicetype/delete') auth_api_request.execute('DeviceType remove failure.') self._id = None self.name = None self.description = None
def save(self): command = {self.STATUS_KEY: self.status, self.RESULT_KEY: self.result} auth_api_request = AuthApiRequest(self._api) auth_api_request.method('PUT') auth_api_request.url('device/{deviceId}/command/{commandId}', deviceId=self._device_id, commandId=self._id) auth_api_request.action('command/update') auth_api_request.set('command', command, True) auth_api_request.execute('Command save failure.')
def list_networks(self): self._ensure_exists() auth_api_request = AuthApiRequest(self._api) auth_api_request.url('user/{userId}', userId=self._id) auth_api_request.action('user/get') auth_api_request.response_key('user') user = auth_api_request.execute('List networks failure.') return [ Network(self._api, network) for network in user[User.NETWORKS_KEY] ]
def list_device_types(self): self._ensure_exists() auth_api_request = AuthApiRequest(self._api) auth_api_request.url('user/{userId}/devicetype', userId=self._id) auth_api_request.action('user/getDeviceTypes') auth_api_request.response_key('deviceTypes') device_types = auth_api_request.execute('List device types failure.') return [ DeviceType(self._api, device_type) for device_type in device_types ]
def get_property(self, name): auth_api_request = AuthApiRequest(self) auth_api_request.url('configuration/{name}', name=name) auth_api_request.action('configuration/get') auth_api_request.response_key('configuration') configuration = auth_api_request.execute('Get property failure.') return { 'entity_version': configuration['entityVersion'], 'name': configuration['name'], 'value': configuration['value'] }
def save(self): self._ensure_exists() network = {self.ID_KEY: self._id, self.NAME_KEY: self.name, self.DESCRIPTION_KEY: self.description} auth_api_request = AuthApiRequest(self._api) auth_api_request.method('PUT') auth_api_request.url('network/{networkId}', networkId=self._id) auth_api_request.action('network/update') auth_api_request.set('network', network, True) auth_api_request.execute('Network save failure.')
def save(self): self._ensure_exists() device_type = {self.ID_KEY: self._id, self.NAME_KEY: self.name, self.DESCRIPTION_KEY: self.description} auth_api_request = AuthApiRequest(self._api) auth_api_request.method('PUT') auth_api_request.url('devicetype/{deviceTypeId}', deviceTypeId=self._id) auth_api_request.action('devicetype/update') auth_api_request.set('deviceType', device_type, True) auth_api_request.execute('DeviceType save failure.')
def remove(self): self._ensure_exists() auth_api_request = AuthApiRequest(self._api) auth_api_request.method('DELETE') auth_api_request.url('device/{deviceId}', deviceId=self._id) auth_api_request.action('device/delete') auth_api_request.execute('Device remove failure.') self._id = None self.name = None self.data = None self.network_id = None self.device_type_id = None self.is_blocked = None
def save(self): self._ensure_exists() user = { self.ROLE_KEY: self.role, self.STATUS_KEY: self.status, self.DATA_KEY: self.data } auth_api_request = AuthApiRequest(self._api) auth_api_request.method('PUT') auth_api_request.url('user/{userId}', userId=self._id) auth_api_request.action('user/update') auth_api_request.set('user', user, True) auth_api_request.execute('User save failure.')
def remove(self): self._ensure_exists() auth_api_request = AuthApiRequest(self._api) auth_api_request.method('DELETE') auth_api_request.url('user/{userId}', userId=self._id) auth_api_request.action('user/delete') auth_api_request.execute('User remove failure.') self._id = None self._login = None self._last_login = None self._intro_reviewed = None self.role = None self.status = None self.data = None
def save(self): self._ensure_exists() device = { self.NAME_KEY: self.name, self.DATA_KEY: self.data, self.NETWORK_ID_KEY: self.network_id, self.DEVICE_TYPE_ID_KEY: self.device_type_id, self.IS_BLOCKED_KEY: self.is_blocked } auth_api_request = AuthApiRequest(self._api) auth_api_request.method('PUT') auth_api_request.url('device/{deviceId}', deviceId=self._id) auth_api_request.action('device/save') auth_api_request.set('device', device, True) auth_api_request.execute('Device save failure.')
def create_device_type(self, name, description): device_type = { DeviceType.NAME_KEY: name, DeviceType.DESCRIPTION_KEY: description } auth_api_request = AuthApiRequest(self) auth_api_request.method('POST') auth_api_request.url('devicetype') auth_api_request.action('devicetype/insert') auth_api_request.set('deviceType', device_type, True) auth_api_request.response_key('deviceType') device_type = auth_api_request.execute('Device type create failure.') device_type[DeviceType.NAME_KEY] = name device_type[DeviceType.DESCRIPTION_KEY] = description return DeviceType(self, device_type)
def create_network(self, name, description): network = { Network.NAME_KEY: name, Network.DESCRIPTION_KEY: description } auth_api_request = AuthApiRequest(self) auth_api_request.method('POST') auth_api_request.url('network') auth_api_request.action('network/insert') auth_api_request.set('network', network, True) auth_api_request.response_key('network') network = auth_api_request.execute('Network create failure.') network[Network.NAME_KEY] = name network[Network.DESCRIPTION_KEY] = description return Network(self, network)
def list_device_types(self, name=None, name_pattern=None, sort_field=None, sort_order=None, take=None, skip=None): auth_api_request = AuthApiRequest(self) auth_api_request.url('devicetype') auth_api_request.action('devicetype/list') auth_api_request.param('name', name) auth_api_request.param('namePattern', name_pattern) auth_api_request.param('sortField', sort_field) auth_api_request.param('sortOrder', sort_order) auth_api_request.param('take', take) auth_api_request.param('skip', skip) auth_api_request.response_key('deviceTypes') device_types = auth_api_request.execute('List device types failure.') return [DeviceType(self, device_type) for device_type in device_types]
def list_networks(self, name=None, name_pattern=None, sort_field=None, sort_order=None, take=None, skip=None): auth_api_request = AuthApiRequest(self) auth_api_request.url('network') auth_api_request.action('network/list') auth_api_request.param('name', name) auth_api_request.param('namePattern', name_pattern) auth_api_request.param('sortField', sort_field) auth_api_request.param('sortOrder', sort_order) auth_api_request.param('take', take) auth_api_request.param('skip', skip) auth_api_request.response_key('networks') networks = auth_api_request.execute('List networks failure.') return [Network(self, network) for network in networks]
def send_notification(self, device_id, notification_name, parameters=None, timestamp=None): notification = {'notification': notification_name} if parameters: notification['parameters'] = parameters if timestamp: notification['timestamp'] = timestamp auth_api_request = AuthApiRequest(self) auth_api_request.method('POST') auth_api_request.url('device/{deviceId}/notification', deviceId=device_id) auth_api_request.action('notification/insert') auth_api_request.set('notification', notification, True) auth_api_request.response_key('notification') notification = auth_api_request.execute('Notification send failure.') notification[Notification.DEVICE_ID_KEY] = device_id notification[Notification.NOTIFICATION_KEY] = notification_name notification[Notification.PARAMETERS_KEY] = parameters return Notification(notification)