jellyfin-kodi/resources/lib/websocket_client.py

331 lines
11 KiB
Python
Raw Normal View History

2016-03-31 19:48:52 +00:00
# -*- coding: utf-8 -*-
#################################################################################################
import json
import logging
2016-03-31 19:48:52 +00:00
import threading
import websocket
import xbmc
import clientinfo
import downloadutils
import librarysync
import playlist
import userclient
2016-09-07 07:14:02 +00:00
from utils import window, settings, dialog, language as lang, JSONRPC
2016-03-31 19:48:52 +00:00
##################################################################################################
log = logging.getLogger("EMBY."+__name__)
##################################################################################################
2016-03-31 19:48:52 +00:00
class WebSocket_Client(threading.Thread):
_shared_state = {}
2016-09-07 07:14:02 +00:00
_client = None
_stop_websocket = False
2016-03-31 19:48:52 +00:00
def __init__(self):
self.__dict__ = self._shared_state
self.monitor = xbmc.Monitor()
2016-09-07 07:14:02 +00:00
self.doutils = downloadutils.DownloadUtils()
self.client_info = clientinfo.ClientInfo()
self.device_id = self.client_info.get_device_id()
self.library_sync = librarysync.LibrarySync()
2016-03-31 19:48:52 +00:00
threading.Thread.__init__(self)
2016-09-07 07:14:02 +00:00
def send_progress_update(self, data):
log.debug("sendProgressUpdate")
2016-03-31 19:48:52 +00:00
try:
2016-09-07 07:14:02 +00:00
message = {
2016-03-31 19:48:52 +00:00
'MessageType': "ReportPlaybackProgress",
'Data': data
}
2016-09-07 07:14:02 +00:00
message_str = json.dumps(message)
self._client.send(message_str)
log.debug("Message data: %s", message_str)
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
except Exception as error:
log.exception(error)
2016-03-31 19:48:52 +00:00
def on_message(self, ws, message):
result = json.loads(message)
2016-09-07 07:14:02 +00:00
message_type = result['MessageType']
2016-03-31 19:48:52 +00:00
data = result['Data']
2016-09-07 07:14:02 +00:00
if message_type not in ('SessionEnded'):
2016-03-31 19:48:52 +00:00
# Mute certain events
log.info("Message: %s" % message)
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
if message_type == "Play":
2016-03-31 19:48:52 +00:00
# A remote control play command has been sent from the server.
2016-09-07 07:14:02 +00:00
self._play_(data)
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
elif message_type == "Playstate":
# A remote control update playstate command has been sent from the server.
self._playstate_(data)
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
elif message_type == "UserDataChanged":
2016-03-31 19:48:52 +00:00
# A user changed their personal rating for an item, or their playstate was updated
userdata_list = data['UserDataList']
2016-09-07 07:14:02 +00:00
self.library_sync.triage_items("userdata", userdata_list)
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
elif message_type == "LibraryChanged":
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
librarySync = self.library_sync
2016-03-31 19:48:52 +00:00
processlist = {
'added': data['ItemsAdded'],
'update': data['ItemsUpdated'],
'remove': data['ItemsRemoved']
}
for action in processlist:
librarySync.triage_items(action, processlist[action])
2016-09-07 07:14:02 +00:00
elif message_type == "GeneralCommand":
self._general_commands(data)
elif message_type == "ServerRestarting":
self._server_restarting()
elif message_type == "UserConfigurationUpdated":
2016-03-31 19:48:52 +00:00
# Update user data set in userclient
userclient.UserClient().get_user(data)
2016-09-07 07:14:02 +00:00
self.library_sync.refresh_views = True
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
elif message_type == "ServerShuttingDown":
# Server went offline
window('emby_online', value="false")
2016-09-07 07:14:02 +00:00
@classmethod
def _play_(cls, data):
item_ids = data['ItemIds']
command = data['PlayCommand']
playlist_ = playlist.Playlist()
if command == "PlayNow":
startat = data.get('StartPositionTicks', 0)
playlist_.playAll(item_ids, startat)
dialog(type_="notification",
heading=lang(29999),
message="%s %s" % (len(item_ids), lang(33004)),
icon="{emby}",
sound=False)
elif command == "PlayNext":
newplaylist = playlist_.modifyPlaylist(item_ids)
dialog(type_="notification",
heading=lang(29999),
message="%s %s" % (len(item_ids), lang(33005)),
icon="{emby}",
sound=False)
player = xbmc.Player()
if not player.isPlaying():
# Only start the playlist if nothing is playing
player.play(newplaylist)
@classmethod
def _playstate_(cls, data):
command = data['Command']
player = xbmc.Player()
actions = {
'Stop': player.stop,
'Unpause': player.pause,
'Pause': player.pause,
'NextTrack': player.playnext,
'PreviousTrack': player.playprevious,
'Seek': player.seekTime
}
action = actions[command]
if command == "Seek":
seekto = data['SeekPositionTicks']
seektime = seekto / 10000000.0
action(seektime)
log.info("Seek to %s", seektime)
else:
action()
log.info("Command: %s completed", command)
window('emby_command', value="true")
@classmethod
def _general_commands(cls, data):
command = data['Name']
arguments = data['Arguments']
if command in ('Mute', 'Unmute', 'SetVolume',
'SetSubtitleStreamIndex', 'SetAudioStreamIndex'):
player = xbmc.Player()
# These commands need to be reported back
if command == "Mute":
xbmc.executebuiltin('Mute')
elif command == "Unmute":
xbmc.executebuiltin('Mute')
elif command == "SetVolume":
volume = arguments['Volume']
xbmc.executebuiltin('SetVolume(%s[,showvolumebar])' % volume)
elif command == "SetAudioStreamIndex":
index = int(arguments['Index'])
player.setAudioStream(index - 1)
elif command == "SetSubtitleStreamIndex":
embyindex = int(arguments['Index'])
currentFile = player.getPlayingFile()
mapping = window('emby_%s.indexMapping' % currentFile)
if mapping:
externalIndex = json.loads(mapping)
# If there's external subtitles added via playbackutils
for index in externalIndex:
if externalIndex[index] == embyindex:
player.setSubtitleStream(int(index))
break
else:
# User selected internal subtitles
external = len(externalIndex)
audioTracks = len(player.getAvailableAudioStreams())
player.setSubtitleStream(external + embyindex - audioTracks - 1)
else:
# Emby merges audio and subtitle index together
audioTracks = len(player.getAvailableAudioStreams())
player.setSubtitleStream(index - audioTracks - 1)
# Let service know
window('emby_command', value="true")
elif command == "DisplayMessage":
header = arguments['Header']
text = arguments['Text']
dialog(type_="notification",
heading=header,
message=text,
icon="{emby}",
time=4000)
elif command == "SendString":
params = {
'text': arguments['String'],
'done': False
}
result = JSONRPC("Input.SendText").execute(params)
elif command in ("MoveUp", "MoveDown", "MoveRight", "MoveLeft"):
# Commands that should wake up display
actions = {
'MoveUp': "Input.Up",
'MoveDown': "Input.Down",
'MoveRight': "Input.Right",
'MoveLeft': "Input.Left"
}
result = JSONRPC(actions[command]).execute()
elif command == "GoHome":
result = JSONRPC("GUI.ActivateWindow").execute({"window":"home"})
else:
builtin = {
'ToggleFullscreen': 'Action(FullScreen)',
'ToggleOsdMenu': 'Action(OSD)',
'ToggleContextMenu': 'Action(ContextMenu)',
'Select': 'Action(Select)',
'Back': 'Action(back)',
'PageUp': 'Action(PageUp)',
'NextLetter': 'Action(NextLetter)',
'GoToSearch': 'VideoLibrary.Search',
'GoToSettings': 'ActivateWindow(Settings)',
'PageDown': 'Action(PageDown)',
'PreviousLetter': 'Action(PrevLetter)',
'TakeScreenshot': 'TakeScreenshot',
'ToggleMute': 'Mute',
'VolumeUp': 'Action(VolumeUp)',
'VolumeDown': 'Action(VolumeDown)',
}
action = builtin.get(command)
if action:
xbmc.executebuiltin(action)
@classmethod
def _server_restarting(cls):
if settings('supressRestartMsg') == "true":
dialog(type_="notification",
heading=lang(29999),
message=lang(33006),
icon="{emby}")
2016-03-31 19:48:52 +00:00
def on_close(self, ws):
2016-09-07 07:14:02 +00:00
log.debug("closed")
2016-03-31 19:48:52 +00:00
def on_open(self, ws):
2016-09-07 07:14:02 +00:00
self.doutils.postCapabilities(self.device_id)
2016-03-31 19:48:52 +00:00
def on_error(self, ws, error):
if "10061" in str(error):
# Server is offline
pass
else:
2016-09-07 07:14:02 +00:00
log.debug("Error: %s", error)
2016-03-31 19:48:52 +00:00
def run(self):
# websocket.enableTrace(True)
2016-09-07 07:14:02 +00:00
user_id = window('emby_currUser')
server = window('emby_server%s' % user_id)
token = window('emby_accessToken%s' % user_id)
2016-03-31 19:48:52 +00:00
# Get the appropriate prefix for the websocket
if "https" in server:
server = server.replace('https', "wss")
else:
server = server.replace('http', "ws")
2016-09-07 07:14:02 +00:00
websocket_url = "%s?api_key=%s&deviceId=%s" % (server, token, self.device_id)
log.info("websocket url: %s", websocket_url)
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
self._client = websocket.WebSocketApp(websocket_url,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close)
self._client.on_open = self.on_open
log.warn("----===## Starting WebSocketClient ##===----")
2016-03-31 19:48:52 +00:00
2016-03-31 19:52:09 +00:00
while not self.monitor.abortRequested():
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
self._client.run_forever(ping_interval=10)
if self._stop_websocket:
2016-03-31 19:48:52 +00:00
break
2016-03-31 19:52:09 +00:00
if self.monitor.waitForAbort(5):
2016-03-31 19:48:52 +00:00
# Abort was requested, exit
break
log.warn("##===---- WebSocketClient Stopped ----===##")
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
def stop_client(self):
2016-03-31 19:48:52 +00:00
2016-09-07 07:14:02 +00:00
self._stop_websocket = True
self._client.close()
log.info("Stopping thread")