diff --git a/data/interfaces/default/settings.html b/data/interfaces/default/settings.html index 9a459b10..6b17494e 100644 --- a/data/interfaces/default/settings.html +++ b/data/interfaces/default/settings.html @@ -49,7 +49,7 @@
Link the app to your PlexPy server by scanning a QR code.
+ +Register the app to your PlexPy server by scanning a QR code.
- Scan the QR code below with the PlexPy Remote app to automatically link it with the server. + Scan the QR code below with the PlexPy Android app to automatically register it with the server.
@@ -3025,7 +3025,7 @@ $(document).ready(function() { }); }); - $("a[data-tab-destination]").click(function () { + $('body').on('click', 'a[data-tab-destination]', function () { var tab = $(this).data('tab-destination'); $("a[href=#" + tab + "]").click(); }); diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 3c062f87..77efbf33 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -534,6 +534,12 @@ def dbcheck(): 'media_info TEXT)' ) + # mobile_devices table :: This table keeps record of devices linked with the mobile app + c_db.execute( + 'CREATE TABLE IF NOT EXISTS mobile_devices (id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 'device_token TEXT NOT NULL UNIQUE, device_name TEXT, friendly_name TEXT)' + ) + # Upgrade sessions table from earlier versions try: c_db.execute('SELECT started FROM sessions') diff --git a/plexpy/api2.py b/plexpy/api2.py index 72c01399..36a796cb 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -341,13 +341,13 @@ class API2: return data - def register_android_app(self, notifier_id=None, device_token='', **kwargs): + def register_device(self, device_name='', device_token='', **kwargs): """ Registers the PlexPy Android App for notifications. ``` Required parameters: - notifier_id (int): The notifier id of the PlexPy Android App notifier device_token (str): The device token of the PlexPy Android App + device_name (str): The device name of the PlexPy Android App Optional parameters: None @@ -356,10 +356,8 @@ class API2: None ``` """ - import notifiers - - if not notifier_id: - self._api_msg = 'Device registartion failed: no notifier id provided.' + if not device_name: + self._api_msg = 'Device registartion failed: no device name provided.' self._api_result_type = 'error' return @@ -368,24 +366,23 @@ class API2: self._api_result_type = 'error' return - if notifier_id: - notifier = notifiers.get_notifier_config(notifier_id=notifier_id) - if not notifier or notifier['agent_id'] != 21: - self._api_msg = 'Device registartion failed: Incorrect notifier id provided' - self._api_result_type = 'error' - return + db = database.MonitorDatabase() - config = {'androidapp_api_key': notifier['config']['api_key'], - 'androidapp_device_token': device_token} - - if notifiers.set_notifier_config(notifier_id=notifier_id, agent_id=notifier['agent_id'], **config): - self._api_msg = 'Device registration successful.' - self._api_result_type = 'success' - return + keys = {'device_token': device_token} + values = {'device_name': device_name} - else: - self._api_msg = 'Device registration failed.' - self._api_result_type = 'error' + try: + db.upsert(table_name='mobile_devices', key_dict=keys, value_dict=values) + except Exception as e: + logger.warn(u"PlexPy APIv2 :: Failed to register mobile device in the database: %s." % e) + self._api_msg = 'Device registartion failed: database error.' + self._api_result_type = 'error' + return + + logger.info(u"PlexPy APIv2 :: Registered mobile device in the database: %s." % device_name) + self._api_msg = 'Device registration successful.' + self._api_result_type = 'success' + return def _api_make_md(self): """ Tries to make a API.md to simplify the api docs. """ diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index 83f94844..661e32a1 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -663,7 +663,7 @@ class Notifier(object): class ANDROIDAPP(Notifier): """ - PlexPy Android App notifications + PlexPy Android app notifications """ _DEFAULT_CONFIG = {'api_key': '', 'device_token': '' @@ -687,29 +687,60 @@ class ANDROIDAPP(Notifier): request_status = response.status if request_status == 200: - logger.info(u"PlexPy Notifiers :: Android App notification sent.") + logger.info(u"PlexPy Notifiers :: Android app notification sent.") return True elif request_status >= 400 and request_status < 500: - logger.warn(u"PlexPy Notifiers :: Android App notification failed: [%s] %s" % (request_status, response.reason)) + logger.warn(u"PlexPy Notifiers :: Android app notification failed: [%s] %s" % (request_status, response.reason)) return False else: - logger.warn(u"PlexPy Notifiers :: Android App notification failed.") + logger.warn(u"PlexPy Notifiers :: Android app notification failed.") return False + def get_devices(self): + db = database.MonitorDatabase() + + try: + query = 'SELECT * FROM mobile_devices' + result = db.select(query=query) + except Exception as e: + logger.warn(u"PlexPy Notifiers :: Unable to retrieve Android app devices list: %s." % e) + return {'': ''} + + devices = {} + for device in result: + if device['friendly_name']: + devices['device_token'] = device['friendly_name'] + else: + devices['device_token'] = device['device_name'] + + return devices + def return_config_options(self): + devices = self.get_devices() + + if not devices: + devices_config = {'label': 'Device', + 'description': 'No devices registered. ' \ + 'Register the Android app with PlexPy.', + 'input_type': 'help' + } + else: + devices_config = {'label': 'Device', + 'value': self.config['device_token'], + 'name': 'androidapp_device_token', + 'description': 'Set your Android app device.', + 'input_type': 'select', + 'select_options': devices + } + config_option = [{'label': 'Pushy API Key', 'value': self.config['api_key'], 'name': 'androidapp_api_key', 'description': 'Your Pushy API key.', 'input_type': 'text' }, - {'label': 'Device Token', - 'value': self.config['device_token'], - 'name': 'androidapp_device_token', - 'description': 'Your Android App device token. Filled in automatically after registering the deivce in the app.', - 'input_type': 'text', - 'readonly': True - } + devices_config ] return config_option