2023-01-12 21:02:32 -05:00
|
|
|
from __future__ import (
|
|
|
|
|
division, absolute_import, print_function, unicode_literals
|
|
|
|
|
)
|
2023-01-12 20:50:41 -05:00
|
|
|
|
2017-12-14 13:43:23 +11:00
|
|
|
import json
|
|
|
|
|
import threading
|
2021-11-14 12:57:32 -05:00
|
|
|
import time
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
import xbmc
|
2022-02-27 22:15:54 -05:00
|
|
|
import xbmcaddon
|
2017-12-14 13:43:23 +11:00
|
|
|
import xbmcgui
|
2023-01-12 20:50:41 -05:00
|
|
|
import websocket
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2022-03-09 15:25:35 -05:00
|
|
|
from .jellyfin import API
|
2020-06-21 11:27:09 +10:00
|
|
|
from .functions import play_action
|
2022-03-09 22:27:18 +01:00
|
|
|
from .lazylogger import LazyLogger
|
2020-06-21 11:27:09 +10:00
|
|
|
from .jsonrpc import JsonRpc
|
2019-07-29 15:51:20 +10:00
|
|
|
from .kodi_utils import HomeWindow
|
2022-02-27 22:15:54 -05:00
|
|
|
from .utils import get_device_id, load_user_details
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2020-07-25 01:01:30 -04:00
|
|
|
log = LazyLogger(__name__)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2020-06-21 11:27:09 +10:00
|
|
|
|
2017-12-14 13:43:23 +11:00
|
|
|
class WebSocketClient(threading.Thread):
|
|
|
|
|
|
|
|
|
|
_shared_state = {}
|
|
|
|
|
|
|
|
|
|
_client = None
|
|
|
|
|
_stop_websocket = False
|
2019-07-03 14:15:42 +10:00
|
|
|
_library_monitor = None
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2019-07-03 14:15:42 +10:00
|
|
|
def __init__(self, library_change_monitor):
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
|
self.monitor = xbmc.Monitor()
|
|
|
|
|
|
2021-12-29 16:54:05 -05:00
|
|
|
self.device_id = get_device_id()
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2019-07-03 14:15:42 +10:00
|
|
|
self._library_monitor = library_change_monitor
|
2026-01-10 16:37:18 -05:00
|
|
|
self.websocket_error = False
|
2019-07-03 14:15:42 +10:00
|
|
|
|
2017-12-14 13:43:23 +11:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
|
|
|
|
|
|
def on_message(self, ws, message):
|
|
|
|
|
|
|
|
|
|
result = json.loads(message)
|
|
|
|
|
message_type = result['MessageType']
|
|
|
|
|
|
|
|
|
|
if message_type == 'Play':
|
|
|
|
|
data = result['Data']
|
|
|
|
|
self._play(data)
|
|
|
|
|
|
|
|
|
|
elif message_type == 'Playstate':
|
|
|
|
|
data = result['Data']
|
|
|
|
|
self._playstate(data)
|
|
|
|
|
|
|
|
|
|
elif message_type == "UserDataChanged":
|
2019-07-02 16:08:41 +10:00
|
|
|
data = result['Data']
|
|
|
|
|
self._library_changed(data)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
elif message_type == "LibraryChanged":
|
2019-07-02 16:08:41 +10:00
|
|
|
data = result['Data']
|
|
|
|
|
self._library_changed(data)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
elif message_type == "GeneralCommand":
|
|
|
|
|
data = result['Data']
|
|
|
|
|
self._general_commands(data)
|
|
|
|
|
|
|
|
|
|
else:
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("WebSocket Message Type: {0}".format(message))
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2019-07-02 16:08:41 +10:00
|
|
|
def _library_changed(self, data):
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("Library_Changed: {0}".format(data))
|
2019-07-03 14:15:42 +10:00
|
|
|
self._library_monitor.check_for_updates()
|
2019-07-02 16:08:41 +10:00
|
|
|
|
2020-06-21 11:27:09 +10:00
|
|
|
def _play(self, data):
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
item_ids = data['ItemIds']
|
|
|
|
|
command = data['PlayCommand']
|
|
|
|
|
|
|
|
|
|
if command == 'PlayNow':
|
2019-07-29 15:51:20 +10:00
|
|
|
home_screen = HomeWindow()
|
2020-06-21 11:27:09 +10:00
|
|
|
home_screen.set_property("skip_select_user", "true")
|
2019-07-29 15:51:20 +10:00
|
|
|
|
2019-11-24 20:00:17 +11:00
|
|
|
startat = data.get('StartPositionTicks', -1)
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("WebSocket Message PlayNow: {0}".format(data))
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2017-12-14 16:15:39 +11:00
|
|
|
media_source_id = data.get("MediaSourceId", "")
|
2018-11-08 19:39:28 +11:00
|
|
|
subtitle_stream_index = data.get("SubtitleStreamIndex", None)
|
|
|
|
|
audio_stream_index = data.get("AudioStreamIndex", None)
|
2017-12-14 16:15:39 +11:00
|
|
|
|
2018-08-30 22:42:21 +10:00
|
|
|
start_index = data.get("StartIndex", 0)
|
|
|
|
|
|
|
|
|
|
if start_index > 0 and start_index < len(item_ids):
|
|
|
|
|
item_ids = item_ids[start_index:]
|
|
|
|
|
|
2018-02-11 15:13:41 +11:00
|
|
|
if len(item_ids) == 1:
|
|
|
|
|
item_ids = item_ids[0]
|
|
|
|
|
|
2017-12-14 13:43:23 +11:00
|
|
|
params = {}
|
2018-02-11 15:13:41 +11:00
|
|
|
params["item_id"] = item_ids
|
2017-12-14 13:43:23 +11:00
|
|
|
params["auto_resume"] = str(startat)
|
2017-12-14 16:15:39 +11:00
|
|
|
params["media_source_id"] = media_source_id
|
2018-11-08 19:39:28 +11:00
|
|
|
params["subtitle_stream_index"] = subtitle_stream_index
|
|
|
|
|
params["audio_stream_index"] = audio_stream_index
|
2020-06-21 11:27:09 +10:00
|
|
|
play_action(params)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2020-06-21 11:27:09 +10:00
|
|
|
def _playstate(self, data):
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
command = data['Command']
|
|
|
|
|
player = xbmc.Player()
|
|
|
|
|
|
|
|
|
|
actions = {
|
|
|
|
|
|
|
|
|
|
'Stop': player.stop,
|
|
|
|
|
'Unpause': player.pause,
|
|
|
|
|
'Pause': player.pause,
|
|
|
|
|
'PlayPause': player.pause,
|
|
|
|
|
'NextTrack': player.playnext,
|
|
|
|
|
'PreviousTrack': player.playprevious
|
|
|
|
|
}
|
|
|
|
|
if command == 'Seek':
|
|
|
|
|
|
|
|
|
|
if player.isPlaying():
|
|
|
|
|
seek_to = data['SeekPositionTicks']
|
|
|
|
|
seek_time = seek_to / 10000000.0
|
|
|
|
|
player.seekTime(seek_time)
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("Seek to {0}".format(seek_time))
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
elif command in actions:
|
|
|
|
|
actions[command]()
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("Command: {0} completed".format(command))
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
else:
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("Unknown command: {0}".format(command))
|
2017-12-14 13:43:23 +11:00
|
|
|
return
|
|
|
|
|
|
2020-06-21 11:27:09 +10:00
|
|
|
def _general_commands(self, data):
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
command = data['Name']
|
|
|
|
|
arguments = data['Arguments']
|
|
|
|
|
|
|
|
|
|
if command in ('Mute',
|
|
|
|
|
'Unmute',
|
|
|
|
|
'SetVolume',
|
|
|
|
|
'SetSubtitleStreamIndex',
|
|
|
|
|
'SetAudioStreamIndex',
|
|
|
|
|
'SetRepeatMode'):
|
|
|
|
|
|
|
|
|
|
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']
|
2023-01-15 09:56:53 -05:00
|
|
|
xbmc.executebuiltin(
|
|
|
|
|
'SetVolume({}[,showvolumebar])'.format(volume)
|
|
|
|
|
)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
elif command == 'SetAudioStreamIndex':
|
|
|
|
|
index = int(arguments['Index'])
|
|
|
|
|
player.setAudioStream(index - 1)
|
|
|
|
|
|
2018-11-08 19:39:28 +11:00
|
|
|
elif command == 'SetSubtitleStreamIndex':
|
|
|
|
|
index = int(arguments['Index'])
|
|
|
|
|
player.setSubtitleStream(index - 1)
|
|
|
|
|
|
2017-12-14 13:43:23 +11:00
|
|
|
elif command == 'SetRepeatMode':
|
|
|
|
|
mode = arguments['RepeatMode']
|
2023-01-15 09:56:53 -05:00
|
|
|
xbmc.executebuiltin('xbmc.PlayerControl({})'.format(mode))
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
elif command == 'DisplayMessage':
|
|
|
|
|
|
2020-06-21 11:27:09 +10:00
|
|
|
# header = arguments['Header']
|
2017-12-14 13:43:23 +11:00
|
|
|
text = arguments['Text']
|
|
|
|
|
# show notification here
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("WebSocket DisplayMessage: {0}".format(text))
|
2020-07-04 13:26:21 -04:00
|
|
|
xbmcgui.Dialog().notification("JellyCon", text)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
elif command == 'SendString':
|
|
|
|
|
|
|
|
|
|
params = {
|
|
|
|
|
|
|
|
|
|
'text': arguments['String'],
|
|
|
|
|
'done': False
|
|
|
|
|
}
|
2020-06-21 11:27:09 +10:00
|
|
|
JsonRpc('Input.SendText').execute(params)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
}
|
2020-06-21 11:27:09 +10:00
|
|
|
JsonRpc(actions[command]).execute()
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
elif command == 'GoHome':
|
2020-06-21 11:27:09 +10:00
|
|
|
JsonRpc('GUI.ActivateWindow').execute({'window': "home"})
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
elif command == "Guide":
|
2020-06-21 11:27:09 +10:00
|
|
|
JsonRpc('GUI.ActivateWindow').execute({'window': "tvguide"})
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
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)',
|
|
|
|
|
}
|
|
|
|
|
if command in builtin:
|
|
|
|
|
xbmc.executebuiltin(builtin[command])
|
|
|
|
|
|
|
|
|
|
def on_open(self, ws):
|
2026-01-10 16:37:18 -05:00
|
|
|
# Wait to make sure previous keepalive cycle has ended
|
|
|
|
|
if self.websocket_error:
|
|
|
|
|
time.sleep(30)
|
|
|
|
|
self.websocket_error = False
|
2019-07-07 12:17:36 +10:00
|
|
|
log.debug("Connected")
|
2017-12-14 13:43:23 +11:00
|
|
|
self.post_capabilities()
|
2026-01-10 16:37:18 -05:00
|
|
|
self.send_keepalive(ws)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
def on_error(self, ws, error):
|
2026-01-10 16:37:18 -05:00
|
|
|
self.websocket_error = True
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("Error: {0}".format(error))
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
|
|
|
|
|
token = None
|
|
|
|
|
while token is None or token == "":
|
2022-02-27 22:15:54 -05:00
|
|
|
user_details = load_user_details()
|
|
|
|
|
token = user_details.get('token')
|
2017-12-14 13:43:23 +11:00
|
|
|
if self.monitor.waitForAbort(10):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# Get the appropriate prefix for the websocket
|
2022-02-27 22:15:54 -05:00
|
|
|
settings = xbmcaddon.Addon()
|
|
|
|
|
server = settings.getSetting('server_address')
|
2021-11-20 09:40:33 -05:00
|
|
|
if "https://" in server:
|
|
|
|
|
server = server.replace('https://', 'wss://')
|
2017-12-14 13:43:23 +11:00
|
|
|
else:
|
2021-11-20 09:40:33 -05:00
|
|
|
server = server.replace('http://', 'ws://')
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2023-01-15 09:56:53 -05:00
|
|
|
websocket_url = "{}/socket?api_key={}&deviceId={}".format(
|
|
|
|
|
server, token, self.device_id
|
|
|
|
|
)
|
2020-07-25 01:01:30 -04:00
|
|
|
log.debug("websocket url: {0}".format(websocket_url))
|
2017-12-14 13:43:23 +11:00
|
|
|
|
2021-01-30 23:26:12 -05:00
|
|
|
self._client = websocket.WebSocketApp(
|
|
|
|
|
websocket_url,
|
2021-11-14 12:57:32 -05:00
|
|
|
on_open=lambda ws: self.on_open(ws),
|
2021-01-30 23:26:12 -05:00
|
|
|
on_message=lambda ws, message: self.on_message(ws, message),
|
2021-11-14 12:57:32 -05:00
|
|
|
on_error=lambda ws, error: self.on_error(ws, error))
|
2021-01-30 23:26:12 -05:00
|
|
|
|
2017-12-14 13:43:23 +11:00
|
|
|
log.debug("Starting WebSocketClient")
|
|
|
|
|
|
|
|
|
|
while not self.monitor.abortRequested():
|
|
|
|
|
|
2026-01-10 16:37:18 -05:00
|
|
|
self._client.run_forever(reconnect=30)
|
2017-12-14 13:43:23 +11:00
|
|
|
|
|
|
|
|
if self._stop_websocket:
|
|
|
|
|
break
|
|
|
|
|
|
2017-12-22 11:40:49 +11:00
|
|
|
if self.monitor.waitForAbort(20):
|
2017-12-14 13:43:23 +11:00
|
|
|
# Abort was requested, exit
|
|
|
|
|
break
|
|
|
|
|
|
2017-12-22 11:40:49 +11:00
|
|
|
log.debug("Reconnecting WebSocket")
|
|
|
|
|
|
2017-12-14 13:43:23 +11:00
|
|
|
log.debug("WebSocketClient Stopped")
|
|
|
|
|
|
|
|
|
|
def stop_client(self):
|
|
|
|
|
|
|
|
|
|
self._stop_websocket = True
|
|
|
|
|
if self._client is not None:
|
|
|
|
|
self._client.close()
|
|
|
|
|
log.debug("Stopping WebSocket (stop_client called)")
|
|
|
|
|
|
|
|
|
|
def post_capabilities(self):
|
|
|
|
|
|
2022-02-27 23:36:01 -05:00
|
|
|
settings = xbmcaddon.Addon()
|
2022-02-27 22:15:54 -05:00
|
|
|
user_details = load_user_details()
|
|
|
|
|
|
|
|
|
|
api = API(
|
|
|
|
|
settings.getSetting('server_address'),
|
|
|
|
|
user_details.get('user_id'),
|
|
|
|
|
user_details.get('token')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
api.post_capabilities()
|
2026-01-10 16:37:18 -05:00
|
|
|
|
|
|
|
|
def send_keepalive(self, ws):
|
|
|
|
|
# Stop the keepalive cycle if an error has been detected
|
|
|
|
|
if self.websocket_error:
|
|
|
|
|
return
|
|
|
|
|
keepalive_payload = json.dumps({"MessageType": "KeepAlive", "Data": 30})
|
|
|
|
|
# Send the keepalive, or register an error
|
|
|
|
|
try:
|
|
|
|
|
ws.send(keepalive_payload)
|
|
|
|
|
except:
|
|
|
|
|
self.websocket_error = True
|
|
|
|
|
return
|
|
|
|
|
# Schedule the next message
|
|
|
|
|
self.schedule_keepalive(ws)
|
|
|
|
|
|
|
|
|
|
def schedule_keepalive(self, ws):
|
|
|
|
|
# Schedule a keepalive message in 30 seconds
|
|
|
|
|
timer = threading.Timer(30, self.send_keepalive, kwargs={'ws': ws})
|
|
|
|
|
timer.start()
|