From 236594b79d9eb78d6a7ec8a0d65f7e1c72a627df Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Sat, 15 Jul 2017 03:12:30 +0200 Subject: [PATCH] WIP: Snips (#138) * Initial commit snips * Update config.json * Update config.json * Update run.sh * Create mosquitto.conf * Update mosquitto.conf * Update run.sh * Update Dockerfile * Update run.sh * Update run.sh * finish it * fix spell * fix lint * Update config.json * Update config.json * Update run.sh * fix link * Update config.json * fix copy * map share into * fix unzip * fix run * fix port * fix shell --- snips/Dockerfile | 11 +++++++++ snips/asoundrc | 17 ++++++++++++++ snips/config.json | 40 +++++++++++++++++++++++++++++++ snips/mosquitto.conf | 13 ++++++++++ snips/run.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 snips/Dockerfile create mode 100644 snips/asoundrc create mode 100644 snips/config.json create mode 100644 snips/mosquitto.conf create mode 100644 snips/run.sh diff --git a/snips/Dockerfile b/snips/Dockerfile new file mode 100644 index 0000000..592f228 --- /dev/null +++ b/snips/Dockerfile @@ -0,0 +1,11 @@ +#armhf:FROM snipsdocker/platform:arm-0.4.3 +#amd64:FROM snipsdocker/platform:x86-0.4.3 + +# Copy data +COPY run.sh / +COPY mosquitto.conf /etc/ +COPY asoundrc /root/.asoundrc + +RUN chmod a+x /run.sh + +ENTRYPOINT [ "/run.sh" ] diff --git a/snips/asoundrc b/snips/asoundrc new file mode 100644 index 0000000..21e0a3f --- /dev/null +++ b/snips/asoundrc @@ -0,0 +1,17 @@ +pcm.!default { + type asym + capture.pcm "mic" + playback.pcm "speaker" +} +pcm.mic { + type plug + slave { + pcm "hw:%%MIC%%" + } +} +pcm.speaker { + type plug + slave { + pcm "hw:%%SPEAKER%%" + } +} diff --git a/snips/config.json b/snips/config.json new file mode 100644 index 0000000..215fb35 --- /dev/null +++ b/snips/config.json @@ -0,0 +1,40 @@ +{ + "name": "Snips.AI", + "version": "0.4.3", + "slug": "snips", + "description": "The first on-device voice platform", + "url": "https://home-assistant.io/addons/snips/", + "startup": "application", + "boot": "auto", + "arch": ["armhf", "amd64"], + "ports": { + "1883/tcp": 9898 + }, + "map": ["share"], + "devices": ["/dev/snd:/dev/snd:rwm"], + "options": { + "mqtt_bridge": { + "active": true, + "host": "172.17.0.1", + "port": 1883, + "user": "", + "password": "" + }, + "mic": "0,0", + "speaker": "1,0", + "assistant": "assistant.zip" + }, + "schema": { + "mqtt_bridge": { + "active": "bool", + "host": "str", + "port": "port", + "user": "str", + "password": "str" + }, + "mic": "str", + "speaker": "str", + "assistant": "str" + }, + "image": "homeassistant/{arch}-addon-snips" +} diff --git a/snips/mosquitto.conf b/snips/mosquitto.conf new file mode 100644 index 0000000..b37233d --- /dev/null +++ b/snips/mosquitto.conf @@ -0,0 +1,13 @@ +## +# defaults +listener 1883 +user root + +## +# logging +log_dest stdout + +## +# datastore +persistence true +persistence_location /data/ diff --git a/snips/run.sh b/snips/run.sh new file mode 100644 index 0000000..057f8bc --- /dev/null +++ b/snips/run.sh @@ -0,0 +1,56 @@ +#!/bin/bash +set -e + +CONFIG_PATH=/data/options.json +SNIPS_CONFIG=/data/config + +MQTT_BRIDGE=$(jq --raw-output '.mqtt_bridge.active' $CONFIG_PATH) +ASSISTANT=$(jq --raw-output '.assistant' $CONFIG_PATH) +SPEAKER=$(jq --raw-output '.speaker' $CONFIG_PATH) +MIC=$(jq --raw-output '.mic' $CONFIG_PATH) + +echo "[Info] Show audio device" +aplay -l + +echo "[Info] Setup audio device" +sed -i "s/%%SPEAKER%%/$SPEAKER/g" /root/.asoundrc +sed -i "s/%%MIC%%/$MIC/g" /root/.asoundrc + +# mqtt bridge +if [ "$MQTT_BRIDGE" == "true" ]; then + HOST=$(jq --raw-output '.mqtt_bridge.host' $CONFIG_PATH) + PORT=$(jq --raw-output '.mqtt_bridge.port' $CONFIG_PATH) + USER=$(jq --raw-output '.mqtt_bridge.user' $CONFIG_PATH) + PASSWORD=$(jq --raw-output '.mqtt_bridge.password' $CONFIG_PATH) + + echo "[Info] Setup internal mqtt bridge" + + { + echo "connection main-mqtt" + echo "address $HOST:$PORT" + } >> /etc/mosquitto.conf + + if [ ! -z "$USER" ]; then + { + echo "username $USER" + echo "password $PASSWORD" + } >> /etc/mosquitto.conf + fi + + echo "topic # OUT" >> /etc/mosquitto.conf +fi + +echo "[Info] Start internal mqtt broaker" +mosquitto -c /etc/mosquitto.conf & + +# init snips config +mkdir -p "$SNIPS_CONFIG" +ln -s "$SNIPS_CONFIG/" "/opt/snips/config" + +# check if a new assistant file exists +if [ -f "/share/$ASSISTANT" ]; then + echo "[Info] Install/Update snips assistant" + unzip -u "/share/$ASSISTANT" -d "$SNIPS_CONFIG" +fi + +/opt/snips/snips-entrypoint.sh --mqtt localhost:1883