# Path: kotori/util/common.py
# class NodeId(Singleton):
# 
#     config = None
#     NODE_ID = 'NODE_UNKNOWN'
# 
#     def __init__(self):
#         if not self.config:
#             self.config = ConfigStoreJson()
#         if 'uuid' not in self.config:
#             self.config['uuid'] = str(uuid4())
#         self.NODE_ID = self.config['uuid']
#         logger.debug("NODE ID: {}".format(self.NODE_ID))
# 
#     def __str__(self):
#         return str(self.NODE_ID)
# 
# def get_hostname():
#     return socket.gethostname()


from string import Template
from pkg_resources import resource_filename
from twisted.logger import Logger
from twisted.internet import reactor
from twisted.web.resource import Resource
from twisted.web.server import Site
from twisted.web.static import File
from kotori.util.common import NodeId, get_hostname

log = Logger()


class CustomTemplate(Template):
    delimiter = '$$'


class WebDashboard(Resource):
    def getChild(self, name, request):
        print('getChild:', name)
        if name == '':
            return WebDashboardIndex()
        else:
            document_root = resource_filename('kotori.web', '')
            return File(document_root)


class WebDashboardIndex(Resource):

    def __init__(self, websocket_uri, filename):
        Resource.__init__(self)
        self.websocket_uri = websocket_uri
        self.filename = filename

    def render_GET(self, request):
        index = resource_filename('kotori.vendor.hydro2motion.web', self.filename)
        tpl = CustomTemplate(open(index).read())
        response = tpl.substitute({
            'websocket_uri': self.websocket_uri,

# Path: kotori/util/common.py
# class NodeId(Singleton):
# 
#     config = None
#     NODE_ID = 'NODE_UNKNOWN'
# 
#     def __init__(self):
#         if not self.config:
#             self.config = ConfigStoreJson()
#         if 'uuid' not in self.config:
#             self.config['uuid'] = str(uuid4())
#         self.NODE_ID = self.config['uuid']
#         logger.debug("NODE ID: {}".format(self.NODE_ID))
# 
#     def __str__(self):
#         return str(self.NODE_ID)
# 
# def get_hostname():
#     return socket.gethostname()


# Path: kotori/vendor/hydro2motion/web/server.py
from string import Template
from pkg_resources import resource_filename
from twisted.logger import Logger
from twisted.internet import reactor
from twisted.web.resource import Resource
from twisted.web.server import Site
from twisted.web.static import File
from kotori.util.common import NodeId, get_hostname


log = Logger()


class CustomTemplate(Template):
    delimiter = '$$'


class WebDashboard(Resource):
    def getChild(self, name, request):
        print('getChild:', name)
        if name == '':
            return WebDashboardIndex()
        else:
            document_root = resource_filename('kotori.web', '')
            return File(document_root)


class WebDashboardIndex(Resource):

    def __init__(self, websocket_uri, filename):
        Resource.__init__(self)
        self.websocket_uri = websocket_uri
        self.filename = filename

    def render_GET(self, request):
        index = resource_filename('kotori.vendor.hydro2motion.web', self.filename)
        tpl = CustomTemplate(open(index).read())
        response = tpl.substitute({
            'websocket_uri': self.websocket_uri,

            'node_id': str(NodeId()),