Alexa | node-red-contrib-alexa-remote2

なに

2023/2/8 Alexa が急に止まった。
Alexaはサーバー側の不具合だった模様。 node-red-contrib-alexa-remote2-v2 も止まったけど、こっちは復帰せず Twitter見てみたら同様な人がいて
node-red-contrib-alexa-remote2-mattl0 でスクリプト編集もなく動いたというのを見かけた。
SNS使ってなくて SNSリテラシー低いです。)
何度も試してしばらく動かなかったけど、
で、最終的に動いた方法。

How to

node-red-contrib-alexa-remote2-mattl0 (node) - Node-RED で動いたのだけど、

うまく行かなかったのは

Language を ja_JPにしてしまってたから。
node-red-contrib-alexa-remote2-v2 でも動いてたのだけど、https://flows.nodered.org/node/node-red-contrib-alexa-remote2-applestrudel こっち見ると アンダーバーじゃなくハイフンになってる。
で、ja-JPにしたら動いた。
スクリプトの改変は不要でした。  

一応これで動いた。
で、プロパティ不正とか言われてる模様。
赤で囲まれてるから、まぁそうかなと思ったけど。

File path 適当に埋めたら良い模様。

で、実際に cookieファイルもできていたので、これで勝手に期限切れで動かなくなることもないのかと思われる。
(node-red-contrib-alexa-remote2-v2 の時は なぜかファイルの中身が"null"だった。が今回はjsonぽいファイルになっていた。)

[root@nas alexa-cookie2]# pwd
/root/.node-red/node_modules/alexa-cookie2
[root@nas alexa-cookie2]# ls -la
合計 80
drwxr-xr-x   4 root root   202  2月  9 23:59 .
drwxr-xr-x 103 root root  4096  2月  9 23:51 ..
-rw-r--r--   1 root root   767 10月 26  1985 .eslintrc.json
-rw-r--r--   1 root root    65 10月 26  1985 .gitattributes
-rw-r--r--   1 root root    31 10月 26  1985 .releaseconfig.json
-rw-r--r--   1 root root  1102 10月 26  1985 LICENSE
-rw-r--r--   1 root root  8870 10月 26  1985 README.md
-rw-r--r--   1 root root 34437 10月 26  1985 alexa-cookie.js
drwxr-xr-x   2 root root    42  2月  9 23:51 example
drwxr-xr-x   2 root root    50  2月  9 23:52 lib
-rw-r--r--   1 root root  1966  2月  9 23:51 package.json
-rw-r--r--   1 root root  7154  2月  9 23:59 saved_cookie.txt
[root@nas alexa-cookie2]# 

alexa | node-red-contrib-amazon-echo

なに?

Echo dot 4 で node-red-contrib-amazon-echo を使う。
(Node-RED Alexa Home Skill Bridgeをやめる。)
Home skill bridge はどこかのネットサービスを使ってる。→ ローカルでやりたい。
Echo dot 4 は tcp/80 で 機器を探しに行く。→ HTTPとかぶる。
サーバーにもう一つIp address を割り当てて、NAPT(IPマスカレード)で違う番号に変える。
詳しくは、以下参照。

Amazon Echo(Alexa) + Raspberry Pi を使ってスマートホームシステムを作ろう - Qiita

Alexa-localが突然使えなくなった!【対処法】 | 育児×家事×IoT

Amazon Echo でラズパイのコマンドを実行 – Linux & Android Dialy

【RaspberryPi】Amazon Echoを使った音声認識でNode-REDの処理を行う - uepon日々の備忘録

iptables ばかりで firewalld でやってる人がいなくて苦労したのでメモ。
(firewalld をわかってなかっただけ)

IP address を増やす

[root@nas ssh]# nmcli connection modify enp1s0 +ipv4.addresses 192.168.0.102/24
[root@nas ssh]# nmcli connection up enp1s0

[root@nas ssh]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.101/24 brd 192.168.0.255 scope global noprefixroute enp1s0
       valid_lft forever preferred_lft forever
    inet 192.168.0.102/24 brd 192.168.0.255 scope global secondary noprefixroute enp1s0
       valid_lft forever preferred_lft forever
    inet6 ....

参照 genchan.net

NAPT (Network Address Port Translation)

