# 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/ilaundry/node/util.py
# def tts_say(message, language='de'):
# 
#     # FIXME: add queue here
#     print("say:", message)
# 
#     # Google Translate TTS
#     # FIXME: urlescape "message"
#     tts_url = u'http://translate.google.com/translate_tts?tl={language}&q={message}'.format(language=language, message=urllib.parse.quote(message.encode('utf8')))
#     more_args = u''
#     if sys.platform.startswith('linux'):
#         # TODO: dynamic configuration of output device
#         more_args += u'-ao alsa:device=hw=1.0'
# 
#     user_agent = u"-http-header-fields 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'"
#     command = u"mplayer -really-quiet -noconsolecontrols {user_agent} {more_args} '{tts_url}'".format(**locals())
#     #command = command.encode('utf8')
#     print(command)
#     # FIXME: don't do this synchronously
#     # FIXME: show errors (stdout/stderr) if command fails
#     os.system(command)


import sys
from autobahn.twisted.wamp import ApplicationRunner, ApplicationSession
from twisted.internet.defer import inlineCallbacks
from twisted.python import log
from twisted.internet import reactor
from autobahn.twisted.websocket import connectWS, WampWebSocketClientProtocol, WampWebSocketClientFactory
from kotori.util.common import NodeId, get_hostname
from kotori.vendor.ilaundry.node.util import tts_say
        from kotori.vendor.ilaundry.node.feature import FeatureSet
# -*- coding: utf-8 -*-
# (c) 2014-2015 Andreas Motl, Elmyra UG <andreas.motl@elmyra.de>
#
# derived from https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/wamp/pubsub/simple/example2/client.py

node_manager = None
NODE_ID = str(NodeId())
NODE_HOSTNAME = get_hostname()


class NodeProtocol(WampWebSocketClientProtocol):
    """
    Implements various node services:
    - Heartbeat
    - Text-to-Speech
    """

    def heartbeat(self):
        self.publish("broadcast:node-heartbeat", NODE_ID)
        # FIXME: make this configurable as "heartbeat interval"
        reactor.callLater(60, self.heartbeat)

    def say(self, topic, message, language='de'):

# 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/ilaundry/node/util.py
# def tts_say(message, language='de'):
# 
#     # FIXME: add queue here
#     print("say:", message)
# 
#     # Google Translate TTS
#     # FIXME: urlescape "message"
#     tts_url = u'http://translate.google.com/translate_tts?tl={language}&q={message}'.format(language=language, message=urllib.parse.quote(message.encode('utf8')))
#     more_args = u''
#     if sys.platform.startswith('linux'):
#         # TODO: dynamic configuration of output device
#         more_args += u'-ao alsa:device=hw=1.0'
# 
#     user_agent = u"-http-header-fields 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'"
#     command = u"mplayer -really-quiet -noconsolecontrols {user_agent} {more_args} '{tts_url}'".format(**locals())
#     #command = command.encode('utf8')
#     print(command)
#     # FIXME: don't do this synchronously
#     # FIXME: show errors (stdout/stderr) if command fails
#     os.system(command)


# Path: kotori/vendor/ilaundry/node/nodeservice.py
import sys
from autobahn.twisted.wamp import ApplicationRunner, ApplicationSession
from twisted.internet.defer import inlineCallbacks
from twisted.python import log
from twisted.internet import reactor
from autobahn.twisted.websocket import connectWS, WampWebSocketClientProtocol, WampWebSocketClientFactory
from kotori.util.common import NodeId, get_hostname
from kotori.vendor.ilaundry.node.util import tts_say
        from kotori.vendor.ilaundry.node.feature import FeatureSet

# -*- coding: utf-8 -*-
# (c) 2014-2015 Andreas Motl, Elmyra UG <andreas.motl@elmyra.de>
#
# derived from https://github.com/tavendo/AutobahnPython/blob/master/examples/twisted/wamp/pubsub/simple/example2/client.py

node_manager = None
NODE_ID = str(NodeId())
NODE_HOSTNAME = get_hostname()


class NodeProtocol(WampWebSocketClientProtocol):
    """
    Implements various node services:
    - Heartbeat
    - Text-to-Speech
    """

    def heartbeat(self):
        self.publish("broadcast:node-heartbeat", NODE_ID)
        # FIXME: make this configurable as "heartbeat interval"
        reactor.callLater(60, self.heartbeat)

    def say(self, topic, message, language='de'):

        return tts_say(message, language=language)