2016-09-21 07:39:50 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import sys
|
2016-10-13 00:47:43 +00:00
|
|
|
import time
|
2016-09-21 07:39:50 +00:00
|
|
|
import _strptime # Workaround for threads using datetime: _striptime is locked
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
|
|
|
|
import userclient
|
|
|
|
import clientinfo
|
|
|
|
import initialsetup
|
|
|
|
import kodimonitor
|
|
|
|
import librarysync
|
|
|
|
import player
|
|
|
|
import videonodes
|
|
|
|
import websocket_client as wsc
|
|
|
|
from utils import window, settings, dialog, language as lang
|
2016-10-09 09:14:45 +00:00
|
|
|
from ga_client import GoogleAnalytics
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
log = logging.getLogger("EMBY."+__name__)
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class Service(object):
|
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
startup = False
|
2016-09-21 07:39:50 +00:00
|
|
|
server_online = True
|
|
|
|
warn_auth = True
|
|
|
|
|
|
|
|
userclient_running = False
|
2016-09-27 02:50:58 +00:00
|
|
|
userclient_thread = None
|
2016-09-21 07:39:50 +00:00
|
|
|
websocket_running = False
|
2016-09-27 02:50:58 +00:00
|
|
|
websocket_thread = None
|
2016-09-21 07:39:50 +00:00
|
|
|
library_running = False
|
2016-09-27 02:50:58 +00:00
|
|
|
library_thread = None
|
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
last_progress = datetime.today()
|
2016-10-13 00:47:43 +00:00
|
|
|
lastMetricPing = time.time()
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
self.client_info = clientinfo.ClientInfo()
|
|
|
|
self.addon_name = self.client_info.get_addon_name()
|
|
|
|
log_level = settings('logLevel')
|
|
|
|
|
|
|
|
window('emby_logLevel', value=str(log_level))
|
|
|
|
window('emby_kodiProfile', value=xbmc.translatePath('special://profile'))
|
|
|
|
context_menu = "true" if settings('enableContext') == "true" else ""
|
|
|
|
window('emby_context', value=context_menu)
|
|
|
|
|
|
|
|
# Initial logging
|
|
|
|
log.warn("======== START %s ========", self.addon_name)
|
|
|
|
log.warn("Python Version: %s", sys.version)
|
|
|
|
log.warn("Platform: %s", self.client_info.get_platform())
|
|
|
|
log.warn("KODI Version: %s", xbmc.getInfoLabel('System.BuildVersion'))
|
|
|
|
log.warn("%s Version: %s", self.addon_name, self.client_info.get_version())
|
|
|
|
log.warn("Using plugin paths: %s", settings('useDirectPaths') == "0")
|
|
|
|
log.warn("Log Level: %s", log_level)
|
|
|
|
|
|
|
|
# Reset window props for profile switch
|
|
|
|
properties = [
|
|
|
|
|
2016-10-21 04:37:58 +00:00
|
|
|
"emby_online", "emby_state.json", "emby_serverStatus", "emby_onWake",
|
2016-09-21 07:39:50 +00:00
|
|
|
"emby_syncRunning", "emby_dbCheck", "emby_kodiScan",
|
|
|
|
"emby_shouldStop", "emby_currUser", "emby_dbScan", "emby_sessionId",
|
|
|
|
"emby_initialScan", "emby_customplaylist", "emby_playbackProps"
|
|
|
|
]
|
|
|
|
for prop in properties:
|
|
|
|
window(prop, clear=True)
|
|
|
|
|
|
|
|
# Clear video nodes properties
|
|
|
|
videonodes.VideoNodes().clearProperties()
|
|
|
|
|
|
|
|
# Set the minimum database version
|
|
|
|
window('emby_minDBVersion', value="1.1.63")
|
|
|
|
|
|
|
|
|
|
|
|
def service_entry_point(self):
|
|
|
|
# Important: Threads depending on abortRequest will not trigger
|
|
|
|
# if profile switch happens more than once.
|
2016-09-27 02:50:58 +00:00
|
|
|
self.monitor = kodimonitor.KodiMonitor()
|
2016-10-07 04:15:23 +00:00
|
|
|
self.kodi_player = player.Player()
|
|
|
|
kodi_profile = xbmc.translatePath('special://profile')
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
# Server auto-detect
|
|
|
|
initialsetup.InitialSetup().setup()
|
|
|
|
|
|
|
|
# Initialize important threads
|
2016-09-27 02:50:58 +00:00
|
|
|
self.userclient_thread = userclient.UserClient()
|
2016-10-07 04:15:23 +00:00
|
|
|
user_client = self.userclient_thread
|
2016-09-27 02:50:58 +00:00
|
|
|
self.websocket_thread = wsc.WebSocketClient()
|
|
|
|
self.library_thread = librarysync.LibrarySync()
|
2016-10-07 04:15:23 +00:00
|
|
|
|
2016-09-21 07:39:50 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
while not self.monitor.abortRequested():
|
2016-09-21 07:39:50 +00:00
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
if window('emby_kodiProfile') != kodi_profile:
|
2016-09-21 07:39:50 +00:00
|
|
|
# Profile change happened, terminate this thread and others
|
|
|
|
log.info("Kodi profile was: %s and changed to: %s. Terminating old Emby thread.",
|
2016-10-07 04:15:23 +00:00
|
|
|
kodi_profile, window('emby_kodiProfile'))
|
|
|
|
raise RuntimeError("Kodi profile changed detected")
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
# Before proceeding, need to make sure:
|
|
|
|
# 1. Server is online
|
|
|
|
# 2. User is set
|
|
|
|
# 3. User has access to the server
|
|
|
|
|
|
|
|
if window('emby_online') == "true":
|
|
|
|
|
|
|
|
# Emby server is online
|
|
|
|
# Verify if user is set and has access to the server
|
2016-10-07 04:15:23 +00:00
|
|
|
if user_client.get_user() is not None and user_client.get_access():
|
|
|
|
|
|
|
|
# If an item is playing
|
|
|
|
if self.kodi_player.isPlaying():
|
2016-10-14 04:02:40 +00:00
|
|
|
# ping metrics server to keep sessions alive while playing
|
|
|
|
# ping every 5 min
|
|
|
|
timeSinceLastPing = time.time() - self.lastMetricPing
|
|
|
|
if(timeSinceLastPing > 300):
|
|
|
|
self.lastMetricPing = time.time()
|
|
|
|
ga = GoogleAnalytics()
|
|
|
|
ga.sendEventData("PlayAction", "PlayPing")
|
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
self._report_progress()
|
|
|
|
|
|
|
|
elif not self.startup:
|
|
|
|
self.startup = self._startup()
|
2016-09-21 07:39:50 +00:00
|
|
|
else:
|
2016-10-07 04:15:23 +00:00
|
|
|
|
|
|
|
if (user_client.get_user() is None) and self.warn_auth:
|
2016-09-21 07:39:50 +00:00
|
|
|
# Alert user is not authenticated and suppress future warning
|
|
|
|
self.warn_auth = False
|
|
|
|
log.info("Not authenticated yet.")
|
|
|
|
|
|
|
|
# User access is restricted.
|
|
|
|
# Keep verifying until access is granted
|
|
|
|
# unless server goes offline or Kodi is shut down.
|
2016-10-07 04:15:23 +00:00
|
|
|
self._access_check()
|
2016-09-21 07:39:50 +00:00
|
|
|
else:
|
|
|
|
# Wait until Emby server is online
|
|
|
|
# or Kodi is shut down.
|
2016-09-27 02:50:58 +00:00
|
|
|
self._server_online_check()
|
2016-10-13 00:47:43 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
if self.monitor.waitForAbort(1):
|
|
|
|
# Abort was requested while waiting. We should exit
|
|
|
|
break
|
2016-09-21 07:39:50 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
##### Emby thread is terminating. #####
|
2016-10-07 04:15:23 +00:00
|
|
|
self.shutdown()
|
|
|
|
|
|
|
|
def _startup(self):
|
2016-10-10 11:14:10 +00:00
|
|
|
|
2016-10-09 09:14:45 +00:00
|
|
|
ga = GoogleAnalytics()
|
|
|
|
ga.sendEventData("Application", "Startup")
|
2016-10-10 11:14:10 +00:00
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
# Start up events
|
|
|
|
self.warn_auth = True
|
|
|
|
|
2016-10-20 07:39:37 +00:00
|
|
|
username = self.userclient_thread.get_username()
|
|
|
|
if settings('connectMsg') == "true" and username:
|
2016-10-07 04:15:23 +00:00
|
|
|
# Get additional users
|
|
|
|
add_users = ", ".join(settings('additionalUsers').split(','))
|
|
|
|
|
|
|
|
dialog(type_="notification",
|
|
|
|
heading="{emby}",
|
2016-10-12 10:21:39 +00:00
|
|
|
message=("%s %s %s"
|
2016-10-20 07:39:37 +00:00
|
|
|
% (lang(33000), username.decode('utf-8'),
|
2016-10-07 04:15:23 +00:00
|
|
|
add_users.decode('utf-8'))),
|
|
|
|
icon="{emby}",
|
|
|
|
time=2000,
|
|
|
|
sound=False)
|
|
|
|
|
|
|
|
# Start the Websocket Client
|
|
|
|
self.websocket_running = True
|
|
|
|
self.websocket_thread.start()
|
|
|
|
# Start the syncing thread
|
|
|
|
self.library_running = True
|
|
|
|
self.library_thread.start()
|
|
|
|
|
|
|
|
return True
|
2016-09-27 02:50:58 +00:00
|
|
|
|
|
|
|
def _server_online_check(self):
|
|
|
|
# Set emby_online true/false property
|
2016-10-07 04:15:23 +00:00
|
|
|
user_client = self.userclient_thread
|
2016-09-27 02:50:58 +00:00
|
|
|
while not self.monitor.abortRequested():
|
2016-10-07 04:15:23 +00:00
|
|
|
|
|
|
|
if user_client.get_server() is None:
|
2016-09-27 02:50:58 +00:00
|
|
|
# No server info set in add-on settings
|
|
|
|
pass
|
2016-10-07 04:15:23 +00:00
|
|
|
|
|
|
|
elif not user_client.verify_server():
|
2016-09-27 02:50:58 +00:00
|
|
|
# Server is offline.
|
|
|
|
# Alert the user and suppress future warning
|
|
|
|
if self.server_online:
|
|
|
|
log.info("Server is offline")
|
|
|
|
window('emby_online', value="false")
|
|
|
|
|
|
|
|
if settings('offlineMsg') == "true":
|
|
|
|
dialog(type_="notification",
|
|
|
|
heading=lang(33001),
|
|
|
|
message="%s %s" % (self.addon_name, lang(33002)),
|
|
|
|
icon="{emby}",
|
|
|
|
sound=False)
|
|
|
|
|
|
|
|
self.server_online = False
|
|
|
|
|
|
|
|
elif window('emby_online') == "sleep":
|
|
|
|
# device going to sleep
|
|
|
|
if self.websocket_running:
|
|
|
|
self.websocket_thread.stop_client()
|
|
|
|
self.websocket_thread = wsc.WebSocketClient()
|
|
|
|
self.websocket_running = False
|
|
|
|
|
|
|
|
if self.library_running:
|
|
|
|
self.library_thread.stopThread()
|
|
|
|
self.library_thread = librarysync.LibrarySync()
|
|
|
|
self.library_running = False
|
2016-09-21 07:39:50 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
else:
|
|
|
|
# Server is online
|
|
|
|
if not self.server_online:
|
|
|
|
# Server was offline when Kodi started.
|
|
|
|
# Wait for server to be fully established.
|
|
|
|
if self.monitor.waitForAbort(5):
|
2016-09-21 07:39:50 +00:00
|
|
|
# Abort was requested while waiting.
|
|
|
|
break
|
2016-09-27 02:50:58 +00:00
|
|
|
# Alert the user that server is online.
|
|
|
|
dialog(type_="notification",
|
|
|
|
heading="{emby}",
|
|
|
|
message=lang(33003),
|
|
|
|
icon="{emby}",
|
|
|
|
time=2000,
|
|
|
|
sound=False)
|
|
|
|
|
|
|
|
self.server_online = True
|
|
|
|
window('emby_online', value="true")
|
|
|
|
log.info("Server is online and ready")
|
|
|
|
|
|
|
|
# Start the userclient thread
|
|
|
|
if not self.userclient_running:
|
|
|
|
self.userclient_running = True
|
2016-10-07 04:15:23 +00:00
|
|
|
user_client.start()
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
break
|
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
if self.monitor.waitForAbort(1):
|
|
|
|
# Abort was requested while waiting.
|
|
|
|
break
|
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
def _access_check(self):
|
|
|
|
# Keep verifying until access is granted
|
|
|
|
# unless server goes offline or Kodi is shut down.
|
|
|
|
while not self.userclient_thread.get_access():
|
|
|
|
|
|
|
|
if window('emby_online') != "true":
|
|
|
|
# Server went offline
|
|
|
|
break
|
|
|
|
|
|
|
|
if self.monitor.waitForAbort(5):
|
|
|
|
# Abort was requested while waiting. We should exit
|
|
|
|
break
|
|
|
|
|
|
|
|
def _report_progress(self):
|
|
|
|
# Update and report playback progress
|
|
|
|
kodi_player = self.kodi_player
|
|
|
|
try:
|
|
|
|
play_time = kodi_player.getTime()
|
|
|
|
filename = kodi_player.currentFile
|
|
|
|
|
|
|
|
# Update positionticks
|
|
|
|
if filename in kodi_player.played_info:
|
|
|
|
kodi_player.played_info[filename]['currentPosition'] = play_time
|
|
|
|
|
|
|
|
difference = datetime.today() - self.last_progress
|
|
|
|
difference_seconds = difference.seconds
|
|
|
|
|
|
|
|
# Report progress to Emby server
|
|
|
|
if difference_seconds > 3:
|
|
|
|
kodi_player.reportPlayback()
|
|
|
|
self.last_progress = datetime.today()
|
|
|
|
|
|
|
|
elif window('emby_command') == "true":
|
|
|
|
# Received a remote control command that
|
|
|
|
# requires updating immediately
|
|
|
|
window('emby_command', clear=True)
|
|
|
|
kodi_player.reportPlayback()
|
|
|
|
self.last_progress = datetime.today()
|
|
|
|
|
|
|
|
except Exception as error:
|
|
|
|
log.exception(error)
|
|
|
|
|
|
|
|
def shutdown(self):
|
2016-10-10 11:14:10 +00:00
|
|
|
|
2016-10-14 03:35:34 +00:00
|
|
|
#ga = GoogleAnalytics()
|
|
|
|
#ga.sendEventData("Application", "Shutdown")
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
if self.userclient_running:
|
2016-09-27 02:50:58 +00:00
|
|
|
self.userclient_thread.stop_client()
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
if self.library_running:
|
2016-09-27 02:50:58 +00:00
|
|
|
self.library_thread.stopThread()
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
if self.websocket_running:
|
2016-09-27 02:50:58 +00:00
|
|
|
self.websocket_thread.stop_client()
|
2016-09-21 07:39:50 +00:00
|
|
|
|
|
|
|
log.warn("======== STOP %s ========", self.addon_name)
|