Update Android app device registration

This commit is contained in:
JonnyWong16 2017-03-27 20:50:00 -07:00
parent 203a6d47b4
commit 896922de4f
4 changed files with 75 additions and 41 deletions

View file

@ -49,7 +49,7 @@
<li role="presentation"><a href="#tabs-notification_agents" aria-controls="tabs-notification_agents" role="tab" data-toggle="tab">Notification Agents</a></li>
<li role="presentation"><a href="#tabs-extra_settings" aria-controls="tabs-extra_settings" role="tab" data-toggle="tab">Extra Settings</a></li>
<li role="presentation"><a href="#tabs-import_backups" aria-controls="tabs-import_backups" role="tab" data-toggle="tab">Import & Backups</a></li>
<li role="presentation"><a href="#tabs-remote_app" aria-controls="tabs-remote_app" role="tab" data-toggle="tab">PlexPy Remote App</a></li>
<li role="presentation"><a href="#tabs-android_app" aria-controls="tabs-android_app" role="tab" data-toggle="tab">PlexPy Android App</a></li>
</ul>
</div>
<div class="col-md-9">
@ -1087,10 +1087,10 @@
</div>
<div role="tabpanel" class="tab-pane" id="tabs-remote_app">
<div role="tabpanel" class="tab-pane" id="tabs-android_app">
<div class="padded-header">
<h3>PlexPy Remote App</h3>
<h3>PlexPy Android App</h3>
</div>
<div class="form-group">
@ -1103,8 +1103,8 @@
</p>
</div>
<div class="form-group">
<label>Link the App</label>
<p class="help-block">Link the app to your PlexPy server by scanning a QR code.</p>
<label>Register the App</label>
<p class="help-block">Register the app to your PlexPy server by scanning a QR code.</p>
<div class="btn-group" style="display: table-cell; vertical-align: middle;">
<button class="btn btn-form" type="button" id="generate_qr" data-target="#api-qr-modal" data-toggle="modal">Generate QR Code</button>
</div>
@ -2366,12 +2366,12 @@
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-remove"></i></button>
<h4 class="modal-title">Link PlexPy Remote App</h4>
<h4 class="modal-title">Register PlexPy Android App</h4>
</div>
<div class="modal-body">
<label>Instructions</label>
<p class="help-block">
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.
</p>
<label>QR Code</label>
<pre id="api_qr_code" style="text-align: center"></pre>
@ -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();
});

View file

@ -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')

View file

@ -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}
keys = {'device_token': device_token}
values = {'device_name': device_name}
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
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
else:
self._api_msg = 'Device registration failed.'
self._api_result_type = 'error'
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. """

View file

@ -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. ' \
'<a data-tab-destination="tabs-android_app" data-toggle="tab" data-dismiss="modal" ' \
'style="cursor: pointer;">Register the Android app with PlexPy.</a>',
'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