2018年1月20日土曜日

Google Home:喋らせるぞ!!(サービス化)

いつでも喋らせられるようにするよ♪

やっと喋るようになったので
いつでも喋らせられるようにしたい。
でもリモートコンソールを使用してサーバを起動しても、
コンソールを閉じるとサーバもダウンするんだよね。。。
さて、どうすれば良いのだろう???

やはり頼りはGoogle先生です^^;
先生に聞きまくってやっと
Google Homeで時報を知らせる
で、サービス化するとできるよと教えてくれました。
ありがとうございます。

早速、参考にしてやってみます。

1.準備
1)サービス化するスクリプトを特定する
修正した「example.js」を名前を変えて「kita.js」にした。
/home/pi/google-home-notifier/kita.js

2)nodeのパスを調べる
pi@raspberrypi:~/google-home-notifier $ which node
/usr/local/nvm/versions/node/v6.12.2/bin/node

2.サービスとして登録する
サービス化するには「Systemd」という仕組みを使うようだ。
詳しいことは分からないので。。。
HPの記載内容を参考にして作成しました。
pi@raspberrypi:~ $ cd /etc/systemd/system
pi@raspberrypi:/etc/systemd/system $ sudo vi googlehomenotifier.service

設定内容をcatで表示しておきます。
ボールド表示している部分が変更した箇所です。
pi@raspberrypi:/etc/systemd/system $ cat googlehomenotifier.service

[Unit]
Description=google-home-notifier Server
After=syslog.target network-online.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/nvm/versions/node/v6.12.2/bin/node kita.js
Restart=on-failure
RestartSec=10
KillMode=process
WorkingDirectory=/home/pi/google-home-notifier

[Install]
WantedBy=multi-user.target

3.属性を変更する。
他のサービスの属性を確認してみた。
pi@raspberrypi:/etc/systemd/system $ ls -l
・・・
drwxr-xr-x 2 root root 4096  9月  8 00:13 dhcpcd.service.d
drwxr-xr-x 2 root root 4096  9月  7 23:47 getty.target.wants
drwxr-xr-x 2 root root 4096  9月  7 23:59 getty@tty1.service.d
-rw-r--r-- 1 root root  392 12月 19 22:23 googlehomenotifier.service
drwxr-xr-x 2 root root 4096  9月  8 00:05 halt.target.wants
drwxr-xr-x 2 root root 4096 12月 19 22:40 multi-user.target.wants
・・・

実行権限が付与されているようだったので合わせて設定しました。
pi@raspberrypi:/etc/systemd/system $ sudo chmod +x googlehomenotifier.service

再表示して確認してみる。
pi@raspberrypi:/etc/systemd/system $ ls -l
・・・
drwxr-xr-x 2 root root 4096  9月  8 00:13 dhcpcd.service.d
drwxr-xr-x 2 root root 4096  9月  7 23:47 getty.target.wants
drwxr-xr-x 2 root root 4096  9月  7 23:59 getty@tty1.service.d
-rwxr-xr-x 1 root root  392 12月 19 22:23 googlehomenotifier.service
drwxr-xr-x 2 root root 4096  9月  8 00:05 halt.target.wants
drwxr-xr-x 2 root root 4096 12月 19 22:40 multi-user.target.wants
・・・

4.サービスを有効にする。
ここからはホームディレクトリで処理できるので戻しておく。
pi@raspberrypi:/etc/systemd/system $ cd

1)サービスの状態を確認してみる。
自動起動の状態
pi@raspberrypi:~ $ sudo systemctl list-unit-files --type=service | grep googlehommenotifier
googlehomenotifier.service             disabled 

起動状態
pi@raspberrypi:~ $ sudo systemctl status googlehomenotifier
● googlehomenotifier.service - google-home-notifier Server
   Loaded: loaded (/etc/systemd/system/googlehomenotifier.service; enabled; vend
   Active: inactive (dead)

2)自動起動を有効にする
pi@raspberrypi:~ $ sudo systemctl enable googlehomenotifier
Created symlink /etc/systemd/system/multi-user.target.wants/googlehomenotifier.service → /etc/systemd/system/googlehomenotifier.service.

