-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathocs.sh
executable file
·190 lines (158 loc) · 4.95 KB
/
ocs.sh
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash
# Set configurable variables
source "/opt/uas/Occupancy/ocs.cfg"
set -euo pipefail
###############################################################################
# Occupancy Service
# Unallocated Space
# Written by Pax - 2014-11-08
# filename = ocs.sh
#
# Startup instructions:
# run at startup, and restart it if it crashes
#
# Must have access to /tmp/ to write JPEGs and TXT files
###############################################################################
# log()
# A function to make log messages consistant
log()
{
echo "[$(date "+%Y-%m-%d %T")]: $*" >> "${OCS_LOGFILE}"
}
# getWallPicture()
# moves camera to preset 'TheWall' and sets flag
# then takes picture and puts it at /tmp/thewall.jpg
getWallPicture ()
{
curl -s "http://${OCS_AXISCAMERA_IP}/axis-cgi/com/ptz.cgi?gotoserverpresetname=TheWall&camera=1"
sleep 3
curl -s "http://${OCS_AXISCAMERA_IP}/axis-cgi/com/ptz.cgi?camera=1&rzoom=-2500"
sleep 1
curl -s "http://${OCS_AXISCAMERA_IP}/axis-cgi/com/ptz.cgi?camera=1&rzoom=+2500"
sleep 4
wget "http://${OCS_AXISCAMERA_IP}/axis-cgi/jpg/image.cgi" -q -O "${OCS_TMP_WALL}"
sleep 1
}
# currentlyOpen()
# Get the current override status from the website
currentlyOpen()
{
[ -f "${OCS_OVERRIDE_FILE}" ]
}
###############################################################################
# Website functions
#
# pushStatusToWebsite()
# moves the /tmp/status file to the websites status file
pushStatusToWebsite ()
{
log "Call to pushStatusToWebsite"
ftp -n "${OCS_UAS_URL}" << END_FTP_COMMANDS
quote USER ${OCS_UAS_USER}
quote PASS ${OCS_UAS_PASS}
ascii
passive
put ${OCS_TMP_STATUS} ${OCS_UAS_STATUS_FILE}
quit
END_FTP_COMMANDS
}
# pushWallToWebsite()
# moves the /tmp/thewall.jpg file to the websites status file
pushWallToWebsite ()
{
stamp=$(date '+%F_%T')
ftp -n "${OCS_UAS_URL}" << END_FTP_COMMANDS
quote USER ${OCS_UAS_USER}
quote PASS ${OCS_UAS_PASS}
ascii
passive
put ${OCS_TMP_WALL} ${OCS_UAS_WALL_FILE}
put ${OCS_TMP_WALL} ${OCS_UAS_WALL_ARCHIVE_FILEPATH}/${stamp}.jpg
quit
END_FTP_COMMANDS
#nc "${OCS_IRC_IP}" "${OCS_IRC_PORT}" \
# "!JSON" \
# "{\"Service\":${OCS_IRC_SERVICE}, \"Key\":${OCS_IRC_KEY}, \"Data\":\"New Wall Image: http://${OCS_UAS_WALL_ARCHIVE_FILEPATH}/${stamp}.jpg\"}" \
# &>/dev/null
}
# openTheSpace()
# Tells the world that we're open by posting to all of our various social media
# and other services
openTheSpace()
{
# status file
echo "The space is currently open (Updated: $(date '+%m/%d %H:%M'))" > "${OCS_TMP_STATUS}"
# website status
pushStatusToWebsite
# Tweet (not correct yet)
python /opt/uas/statustweet/statustweet.py "$(cat "${OCS_TMP_STATUS}") #Unallocated" &>/dev/null
# IRC
#curl -X POST 127.0.0.1:9999/ --data '{"Service":"Occupancy","Data":"The space is now open"}'
#Wall image to website
getWallPicture
pushWallToWebsite
}
# closeTheSpace()
# Tells the world that we're closed by posting to all of our various social
# media and other services
closeTheSpace()
{
#Update flags, IRC, website status file, checkin, logging
echo "The space is currently closed (Updated: $(date '+%m/%d %H:%M'))" > "${OCS_TMP_STATUS}"
#website status
pushStatusToWebsite
#checkin
#python "${OCS_CHECKIN_SCRIPT}" "closing"
# Twitter
python /opt/uas/statustweet/statustweet.py "$(cat "${OCS_TMP_STATUS}") #Unallocated" &>/dev/null
# IRC
#curl -X POST 127.0.0.1:9999/ --data '{"Service":"Occupancy","Data":"The space is now closed"}'
}
# cleanUp()
# Perform any necessary script clean up here like deleting the PID
cleanUp()
{
log "Caught signal, exiting"
rm "${OCS_PID_FILE_PATH}"
exit
}
###############################################################################
# main()
# Main logic function
# Runs in loop to constantly check occupancy
main ()
{
# Write the PID to file for the service script
echo $BASHPID > "${OCS_PID_FILE_PATH}"
# Capture signals so we clean up the pid file properly.
trap cleanUp SIGHUP SIGINT SIGTERM
#Inital values/flags
log "STARTING ocs.sh"
previouslyOpen=false
#Loop
while true; do
if currentlyOpen && $previouslyOpen ; then
: # do nothing
elif ! currentlyOpen && $previouslyOpen ; then
closeTheSpace
log "Space is CLOSED"
previouslyOpen=false
elif currentlyOpen && ! $previouslyOpen; then
openTheSpace
log "Space is OPEN"
previouslyOpen=true
elif ! currentlyOpen && ! $previouslyOpen; then
: # do nothing
fi
sleep "${OCS_DELAY}"
done
# We'll probably never reach here properly, but if we do, clean up the PID file.
rm "$OCS_PID_FILE_PATH"
}
###############################################################################
# Script Entry
# All functions and variables need to be set above these lines
# (i.e. keep this at the end)
log "starting main"
main
exit 0