package com.ibm.javaone2016.demo.furby.sensor;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ibm.javaone2016.demo.furby.AbstractActiveSensor;

/*
 * Output sensor for converting a command into speech 
 * and outputing via available audio 
 * 
 */
public class TextToSpeechSensor extends AbstractActiveSensor {
	private String userid;
	private String password;

	@Override
	public void start() {

		while (isAlive()) {

			publishEvent("talk", "status", "ok");

			pause(10);

		}

	}

	@Override
	public String[] getCommands() {
		return new String[] { "say", "sleep" };
	}

	@Override
	public void handleCommand(String command, JsonObject object) {

		try {
			switch (command) {

			case "say":
				wake(object);
				say(object);
				break;
			case "sleep":
				sleep(object);
				break;
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private void wake(JsonObject object) {
		// TODO Auto-generated method stub
		
	}

	private void sleep(JsonObject object) {
		// TODO Auto-generated method stub
		
	}

	private void say(JsonObject object) throws IOException, IOException {
		JsonElement e = object.get("text");
		if (e == null)
			return;
		String text = e.getAsString();
		File sound = getAudioTranslation(text);
		CommandLine cmdLine = CommandLine.parse("aplay " + sound.getAbsolutePath());
		DefaultExecutor executor = new DefaultExecutor();
		executor.setExitValue(0);
		ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
		executor.setWatchdog(watchdog);
		int exitValue = executor.execute(cmdLine);

	}

	private File getAudioTranslation(String text) throws IOException {

		long then = System.currentTimeMillis();
		File result = new File("/tmp/say.wav");

		String command = "/usr/bin/curl -s  -X GET -u \"" + userid + "\":\"" + password + "\" --output "
				+ result.getAbsolutePath()
				+ " \"https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/wav&text="
				+ URLEncoder.encode(text) + "&voice=en-US_AllisonVoice\"";

		CommandLine cmdLine = CommandLine.parse(command);
		DefaultExecutor executor = new DefaultExecutor();
		executor.setExitValue(0);
		ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
		executor.setWatchdog(watchdog);
		int exitValue = executor.execute(cmdLine);
		long now = System.currentTimeMillis();

		LOG.info("got {} bytes in {} millseconds", result.length(), now - then);

		return result;

	}

	@Override
	public void configure(JsonObject serviceConfig) {

		userid = serviceConfig.get("username").getAsString();
		password = serviceConfig.get("password").getAsString();
	}

}

@@ -215,57 +215,8 @@
 or;%0A
-import org.apache.commons.exec.ExecuteException;%0A
 impo
@@ -609,16 +609,83 @@
 ssword;%0A
+%09private FurbyMotionController furby=new FurbyMotionController();%0A%09
 %0A%09@Overr
@@ -1257,45 +1257,21 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A%09%09
+furby.wake();
 %0A%09%7D%0A
@@ -1318,42 +1318,22 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub
+furby.sleep();
 %0A%09%09%0A

Enable furry controller