Move init of syncs to state.py

This commit is contained in:
croneter 2017-08-21 18:53:38 +02:00
parent 5585f8a4e0
commit 31be5f30f3
7 changed files with 130 additions and 111 deletions

View file

@ -33,7 +33,7 @@ sys_path.append(_base_resource)
import entrypoint import entrypoint
from utils import window, pickl_window, reset, passwordsXML, language as lang,\ from utils import window, pickl_window, reset, passwordsXML, language as lang,\
dialog dialog, plex_command
from pickler import unpickle_me from pickler import unpickle_me
from PKC_listitem import convert_PKC_to_listitem from PKC_listitem import convert_PKC_to_listitem
import variables as v import variables as v
@ -127,28 +127,29 @@ class Main():
log.error('Not connected to a PMS.') log.error('Not connected to a PMS.')
else: else:
if mode == 'repair': if mode == 'repair':
window('plex_runLibScan', value='repair')
log.info('Requesting repair lib sync') log.info('Requesting repair lib sync')
plex_command('RUN_LIB_SCAN', 'repair')
elif mode == 'manualsync': elif mode == 'manualsync':
log.info('Requesting full library scan') log.info('Requesting full library scan')
window('plex_runLibScan', value='full') plex_command('RUN_LIB_SCAN', 'full')
elif mode == 'texturecache': elif mode == 'texturecache':
window('plex_runLibScan', value='del_textures') log.info('Requesting texture caching of all textures')
plex_command('RUN_LIB_SCAN', 'textures')
elif mode == 'chooseServer': elif mode == 'chooseServer':
entrypoint.chooseServer() entrypoint.chooseServer()
elif mode == 'refreshplaylist': elif mode == 'refreshplaylist':
log.info('Requesting playlist/nodes refresh') log.info('Requesting playlist/nodes refresh')
window('plex_runLibScan', value='views') plex_command('RUN_LIB_SCAN', 'views')
elif mode == 'deviceid': elif mode == 'deviceid':
self.deviceid() self.deviceid()
elif mode == 'fanart': elif mode == 'fanart':
log.info('User requested fanarttv refresh') log.info('User requested fanarttv refresh')
window('plex_runLibScan', value='fanart') plex_command('RUN_LIB_SCAN', 'fanart')
elif '/extrafanart' in argv[0]: elif '/extrafanart' in argv[0]:
plexpath = argv[2][1:] plexpath = argv[2][1:]
@ -165,7 +166,8 @@ class Main():
else: else:
entrypoint.doMainListing(content_type=params.get('content_type')) entrypoint.doMainListing(content_type=params.get('content_type'))
def play(self): @staticmethod
def play():
""" """
Start up playback_starter in main Python thread Start up playback_starter in main Python thread
""" """
@ -190,7 +192,8 @@ class Main():
listitem = convert_PKC_to_listitem(result.listitem) listitem = convert_PKC_to_listitem(result.listitem)
setResolvedUrl(HANDLE, True, listitem) setResolvedUrl(HANDLE, True, listitem)
def deviceid(self): @staticmethod
def deviceid():
deviceId_old = window('plex_client_Id') deviceId_old = window('plex_client_Id')
from clientinfo import getDeviceId from clientinfo import getDeviceId
try: try:
@ -205,6 +208,7 @@ class Main():
dialog('ok', lang(29999), lang(33033)) dialog('ok', lang(29999), lang(33033))
executebuiltin('RestartApp') executebuiltin('RestartApp')
if __name__ == '__main__': if __name__ == '__main__':
log.info('%s started' % v.ADDON_ID) log.info('%s started' % v.ADDON_ID)
Main() Main()

View file

@ -64,6 +64,8 @@ class Monitor_Window(Thread):
elif value.startswith('PLEX_USERNAME-'): elif value.startswith('PLEX_USERNAME-'):
state.PLEX_USERNAME = \ state.PLEX_USERNAME = \
value.replace('PLEX_USERNAME-', '') or None value.replace('PLEX_USERNAME-', '') or None
elif value.startswith('RUN_LIB_SCAN-'):
state.RUN_LIB_SCAN = value.replace('RUN_LIB_SCAN-', '')
else: else:
raise NotImplementedError('%s not implemented' % value) raise NotImplementedError('%s not implemented' % value)
else: else:

