Added server online check
This commit is contained in:
parent
60390a2f24
commit
38d0d1c02b
4 changed files with 211 additions and 157 deletions
|
@ -151,6 +151,8 @@ class DownloadUtils():
|
||||||
timeout = self.timeout
|
timeout = self.timeout
|
||||||
default_link = ""
|
default_link = ""
|
||||||
|
|
||||||
|
try:
|
||||||
|
|
||||||
# If user is authenticated
|
# If user is authenticated
|
||||||
if (authenticate):
|
if (authenticate):
|
||||||
# Get requests session
|
# Get requests session
|
||||||
|
@ -207,7 +209,7 @@ class DownloadUtils():
|
||||||
# If user is not authenticated
|
# If user is not authenticated
|
||||||
elif not authenticate:
|
elif not authenticate:
|
||||||
|
|
||||||
self.logMsg("URL: %s" % url, 1)
|
self.logMsg("URL: %s" % url, 2)
|
||||||
header = self.getHeader(authenticate=False)
|
header = self.getHeader(authenticate=False)
|
||||||
verifyssl = False
|
verifyssl = False
|
||||||
|
|
||||||
|
@ -224,14 +226,11 @@ class DownloadUtils():
|
||||||
r = requests.post(url, json=postBody, headers=header, timeout=timeout, verify=verifyssl)
|
r = requests.post(url, json=postBody, headers=header, timeout=timeout, verify=verifyssl)
|
||||||
|
|
||||||
# Process the response
|
# Process the response
|
||||||
try:
|
|
||||||
r.raise_for_status()
|
|
||||||
|
|
||||||
if r.status_code == 204:
|
if r.status_code == 204:
|
||||||
# No response in body
|
# No body in the response
|
||||||
self.logMsg("====== 204 Success ======", 2)
|
self.logMsg("====== 204 Success ======", 2)
|
||||||
return default_link
|
return default_link
|
||||||
# Response code 200
|
|
||||||
elif r.status_code == requests.codes.ok:
|
elif r.status_code == requests.codes.ok:
|
||||||
try:
|
try:
|
||||||
# UTF-8 - JSON object
|
# UTF-8 - JSON object
|
||||||
|
@ -240,13 +239,19 @@ class DownloadUtils():
|
||||||
return r
|
return r
|
||||||
except:
|
except:
|
||||||
self.logMsg("Unable to convert the response for: %s" % url, 1)
|
self.logMsg("Unable to convert the response for: %s" % url, 1)
|
||||||
|
else:
|
||||||
|
r.raise_for_status()
|
||||||
|
|
||||||
return default_link
|
return default_link
|
||||||
|
|
||||||
# TO REVIEW EXCEPTIONS
|
# TO REVIEW EXCEPTIONS
|
||||||
except requests.exceptions.ConnectionError as e:
|
except requests.exceptions.ConnectionError as e:
|
||||||
|
# Make the addon aware of status
|
||||||
|
if WINDOW.getProperty("Server_online") != "false":
|
||||||
self.logMsg("Server unreachable at: %s" % url, 0)
|
self.logMsg("Server unreachable at: %s" % url, 0)
|
||||||
self.logMsg(e, 1)
|
self.logMsg(e, 2)
|
||||||
|
WINDOW.setProperty("Server_online", "false")
|
||||||
|
pass
|
||||||
|
|
||||||
except requests.exceptions.ConnectTimeout as e:
|
except requests.exceptions.ConnectTimeout as e:
|
||||||
self.logMsg("Server timeout at: %s" % url, 0)
|
self.logMsg("Server timeout at: %s" % url, 0)
|
||||||
|
|
|
@ -45,17 +45,17 @@ class UserClient(threading.Thread):
|
||||||
def __init__(self, *args):
|
def __init__(self, *args):
|
||||||
|
|
||||||
self.__dict__ = self._shared_state
|
self.__dict__ = self._shared_state
|
||||||
self.className = self.__class__.__name__
|
|
||||||
|
|
||||||
threading.Thread.__init__(self, *args)
|
threading.Thread.__init__(self, *args)
|
||||||
|
|
||||||
def logMsg(self, msg, lvl=1):
|
def logMsg(self, msg, lvl=1):
|
||||||
|
|
||||||
utils.logMsg("%s %s" % (self.addonName, self.className), str(msg), int(lvl))
|
className = self.__class__.__name__
|
||||||
|
utils.logMsg("%s %s" % (self.addonName, className), str(msg), int(lvl))
|
||||||
|
|
||||||
def getUsername(self):
|
def getUsername(self):
|
||||||
|
|
||||||
username = self.addon.getSetting('username')
|
addon = xbmcaddon.Addon(id=self.addonId)
|
||||||
|
username = addon.getSetting('username')
|
||||||
|
|
||||||
if (username == ""):
|
if (username == ""):
|
||||||
self.logMsg("No username saved.", 2)
|
self.logMsg("No username saved.", 2)
|
||||||
|
@ -90,7 +90,7 @@ class UserClient(threading.Thread):
|
||||||
def getServer(self, prefix=True):
|
def getServer(self, prefix=True):
|
||||||
|
|
||||||
# For https support
|
# For https support
|
||||||
addon = self.addon
|
addon = xbmcaddon.Addon(id=self.addonId)
|
||||||
HTTPS = addon.getSetting('https')
|
HTTPS = addon.getSetting('https')
|
||||||
host = addon.getSetting('ipaddress')
|
host = addon.getSetting('ipaddress')
|
||||||
port = addon.getSetting('port')
|
port = addon.getSetting('port')
|
||||||
|
@ -161,6 +161,9 @@ class UserClient(threading.Thread):
|
||||||
|
|
||||||
if (result != ""):
|
if (result != ""):
|
||||||
users = result
|
users = result
|
||||||
|
else:
|
||||||
|
# Server connection failed
|
||||||
|
return False
|
||||||
|
|
||||||
return users
|
return users
|
||||||
|
|
||||||
|
@ -226,9 +229,6 @@ class UserClient(threading.Thread):
|
||||||
users = self.getPublicUsers()
|
users = self.getPublicUsers()
|
||||||
password = ""
|
password = ""
|
||||||
|
|
||||||
'''if users == "":
|
|
||||||
self.WINDOW.setProperty("Server_status", "Stop")
|
|
||||||
return'''
|
|
||||||
# Find user in list
|
# Find user in list
|
||||||
for user in users:
|
for user in users:
|
||||||
name = user[u'Name']
|
name = user[u'Name']
|
||||||
|
|
|
@ -205,11 +205,15 @@ class WebSocketThread(threading.Thread):
|
||||||
LibrarySync().IncrementalSync(itemsToUpdate)
|
LibrarySync().IncrementalSync(itemsToUpdate)
|
||||||
|
|
||||||
def on_error(self, ws, error):
|
def on_error(self, ws, error):
|
||||||
self.logMsg("Error : " + str(error))
|
if "10061" in str(error):
|
||||||
|
# Server is offline
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.logMsg("Error: %s" % error, 1)
|
||||||
#raise
|
#raise
|
||||||
|
|
||||||
def on_close(self, ws):
|
def on_close(self, ws):
|
||||||
self.logMsg("Closed")
|
self.logMsg("Closed", 2)
|
||||||
|
|
||||||
def on_open(self, ws):
|
def on_open(self, ws):
|
||||||
pass
|
pass
|
||||||
|
@ -243,12 +247,19 @@ class WebSocketThread(threading.Thread):
|
||||||
self.client.on_open = self.on_open
|
self.client.on_open = self.on_open
|
||||||
|
|
||||||
while not self.KodiMonitor.abortRequested():
|
while not self.KodiMonitor.abortRequested():
|
||||||
self.logMsg("Client Starting")
|
|
||||||
self.client.run_forever()
|
self.client.run_forever()
|
||||||
|
|
||||||
if (self.keepRunning):
|
if (self.keepRunning):
|
||||||
self.logMsg("Client Needs To Restart")
|
# Server is not online
|
||||||
|
if WINDOW.getProperty("Server_online") == "true":
|
||||||
|
self.logMsg("Server is unreachable.", 1)
|
||||||
|
WINDOW.setProperty("Server_online", "false")
|
||||||
|
xbmcgui.Dialog().notification("Error connecting", "Server is unreachable.")
|
||||||
|
|
||||||
if self.KodiMonitor.waitForAbort(5):
|
if self.KodiMonitor.waitForAbort(5):
|
||||||
break
|
break
|
||||||
|
|
||||||
self.logMsg("Thread Exited")
|
self.logMsg("Thread Exited")
|
||||||
|
|
||||||
|
|
||||||
|
|
64
service.py
64
service.py
|
@ -31,7 +31,10 @@ class Service():
|
||||||
|
|
||||||
clientInfo = ClientInformation()
|
clientInfo = ClientInformation()
|
||||||
addonName = clientInfo.getAddonName()
|
addonName = clientInfo.getAddonName()
|
||||||
className = None
|
WINDOW = xbmcgui.Window(10000)
|
||||||
|
|
||||||
|
warn_auth = True
|
||||||
|
server_online = True
|
||||||
|
|
||||||
def __init__(self, *args ):
|
def __init__(self, *args ):
|
||||||
self.KodiMonitor = KodiMonitor.Kodi_Monitor()
|
self.KodiMonitor = KodiMonitor.Kodi_Monitor()
|
||||||
|
@ -41,21 +44,21 @@ class Service():
|
||||||
self.logMsg("======== START %s ========" % addonName, 0)
|
self.logMsg("======== START %s ========" % addonName, 0)
|
||||||
self.logMsg("KODI Version: %s" % xbmc.getInfoLabel("System.BuildVersion"), 0)
|
self.logMsg("KODI Version: %s" % xbmc.getInfoLabel("System.BuildVersion"), 0)
|
||||||
self.logMsg("%s Version: %s" % (addonName, self.clientInfo.getVersion()), 0)
|
self.logMsg("%s Version: %s" % (addonName, self.clientInfo.getVersion()), 0)
|
||||||
|
self.logMsg("Platform: %s" % (self.clientInfo.getPlatform()), 0)
|
||||||
|
|
||||||
def logMsg(self, msg, lvl=1):
|
def logMsg(self, msg, lvl=1):
|
||||||
|
|
||||||
self.className = self.__class__.__name__
|
className = self.__class__.__name__
|
||||||
utils.logMsg("%s %s" % (self.addonName, self.className), str(msg), int(lvl))
|
utils.logMsg("%s %s" % (self.addonName, className), str(msg), int(lvl))
|
||||||
|
|
||||||
def ServiceEntryPoint(self):
|
def ServiceEntryPoint(self):
|
||||||
|
|
||||||
|
WINDOW = self.WINDOW
|
||||||
|
WINDOW.setProperty("Server_online", "")
|
||||||
|
|
||||||
ConnectionManager().checkServer()
|
ConnectionManager().checkServer()
|
||||||
|
|
||||||
lastProgressUpdate = datetime.today()
|
lastProgressUpdate = datetime.today()
|
||||||
|
|
||||||
startupComplete = False
|
startupComplete = False
|
||||||
WINDOW = xbmcgui.Window(10000)
|
|
||||||
|
|
||||||
|
|
||||||
user = UserClient()
|
user = UserClient()
|
||||||
player = Player()
|
player = Player()
|
||||||
|
@ -69,6 +72,8 @@ class Service():
|
||||||
# Abort was requested while waiting. We should exit
|
# Abort was requested while waiting. We should exit
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if WINDOW.getProperty('Server_online') == "true":
|
||||||
|
# Server is online
|
||||||
if xbmc.Player().isPlaying():
|
if xbmc.Player().isPlaying():
|
||||||
try:
|
try:
|
||||||
playTime = xbmc.Player().getTime()
|
playTime = xbmc.Player().getTime()
|
||||||
|
@ -97,11 +102,9 @@ class Service():
|
||||||
self.logMsg("Exception in Playback Monitor Service: %s" % e)
|
self.logMsg("Exception in Playback Monitor Service: %s" % e)
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if (self.newUserClient == None):
|
|
||||||
self.newUserClient = "Started"
|
|
||||||
user.start()
|
|
||||||
# background worker for database sync
|
# background worker for database sync
|
||||||
if (user.currUser != None):
|
if (user.currUser != None):
|
||||||
|
self.warn_auth = True
|
||||||
|
|
||||||
# Correctly launch the websocket, if user manually launches the add-on
|
# Correctly launch the websocket, if user manually launches the add-on
|
||||||
if (self.newWebSocketThread == None):
|
if (self.newWebSocketThread == None):
|
||||||
|
@ -123,12 +126,46 @@ class Service():
|
||||||
WebSocketThread().processPendingActions()
|
WebSocketThread().processPendingActions()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.logMsg("Not authenticated yet", 0)
|
if self.warn_auth:
|
||||||
|
self.logMsg("Not authenticated yet.", 1)
|
||||||
|
self.warn_auth = False
|
||||||
|
else:
|
||||||
|
# Wait until server becomes online or shut down is requested
|
||||||
|
while not self.KodiMonitor.abortRequested():
|
||||||
|
|
||||||
self.logMsg("stopping Service", 0)
|
if user.getServer() == "":
|
||||||
|
pass
|
||||||
|
elif not user.getPublicUsers():
|
||||||
|
# Server is not online, suppress future warning
|
||||||
|
if self.server_online:
|
||||||
|
WINDOW.setProperty("Server_online", "false")
|
||||||
|
self.logMsg("Server is offline.", 1)
|
||||||
|
xbmcgui.Dialog().notification("Error connecting", "%s Server is unreachable." % self.addonName)
|
||||||
|
self.server_online = False
|
||||||
|
else:
|
||||||
|
# Server is online
|
||||||
|
if not self.server_online:
|
||||||
|
# Server was not online when Kodi started.
|
||||||
|
# Wait for server to be fully established.
|
||||||
|
if self.KodiMonitor.waitForAbort(5):
|
||||||
|
# Abort was requested while waiting.
|
||||||
|
break
|
||||||
|
self.server_online = True
|
||||||
|
self.logMsg("Server is online and ready.", 1)
|
||||||
|
xbmcgui.Dialog().notification("Connection successful", "%s Server is online." % self.addonName, time=2000)
|
||||||
|
WINDOW.setProperty("Server_online", "true")
|
||||||
|
|
||||||
|
# Server is online, proceed.
|
||||||
|
if (self.newUserClient == None):
|
||||||
|
self.newUserClient = "Started"
|
||||||
|
user.start()
|
||||||
|
break
|
||||||
|
|
||||||
|
if self.KodiMonitor.waitForAbort(1):
|
||||||
|
# Abort was requested while waiting.
|
||||||
|
break
|
||||||
|
|
||||||
# If user reset library database.
|
# If user reset library database.
|
||||||
WINDOW = xbmcgui.Window(10000)
|
|
||||||
if WINDOW.getProperty("SyncInstallRunDone") == "false":
|
if WINDOW.getProperty("SyncInstallRunDone") == "false":
|
||||||
addon = xbmcaddon.Addon('plugin.video.emby')
|
addon = xbmcaddon.Addon('plugin.video.emby')
|
||||||
addon.setSetting("SyncInstallRunDone", "false")
|
addon.setSetting("SyncInstallRunDone", "false")
|
||||||
|
@ -139,6 +176,7 @@ class Service():
|
||||||
if (self.newUserClient != None):
|
if (self.newUserClient != None):
|
||||||
user.stopClient()
|
user.stopClient()
|
||||||
|
|
||||||
|
self.logMsg("======== STOP %s ========" % self.addonName, 0)
|
||||||
|
|
||||||
#start the service
|
#start the service
|
||||||
Service().ServiceEntryPoint()
|
Service().ServiceEntryPoint()
|
||||||
|
|
Loading…
Reference in a new issue