it:camswitch
#!/usr/bin/env bash LOG_FILE=/var/log/camswitch.log AWAY_THRESHOLD=3 CHECK_INTERVAL=50 CHECK_DURATION=7 CAMERA_IP=192.168.100.7 PROGRAM_NAME=$(basename $0) function log { time=$(date "+%Y-%m-%d %H:%M:%S") echo "[$time] $1" | tee -a $LOG_FILE } function check_presence { chipolo_id=C18002ABD9578 check_result=$(timeout --preserve-status --signal=SIGINT --foreground ${CHECK_DURATION}s hcitool lescan) if [ $? -ne 0 ]; then log "hcitool lescan failed, restarting hciuart.service" systemctl restart hciuart.service sleep $CHECK_INTERVAL continue fi if [ $(echo "$check_result" | grep -c $chipolo_id) -gt 0 ]; then presence_status=present else presence_status=away fi log "chipolo ($chipolo_id) $presence_status" #log "away count: $away_count" #log "last status: $last_presence_status" } function switch_camera_off { log "switching camera off" echo -en '\xAF\xFF\x02\x02\xDF' > /dev/ttyUSB0 sleep 3 echo -en '\xAF\xFF\x02\x02\xDF' > /dev/ttyUSB0 } function switch_camera_on { log "switching camera on" echo -en '\xAF\xFF\x01\x01\xDF' > /dev/ttyUSB0 sleep 3 echo -en '\xAF\xFF\x01\x01\xDF' > /dev/ttyUSB0 } function check_camera { ping -c3 -q $CAMERA_IP if [ $? -eq 0 ]; then camera_status="pingable" else camera_status="not pingable" fi log "camera $camera_status" } away_count=0 last_presence_status=present ######################### log "$PROGRAM_NAME started" trap 'log "$PROGRAM_NAME stopped"; exit' SIGINT SIGTERM SIGHUP while true; do check_presence if [ "$presence_status" = "away" ] ; then if [ $away_count -ne $AWAY_THRESHOLD ]; then away_count=$((away_count+1)) else check_camera if [ "$camera_status" = "not pingable" ]; then switch_camera_on fi fi fi if [ "$presence_status" = "present" ]; then #if [ $away_count -ge $AWAY_THRESHOLD ]; then check_camera if [ "$camera_status" = "pingable" ]; then switch_camera_off sleep 3 check_camera fi if [ "$last_presence_status" = "away" ]; then away_count=0 fi #fi fi sleep $CHECK_INTERVAL last_presence_status=$presence_status done
it/camswitch.txt · Utolsó módosítás: 2017/03/22 23:06 szerkesztette: rblst