[root@nas zones]# firewall-cmd --zone=public --add-rich-rule='rule family=ipv4 destination address=192.168.0.102/32 forward-port port=80 protocol=tcp to-port=8111 to-addr=192.168.0.101' --permanent
success
[root@nas zones]# firewall-cmd --reload
success


[root@nas zones]# firewall-cmd --zone=public --list-all
public (active)
  target: ACCEPT
  icmp-block-inversion: no
  interfaces: enp1s0
  sources: 
  services: cockpit dhcpv6-client ftp http nodered plexmediaserver samba smtp smtps ssh vnc-server
  ports: 8888/tcp 1880/tcp 3456/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
    rule family="ipv4" destination address="192.168.0.102/32" forward-port port="80" protocol="tcp" to-port="8111" to-addr="192.168.0.101"


[root@nas zones]# cat /etc/firewalld/zones/public.xml
<?xml version="1.0" encoding="utf-8"?>
<zone target="ACCEPT">
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="ssh"/>
  <service name="dhcpv6-client"/>
  <service name="cockpit"/>
  <service name="samba"/>
  <service name="vnc-server"/>
  <service name="http"/>
  <service name="smtp"/>
  <service name="smtps"/>
  <service name="ftp"/>
  <service name="plexmediaserver"/>
  <service name="nodered"/>
  <port port="8888" protocol="tcp"/>
  <port port="1880" protocol="tcp"/>
  <port port="3456" protocol="tcp"/>
  <rule family="ipv4">
    <destination address="192.168.0.102/32"/>
    <forward-port port="80" protocol="tcp" to-port="8111" to-addr="192.168.0.101"/>
  </rule>
</zone>

rich rule

rule family=(ipv4/ipv6)
② [ source address =(souce address)[/mask] [invert="true"] ]
② [ destination address =(souce address)[/mask] [invert="true"] ]
③ [ service name=(service) ]
③ [ port port=(portid) protocol=(protocol) ]
③ [ forward-port port=(portid) protocol=(protocol) to-port=(portid) to-addr=(address) ]
④ [ log [ prefix=(prefix) ] [ level=(loglevel) ] [ limit value=(rate)/(duration) ]
⑤ [ masquerade ]
⑥ [ accept|reject|drop ]

参照 www.nedia.ne.jp

Appendix

Echo から 出てるのをみる。

[root@nas ssh]# tcpdump src host 192.168.0.15
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on enp1s0, link-type EN10MB (Ethernet), capture size 262144 bytes
23:56:00.214981 IP 192.168.0.15.50000 > 239.255.255.250.ssdp: UDP, length 94
23:56:00.215138 IP 192.168.0.15.50000 > 239.255.255.250.ssdp: UDP, length 101
23:56:00.222416 IP 192.168.0.15.50032 > 255.255.255.255.56700: UDP, length 36
23:56:00.222681 IP 192.168.0.15.50032 > 255.255.255.255.56700: UDP, length 36
....

Alexa | Echo dot bluetooth接続時に「接続しました」を止める。(不完全)

なに?

基本的に個人的なメモです。
誰かのヒントにはなるかもしれないけど、見たままやればできるような説明はないです。めんどくさいので。すいません。

Background

Echo に bluetooth で接続している機材があると、たまに、外れたり接続復活したりした時にいきなり喋ります。
止めるオプションはない模様。
jp.amazonforum.com USのフォーラムでは チップにハードコードされている といった書き込みもあり、amazon staff も

"We're sorry for this frustration. The Bluetooth connection notification cannot be disabled."

と言い切っちゃってる。
以下参照

amazonforums.force.com

Alexa | Echo dot 4 clock で音楽ファイルを再生しようとしてわかったこと。 - Mobile Development Memorandum の後、ラズパイを使って再生するとかを見て、余ってるAndroid端末を使いAlexaから手持ちの音楽を再生してました。(node-red と Node-RED Alexa Home Skill Bridge をつかう)
いきなり喋るのやめて欲しい。

本題

接続しました。 を止める方法

1.まずは、不意なタイミングで鳴るのを止める方法

bluetooth に繋いでいるデバイス(自分の場合Android)側で bluetooth を Off にしてれば勝手に喋ることはないので、使う時だけ On にし、使い終わったら(再生が終了したら) 自動で Off にする。(Android の自動化はmacrodorid)
Echo がわの bluetooth の On/Off を node-red からコントロールも試したがいまいちうまくない。

2.使う時(On/Off時)に喋るのを(できるだけ)止める方法

Alexa の 定型アクション で音量変更をすると確認音が鳴るのを、直前に「echo のオーディオを停止」(echo は付けたechoの名前)を入れると、音が鳴らないのに気づいたので、bluetooth に接続する時にこれを入れるようにする。だけです。
だけですけど、bluetooth 接続は完了するタイミングが不定なのでオーディオ停止を1度入れただけでは、ほとんど止まらないです。
ので、node-red で 何度もコマンドを送る感じです。
あとは、alexa の定型アクション と macrodroid と node-red (Node-RED Alexa Home Skill Bridge, node-red-contrib-alexa-remote2-v2) を組み合わせて、ゴニョゴニョやっただけです。
不完全 のタイトル通り、完璧な動作には至りません。
タイミング的に、ぴぽっ! の半分くらいは鳴ることが多い。「接続しました」とか「〜〜〜で再生します」の部分はほぼ鳴らないです。 なんかうまくいかなくなった。めんどくさいので放置。
ちなみに、DND(Do not disturb/おやすみモード)では、この音は止まらないようです。何のためのDND??
ちなみにちなみに、鳴る間だけvolume 0 にする方法ではだめでした。勝手にvolume 1 になる。

beep 音は諦める。方法。(未確認)

Beep 音ではなく、喋る音声だけなら以下の方法で止まる模様。(未確認)

I checked with Customer Service on disabling this feature and this worked for me. I have Echo dot 4th Gen.

  1. Go to Settings on the Alexa app

  2. Choose Device settings

  3. Select the specific device

  4. In that device specific settings, you'll find an option called Communication

  5. Under Communications disable Announcements.

After disabling I restarted my Echo device. Now, it is only making the beep sound when connecting to and disconnecting from my laptop with Bluetooth. Also, it's not making the "Playing from ..." announcement when media is played. Hope this helps.

以下参照 www.reddit.com

以下メモ

CentOS8(常時稼働自家サーバー) に node-red のインストール

以下を参照 serverarekore.blogspot.com

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/rpm/update-nodejs-and-nodered)