View file

@ -975,7 +975,7 @@ def __LogIn():
SUSPEND_LIBRARY_THREAD is set to False in service.py if user was signed SUSPEND_LIBRARY_THREAD is set to False in service.py if user was signed
out! out!
""" """
window('plex_runLibScan', value='full') plex_command('RUN_LIB_SCAN', 'full')
# Restart user client # Restart user client
plex_command('SUSPEND_USER_CLIENT', 'False') plex_command('SUSPEND_USER_CLIENT', 'False')

View file

@ -9,7 +9,8 @@ from xbmc import Monitor, Player, sleep
import downloadutils import downloadutils
import plexdb_functions as plexdb import plexdb_functions as plexdb
from utils import window, settings, CatchExceptions, tryDecode, tryEncode from utils import window, settings, CatchExceptions, tryDecode, tryEncode, \
plex_command
from PlexFunctions import scrobble from PlexFunctions import scrobble
from kodidb_functions import get_kodiid_from_filename from kodidb_functions import get_kodiid_from_filename
from PlexAPI import API from PlexAPI import API
@ -80,7 +81,7 @@ class KodiMonitor(Monitor):
window(window_value, value=settings(settings_value)) window(window_value, value=settings(settings_value))
if settings_value == 'fetch_pms_item_number': if settings_value == 'fetch_pms_item_number':
log.info('Requesting playlist/nodes refresh') log.info('Requesting playlist/nodes refresh')
window('plex_runLibScan', value="views") plex_command('RUN_LIB_SCAN', 'views')
# Reset the state variables in state.py # Reset the state variables in state.py
for settings_value, state_value in STATE_SETTINGS.iteritems(): for settings_value, state_value in STATE_SETTINGS.iteritems():
new = settings(settings_value) new = settings(settings_value)
@ -157,7 +158,7 @@ class KodiMonitor(Monitor):
elif method == "GUI.OnScreensaverDeactivated": elif method == "GUI.OnScreensaverDeactivated":
if settings('dbSyncScreensaver') == "true": if settings('dbSyncScreensaver') == "true":
sleep(5000) sleep(5000)
window('plex_runLibScan', value="full") plex_command('RUN_LIB_SCAN', 'full')
elif method == "System.OnQuit": elif method == "System.OnQuit":
log.info('Kodi OnQuit detected - shutting down') log.info('Kodi OnQuit detected - shutting down')

View file

