diff --git a/default.py b/default.py index 8eab9599..e2d8c187 100644 --- a/default.py +++ b/default.py @@ -117,7 +117,8 @@ class Main(object): transfer.plex_command('choose_pms_server') elif mode == 'deviceid': - self.deviceid() + LOG.info('New PKC UUID / unique device id requested') + transfer.plex_command('generate_new_uuid') elif mode == 'fanart': LOG.info('User requested fanarttv refresh') @@ -173,23 +174,6 @@ class Main(object): # Received a xbmcgui.ListItem() xbmcplugin.setResolvedUrl(HANDLE, True, result) - @staticmethod - def deviceid(): - window = xbmcgui.Window(10000) - deviceId_old = window.getProperty('plex_client_Id') - from resources.lib import clientinfo - try: - deviceId = clientinfo.getDeviceId(reset=True) - except Exception as e: - LOG.error('Failed to generate a new device Id: %s' % e) - utils.messageDialog(utils.lang(29999), utils.lang(33032)) - else: - LOG.info('Successfully removed old device ID: %s New deviceId:' - '%s' % (deviceId_old, deviceId)) - # 'Kodi will now restart to apply the changes' - utils.messageDialog(utils.lang(29999), utils.lang(33033)) - xbmc.executebuiltin('RestartApp') - if __name__ == '__main__': LOG.info('%s started' % v.ADDON_ID) diff --git a/resources/lib/clientinfo.py b/resources/lib/clientinfo.py index 92b7ebf5..5c5e51d8 100644 --- a/resources/lib/clientinfo.py +++ b/resources/lib/clientinfo.py @@ -59,10 +59,20 @@ def getDeviceId(reset=False): If id does not exist, create one and save in Kodi settings file. """ - if reset is True: - v.PKC_MACHINE_IDENTIFIER = None - utils.window('plex_client_Id', clear=True) - utils.settings('plex_client_Id', value="") + if reset: + LOG.info("Generating a new deviceid.") + from uuid import uuid4 + client_id = str(uuid4()) + utils.settings('plex_client_Id', value=client_id) + v.PKC_MACHINE_IDENTIFIER = client_id + utils.window('plex_client_Id', value=client_id) + LOG.info("Unique device Id plex_client_Id generated: %s", client_id) + # IF WE EXIT KODI NOW, THE SETTING WON'T STICK! + # 'Kodi will now restart to apply the changes' + # utils.messageDialog(utils.lang(29999), utils.lang(33033)) + # xbmc.executebuiltin('RestartApp') + utils.messageDialog(utils.lang(29999), 'Please restart Kodi now!') + return client_id client_id = v.PKC_MACHINE_IDENTIFIER if client_id: @@ -75,12 +85,3 @@ def getDeviceId(reset=False): utils.window('plex_client_Id', value=client_id) LOG.info("Unique device Id plex_client_Id loaded: %s", client_id) return client_id - - LOG.info("Generating a new deviceid.") - from uuid import uuid4 - client_id = str(uuid4()) - utils.settings('plex_client_Id', value=client_id) - v.PKC_MACHINE_IDENTIFIER = client_id - utils.window('plex_client_Id', value=client_id) - LOG.info("Unique device Id plex_client_Id generated: %s", client_id) - return client_id diff --git a/resources/lib/service_entry.py b/resources/lib/service_entry.py index 033613e5..a966576c 100644 --- a/resources/lib/service_entry.py +++ b/resources/lib/service_entry.py @@ -496,6 +496,9 @@ class Service(object): elif plex_command == 'EXIT-PKC': LOG.info('Received command from another instance to quit') app.APP.stop_pkc = True + elif plex_command == 'generate_new_uuid': + LOG.info('Generating new UUID for PKC') + clientinfo.getDeviceId(reset=True) else: raise RuntimeError('Unknown command: %s', plex_command) if task: diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 9355c5b8..2e104aed 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -114,7 +114,7 @@ def settings(setting, value=None): """ # We need to instantiate every single time to read changed variables! with SETTINGS_LOCK: - addon = xbmcaddon.Addon() + addon = xbmcaddon.Addon('plugin.video.plexkodiconnect') if value is not None: # Takes string or unicode by default! addon.setSetting(setting, value) diff --git a/resources/lib/variables.py b/resources/lib/variables.py index ada62b23..e457a5bd 100644 --- a/resources/lib/variables.py +++ b/resources/lib/variables.py @@ -21,7 +21,7 @@ MARK_PLAYED_AT = 0.9 # watched? IGNORE_SECONDS_AT_START = 60 -_ADDON = Addon() +_ADDON = Addon('plugin.video.plexkodiconnect') ADDON_NAME = 'PlexKodiConnect' ADDON_ID = 'plugin.video.plexkodiconnect' ADDON_VERSION = _ADDON.getAddonInfo('version') diff --git a/resources/lib/websocket_client.py b/resources/lib/websocket_client.py index 56901c15..0ac1e5e6 100644 --- a/resources/lib/websocket_client.py +++ b/resources/lib/websocket_client.py @@ -198,14 +198,19 @@ class PlexWebSocketApp(websocket.WebSocketApp, log.exception('Exception of type %s occured: %s', type(err), err) finally: self.close() - # Status = Not connected + if self._enabled: + # Status = Not connected + message = utils.lang(15208) + else: + # Status = Disabled + message = utils.lang(24023) utils.settings(self.name + SETTINGS_STRING, - value=utils.lang(15208)) + value=message) app.APP.deregister_thread(self) log.info("----===## %s stopped ##===----", self.name) def _run(self): - while not self.should_cancel(): + while not self.should_cancel() and self._enabled: # In the event the server goes offline while self.should_suspend(): # We will be caught in this loop if either another thread @@ -231,16 +236,13 @@ class PlexWebSocketApp(websocket.WebSocketApp, class PMSWebsocketApp(PlexWebSocketApp): name = 'pms_websocket' + def __init__(self, *args, **kwargs): + self._enabled = utils.settings('enableBackgroundSync') == 'true' + super().__init__(*args, **kwargs) + def get_uri(self): return get_pms_uri() - def should_suspend(self): - """ - Returns True if the thread needs to suspend. - """ - return (self._suspended or - utils.settings('enableBackgroundSync') != 'true') - def set_suspension_settings_status(self): if utils.settings('enableBackgroundSync') != 'true': # Status = Disabled @@ -255,6 +257,10 @@ class PMSWebsocketApp(PlexWebSocketApp): class AlexaWebsocketApp(PlexWebSocketApp): name = 'alexa_websocket' + def __init__(self, *args, **kwargs): + self._enabled = utils.settings('enable_alexa') == 'true' + super().__init__(*args, **kwargs) + def get_uri(self): return get_alexa_uri() @@ -263,7 +269,6 @@ class AlexaWebsocketApp(PlexWebSocketApp): Returns True if the thread needs to suspend. """ return self._suspended or \ - utils.settings('enable_alexa') != 'true' or \ app.ACCOUNT.restricted_user or \ not app.ACCOUNT.plex_token diff --git a/resources/lib/windows/kodigui.py b/resources/lib/windows/kodigui.py index 358ce98a..e860dd2e 100644 --- a/resources/lib/windows/kodigui.py +++ b/resources/lib/windows/kodigui.py @@ -994,8 +994,7 @@ class WindowProperty(object): class GlobalProperty(object): def __init__(self, prop, val='1', end=None): - import xbmcaddon - self._addonID = xbmcaddon.Addon().getAddonInfo('id') + self._addonID = 'plugin.video.plexkodiconnect' self.prop = prop self.val = val self.end = end