boot 時に node-red 実行

以下を参照 nodered.jp

sudo npm install -g pm2

pm2 start /usr/bin/node-red -- -v

pm2 save
#  pm2 startup
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=ssh
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/home/ssh/.local/bin:/home/ssh/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-ssh.service
Command list
[ 'systemctl enable pm2-ssh' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-ssh.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-ssh...
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd

再起動したらどうもうまく行かないので、以下参照

https://yasuttiblog.inet-yt.jp/2018/09/28/pm2%E3%81%A7node-red%E3%82%92%E8%B5%B7%E5%8B%95/yasuttiblog.inet-yt.jp

が、内容が違うので、まぁ、ドキュメント読むより勘で、以下でうまく行ったっぽい。root です。外に開いてないので。

sudo env PATH=$PATH:/usr/local/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u root --hp /root

node-red

使ったのは Node-RED Alexa Home Skill Bridge

alexa-node-red.bm.hardill.me.uk

以下も参照

chasuke.com

qiita.com

と node-red-contrib-alexa-remote2-v2 3.10.5

flows.nodered.org

node-red-contrib-alexa-remote2 (node) - Node-RED というのもあるけど、v2が新しい。ぽい。違いはわからない。どっちも、amazon.co.jp で使うにはソースの改変が必要。(同じで大丈夫ぽい。alexa-cookie2が同じかな?)

[root@nas alexa-cookie2]# pwd
/root/.node-red/node_modules/alexa-cookie2
[root@nas alexa-cookie2]# ls -la
合計 64
drwxr-xr-x   5 root root   171  2月  4 20:54 .
drwxr-xr-x 103 root root  4096  2月  4 20:54 ..
-rw-r--r--   1 root root    78 10月 26  1985 .commitmessage
-rw-r--r--   1 root root    65 10月 26  1985 .gitattributes
-rw-r--r--   1 root root  1102 10月 26  1985 LICENSE
-rw-r--r--   1 root root  7059 10月 26  1985 README.md
-rw-r--r--   1 root root 33210 10月 26  1985 alexa-cookie.js
drwxr-xr-x   2 root root    42  2月  4 20:54 example
drwxr-xr-x   2 root root    50  2月  4 20:57 lib
drwxr-xr-x   3 root root    20  2月  4 20:54 node_modules
-rw-r--r--   1 root root  1840  2月  4 20:54 package.json
[root@nas alexa-cookie2]# cp alexa-cookie.js alexa-cookie.js.org
[root@nas alexa-cookie2]# ls -la
合計 100
drwxr-xr-x   5 root root   198  2月  4 20:59 .
drwxr-xr-x 103 root root  4096  2月  4 20:54 ..
-rw-r--r--   1 root root    78 10月 26  1985 .commitmessage
-rw-r--r--   1 root root    65 10月 26  1985 .gitattributes
-rw-r--r--   1 root root  1102 10月 26  1985 LICENSE
-rw-r--r--   1 root root  7059 10月 26  1985 README.md
-rw-r--r--   1 root root 33210 10月 26  1985 alexa-cookie.js
-rw-r--r--   1 root root 33210  2月  4 20:59 alexa-cookie.js.org
drwxr-xr-x   2 root root    42  2月  4 20:54 example
drwxr-xr-x   2 root root    50  2月  4 20:57 lib
drwxr-xr-x   3 root root    20  2月  4 20:54 node_modules
-rw-r--r--   1 root root  1840  2月  4 20:54 package.json
[root@nas alexa-cookie2]# vim alexa-cookie.js

line 261 _options.baseAmazonPage を 日本ドメイン

_options = __options;
_options.baseAmazonPage='amazon.co.jp';  //<--この行を追加
[root@nas alexa-cookie2]# cd lib
[root@nas lib]# pwd
/root/.node-red/node_modules/alexa-cookie2/lib
[root@nas lib]# ls -la
合計 24
drwxr-xr-x 2 root root    50  2月  4 20:57 .
drwxr-xr-x 5 root root   198  2月  4 21:00 ..
-rw-r--r-- 1 root root   777  2月  4 20:57 formerDataStore.json
-rw-r--r-- 1 root root 19280 10月 26  1985 proxy.js
[root@nas lib]# cp proxy.js proxy.js.org
[root@nas lib]# ls -la
合計 44
drwxr-xr-x 2 root root    70  2月  4 21:01 .
drwxr-xr-x 5 root root   198  2月  4 21:00 ..
-rw-r--r-- 1 root root   777  2月  4 20:57 formerDataStore.json
-rw-r--r-- 1 root root 19280 10月 26  1985 proxy.js
-rw-r--r-- 1 root root 19280  2月  4 21:01 proxy.js.org
[root@nas lib]# vim proxy.js

line 181 returnedInitUrl を 日本ドメインに(詳細は知らない。)
//returnedInitUrl = https://www.${_options.baseAmazonPage}/ap/signin?openid.return_to=https%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fmaplanding&openid.assoc_handle=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&pageId=amzn_dp_project_dee_ios${_options.baseAmazonPageHandle}&accountStatusPolicy=P1&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns.oa2=http%3A%2F%2Fwww.${_options.baseAmazonPage}%2Fap%2Fext%2Foauth%2F2&openid.oa2.client_id=device%3A${deviceId}&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.oa2.response_type=token&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.pape.max_auth_age=0&openid.oa2.scope=device_auth_access&language=${_options.amazonPageProxyLanguage};

returnedInitUrl = https://www.amazon.co.jp/ap/signin?showRmrMe=1&openid.return_to=https%3A%2F%2Falexa.amazon.co.jp%2F&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=amzn_dp_project_dee_jp&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&;

はてなMarkdownのコードシンタックスが1行が長いと文字が切れてしまうんだがなんだこれ。)

