2015-08-05 08:40:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import json
|
|
|
|
import threading
|
|
|
|
import websocket
|
|
|
|
import logging
|
|
|
|
|
2015-03-23 04:54:16 +00:00
|
|
|
#################################################################################################
|
|
|
|
# WebSocket Client thread
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
|
2015-04-11 12:56:07 +00:00
|
|
|
import KodiMonitor
|
2015-04-28 22:23:26 +00:00
|
|
|
import Utils as utils
|
2015-03-23 04:54:16 +00:00
|
|
|
from ClientInformation import ClientInformation
|
|
|
|
from DownloadUtils import DownloadUtils
|
|
|
|
from PlaybackUtils import PlaybackUtils
|
2015-03-26 05:15:45 +00:00
|
|
|
from LibrarySync import LibrarySync
|
2015-05-07 09:45:24 +00:00
|
|
|
from WriteKodiVideoDB import WriteKodiVideoDB
|
2015-06-13 01:21:21 +00:00
|
|
|
from WriteKodiMusicDB import WriteKodiMusicDB
|
2015-03-23 04:54:16 +00:00
|
|
|
|
2015-05-22 01:41:11 +00:00
|
|
|
logging.basicConfig()
|
|
|
|
|
2015-03-23 04:54:16 +00:00
|
|
|
|
|
|
|
class WebSocketThread(threading.Thread):
|
|
|
|
|
2015-04-28 22:23:26 +00:00
|
|
|
_shared_state = {}
|
|
|
|
|
2015-05-14 04:48:35 +00:00
|
|
|
doUtils = DownloadUtils()
|
2015-04-28 22:23:26 +00:00
|
|
|
clientInfo = ClientInformation()
|
|
|
|
KodiMonitor = KodiMonitor.Kodi_Monitor()
|
2015-05-17 05:37:53 +00:00
|
|
|
|
2015-04-28 22:23:26 +00:00
|
|
|
addonName = clientInfo.getAddonName()
|
|
|
|
|
2015-03-23 04:54:16 +00:00
|
|
|
client = None
|
|
|
|
keepRunning = True
|
|
|
|
|
|
|
|
def __init__(self, *args):
|
2015-04-11 12:56:07 +00:00
|
|
|
|
2015-04-28 22:23:26 +00:00
|
|
|
self.__dict__ = self._shared_state
|
2015-03-23 04:54:16 +00:00
|
|
|
threading.Thread.__init__(self, *args)
|
|
|
|
|
2015-04-28 22:23:26 +00:00
|
|
|
def logMsg(self, msg, lvl=1):
|
2015-03-23 04:54:16 +00:00
|
|
|
|
2015-04-28 22:23:26 +00:00
|
|
|
self.className = self.__class__.__name__
|
|
|
|
utils.logMsg("%s %s" % (self.addonName, self.className), msg, int(lvl))
|
|
|
|
|
|
|
|
def sendProgressUpdate(self, data):
|
|
|
|
self.logMsg("sendProgressUpdate", 1)
|
|
|
|
if self.client:
|
2015-03-23 04:54:16 +00:00
|
|
|
try:
|
2015-04-28 22:23:26 +00:00
|
|
|
# Send progress update
|
|
|
|
messageData = {
|
|
|
|
'MessageType': "ReportPlaybackProgress",
|
|
|
|
'Data': data
|
|
|
|
}
|
2015-03-23 04:54:16 +00:00
|
|
|
messageString = json.dumps(messageData)
|
|
|
|
self.client.send(messageString)
|
2015-04-28 22:23:26 +00:00
|
|
|
self.logMsg("Message data: %s" % messageString, 2)
|
2015-08-05 08:40:54 +00:00
|
|
|
except Exception as e:
|
2015-04-28 22:23:26 +00:00
|
|
|
self.logMsg("Exception: %s" % e, 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
|
|
|
|
def stopClient(self):
|
|
|
|
# stopping the client is tricky, first set keep_running to false and then trigger one
|
|
|
|
# more message by requesting one SessionsStart message, this causes the
|
|
|
|
# client to receive the message and then exit
|
2015-08-05 08:40:54 +00:00
|
|
|
if self.client:
|
2015-03-23 04:54:16 +00:00
|
|
|
self.logMsg("Stopping Client")
|
|
|
|
self.keepRunning = False
|
|
|
|
self.client.keep_running = False
|
|
|
|
self.client.close()
|
|
|
|
self.logMsg("Stopping Client : KeepRunning set to False")
|
|
|
|
else:
|
|
|
|
self.logMsg("Stopping Client NO Object ERROR")
|
|
|
|
|
|
|
|
def on_message(self, ws, message):
|
2015-08-05 08:40:54 +00:00
|
|
|
|
|
|
|
WINDOW = xbmcgui.Window(10000)
|
|
|
|
addon = xbmcaddon.Addon()
|
|
|
|
self.logMsg("Message: %s" % message, 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
|
2015-08-05 08:40:54 +00:00
|
|
|
result = json.loads(message)
|
|
|
|
messageType = result['MessageType']
|
2015-03-23 04:54:16 +00:00
|
|
|
data = result.get("Data")
|
2015-05-16 06:31:08 +00:00
|
|
|
|
2015-08-05 08:40:54 +00:00
|
|
|
if messageType == "Play":
|
|
|
|
# A remote control play command has been sent from the server.
|
|
|
|
itemIds = data['ItemIds']
|
|
|
|
playCommand = data['PlayCommand']
|
|
|
|
|
|
|
|
if "PlayNow" in playCommand:
|
|
|
|
startPositionTicks = data.get('StartPositionTicks', 0)
|
2015-03-23 04:54:16 +00:00
|
|
|
xbmc.executebuiltin("Dialog.Close(all,true)")
|
2015-08-05 08:40:54 +00:00
|
|
|
xbmc.executebuiltin("XBMC.Notification(Playlist: Added %s items to Playlist,)" % len(itemIds))
|
2015-03-23 04:54:16 +00:00
|
|
|
PlaybackUtils().PLAYAllItems(itemIds, startPositionTicks)
|
2015-08-06 04:30:33 +00:00
|
|
|
|
2015-08-05 08:40:54 +00:00
|
|
|
elif "PlayNext" in playCommand:
|
|
|
|
xbmc.executebuiltin("XBMC.Notification(Playlist: Added %s items to Playlist,)" % len(itemIds))
|
2015-03-23 04:54:16 +00:00
|
|
|
playlist = PlaybackUtils().AddToPlaylist(itemIds)
|
2015-08-05 08:40:54 +00:00
|
|
|
if not xbmc.Player().isPlaying():
|
2015-03-23 04:54:16 +00:00
|
|
|
xbmc.Player().play(playlist)
|
2015-08-05 08:40:54 +00:00
|
|
|
|
|
|
|
elif messageType == "Playstate":
|
|
|
|
# A remote control update playstate command has been sent from the server.
|
|
|
|
command = data['Command']
|
|
|
|
|
|
|
|
if "Stop" in command:
|
|
|
|
self.logMsg("Playback Stopped.", 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
xbmc.Player().stop()
|
2015-08-05 08:40:54 +00:00
|
|
|
elif "Unpause" in command:
|
|
|
|
self.logMsg("Playback unpaused.", 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
xbmc.Player().pause()
|
2015-08-05 08:40:54 +00:00
|
|
|
elif "Pause" in command:
|
|
|
|
self.logMsg("Playback paused.", 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
xbmc.Player().pause()
|
2015-08-05 08:40:54 +00:00
|
|
|
elif "NextTrack" in command:
|
|
|
|
self.logMsg("Playback next track.", 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
xbmc.Player().playnext()
|
2015-08-05 08:40:54 +00:00
|
|
|
elif "PreviousTrack" in command:
|
|
|
|
self.logMsg("Playback previous track.", 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
xbmc.Player().playprevious()
|
2015-08-05 08:40:54 +00:00
|
|
|
elif "Seek" in command:
|
|
|
|
seekPositionTicks = data['SeekPositionTicks']
|
|
|
|
seekTime = seekPositionTicks / 10000000.0
|
|
|
|
self.logMsg("Seek to %s" % seekTime, 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
xbmc.Player().seekTime(seekTime)
|
2015-07-20 06:36:58 +00:00
|
|
|
# Report playback
|
2015-08-05 08:40:54 +00:00
|
|
|
WINDOW.setProperty('commandUpdate', "true")
|
|
|
|
|
|
|
|
elif messageType == "UserDataChanged":
|
|
|
|
# A user changed their personal rating for an item, or their playstate was updated
|
|
|
|
userdataList = data['UserDataList']
|
|
|
|
self.logMsg("Message: Doing UserDataChanged: UserDataList: %s" % userdataList, 1)
|
|
|
|
LibrarySync().user_data_update(userdataList)
|
|
|
|
|
|
|
|
elif messageType == "LibraryChanged":
|
|
|
|
# Library items
|
2015-04-03 02:12:09 +00:00
|
|
|
itemsRemoved = data.get("ItemsRemoved")
|
|
|
|
itemsAdded = data.get("ItemsAdded")
|
|
|
|
itemsUpdated = data.get("ItemsUpdated")
|
|
|
|
itemsToUpdate = itemsAdded + itemsUpdated
|
2015-08-05 08:40:54 +00:00
|
|
|
|
|
|
|
self.logMsg("Message: WebSocket LibraryChanged: Items Added: %s" % itemsAdded, 1)
|
|
|
|
self.logMsg("Message: WebSocket LibraryChanged: Items Updated: %s" % itemsUpdated, 1)
|
|
|
|
self.logMsg("Message: WebSocket LibraryChanged: Items Removed: %s" % itemsRemoved, 1)
|
2015-04-18 17:10:06 +00:00
|
|
|
|
2015-06-28 12:08:06 +00:00
|
|
|
LibrarySync().remove_items(itemsRemoved)
|
|
|
|
LibrarySync().update_items(itemsToUpdate)
|
2015-04-18 17:10:06 +00:00
|
|
|
|
2015-05-09 07:17:58 +00:00
|
|
|
elif messageType == "GeneralCommand":
|
|
|
|
|
2015-08-05 08:40:54 +00:00
|
|
|
command = data['Name']
|
2015-05-16 06:31:08 +00:00
|
|
|
arguments = data.get("Arguments")
|
|
|
|
|
2015-08-06 04:30:33 +00:00
|
|
|
if command in ('Mute', 'Unmute', 'SetVolume', 'SetSubtitleStreamIndex', 'SetAudioStreamIndex'):
|
2015-05-16 06:31:08 +00:00
|
|
|
# These commands need to be reported back
|
|
|
|
if command == "Mute":
|
|
|
|
xbmc.executebuiltin('Mute')
|
|
|
|
elif command == "Unmute":
|
|
|
|
xbmc.executebuiltin('Mute')
|
|
|
|
elif command == "SetVolume":
|
2015-08-05 08:40:54 +00:00
|
|
|
volume = arguments['Volume']
|
2015-05-16 06:31:08 +00:00
|
|
|
xbmc.executebuiltin('SetVolume(%s[,showvolumebar])' % volume)
|
2015-08-06 04:30:33 +00:00
|
|
|
elif command == "SetSubtitleStreamIndex":
|
|
|
|
# Emby merges audio and subtitle index together
|
|
|
|
audioTracks = len(xbmc.Player().getAvailableAudioStreams())
|
|
|
|
index = int(arguments['Index']) - audioTracks
|
|
|
|
xbmc.Player().setSubtitleStream(index - 1)
|
|
|
|
elif command == "SetAudioStreamIndex":
|
|
|
|
index = int(arguments['Index'])
|
|
|
|
xbmc.Player().setAudioStream(index - 1)
|
2015-05-16 06:31:08 +00:00
|
|
|
# Report playback
|
2015-08-05 08:40:54 +00:00
|
|
|
WINDOW.setProperty('commandUpdate', "true")
|
2015-05-16 06:31:08 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
# GUI commands
|
|
|
|
if command == "ToggleFullscreen":
|
|
|
|
xbmc.executebuiltin('Action(FullScreen)')
|
|
|
|
elif command == "ToggleOsdMenu":
|
|
|
|
xbmc.executebuiltin('Action(OSD)')
|
|
|
|
elif command == "MoveUp":
|
|
|
|
xbmc.executebuiltin('Action(Up)')
|
|
|
|
elif command == "MoveDown":
|
|
|
|
xbmc.executebuiltin('Action(Down)')
|
|
|
|
elif command == "MoveLeft":
|
|
|
|
xbmc.executebuiltin('Action(Left)')
|
|
|
|
elif command == "MoveRight":
|
|
|
|
xbmc.executebuiltin('Action(Right)')
|
|
|
|
elif command == "Select":
|
|
|
|
xbmc.executebuiltin('Action(Select)')
|
|
|
|
elif command == "Back":
|
|
|
|
xbmc.executebuiltin('Action(back)')
|
|
|
|
elif command == "ToggleContextMenu":
|
|
|
|
xbmc.executebuiltin('Action(ContextMenu)')
|
|
|
|
elif command == "GoHome":
|
|
|
|
xbmc.executebuiltin('ActivateWindow(Home)')
|
|
|
|
elif command == "PageUp":
|
|
|
|
xbmc.executebuiltin('Action(PageUp)')
|
|
|
|
elif command == "NextLetter":
|
|
|
|
xbmc.executebuiltin('Action(NextLetter)')
|
|
|
|
elif command == "GoToSearch":
|
|
|
|
xbmc.executebuiltin('VideoLibrary.Search')
|
|
|
|
elif command == "GoToSettings":
|
|
|
|
xbmc.executebuiltin('ActivateWindow(Settings)')
|
|
|
|
elif command == "PageDown":
|
|
|
|
xbmc.executebuiltin('Action(PageDown)')
|
|
|
|
elif command == "PreviousLetter":
|
|
|
|
xbmc.executebuiltin('Action(PrevLetter)')
|
|
|
|
elif command == "TakeScreenshot":
|
|
|
|
xbmc.executebuiltin('TakeScreenshot')
|
|
|
|
elif command == "ToggleMute":
|
|
|
|
xbmc.executebuiltin('Mute')
|
|
|
|
elif command == "VolumeUp":
|
|
|
|
xbmc.executebuiltin('Action(VolumeUp)')
|
|
|
|
elif command == "VolumeDown":
|
|
|
|
xbmc.executebuiltin('Action(VolumeDown)')
|
|
|
|
elif command == "DisplayMessage":
|
2015-08-05 08:40:54 +00:00
|
|
|
header = arguments['Header']
|
|
|
|
text = arguments['Text']
|
|
|
|
xbmcgui.Dialog().notification(header, text, icon="special://home/addons/plugin.video.emby/icon.png", time=4000)
|
2015-05-16 06:31:08 +00:00
|
|
|
elif command == "SendString":
|
2015-08-05 08:40:54 +00:00
|
|
|
string = arguments['String']
|
2015-05-16 07:11:41 +00:00
|
|
|
text = '{"jsonrpc": "2.0", "method": "Input.SendText", "params": { "text": "%s", "done": false }, "id": 0}' % string
|
|
|
|
result = xbmc.executeJSONRPC(text)
|
2015-05-16 06:31:08 +00:00
|
|
|
else:
|
|
|
|
self.logMsg("Unknown command.", 1)
|
2015-08-05 08:40:54 +00:00
|
|
|
|
|
|
|
elif messageType == "ServerRestarting":
|
|
|
|
if addon.getSetting('supressRestartMsg') == "true":
|
|
|
|
xbmcgui.Dialog().notification("Emby server", "Server is restarting.", icon="special://home/addons/plugin.video.emby/icon.png")
|
|
|
|
|
2015-03-23 04:54:16 +00:00
|
|
|
def on_error(self, ws, error):
|
2015-05-03 05:27:43 +00:00
|
|
|
if "10061" in str(error):
|
|
|
|
# Server is offline
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.logMsg("Error: %s" % error, 1)
|
2015-03-23 04:54:16 +00:00
|
|
|
#raise
|
|
|
|
|
|
|
|
def on_close(self, ws):
|
2015-05-03 05:27:43 +00:00
|
|
|
self.logMsg("Closed", 2)
|
2015-03-23 04:54:16 +00:00
|
|
|
|
|
|
|
def on_open(self, ws):
|
2015-06-04 10:37:10 +00:00
|
|
|
deviceId = ClientInformation().getMachineId()
|
|
|
|
self.doUtils.postCapabilities(deviceId)
|
2015-03-23 04:54:16 +00:00
|
|
|
|
|
|
|
def run(self):
|
2015-04-28 22:23:26 +00:00
|
|
|
|
2015-04-13 18:56:36 +00:00
|
|
|
WINDOW = xbmcgui.Window(10000)
|
2015-06-05 10:12:09 +00:00
|
|
|
logLevel = int(WINDOW.getProperty('getLogLevel'))
|
2015-04-13 18:56:36 +00:00
|
|
|
username = WINDOW.getProperty('currUser')
|
|
|
|
server = WINDOW.getProperty('server%s' % username)
|
2015-04-28 22:23:26 +00:00
|
|
|
token = WINDOW.getProperty('accessToken%s' % username)
|
|
|
|
deviceId = ClientInformation().getMachineId()
|
|
|
|
|
2015-06-05 10:12:09 +00:00
|
|
|
'''if (logLevel == 2):
|
2015-08-05 08:40:54 +00:00
|
|
|
websocket.enableTrace(True)'''
|
2015-04-28 22:23:26 +00:00
|
|
|
|
|
|
|
# Get the appropriate prefix for websocket
|
2015-04-13 18:56:36 +00:00
|
|
|
if "https" in server:
|
2015-04-28 22:23:26 +00:00
|
|
|
server = server.replace('https', 'wss')
|
2015-04-13 18:56:36 +00:00
|
|
|
else:
|
2015-04-28 22:23:26 +00:00
|
|
|
server = server.replace('http', 'ws')
|
|
|
|
|
|
|
|
websocketUrl = "%s?api_key=%s&deviceId=%s" % (server, token, deviceId)
|
|
|
|
self.logMsg("websocket URL: %s" % websocketUrl)
|
|
|
|
|
|
|
|
self.client = websocket.WebSocketApp(websocketUrl,
|
2015-03-23 04:54:16 +00:00
|
|
|
on_message = self.on_message,
|
|
|
|
on_error = self.on_error,
|
|
|
|
on_close = self.on_close)
|
|
|
|
|
|
|
|
self.client.on_open = self.on_open
|
|
|
|
|
2015-06-04 10:37:10 +00:00
|
|
|
while self.keepRunning:
|
2015-05-03 05:27:43 +00:00
|
|
|
|
2015-06-04 10:37:10 +00:00
|
|
|
self.client.run_forever()
|
2015-05-03 05:27:43 +00:00
|
|
|
|
2015-06-04 10:37:10 +00:00
|
|
|
if self.keepRunning:
|
|
|
|
self.logMsg("Client Needs To Restart", 2)
|
2015-04-12 03:01:44 +00:00
|
|
|
if self.KodiMonitor.waitForAbort(5):
|
2015-04-11 12:56:07 +00:00
|
|
|
break
|
2015-06-04 10:37:10 +00:00
|
|
|
|
2015-08-05 08:40:54 +00:00
|
|
|
self.logMsg("Thread Exited", 1)
|