3)サービスを起動する
pi@raspberrypi:~ $ sudo systemctl start googlehomenotifier

4)サービスの状態を確認してみる。
自動起動の状態
pi@raspberrypi:~ $ sudo systemctl list-unit-files --type=service | grep googlehommenotifier
googlehomenotifier.service             enabled

起動状態
pi@raspberrypi:~ $ sudo systemctl status googlehomenotifier
● googlehomenotifier.service - google-home-notifier Server
   Loaded: loaded (/etc/systemd/system/googlehomenotifier.service; enabled; vend
   Active: active (running) since Sat 2018-01-20 17:24:34 JST; 7min ago
 Main PID: 23138 (node)
   CGroup: /system.slice/googlehomenotifier.service
           tq23138 /usr/local/nvm/versions/node/v6.12.2/bin/node kita.js
           mq23148 ./ngrok start --none --log=stdout
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** The program 'node' call
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** Please fix your applica
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** For more information se
 1月 20 17:24:54 raspberrypi node[23138]: Endpoints:
 1月 20 17:24:54 raspberrypi node[23138]:     http://192.168.3.28:8091/google-hom
 1月 20 17:24:54 raspberrypi node[23138]:     https://627789f3.ngrok.io/google-ho
 1月 20 17:24:54 raspberrypi node[23138]: GET example:
 1月 20 17:24:54 raspberrypi node[23138]: curl -X GET https://627789f3.ngrok.io/g
 1月 20 17:24:54 raspberrypi node[23138]: POST example:
 1月 20 17:24:54 raspberrypi node[23138]: curl -X POST -d "text=Hello Google Home

5.ログを確認
googlehomenotifierの抽出して確認します。
pi@raspberrypi:~ $ journalctl -u googlehomenotifier > service.log
pi@raspberrypi:~ $ cat service.log
 1月 20 17:24:34 raspberrypi systemd[1]: Started google-home-notifier Server.
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi.
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** The program 'node' uses the Apple Bonjour compatibility layer of Avahi.
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** Please fix your application to use the native API of Avahi!
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node>
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** Please fix your application to use the native API of Avahi!
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node&f=DNSServiceRegister>
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** Please fix your application to use the native API of Avahi!
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node>
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** The program 'node' called 'DNSServiceRegister()' which is not supported (or only supported partially) in the Apple Bonjour compatibility layer of Avahi.
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** Please fix your application to use the native API of Avahi!
 1月 20 17:24:41 raspberrypi node[23138]: *** WARNING *** For more information see <http://0pointer.de/avahi-compat?s=libdns_sd&e=node&f=DNSServiceRegister>
 1月 20 17:24:54 raspberrypi node[23138]: Endpoints:
 1月 20 17:24:54 raspberrypi node[23138]:     http://192.168.3.28:8091/google-home-notifier
 1月 20 17:24:54 raspberrypi node[23138]:     https://627789f3.ngrok.io/google-home-notifier
 1月 20 17:24:54 raspberrypi node[23138]: GET example:
 1月 20 17:24:54 raspberrypi node[23138]: curl -X GET https://627789f3.ngrok.io/google-home-notifier?text=Hello+Google+Home
 1月 20 17:24:54 raspberrypi node[23138]: POST example:
 1月 20 17:24:54 raspberrypi node[23138]: curl -X POST -d "text=Hello Google Home" https://627789f3.ngrok.io/google-home-notifier
こんな感じでコンソールに表示されていたメッセージが確認できればOKです♪

コンソールから
curl -X POST -d "text=Hello Google Home" https://627789f3.ngrok.io/google-home-notifier
と試してみると喋るよ\(^o^)/

0 件のコメント:

コメントを投稿