# Path: tilde/core/settings.py
# DB_SCHEMA_VERSION = '5.20'
# SETTINGS_FILE = 'settings.json'
# DEFAULT_SQLITE_DB = 'default.db'
# BASE_DIR = os.path.dirname(os.path.realpath(os.path.abspath(__file__)))
# ROOT_DIR = os.path.normpath(BASE_DIR + '/../')
# DATA_DIR = os.path.join(ROOT_DIR, 'data')
# EXAMPLE_DIR = os.path.join(ROOT_DIR, '../tests/data')
# INIT_DATA = os.path.join(DATA_DIR, 'sql/init-data.sql')
# TEST_DBS_FILE = os.path.join(DATA_DIR, 'test_dbs.txt')
# TEST_DBS_REF_FILE = os.path.join(DATA_DIR, 'test_dbs_ref.txt')
# SETTINGS_PATH = DATA_DIR + os.sep + SETTINGS_FILE
# GUI_URL_TPL = 'http://tilde-lab.github.io/berlinium/?http://127.0.0.1:%s' # ?https://db.tilde.pro
# DEFAULT_SETUP = {
# 
#     # General part
#     'debug_regime': False, # TODO
#     'log_dir': os.path.join(DATA_DIR, "logs"), # TODO
#     'skip_unfinished': False,
#     'skip_notenergy': False,
#     'skip_if_path': [],
# 
#     # DB part
#     'db': {
#         'default_sqlite_db': DEFAULT_SQLITE_DB,
#         'engine': 'sqlite', # if sqlite is chosen: further info is not used
#         'host': 'localhost',
#         'port': 5432,
#         'user': 'postgres',
#         'password': '',
#         'dbname': 'tilde'
#     },
# 
#     # Server part
#     'webport': 8070,
#     'title': "Tilde GUI"
# }
# def virtualize_path(item):
# def connect_url(settings, named=None):
# def connect_database(settings, named=None, no_pooling=False, default_actions=True, scoped=False):
# def write_settings(settings):
# def get_hierarchy(settings):


import sys
import time
import logging
import websocket
import ujson as json
import set_path
from tilde.core.settings import settings
class RespHandler(object):
    @classmethod
    def on_open(self, ws):
        logging.debug("Client connected")
        to_send = {'act': 'login'}
        ws.send(json.dumps(to_send))

    @classmethod
    def on_message(self, ws, message):
        logging.debug("Client got: %s" % message)
        message = json.loads(message)
        if message['act'] == 'login':
            to_send = {'act': 'sleep', 'req': 4}
            ws.send(json.dumps(to_send))
        else:
            logging.info("Client done in: %1.2f sc" % (time.time() - START_TIME))
            ws.close()
            sys.exit(0)

    @classmethod
    def on_error(self, ws, error):
        logging.error(error)

    @classmethod
    def on_close(self, ws):
        logging.debug("Client finished")
        ws.close()

if __name__ == "__main__":
    websocket.enableTrace(False)

# Path: tilde/core/settings.py
# DB_SCHEMA_VERSION = '5.20'
# SETTINGS_FILE = 'settings.json'
# DEFAULT_SQLITE_DB = 'default.db'
# BASE_DIR = os.path.dirname(os.path.realpath(os.path.abspath(__file__)))
# ROOT_DIR = os.path.normpath(BASE_DIR + '/../')
# DATA_DIR = os.path.join(ROOT_DIR, 'data')
# EXAMPLE_DIR = os.path.join(ROOT_DIR, '../tests/data')
# INIT_DATA = os.path.join(DATA_DIR, 'sql/init-data.sql')
# TEST_DBS_FILE = os.path.join(DATA_DIR, 'test_dbs.txt')
# TEST_DBS_REF_FILE = os.path.join(DATA_DIR, 'test_dbs_ref.txt')
# SETTINGS_PATH = DATA_DIR + os.sep + SETTINGS_FILE
# GUI_URL_TPL = 'http://tilde-lab.github.io/berlinium/?http://127.0.0.1:%s' # ?https://db.tilde.pro
# DEFAULT_SETUP = {
# 
#     # General part
#     'debug_regime': False, # TODO
#     'log_dir': os.path.join(DATA_DIR, "logs"), # TODO
#     'skip_unfinished': False,
#     'skip_notenergy': False,
#     'skip_if_path': [],
# 
#     # DB part
#     'db': {
#         'default_sqlite_db': DEFAULT_SQLITE_DB,
#         'engine': 'sqlite', # if sqlite is chosen: further info is not used
#         'host': 'localhost',
#         'port': 5432,
#         'user': 'postgres',
#         'password': '',
#         'dbname': 'tilde'
#     },
# 
#     # Server part
#     'webport': 8070,
#     'title': "Tilde GUI"
# }
# def virtualize_path(item):
# def connect_url(settings, named=None):
# def connect_database(settings, named=None, no_pooling=False, default_actions=True, scoped=False):
# def write_settings(settings):
# def get_hierarchy(settings):


# Path: tests/berlinium/asleep_client.py
import sys
import time
import logging
import websocket
import ujson as json
import set_path
from tilde.core.settings import settings

class RespHandler(object):
    @classmethod
    def on_open(self, ws):
        logging.debug("Client connected")
        to_send = {'act': 'login'}
        ws.send(json.dumps(to_send))

    @classmethod
    def on_message(self, ws, message):
        logging.debug("Client got: %s" % message)
        message = json.loads(message)
        if message['act'] == 'login':
            to_send = {'act': 'sleep', 'req': 4}
            ws.send(json.dumps(to_send))
        else:
            logging.info("Client done in: %1.2f sc" % (time.time() - START_TIME))
            ws.close()
            sys.exit(0)

    @classmethod
    def on_error(self, ws, error):
        logging.error(error)

    @classmethod
    def on_close(self, ws):
        logging.debug("Client finished")
        ws.close()

if __name__ == "__main__":
    websocket.enableTrace(False)

    ws = websocket.WebSocketApp("ws://localhost:%s/websocket" % settings['webport'],