# Path: resources/lib/kodiutils.py
# def decode(s, encoding='utf-8', errors='strict'):
#     if PY2 and isinstance(s, str):
#         s = s.decode(encoding, errors)
#     return s
# 
# def sleep(sleep_time):
#     mon = xbmc.Monitor()
#     while not mon.abortRequested() and sleep_time > 0:
#         sleep_time -= 100
#         xbmc.sleep(99)
# 
# def get_uuid():
#     # pylint: disable=global-statement
#     global __CRYPT_KEY__
#     if not __CRYPT_KEY__:
#         __CRYPT_KEY__ = _get_system_uuid()
#     return __CRYPT_KEY__
# 
# Path: resources/lib/common/logger.py
# LOG = 2
# APP = 'SC'
#     def debug(text):
#     def info(text):
#     def warning(text):
#     def error(text):
#     def debug(text):
#     def info(text):
#     def warning(text):
#     def error(text):
# 
# Path: resources/lib/services/Monitor.py
# class Monitor(KMonitor):
#     SORT_ASC = 1
#     SORT_DESC = 2
#     SORT_METHOD_NONE = 0
#     SORT_METHOD_RATING = 17
#     SORT_METHOD_MPAA = 31
#     SORT_METHOD_TITLE = 29
#     SORT_METHOD_YEAR = 16
#     SORT_METHOD_DATE_ADDED = 40
#     def __init__(self):
#     def onSettingsChanged(self):
#     def onScreensaverActivated(self):
#     def onScreensaverDeactivated(self):
#     def onDPMSActivated(self):
#     def onDPMSDeactivated(self):
#     def onScanStarted(self, library):
#     def onScanFinished(self, library):
#     def onCleanStarted(self, library):
#     def onCleanFinished(self, library):
#     def onSleep(self, data):
#     def onWake(self, data):
#     def PlayerOnAVStart(self, data):
#     def onNotification(self, sender, method, data):
#     def periodical_check(self):
#     def can_check(self):


import select
import traceback
import websocket
    import thread
    import _thread as thread
from xbmc import Monitor
from resources.lib.kodiutils import decode, sleep, get_uuid
from resources.lib.common import logger
from resources.lib.services.Monitor import monitor



class SCDispatcher:
    def __init__(self, app, ping_timeout):
        self.app = app
        self.ping_timeout = ping_timeout

    def read(self, sock, read_callback, check_callback):
        while self.app.keep_running and not monitor.abortRequested():
            logger.debug('[WS] loop')
            r, w, e = select.select(
                (self.app.sock.sock,), (), (), self.ping_timeout)
            if r:
                if not read_callback():
                    break
            check_callback()
        logger.debug('[WS] Koncim....')
        self.app.close(status=websocket.STATUS_PROTOCOL_ERROR)


class SSLSCDispatcher:
    def __init__(self, app, ping_timeout):
        self.app = app
        self.ping_timeout = ping_timeout

    def read(self, sock, read_callback, check_callback):
        while self.app.keep_running and not monitor.abortRequested():
            logger.debug('[WS] loop')
            r = self.select()

# Path: resources/lib/kodiutils.py
# def decode(s, encoding='utf-8', errors='strict'):
#     if PY2 and isinstance(s, str):
#         s = s.decode(encoding, errors)
#     return s
# 
# def sleep(sleep_time):
#     mon = xbmc.Monitor()
#     while not mon.abortRequested() and sleep_time > 0:
#         sleep_time -= 100
#         xbmc.sleep(99)
# 
# def get_uuid():
#     # pylint: disable=global-statement
#     global __CRYPT_KEY__
#     if not __CRYPT_KEY__:
#         __CRYPT_KEY__ = _get_system_uuid()
#     return __CRYPT_KEY__
# 
# Path: resources/lib/common/logger.py
# LOG = 2
# APP = 'SC'
#     def debug(text):
#     def info(text):
#     def warning(text):
#     def error(text):
#     def debug(text):
#     def info(text):
#     def warning(text):
#     def error(text):
# 
# Path: resources/lib/services/Monitor.py
# class Monitor(KMonitor):
#     SORT_ASC = 1
#     SORT_DESC = 2
#     SORT_METHOD_NONE = 0
#     SORT_METHOD_RATING = 17
#     SORT_METHOD_MPAA = 31
#     SORT_METHOD_TITLE = 29
#     SORT_METHOD_YEAR = 16
#     SORT_METHOD_DATE_ADDED = 40
#     def __init__(self):
#     def onSettingsChanged(self):
#     def onScreensaverActivated(self):
#     def onScreensaverDeactivated(self):
#     def onDPMSActivated(self):
#     def onDPMSDeactivated(self):
#     def onScanStarted(self, library):
#     def onScanFinished(self, library):
#     def onCleanStarted(self, library):
#     def onCleanFinished(self, library):
#     def onSleep(self, data):
#     def onWake(self, data):
#     def PlayerOnAVStart(self, data):
#     def onNotification(self, sender, method, data):
#     def periodical_check(self):
#     def can_check(self):


# Path: resources/lib/services/websocket.py
import select
import traceback
import websocket
    import thread
    import _thread as thread
from xbmc import Monitor
from resources.lib.kodiutils import decode, sleep, get_uuid
from resources.lib.common import logger
from resources.lib.services.Monitor import monitor




class SCDispatcher:
    def __init__(self, app, ping_timeout):
        self.app = app
        self.ping_timeout = ping_timeout

    def read(self, sock, read_callback, check_callback):
        while self.app.keep_running and not monitor.abortRequested():
            logger.debug('[WS] loop')
            r, w, e = select.select(
                (self.app.sock.sock,), (), (), self.ping_timeout)
            if r:
                if not read_callback():
                    break
            check_callback()
        logger.debug('[WS] Koncim....')
        self.app.close(status=websocket.STATUS_PROTOCOL_ERROR)


class SSLSCDispatcher:
    def __init__(self, app, ping_timeout):
        self.app = app
        self.ping_timeout = ping_timeout

    def read(self, sock, read_callback, check_callback):
        while self.app.keep_running and not monitor.abortRequested():
            logger.debug('[WS] loop')
            r = self.select()

            logger.debug('[WS] loop 2 {}'.format(r))