package org.lskk.lumen.avatar.nao;

import com.aldebaran.proxy.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.language.HeaderExpression;
import org.apache.commons.lang3.StringUtils;
import org.lskk.lumen.core.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.inject.Inject;

/**
 * Created by ceefour on 10/2/15.
 */
@Component
public class SpeechExpressionRouter extends RouteBuilder {

    private static final Logger log = LoggerFactory.getLogger(SpeechExpressionRouter.class);

    @Inject
    private ToJson toJson;
    @Inject
    private ALTextToSpeechProxy tts;

    @Override
    public void configure() throws Exception {
        // lumen.speech.expression
        from("rabbitmq://localhost/amq.topic?connectionFactory=#amqpConnFactory&exchangeType=topic&autoDelete=false&routingKey=lumen.speech.expression")
                .to("log:IN.lumen.speech.expression?showHeaders=true&showAll=true&multiline=true")
                .process(exchange -> {
                    final LumenThing thing = toJson.getMapper().readValue(
                            exchange.getIn().getBody(byte[].class), LumenThing.class);
                    log.info("Got speech.expression command: {}", thing);
                    if (thing instanceof Speech) {
                        final Speech speech = (Speech) thing;
                        if (StringUtils.startsWith(speech.getAvatarId(), "nao")) {
                            log.info("Speaking for {}: {}", speech.getAvatarId(), speech.getMarkup());
                            tts.say(speech.getMarkup());
                            log.debug("Spoken for {}: {}", speech.getAvatarId(), speech.getMarkup());
                        }
                    }

                    // reply
                    exchange.getOut().setBody("{}");
                    final String replyTo = exchange.getIn().getHeader("rabbitmq.REPLY_TO", String.class);
                    if (replyTo != null) {
                        log.debug("Sending reply to {} ...", replyTo);
                        exchange.getOut().setHeader("rabbitmq.ROUTING_KEY", replyTo);
                        exchange.getOut().setHeader("rabbitmq.EXCHANGE_NAME", "");
                        exchange.getOut().setHeader("recipients",
                                "rabbitmq://localhost/dummy?connectionFactory=#amqpConnFactory&autoDelete=false,log:OUT.lumen.speech.expression");
                    } else {
                        exchange.getOut().setHeader("recipients", "log:OUT.lumen.speech.expression");
                    }
                })
                .routingSlip(new HeaderExpression("recipients"));
    }
}

@@ -765,16 +765,198 @@
 ression%0A
+        // TODO: we should delay e.g. 500ms to see if speech-expression handles it (and notifies with actionStatus=ACTIVE_ACTION_STATUS),%0A        // otherwise NAO TTS will handle it%0A
         
@@ -1100,16 +1100,16 @@
 ssion%22)%0A
-
         
@@ -1515,22 +1515,33 @@
 tanceof 
-Speech
+CommunicateAction
 ) %7B%0A    
@@ -1570,31 +1570,64 @@
 nal 
-Speech speech = (Speech
+CommunicateAction communicateAction = (CommunicateAction
 ) th
@@ -1682,22 +1682,33 @@
 rtsWith(
-speech
+communicateAction
 .getAvat
@@ -1771,16 +1771,19 @@
 Speaking
+ %7B%7D
  for %7B%7D:
@@ -1792,46 +1792,103 @@
 %7D%22, 
-speech.getAvatarId(), speech.getMarkup
+communicateAction.getInLanguage(), communicateAction.getAvatarId(), communicateAction.getObject
 ());
@@ -1924,32 +1924,43 @@
 tts.say(
-speech.getMarkup
+communicateAction.getObject
 ());%0A   
@@ -2001,16 +2001,19 @@
 (%22Spoken
+ %7B%7D
  for %7B%7D:
@@ -2022,46 +2022,103 @@
 %7D%22, 
-speech.getAvatarId(), speech.getMarkup
+communicateAction.getInLanguage(), communicateAction.getAvatarId(), communicateAction.getObject
 ());

Prepare to integrate Expressive Speech Synthesis by @ulitiaaa