node-red restart

[root@nas lib]# systemctl restart nodered.service

以下参照 dream-soft.mydns.jp

で node-red はこんな感じ。

alexa に ミュージックプレーヤー とか言えば、定型アクションからnode-redがトリガーされhttp request から webhook で macrodroid をトリガーして、音楽プレーヤーアプリ(AIMP)を起動し再生開始。
set msg.counter -> switch -> delay 1s -> increment -> routine stop audio のループは while 的な感じ。
routine stop audio が 「echo のオーディオを停止」を 単独で 定型アクションとしてalexa に登録したもの。
[get] /stop_audio は get request で macrodroid 側から任意のタイミングで「echo のオーディオを停止」を 呼べるようにしたもの。
これ以上の説明はめんどくさいんで略。あとは察して。

Mac | pmset

まずは、man

よくわからんけど、検索したら出てきた。 www.unix.com

man pmset

と一緒です。たぶん。
使ってないと忘れるんだけど、抜け方は "q"

pmset -a

で、

EXAMPLES
     This command sets displaysleep to a 5 minute timer on battery power, leaving other settings on battery power and
     other power sources unperturbed.

     pmset -b displaysleep 5

て、書いてあるんで、

pmset -a displaysleep 5

とやったら、変わらなかった。
-a は

The -a, -b, -c, -u flags determine whether the settings apply to battery ( -b ), charger (wall power) ( -c ), UPS ( -u ) or all ( -a ).

