#!/bin/bash
export PATH=/home/noizumi/bin:$PATH
GPIO=26
GPIO_DIR=/sys/class/gpio
MOTION_MOVIE_DIR=/ramdisk/motion/detected
MOTION_PID=/ramdisk/motion/motion.pid
MOTION_STOP=/ramdisk/motion.stop
GPS_BASE=/ramdisk/gps_base
GPS_LATLON=/home/noizumi/bin/gpslatlon.sh
# bluetooth 接続による動体検知OFF用
#BTMAC=XX:XX:XX:XX:XX:XX # OPPO Reno3 A
BTMAC=XX:XX:XX:XX:XX:XX # OpenRun Pro by Shokz
BTKEY=XX:XX:XX:XX:XX:XX # AB Shutter3
BTKEY_ACTIVATE=0
initialize(){
# GPIO26を有効化。プルアップ抵抗ON
echo $GPIO >${GPIO_DIR}/export
sleep 0.1
echo in >${GPIO_DIR}/gpio${GPIO}/direction
sleep 0.1
echo high >${GPIO_DIR}/gpio${GPIO}/direction
}
shutdown_handler(){
# GPIOクリーンアップ
echo $GPIO >$GPIO_DIR/unexport
motion_stop
exit 0
}
mail_send(){
cat << _EOD_|/usr/sbin/sendmail -t -f yuji@noizumi.org
From: yuji@noizumi.org
To: yuji@noizumi.org
Subject: Car Security alert $1
Car Security alarm was $1 at `date +"%Y/%m/%d %H:%M:%S"`
$2
_EOD_
}
sms_send(){
DATEMES=`date +"%Y/%m/%d %H:%M:%S"`
curl -X POST https://api.twilio.com/2010-04-01/Accounts/[twilioアカウント]/Messages.json \
--data-urlencode "Body=Car Security alarm was $1 at $DATEMES" \
--data-urlencode "From=[twilio電話番号]" \
--data-urlencode "To=[わしの携帯電話番号]" \
-u [twilioアカウント]:[twilioパスワード] -o /home/noizumi/logs/twilio.log
}
button_pushed(){
# 車載警報装置発報で、携帯電話のイヤホンマイクスイッチが押され、GPIO26に信号が来る
motion_start
mail_send activated
sms_send activated
# ボタンが押されている間、ループ
while [ "`cat ${GPIO_DIR}/gpio${GPIO}/value`" = "0" ]; do
sleep 1
done
mail_send stopped
sms_send stopped
}
motion_start(){
MOTION_STATUS=`curl -s http://localhost:8080/0/detection/status |grep "Detection status" | /bin/sed -r 's/.*Detection status (.*).*/\1/'`
if [ "${MOTION_STATUS}" != "ACTIVE" ]; then
curl -s http://localhost:8080/0/detection/start >/dev/null 2>&1
fi
if [ ! -e ${GPS_BASE} ]; then
GPS_NOW=`${GPS_LATLON}`
if [ "${GPS_NOW}" != "" ]; then
echo "${GPS_NOW}" > ${GPS_BASE}
fi
fi
}
motion_stop(){
MOTION_STATUS=`curl -s http://localhost:8080/0/detection/status |grep "Detection status" | /bin/sed -r 's/.*Detection status (.*).*/\1/'`
if [ "${MOTION_STATUS}" = "ACTIVE" ]; then
curl -s http://localhost:8080/0/detection/pause >/dev/null 2>&1
fi
if [ -e ${GPS_BASE} ]; then
rm -f ${GPS_BASE} || true
fi
}
bluetooth_connect(){
# 接続状態確認
CONNECTED=`cat << _EOD_|bluetoothctl|grep "Connected: yes"
power on
info ${BTMAC}
quit
_EOD_
`
# bluetooth 接続状態確認
if [ "${CONNECTED}" != "" ] || [ -e ${MOTION_STOP} ]; then
# 接続してたら、動体検知止める
motion_stop
else
# 接続してなかったら、動体検知スタート
motion_start
fi
# AB Shutter3 接続状態確認
CONNECTED=`cat << _EOD_|bluetoothctl|grep "Connected: yes"
power on
info ${BTKEY}
quit
_EOD_
`
# 接続してたら triggerhappy再起動
if [ "${CONNECTED}" != "" ] && [ -e /dev/input/event0 ]; then
if [ "${BTKEY_ACTIVATE}" != "1" ]; then
BTKEY_ACTIVATE=1
sudo systemctl restart triggerhappy.service
fi
fi
}
initialize
trap shutdown_handler 1 2 3 15
# motion を daemon モードで起動
if [ ! -e ${MOTION_PID} ]; then
mkdir -p ${MOTION_MOVIE_DIR}
motion -b
fi
while :; do
# pulseaudo 起動確認
if [ "`ps ux|grep -e \"${USER}.*pulseaudio\"|grep -v grep`" = "" ]; then
pulseaudio --start
sudo systemctl restart bluetooth.service
fi
# イヤホンマイクボタンが押された時(車載警報装置発報)
if [ "`cat ${GPIO_DIR}/gpio${GPIO}/value`" = "0" ]; then
button_pushed
fi
bluetooth_connect
if [ -e ${GPS_BASE} ]; then
GPS_SAVE=`cat ${GPS_BASE}`
GPS_NOW=`${GPS_LATLON}`
if [ "${GPS_NOW}" != "" ] && [ "${GPS_BASE}" != "" ]; then
DISTANCE=`distance.sh ${GPS_SAVE} ${GPS_NOW}`
if [ "`echo \"${DISTANCE}>100\"|bc`" = "1" ]; then
mail_send "GPS Warning! ${DISTANCE}m moved." "https://maps.google.com/maps?q=${GPS_NOW}"
echo "${GPS_NOW}" > ${GPS_BASE}
fi
fi
fi
sleep 1
done