summaryrefslogtreecommitdiff
path: root/bin/nest_monitor.sh
blob: 225f82bfacef9c4dd7373915c325ce991840579e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash

# Name or IP address of remote server
remote=""
# Full path to the img directory on remote server
rdir=""
# User name used for connection on remote server 
user=""
# Full path to temporary files directory
workdir="/tmp/nest"
# GPIO number used to drive IR LED
gpio=7
# Size of captured images thumbnails
medium=440
small=180
# Interval between captures
interval=300
# Local machine name
machine=$(hostname)

init() {
        timestamp=$(timestamp)
        # Test connection
        echo "Running nestmonitor on ${machine}, date: ${timestamp}"
        status=$(/usr/bin/ssh -q -o BatchMode=yes -o ConnectTimeout=5 ${user}@${remote} echo ok 2>&1)                                                                                                                                                                          
        if [[ $status == ok ]] ; then                                                                                                                                                                                                                                          
                echo "${timestamp}: Connection to remote server is OK"                                                                                                                                                                                                         
        elif [[ $status == "Permission denied"* ]] ; then                                                                                                                                                                                                                      
                echo "${timestamp}: User ${user} is not allowed to connect to ${remote}. Check your configuration. Exiting."                                                                                                                                                   
                exit 2                                                                                                                                                                                                                                                         
        else                                                                                                                                                                                                                                                                   
                echo "${timestamp}: There is problem with your connection to ${remote}. Exiting."                                                                                                                                                                              
                exit 2                                                                                                                                                                                                                                                         
        fi                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                               
        # Init workdir                                                                                                                                                                                                                                                         
        if ! [ -d ${workdir} ] ; then                                                                                                                                                                                                                                          
                mkdir -p ${workdir}                                                                                                                                                                                                                                            
                echo "${timestamp}: Creating working directory in ${workdir}" || exit 2                                                                                                                                                                                        
        else                                                                                                                                                                                                                                                                   
                echo "${timestamp}: Working directory ${workdir} already exists"                                                                                                                                                                                               
        fi                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                               
        # Init GPIO for LED                                                                                                                                                                                                                                                    
        echo "${timestamp}: Initializing GPIO for IR LED light"                                                                                                                                                                                                                
        /usr/bin/gpio mode ${gpio} output || exit                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                               
        # Check for camera connection                                                                                                                                                                                                                                          
        if [[ $(/usr/bin/vcgencmd get_camera | grep -q "detected=0") -eq 1 ]]; then                                                                                                                                                                                            
                echo "${timestamp}: Camera not detected. Exiting!"                                                                                                                                                                                                             
                exit 2                                                                                                                                                                                                                                                         
        else                                                                                                                                                                                                                                                                   
                echo "${timestamp}: Camera found. All systems clear for operation!"
                echo "============================================================"
                echo "Starting to take images!"
                echo "============================================================"
        fi
}

led_on() {
        echo "${timestamp}: Turning on IR LED light"
        /usr/bin/gpio write ${gpio} 1
}

led_off() {
        echo "${timestamp}: Turning off IR LED light"
        /usr/bin/gpio write ${gpio} 0
}

clean() {
        timestamp=$(timestamp)
        echo "===================================================================="
        echo "Nest monitor failed!"
        echo "===================================================================="
        echo "${timestamp}: Cleaning ${workdir}"
        rm -r ${workdir}
        echo "${timestamp}: Disabling GPIO pin for IR LED light"
        /usr/bin/gpio mode 7 input
        exit 2
}

take() {
        led_on
        echo "${timestamp}: Taking a new shot!"
        /usr/bin/raspistill --rotation 180 --hflip -awb off -awbg 1.0,1.0 -q 100 --width 1000 --height 750 --output ${workdir}/${1}-full.jpeg
        led_off
}

process() {
        echo "${timestamp}: Processing image thumbnails"
        /usr/bin/convert ${workdir}/${1}-full.jpeg -resize ${medium} ${workdir}/${1}-${medium}.jpeg || echo "Failed to create thumbnail!"
        /usr/bin/convert ${workdir}/${1}-full.jpeg -resize ${small} ${workdir}/${1}-${small}.jpeg || echo "Failed to create thumbnail!"
}

upload() {
        echo "${timestamp}: Uploading images to ${remote}"
        for i in "full" "${medium}" "${small}"; do
                /usr/bin/scp -q ${workdir}/${1}-${i}.jpeg ${user}@${remote}:${rdir}/
                rm ${workdir}/${1}-${i}.jpeg
        done
        /usr/bin/ssh -q ${user}@${remote} "/bin/ln -sf ${rdir}/${timestamp}-full.jpeg ${rdir}/latest.jpeg"
}

timestamp() {
        /bin/date +%Y%m%d%H%M
}


run() {
        init
        timestamp=$(timestamp)
        take "${timestamp}"
        process "${timestamp}"
        upload "${timestamp}"
        echo "Shot complete. See you in ${interval} seconds!"
}

run

trap clean SIGINT SIGHUP SIGQUIT SIGTERM