Clean up + encoding utf-8

Added message when server is restarting. Optional.
This commit is contained in:
angelblue05 2015-08-05 03:40:54 -05:00
parent 7e281ecf13
commit e972ae2729
2 changed files with 76 additions and 109 deletions

View file

@ -1,3 +1,10 @@
# -*- coding: utf-8 -*-
import json
import threading
import websocket
import logging
################################################################################################# #################################################################################################
# WebSocket Client thread # WebSocket Client thread
################################################################################################# #################################################################################################
@ -6,28 +13,17 @@ import xbmc
import xbmcgui import xbmcgui
import xbmcaddon import xbmcaddon
import json
import threading
import urllib
import socket
import websocket
import KodiMonitor import KodiMonitor
import Utils as utils import Utils as utils
from ClientInformation import ClientInformation from ClientInformation import ClientInformation
from DownloadUtils import DownloadUtils from DownloadUtils import DownloadUtils
from PlaybackUtils import PlaybackUtils from PlaybackUtils import PlaybackUtils
from LibrarySync import LibrarySync from LibrarySync import LibrarySync
from WriteKodiVideoDB import WriteKodiVideoDB from WriteKodiVideoDB import WriteKodiVideoDB
from ReadEmbyDB import ReadEmbyDB
from ReadKodiDB import ReadKodiDB
from WriteKodiMusicDB import WriteKodiMusicDB from WriteKodiMusicDB import WriteKodiMusicDB
import logging
logging.basicConfig() logging.basicConfig()
_MODE_BASICPLAY=12
class WebSocketThread(threading.Thread): class WebSocketThread(threading.Thread):
@ -36,7 +32,6 @@ class WebSocketThread(threading.Thread):
doUtils = DownloadUtils() doUtils = DownloadUtils()
clientInfo = ClientInformation() clientInfo = ClientInformation()
KodiMonitor = KodiMonitor.Kodi_Monitor() KodiMonitor = KodiMonitor.Kodi_Monitor()
WINDOW = xbmcgui.Window(10000)
addonName = clientInfo.getAddonName() addonName = clientInfo.getAddonName()
@ -65,138 +60,112 @@ class WebSocketThread(threading.Thread):
messageString = json.dumps(messageData) messageString = json.dumps(messageData)
self.client.send(messageString) self.client.send(messageString)
self.logMsg("Message data: %s" % messageString, 2) self.logMsg("Message data: %s" % messageString, 2)
except Exception, e: except Exception as e:
self.logMsg("Exception: %s" % e, 1) self.logMsg("Exception: %s" % e, 1)
def stopClient(self): def stopClient(self):
# stopping the client is tricky, first set keep_running to false and then trigger one # 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 # more message by requesting one SessionsStart message, this causes the
# client to receive the message and then exit # client to receive the message and then exit
if(self.client != None): if self.client:
self.logMsg("Stopping Client") self.logMsg("Stopping Client")
self.keepRunning = False self.keepRunning = False
self.client.keep_running = False self.client.keep_running = False
self.client.close() self.client.close()
self.logMsg("Stopping Client : KeepRunning set to False") self.logMsg("Stopping Client : KeepRunning set to False")
'''
try:
self.keepRunning = False
self.client.keep_running = False
self.logMsg("Stopping Client")
self.logMsg("Calling Ping")
self.client.sock.ping()
self.logMsg("Calling Socket Shutdown()")
self.client.sock.sock.shutdown(socket.SHUT_RDWR)
self.logMsg("Calling Socket Close()")
self.client.sock.sock.close()
self.logMsg("Stopping Client Done")
self.logMsg("Calling Ping")
self.client.sock.ping()
except Exception, e:
self.logMsg("Exception : " + str(e), level=0)
'''
else: else:
self.logMsg("Stopping Client NO Object ERROR") self.logMsg("Stopping Client NO Object ERROR")
def on_message(self, ws, message): def on_message(self, ws, message):
self.logMsg("Message : " + str(message), 0)
result = json.loads(message)
messageType = result.get("MessageType")
data = result.get("Data")
WINDOW = xbmcgui.Window(10000) WINDOW = xbmcgui.Window(10000)
addon = xbmcaddon.Addon()
self.logMsg("Message: %s" % message, 1)
if(messageType != None and messageType == "Play" and data != None): result = json.loads(message)
itemIds = data.get("ItemIds") messageType = result['MessageType']
playCommand = data.get("PlayCommand") data = result.get("Data")
if(playCommand != None and playCommand == "PlayNow"): 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)
xbmc.executebuiltin("Dialog.Close(all,true)") xbmc.executebuiltin("Dialog.Close(all,true)")
startPositionTicks = data.get("StartPositionTicks") xbmc.executebuiltin("XBMC.Notification(Playlist: Added %s items to Playlist,)" % len(itemIds))
PlaybackUtils().PLAYAllItems(itemIds, startPositionTicks) PlaybackUtils().PLAYAllItems(itemIds, startPositionTicks)
xbmc.executebuiltin("XBMC.Notification(Playlist: Added " + str(len(itemIds)) + " items to Playlist,)") # Don't think this is being used.
elif "PlayNext" in playCommand:
elif(playCommand != None and playCommand == "PlayNext"): xbmc.executebuiltin("XBMC.Notification(Playlist: Added %s items to Playlist,)" % len(itemIds))
playlist = PlaybackUtils().AddToPlaylist(itemIds) playlist = PlaybackUtils().AddToPlaylist(itemIds)
xbmc.executebuiltin("XBMC.Notification(Playlist: Added " + str(len(itemIds)) + " items to Playlist,)") if not xbmc.Player().isPlaying():
if(xbmc.Player().isPlaying() == False):
xbmc.Player().play(playlist) xbmc.Player().play(playlist)
elif(messageType != None and messageType == "Playstate"): elif messageType == "Playstate":
command = data.get("Command") # A remote control update playstate command has been sent from the server.
if(command != None and command == "Stop"): command = data['Command']
self.logMsg("Playback Stopped")
xbmc.executebuiltin('xbmc.activatewindow(10000)') if "Stop" in command:
self.logMsg("Playback Stopped.", 1)
xbmc.Player().stop() xbmc.Player().stop()
elif(command != None and command == "Pause"): elif "Unpause" in command:
self.logMsg("Playback Paused") self.logMsg("Playback unpaused.", 1)
xbmc.Player().pause() xbmc.Player().pause()
elif(command != None and command == "Unpause"): elif "Pause" in command:
self.logMsg("Playback UnPaused") self.logMsg("Playback paused.", 1)
xbmc.Player().pause() xbmc.Player().pause()
elif(command != None and command == "NextTrack"): elif "NextTrack" in command:
self.logMsg("Playback NextTrack") self.logMsg("Playback next track.", 1)
xbmc.Player().playnext() xbmc.Player().playnext()
elif(command != None and command == "PreviousTrack"): elif "PreviousTrack" in command:
self.logMsg("Playback PreviousTrack") self.logMsg("Playback previous track.", 1)
xbmc.Player().playprevious() xbmc.Player().playprevious()
elif(command != None and command == "Seek"): elif "Seek" in command:
seekPositionTicks = data.get("SeekPositionTicks") seekPositionTicks = data['SeekPositionTicks']
self.logMsg("Playback Seek : " + str(seekPositionTicks)) seekTime = seekPositionTicks / 10000000.0
seekTime = (seekPositionTicks / 1000) / 10000 self.logMsg("Seek to %s" % seekTime, 1)
xbmc.Player().seekTime(seekTime) xbmc.Player().seekTime(seekTime)
# Report playback # Report playback
WINDOW.setProperty('commandUpdate', 'true') WINDOW.setProperty('commandUpdate', "true")
elif(messageType != None and messageType == "UserDataChanged"): elif messageType == "UserDataChanged":
# for now just do a full playcount sync # A user changed their personal rating for an item, or their playstate was updated
self.logMsg("Message : Doing UserDataChanged", 0) userdataList = data['UserDataList']
userDataList = data.get("UserDataList") self.logMsg("Message: Doing UserDataChanged: UserDataList: %s" % userdataList, 1)
self.logMsg("Message : Doing UserDataChanged : UserDataList : " + str(userDataList), 0) LibrarySync().user_data_update(userdataList)
if(userDataList != None):
LibrarySync().user_data_update(userDataList)
elif(messageType != None and messageType == "LibraryChanged"): elif messageType == "LibraryChanged":
foldersAddedTo = data.get("FoldersAddedTo") # Library items
foldersRemovedFrom = data.get("FoldersRemovedFrom")
# doing items removed
itemsRemoved = data.get("ItemsRemoved") itemsRemoved = data.get("ItemsRemoved")
itemsAdded = data.get("ItemsAdded") itemsAdded = data.get("ItemsAdded")
itemsUpdated = data.get("ItemsUpdated") itemsUpdated = data.get("ItemsUpdated")
itemsToUpdate = itemsAdded + itemsUpdated itemsToUpdate = itemsAdded + itemsUpdated
self.logMsg("Message : WebSocket LibraryChanged : Items Added : " + str(itemsAdded), 0)
self.logMsg("Message : WebSocket LibraryChanged : Items Updated : " + str(itemsUpdated), 0) self.logMsg("Message: WebSocket LibraryChanged: Items Added: %s" % itemsAdded, 1)
self.logMsg("Message : WebSocket LibraryChanged : Items Removed : " + str(itemsRemoved), 0) self.logMsg("Message: WebSocket LibraryChanged: Items Updated: %s" % itemsUpdated, 1)
self.logMsg("Message: WebSocket LibraryChanged: Items Removed: %s" % itemsRemoved, 1)
LibrarySync().remove_items(itemsRemoved) LibrarySync().remove_items(itemsRemoved)
LibrarySync().update_items(itemsToUpdate) LibrarySync().update_items(itemsToUpdate)
elif messageType == "GeneralCommand": elif messageType == "GeneralCommand":
command = data.get("Name") command = data['Name']
arguments = data.get("Arguments") arguments = data.get("Arguments")
commandsPlayback = [ if command in ('Mute', 'Unmute', 'SetVolume'):
'Mute','Unmute','SetVolume',
'SetAudioStreamIndex'
]
if command in commandsPlayback:
# These commands need to be reported back # These commands need to be reported back
if command == "Mute": if command == "Mute":
xbmc.executebuiltin('Mute') xbmc.executebuiltin('Mute')
elif command == "Unmute": elif command == "Unmute":
xbmc.executebuiltin('Mute') xbmc.executebuiltin('Mute')
elif command == "SetVolume": elif command == "SetVolume":
volume = arguments[u'Volume'] volume = arguments['Volume']
xbmc.executebuiltin('SetVolume(%s[,showvolumebar])' % volume) xbmc.executebuiltin('SetVolume(%s[,showvolumebar])' % volume)
# Report playback # Report playback
WINDOW.setProperty('commandUpdate', 'true') WINDOW.setProperty('commandUpdate', "true")
else: else:
# GUI commands # GUI commands
@ -241,16 +210,20 @@ class WebSocketThread(threading.Thread):
elif command == "VolumeDown": elif command == "VolumeDown":
xbmc.executebuiltin('Action(VolumeDown)') xbmc.executebuiltin('Action(VolumeDown)')
elif command == "DisplayMessage": elif command == "DisplayMessage":
header = arguments[u'Header'] header = arguments['Header']
text = arguments[u'Text'] text = arguments['Text']
xbmcgui.Dialog().notification(header, text, time=4000) xbmcgui.Dialog().notification(header, text, icon="special://home/addons/plugin.video.emby/icon.png", time=4000)
elif command == "SendString": elif command == "SendString":
string = arguments[u'String'] string = arguments['String']
text = '{"jsonrpc": "2.0", "method": "Input.SendText", "params": { "text": "%s", "done": false }, "id": 0}' % string text = '{"jsonrpc": "2.0", "method": "Input.SendText", "params": { "text": "%s", "done": false }, "id": 0}' % string
result = xbmc.executeJSONRPC(text) result = xbmc.executeJSONRPC(text)
else: else:
self.logMsg("Unknown command.", 1) self.logMsg("Unknown command.", 1)
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")
def on_error(self, ws, error): def on_error(self, ws, error):
if "10061" in str(error): if "10061" in str(error):
# Server is offline # Server is offline
@ -260,13 +233,7 @@ class WebSocketThread(threading.Thread):
#raise #raise
def on_close(self, ws): def on_close(self, ws):
WINDOW = self.WINDOW
self.logMsg("Closed", 2) self.logMsg("Closed", 2)
# Server is not online
'''if WINDOW.getProperty("Server_online") == "true":
self.logMsg("Server is unreachable.", 1)
WINDOW.setProperty("Server_online", "false")
xbmcgui.Dialog().notification("Error connecting", "%s Server is unreachable." % self.addonName)'''
def on_open(self, ws): def on_open(self, ws):
deviceId = ClientInformation().getMachineId() deviceId = ClientInformation().getMachineId()
@ -310,4 +277,3 @@ class WebSocketThread(threading.Thread):
break break
self.logMsg("Thread Exited", 1) self.logMsg("Thread Exited", 1)

View file

@ -45,6 +45,7 @@
<category label="30022"> <category label="30022">
<setting id="logLevel" type="enum" label="30004" values="None|Info|Debug" default="0" /> <setting id="logLevel" type="enum" label="30004" values="None|Info|Debug" default="0" />
<setting id="supressConnectMsg" type="bool" label="30249" default="false" visible="true" enable="true" /> <setting id="supressConnectMsg" type="bool" label="30249" default="false" visible="true" enable="true" />
<setting id="supressRestartMsg" type="bool" label="Enable server message when it's restarting" default="false" visible="true" enable="true" />
<setting label="30239" type="action" action="RunPlugin(plugin://plugin.video.emby?mode=reset)" /> <setting label="30239" type="action" action="RunPlugin(plugin://plugin.video.emby?mode=reset)" />
</category> </category>