@ -1391,6 +1391,69 @@ class LibrarySync(Thread):
'refresh': refresh 'refresh': refresh
}) })
def triage_lib_scans(self):
"""
"""
if state.RUN_LIB_SCAN in ("full", "repair"):
log.info('Full library scan requested, starting')
window('plex_dbScan', value="true")
state.DB_SCAN = True
if state.RUN_LIB_SCAN == "full":
self.fullSync()
elif state.RUN_LIB_SCAN == "repair":
self.fullSync(repair=True)
state.RUN_LIB_SCAN = None
window('plex_dbScan', clear=True)
state.DB_SCAN = False
# Full library sync finished
self.showKodiNote(lang(39407))
# Reset views was requested from somewhere else
elif state.RUN_LIB_SCAN == "views":
log.info('Refresh playlist and nodes requested, starting')
window('plex_dbScan', value="true")
state.DB_SCAN = True
state.RUN_LIB_SCAN = None
# First remove playlists
deletePlaylists()
# Remove video nodes
deleteNodes()
# Kick off refresh
if self.maintainViews() is True:
# Ran successfully
log.info("Refresh playlists/nodes completed")
# "Plex playlists/nodes refreshed"
self.showKodiNote(lang(39405))
else:
# Failed
log.error("Refresh playlists/nodes failed")
# "Plex playlists/nodes refresh failed"
self.showKodiNote(lang(39406),
icon="error")
window('plex_dbScan', clear=True)
state.DB_SCAN = False
elif state.RUN_LIB_SCAN == 'fanart':
state.RUN_LIB_SCAN = None
# Only look for missing fanart (No)
# or refresh all fanart (Yes)
self.fanartSync(refresh=dialog(
'yesno',
heading='{plex}',
line1=lang(39223),
nolabel=lang(39224),
yeslabel=lang(39225)))
elif state.RUN_LIB_SCAN == 'textures':
state.RUN_LIB_SCAN = None
state.DB_SCAN = True
window('plex_dbScan', value="true")
import artwork
artwork.Artwork().fullTextureCacheSync()
window('plex_dbScan', clear=True)
state.DB_SCAN = False
else:
raise NotImplementedError('Library scan not defined: %s'
% state.RUN_LIB_SCAN)
def run(self): def run(self):
try: try:
self.run_internal() self.run_internal()
@ -1522,115 +1585,62 @@ class LibrarySync(Thread):
# Currently no db scan, so we can start a new scan # Currently no db scan, so we can start a new scan
elif state.DB_SCAN is False: elif state.DB_SCAN is False:
# Force-show dialogs since they are user-initiated
self.force_dialog = True
# Full scan was requested from somewhere else, e.g. userclient # Full scan was requested from somewhere else, e.g. userclient
if window('plex_runLibScan') in ("full", "repair"): if state.RUN_LIB_SCAN is not None:
log.info('Full library scan requested, starting') # Force-show dialogs since they are user-initiated
window('plex_dbScan', value="true") self.force_dialog = True
self.triage_lib_scans()
self.force_dialog = False
continue
now = getUnixTimestamp()
# Standard syncs - don't force-show dialogs
self.force_dialog = False
if (now - lastSync > fullSyncInterval and
not self.xbmcplayer.isPlaying()):
lastSync = now
log.info('Doing scheduled full library scan')
state.DB_SCAN = True state.DB_SCAN = True
if window('plex_runLibScan') == "full": window('plex_dbScan', value="true")
fullSync() if fullSync() is False and not thread_stopped():
elif window('plex_runLibScan') == "repair": log.error('Could not finish scheduled full sync')
fullSync(repair=True) self.force_dialog = True
window('plex_runLibScan', clear=True) self.showKodiNote(lang(39410),
icon='error')
self.force_dialog = False
window('plex_dbScan', clear=True) window('plex_dbScan', clear=True)
state.DB_SCAN = False state.DB_SCAN = False
# Full library sync finished # Full library sync finished
self.showKodiNote(lang(39407)) self.showKodiNote(lang(39407))
# Reset views was requested from somewhere else elif now - lastTimeSync > oneDay:
elif window('plex_runLibScan') == "views": lastTimeSync = now
log.info('Refresh playlist and nodes requested, starting') log.info('Starting daily time sync')
window('plex_dbScan', value="true")
state.DB_SCAN = True
window('plex_runLibScan', clear=True)
# First remove playlists
deletePlaylists()
# Remove video nodes
deleteNodes()
# Kick off refresh
if self.maintainViews() is True:
# Ran successfully
log.info("Refresh playlists/nodes completed")
# "Plex playlists/nodes refreshed"
self.showKodiNote(lang(39405))
else:
# Failed
log.error("Refresh playlists/nodes failed")
# "Plex playlists/nodes refresh failed"
self.showKodiNote(lang(39406),
icon="error")
window('plex_dbScan', clear=True)
state.DB_SCAN = False
elif window('plex_runLibScan') == 'fanart':
window('plex_runLibScan', clear=True)
# Only look for missing fanart (No)
# or refresh all fanart (Yes)
self.fanartSync(refresh=dialog(
'yesno',
heading='{plex}',
line1=lang(39223),
nolabel=lang(39224),
yeslabel=lang(39225)))
elif window('plex_runLibScan') == 'del_textures':
window('plex_runLibScan', clear=True)
state.DB_SCAN = True state.DB_SCAN = True
window('plex_dbScan', value="true") window('plex_dbScan', value="true")
import artwork self.syncPMStime()
artwork.Artwork().fullTextureCacheSync()
window('plex_dbScan', clear=True) window('plex_dbScan', clear=True)
state.DB_SCAN = False state.DB_SCAN = False
else: elif enableBackgroundSync:
now = getUnixTimestamp() # Check back whether we should process something
# Standard syncs - don't force-show dialogs # Only do this once every while (otherwise, potentially
self.force_dialog = False # many screen refreshes lead to flickering)
if (now - lastSync > fullSyncInterval and if now - lastProcessing > 5:
not self.xbmcplayer.isPlaying()): lastProcessing = now
lastSync = now processItems()
log.info('Doing scheduled full library scan') # See if there is a PMS message we need to handle
state.DB_SCAN = True try:
window('plex_dbScan', value="true") message = queue.get(block=False)
if fullSync() is False and not thread_stopped(): except Queue.Empty:
log.error('Could not finish scheduled full sync')
self.force_dialog = True
self.showKodiNote(lang(39410),
icon='error')
self.force_dialog = False
window('plex_dbScan', clear=True)
state.DB_SCAN = False
# Full library sync finished
self.showKodiNote(lang(39407))
elif now - lastTimeSync > oneDay:
lastTimeSync = now
log.info('Starting daily time sync')
state.DB_SCAN = True
window('plex_dbScan', value="true")
self.syncPMStime()
window('plex_dbScan', clear=True)
state.DB_SCAN = False
elif enableBackgroundSync:
# Check back whether we should process something
# Only do this once every while (otherwise, potentially
# many screen refreshes lead to flickering)
if now - lastProcessing > 5:
lastProcessing = now
processItems()
# See if there is a PMS message we need to handle
try:
message = queue.get(block=False)
except Queue.Empty:
xbmc.sleep(100)
continue
# Got a message from PMS; process it
else:
processMessage(message)
queue.task_done()
# NO sleep!
continue
else:
# Still sleep if backgroundsync disabled
xbmc.sleep(100) xbmc.sleep(100)
continue
# Got a message from PMS; process it
else:
processMessage(message)
queue.task_done()
# NO sleep!
continue
else:
# Still sleep if backgroundsync disabled
xbmc.sleep(100)
xbmc.sleep(100) xbmc.sleep(100)

