Added server online check

This commit is contained in:
angelblue05 2015-05-03 00:27:43 -05:00
parent 60390a2f24
commit 38d0d1c02b
4 changed files with 211 additions and 157 deletions

View file

@ -151,6 +151,8 @@ class DownloadUtils():
timeout = self.timeout
default_link = ""
try:
# If user is authenticated
if (authenticate):
# Get requests session
@ -207,7 +209,7 @@ class DownloadUtils():
# If user is not authenticated
elif not authenticate:
self.logMsg("URL: %s" % url, 1)
self.logMsg("URL: %s" % url, 2)
header = self.getHeader(authenticate=False)
verifyssl = False
@ -224,14 +226,11 @@ class DownloadUtils():
r = requests.post(url, json=postBody, headers=header, timeout=timeout, verify=verifyssl)
# Process the response
try:
r.raise_for_status()
if r.status_code == 204:
# No response in body
# No body in the response
self.logMsg("====== 204 Success ======", 2)
return default_link
# Response code 200
elif r.status_code == requests.codes.ok:
try:
# UTF-8 - JSON object
@ -240,13 +239,19 @@ class DownloadUtils():
return r
except:
self.logMsg("Unable to convert the response for: %s" % url, 1)
else:
r.raise_for_status()
return default_link
# TO REVIEW EXCEPTIONS
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(e, 1)
self.logMsg(e, 2)
WINDOW.setProperty("Server_online", "false")
pass
except requests.exceptions.ConnectTimeout as e:
self.logMsg("Server timeout at: %s" % url, 0)

View file

@ -45,17 +45,17 @@ class UserClient(threading.Thread):
def __init__(self, *args):
self.__dict__ = self._shared_state
self.className = self.__class__.__name__
threading.Thread.__init__(self, *args)
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):
username = self.addon.getSetting('username')
addon = xbmcaddon.Addon(id=self.addonId)
username = addon.getSetting('username')
if (username == ""):
self.logMsg("No username saved.", 2)
@ -90,7 +90,7 @@ class UserClient(threading.Thread):
def getServer(self, prefix=True):
# For https support
addon = self.addon
addon = xbmcaddon.Addon(id=self.addonId)
HTTPS = addon.getSetting('https')
host = addon.getSetting('ipaddress')
port = addon.getSetting('port')
@ -161,6 +161,9 @@ class UserClient(threading.Thread):
if (result != ""):
users = result
else:
# Server connection failed
return False
return users
@ -226,9 +229,6 @@ class UserClient(threading.Thread):
users = self.getPublicUsers()
password = ""
'''if users == "":
self.WINDOW.setProperty("Server_status", "Stop")
return'''
# Find user in list
for user in users:
name = user[u'Name']

View file

@ -205,11 +205,15 @@ class WebSocketThread(threading.Thread):
LibrarySync().IncrementalSync(itemsToUpdate)
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
def on_close(self, ws):
self.logMsg("Closed")
self.logMsg("Closed", 2)
def on_open(self, ws):
pass
@ -243,12 +247,19 @@ class WebSocketThread(threading.Thread):
self.client.on_open = self.on_open
while not self.KodiMonitor.abortRequested():
self.logMsg("Client Starting")
self.client.run_forever()
if(self.keepRunning):
self.logMsg("Client Needs To Restart")
if (self.keepRunning):
# 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):
break
self.logMsg("Thread Exited")

View file

@ -31,7 +31,10 @@ class Service():
clientInfo = ClientInformation()
addonName = clientInfo.getAddonName()
className = None
WINDOW = xbmcgui.Window(10000)
warn_auth = True
server_online = True
def __init__(self, *args ):
self.KodiMonitor = KodiMonitor.Kodi_Monitor()
@ -41,21 +44,21 @@ class Service():
self.logMsg("======== START %s ========" % addonName, 0)
self.logMsg("KODI Version: %s" % xbmc.getInfoLabel("System.BuildVersion"), 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):
self.className = self.__class__.__name__
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 ServiceEntryPoint(self):
WINDOW = self.WINDOW
WINDOW.setProperty("Server_online", "")
ConnectionManager().checkServer()
lastProgressUpdate = datetime.today()
startupComplete = False
WINDOW = xbmcgui.Window(10000)
user = UserClient()
player = Player()
@ -69,6 +72,8 @@ class Service():
# Abort was requested while waiting. We should exit
break
if WINDOW.getProperty('Server_online') == "true":
# Server is online
if xbmc.Player().isPlaying():
try:
playTime = xbmc.Player().getTime()
@ -97,11 +102,9 @@ class Service():
self.logMsg("Exception in Playback Monitor Service: %s" % e)
pass
else:
if (self.newUserClient == None):
self.newUserClient = "Started"
user.start()
# background worker for database sync
if (user.currUser != None):
self.warn_auth = True
# Correctly launch the websocket, if user manually launches the add-on
if (self.newWebSocketThread == None):
@ -109,12 +112,12 @@ class Service():
ws.start()
#full sync
if(startupComplete == False):
if (startupComplete == False):
self.logMsg("Doing_Db_Sync: syncDatabase (Started)")
libSync = librarySync.FullLibrarySync()
self.logMsg("Doing_Db_Sync: syncDatabase (Finished) " + str(libSync))
if(libSync):
if (libSync):
startupComplete = True
else:
if self.KodiMonitor.waitForAbort(1):
@ -123,12 +126,46 @@ class Service():
WebSocketThread().processPendingActions()
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.
WINDOW = xbmcgui.Window(10000)
if WINDOW.getProperty("SyncInstallRunDone") == "false":
addon = xbmcaddon.Addon('plugin.video.emby')
addon.setSetting("SyncInstallRunDone", "false")
@ -139,6 +176,7 @@ class Service():
if (self.newUserClient != None):
user.stopClient()
self.logMsg("======== STOP %s ========" % self.addonName, 0)
#start the service
Service().ServiceEntryPoint()