で、

pmset -a displaysleep 5 disksleep 10 sleep 1 womp 0

とやったら変わった。
-a の時は、他の設定も一緒にしないと変わらないの??
Macbook air M1 だから?
分かりませんが、変わったんで追求してません。
それだけ。

関係ないけど

perturb って単語知らなかった。

Mac | プログラマティックに電源入り切り part.2

色々あって root の cron で pmset が上手くないので、違うことを考えてみた。
個人メモなので解説などないです。

visudo

ユーザー名は administrator。
pmset は root でないと変更不可。
最終行に入れないとダメという噂もある。

administrator   ALL=(ALL) NOPASSWD: /usr/bin/pmset

~/.sleep

root の cron を諦め sleepwatcher で。

#!/bin/bash

now=`/opt/homebrew/bin/gdate +%s`
task1=`/opt/homebrew/bin/gdate -d "03:00:00" +%s`
task2=`/opt/homebrew/bin/gdate -d "09:00:00" +%s`
task3=`/opt/homebrew/bin/gdate -d "19:00:00" +%s`

if [ $now -lt $task1 ]; then
    sudo pmset repeat wakeorpoweron MTWRFSU 03:00:00 sleep MTWRFSU 03:10:00
elif [ $now -lt $task2 ]; then
    sudo pmset repeat wakeorpoweron MTWRFSU 09:00:00 sleep MTWRFSU 09:10:00
elif [ $now -lt $task3 ]; then
    sudo pmset repeat wakeorpoweron MTWRFSU 19:00:00 sleep MTWRFSU 19:10:00
else
    sudo pmset repeat wakeorpoweron MTWRFSU 03:00:00 sleep MTWRFSU 03:10:00
fi

回したいタスクは ユーザーの cron に。

gdate

Mac の date はつかえん!

date: illegal option -- d
usage: date [-jnRu] [-r seconds|file] [-v[+|-]val[ymwdHMS]]
            [-I[date | hours | minutes | seconds]]
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

GUN coreutils 入れる。

brew install coreutils

~/.bash_profile に alias してもいいけど。まぁいいや。

Mac | sleep, wake 時に実行

テキトーやったら動いたから満足なので、詳しいことは知らない(調べてない)です。
検索結果のまんまじゃなかったので、自分用メモ。

Install

brew install sleepwatcher

start

brew services start sleepwatcher

script

touch ~/.sleep ~/.wakeup
chmod +x ~/.sleep ~/.wakeup

.plist をコピーしてとか、書いてあるのを参考にしたけど、元ファイルが存在しないし、インストール時にも

For SleepWatcher to work, you will need to write sleep and
wakeup scripts, located here when using brew services:

  ~/.sleep
  ~/.wakeup

と出てたので、多分いらない(いらなくなった)のでしょう。

~/.sleep
~/.wakeup

#!/bin/bash

で書いたらそれだけで動いた。

brew info sleepwatcher 

でなんか、infoがみれる。
おわり。

Mac | プログラマティックに電源入り切り

まんま答えがあった。ブックマーク的メモ

qiita.com

動作

sleep, wake, shutdown, wakeorpoweron

曜日

MTWRFSU

実際に入れたもの。 root で

45  2   *   *   *   /path/to/script
46  2   *   *   *   pmset repeat sleep MTWRFSU 02:59:00 wakeorpoweron MTWRFSU 08:44:00

45  8   *   *   *   /path/to/script
46  8   *   *   *   pmset repeat sleep MTWRFSU 08:59:00 wakeorpoweron MTWRFSU 18:44:00

45  18  *   *   *   /path/to/script
46  18  *   *   *   pmset repeat sleep MTWRFSU 18:59:00 wakeorpoweron MTWRFSU 02:44:00

おわり。