View file

@ -26,6 +26,8 @@ RESTRICTED_USER = False
DIRECT_PATHS = False DIRECT_PATHS = False
# Shall we replace custom user ratings with the number of versions available? # Shall we replace custom user ratings with the number of versions available?
INDICATE_MEDIA_VERSIONS = False INDICATE_MEDIA_VERSIONS = False
# Do we need to run a special library scan?
RUN_LIB_SCAN = None
# Stemming from the PKC settings.xml # Stemming from the PKC settings.xml
# Shall we show Kodi dialogs when synching? # Shall we show Kodi dialogs when synching?

View file

@ -110,7 +110,7 @@ class Service():
"plex_dbCheck", "plex_kodiScan", "plex_dbCheck", "plex_kodiScan",
"plex_shouldStop", "plex_dbScan", "plex_shouldStop", "plex_dbScan",
"plex_initialScan", "plex_customplayqueue", "plex_playbackProps", "plex_initialScan", "plex_customplayqueue", "plex_playbackProps",
"plex_runLibScan", "pms_token", "plex_token", "pms_token", "plex_token",
"pms_server", "plex_machineIdentifier", "plex_servername", "pms_server", "plex_machineIdentifier", "plex_servername",
"plex_authenticated", "PlexUserImage", "useDirectPaths", "plex_authenticated", "PlexUserImage", "useDirectPaths",
"kodiplextimeoffset", "countError", "countUnauthorized", "kodiplextimeoffset", "countError", "countUnauthorized",