-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall_presence.sh
163 lines (130 loc) · 4.35 KB
/
install_presence.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
#!/bin/bash
set -e
[[ $- == *i* ]] && tput setaf 2
echo "Installing presence program"
echo """
A HC-SR501 PIR Sensor was used to test this script, but other kinds of similar sensors could be used
GPIO mapping
-------------------------
| Device | Raspberry Pi |
-------------------------
| VCC | VCC 5V |
| Out | GPIO 4 |
| GND | Ground |
-------------------------
"""
[[ $- == *i* ]] && tput sgr0
if [ ! -n "$OLT_PLATFORM" ]; then
read -p "Provide your platform URL: " OLT_PLATFORM;
fi
if [ ! -n "$OLT_TOKEN" ]; then
read -p "Provide your API Authentication-Token: " OLT_TOKEN;
fi
[[ $- == *i* ]] && tput setaf 2
echo "Create device type"
[[ $- == *i* ]] && tput sgr0
dt=`date +%s`
OLT_PRESENCE_DEVICE_TYPE=`curl -X POST \
https://api.$OLT_PLATFORM/v1/device-types \
-H "Authorization: Bearer $OLT_TOKEN" \
-H 'Content-Type: application/json' \
-d "{
\"name\": \"Presence_$dt\",
\"schema\": {
\"configuration\": {
\"presence\": {
\"type\": \"integer\"
}
}
}
}" | \
python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])"`
[[ $- == *i* ]] && tput setaf 2
echo "Create device"
[[ $- == *i* ]] && tput sgr0
OLT_PRESENCE_DEVICE=`curl -X POST \
https://api.$OLT_PLATFORM/v1/devices \
-H "Authorization: Bearer $OLT_TOKEN" \
-H 'Content-Type: application/json' \
-d "{
\"info\": {
\"name\": \"Presence_$dt\",
\"deviceTypeId\": \"$OLT_PRESENCE_DEVICE_TYPE\"
}
}" | \
python3 -c "import sys, json; print(json.load(sys.stdin)['data']['id'])"`
if [ -d /home/pi/presence ]; then
rm -rf /home/pi/presence;
fi
mkdir -p /home/pi/presence
openssl ecparam -out /home/pi/presence/device_key.pem -name prime256v1 -genkey
if [ ! -n "$OLT_TENANT" ]; then
read -p "Provide your Tenant name: " OLT_TENANT;
fi
if [ ! -n "$OLT_PRESENCE_DEVICE" ]; then
read -p "Provide your Device name: " OLT_PRESENCE_DEVICE;
fi
openssl req -new -key /home/pi/presence/device_key.pem -x509 -days 365 -out /home/pi/presence/device_cert.pem -subj '/O=$OLT_TENANT/CN=$OLT_PRESENCE_DEVICE'
echo "Your device certificate is:"
[[ $- == *i* ]] && tput sgr0
OLT_DEVICE_CERTIFICATE=$(</home/pi/presence/device_cert.pem)
OLT_DEVICE_CERTIFICATE="{\"cert\": \"${OLT_DEVICE_CERTIFICATE//$'\n'/\\\n}\", \"status\":\"valid\"}"
curl -X POST \
"https://api.$OLT_PLATFORM/v1/devices/$OLT_PRESENCE_DEVICE/certificates" \
-H "Authorization: Bearer $OLT_TOKEN" \
-H 'Content-Type: application/json' \
-d "$OLT_DEVICE_CERTIFICATE"
cat << 'EOF' > /home/pi/presence/presence.py
#!/usr/bin/python3
import time
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
import ssl
GPIO.setmode(GPIO.BCM)
GPIO_PRESENCE = 4
GPIO.setup(GPIO_PRESENCE,GPIO.IN)
EOF
echo "url=\"mqtt.$OLT_PLATFORM\"" >> /home/pi/presence/presence.py
cat << 'EOF' >> /home/pi/presence/presence.py
ca = "/home/pi/raspberrypi/olt_ca.pem"
cert = "/home/pi/presence/device_cert.pem"
private = "/home/pi/presence/device_key.pem"
try:
while True:
presence = GPIO.input(GPIO_PRESENCE)
mqttc = mqtt.Client()
ssl_context = ssl.create_default_context()
ssl_context.set_alpn_protocols(["mqttv311"])
ssl_context.load_verify_locations(cafile=ca)
ssl_context.load_cert_chain(certfile=cert, keyfile=private)
mqttc.tls_set_context(context=ssl_context)
mqttc.connect(url, port=8883)
mqttc.publish("data-ingest", '{ "type": "configuration", "value": { "presence": ' + str(presence) + ' } }')
time.sleep(0.5)
except KeyboardInterrupt:
GPIO.cleanup()
EOF
chmod +x /home/pi/presence/presence.py
cat << 'EOF' > /home/pi/presence/cron.sh
#!/bin/bash
kill $(ps aux | grep '[p]resence.py' | awk '{print $2}')
/usr/bin/python3 /home/pi/presence/presence.py > /home/pi/iot.log 2>&1 &
EOF
chmod +x /home/pi/presence/cron.sh
crontab -l > /tmp/crontabentry 2>&1 || true
if grep -q "no crontab" /tmp/crontabentry; then
echo -e "\n* * * * * /home/pi/presence/cron.sh > /home/pi/iot.log 2>&1 \n" > /tmp/crontabentry
crontab /tmp/crontabentry
fi
if ! grep -q "presence/cron.sh" /tmp/crontabentry; then
echo -e "\n* * * * * /home/pi/presence/cron.sh > /home/pi/iot.log 2>&1 \n" >> /tmp/crontabentry
crontab /tmp/crontabentry
fi
crontab -l
mkdir -p /home/pi/out
echo $OLT_PRESENCE_DEVICE_TYPE > /home/pi/out/presence_type.txt
echo $OLT_PRESENCE_DEVICE > /home/pi/out/presence.txt
[[ $- == *i* ]] && tput setaf 2
echo "Installation complete"
[[ $- == *i* ]] && tput sgr0
exit 0