2016-01-23 01:37:20 +11:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import threading
|
|
|
|
import traceback
|
|
|
|
import socket
|
|
|
|
import requests
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
|
|
|
|
import clientinfo
|
|
|
|
import utils
|
|
|
|
from plexbmchelper import listener, plexgdm, subscribers
|
|
|
|
from plexbmchelper.settings import settings
|
|
|
|
|
|
|
|
|
2016-01-27 03:20:13 +11:00
|
|
|
@utils.logging
|
2016-01-27 01:13:03 +11:00
|
|
|
@utils.ThreadMethods
|
2016-01-23 01:37:20 +11:00
|
|
|
class PlexCompanion(threading.Thread):
|
|
|
|
def __init__(self):
|
|
|
|
self.port = int(utils.settings('companionPort'))
|
|
|
|
ci = clientinfo.ClientInfo()
|
|
|
|
self.clientId = ci.getDeviceId()
|
|
|
|
self.deviceName = ci.getDeviceName()
|
|
|
|
self.logMsg("----===## Starting PlexBMC Helper ##===----", 1)
|
|
|
|
|
|
|
|
# Start GDM for server/client discovery
|
|
|
|
self.client = plexgdm.plexgdm(debug=settings['gdm_debug'])
|
|
|
|
self.client.clientDetails(self.clientId, # UUID
|
|
|
|
self.deviceName, # clientName
|
|
|
|
self.port,
|
|
|
|
self.addonName,
|
|
|
|
'1.0') # Version
|
|
|
|
self.logMsg("Registration string is: %s "
|
|
|
|
% self.client.getClientDetails(), 1)
|
|
|
|
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
start_count = 0
|
2016-03-11 02:02:46 +11:00
|
|
|
window = utils.window
|
2016-01-23 01:37:20 +11:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
httpd = listener.ThreadedHTTPServer(
|
|
|
|
('', self.port),
|
|
|
|
listener.MyHandler)
|
|
|
|
httpd.timeout = 0.95
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
self.logMsg("Unable to start PlexCompanion. Traceback:", -1)
|
|
|
|
self.logMsg(traceback.print_exc(), -1)
|
|
|
|
|
|
|
|
xbmc.sleep(3000)
|
|
|
|
|
|
|
|
if start_count == 3:
|
|
|
|
self.logMsg("Error: Unable to start web helper.", -1)
|
|
|
|
httpd = False
|
|
|
|
break
|
|
|
|
|
|
|
|
start_count += 1
|
|
|
|
|
|
|
|
if not httpd:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.client.start_all()
|
|
|
|
message_count = 0
|
|
|
|
is_running = False
|
2016-01-27 01:13:03 +11:00
|
|
|
while not self.threadStopped():
|
|
|
|
while self.threadSuspended():
|
|
|
|
if self.threadStopped():
|
|
|
|
break
|
|
|
|
xbmc.sleep(3000)
|
2016-03-11 02:02:46 +11:00
|
|
|
# If we are not authorized, sleep
|
|
|
|
# Otherwise, we trigger a download which leads to a
|
|
|
|
# re-authorizations
|
|
|
|
if window('emby_serverStatus'):
|
|
|
|
xbmc.sleep(3000)
|
|
|
|
continue
|
2016-01-23 01:37:20 +11:00
|
|
|
try:
|
|
|
|
|
|
|
|
httpd.handle_request()
|
|
|
|
message_count += 1
|
|
|
|
|
|
|
|
if message_count > 30:
|
|
|
|
if self.client.check_client_registration():
|
|
|
|
self.logMsg("Client is still registered", 1)
|
|
|
|
else:
|
2016-01-24 01:53:24 +11:00
|
|
|
self.logMsg("Client is no longer registered", 1)
|
|
|
|
self.logMsg("PlexBMC Helper still running on port %s"
|
|
|
|
% self.port, 1)
|
2016-01-23 01:37:20 +11:00
|
|
|
message_count = 0
|
|
|
|
|
|
|
|
if not is_running:
|
|
|
|
self.logMsg("PleXBMC Helper has started", 0)
|
|
|
|
is_running = True
|
2016-02-07 22:38:50 +11:00
|
|
|
|
|
|
|
subscribers.subMgr.notify()
|
2016-01-23 01:37:20 +11:00
|
|
|
settings['serverList'] = self.client.getServerList()
|
2016-03-08 18:43:12 +11:00
|
|
|
xbmc.sleep(50)
|
2016-01-23 01:37:20 +11:00
|
|
|
except:
|
|
|
|
self.logMsg("Error in loop, continuing anyway", 1)
|
|
|
|
self.logMsg(traceback.print_exc(), 1)
|
2016-03-08 18:43:12 +11:00
|
|
|
xbmc.sleep(50)
|
2016-01-23 01:37:20 +11:00
|
|
|
|
|
|
|
self.client.stop_all()
|
|
|
|
try:
|
|
|
|
httpd.socket.shutdown(socket.SHUT_RDWR)
|
|
|
|
finally:
|
|
|
|
httpd.socket.close()
|
|
|
|
requests.dumpConnections()
|
|
|
|
self.logMsg("----===## STOP PlexBMC Helper ##===----", 0)
|