-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor_big_switch.py
154 lines (124 loc) · 4.22 KB
/
monitor_big_switch.py
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
#!/usr/bin/python
"""
This script runs on a RaspberryPi, monitors the switch and triggers SpaceAPI changes.
"""
from __future__ import print_function
from time import sleep
import sys
import httplib
import json
import subprocess as sp
import RPi.GPIO as GPIO
# print to STDERR
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
#===================== Exexute a shell command ====================================
def cmd_exec(cmd,args,return_output=False):
cmd_str =cmd
for arg in args:
cmd_str += ' ' + arg
try:
str1 = sp.check_output([cmd]+args, stderr=sp.STDOUT)
except sp.CalledProcessError as err:
eprint('ERROR: Execution of shell command "'+str(err.cmd)+'" failed!\n output: '+str(err.output)+'\n retunrcode: '+ str(err.returncode))
if return_output:
return err.returncode, ''
else:
return err.returncode
if return_output:
return 0, str1
else:
return 0
print('setup GPIO')
GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN) # Pin for Monitoring
GPIO.setup(3, GPIO.OUT) # Status LED
GPIO.output(3, GPIO.LOW)
print('setup GPIO: DONE')
error = False
# Block till network is online
# ping google and blinck with status LED
inet_connected = False
while not inet_connected:
print('test online status')
if cmd_exec('ping',['-c','1','-W','1','google.com']) == 0:
inet_connected = True
print('online status: connected to the internet')
else:
print('online status: offline')
for i in range(5):
GPIO.output(3, GPIO.HIGH)
sleep(.2)
GPIO.output(3, GPIO.LOW)
sleep(.2)
# try to get old state from community website (https://www.heidelberg-makerspace.de/dai-status.json)
print('get old opening state')
for i in range(3):
try:
inet_connection = httplib.HTTPSConnection('www.heidelberg-makerspace.de')
inet_connection.request('GET','/dai-status.json')
response = inet_connection.getresponse()
status_json = response.read()
inet_connection.close()
print('attempt',i,': got old opening state')
except:
eprint('attempt',i,': FAILED to get old opening state')
for i in range(3):
GPIO.output(3, GPIO.HIGH)
sleep(.4)
GPIO.output(3, GPIO.LOW)
sleep(.2)
continue
break
# Try to extract old opening state
print('extracting opnening state')
try:
status_dict = json.loads(status_json)
if status_dict['state']['open']:
state = 0
print('opnening state: OPEN')
else:
state = 1
print('opnening state: CLOSED')
except:
state = 10
eprint('extracting opnening state FAILED')
#
# MONITORING LOOP FOR THE LEVER
#
print('starting monitoring loop')
while True:
# In case of error blick with red LED
if error:
GPIO.output(3, GPIO.HIGH)
sleep(.5)
GPIO.output(3, GPIO.LOW)
sleep(.5)
continue
sleep(.5)
new_state = GPIO.input(2)
if state != new_state:
print("set state to: "+ str(GPIO.input(2)))
state = new_state
ssh_retry_count = 0
ret = -1000
if state == 0: # change state to "open"
while ret != 0 and ssh_retry_count < 2:
ssh_retry_count = ssh_retry_count + 1
ret = cmd_exec('ssh',['-i','/root/.ssh/makerspace_hb_open',
if ret != 0:
error = True
eprint('ERROR: could not change the remote state')
elif state == 1: # change state to closed
ssh_retry_count = ssh_retry_count + 1
while ret != 0 and ssh_retry_count < 2:
ssh_retry_count = ssh_retry_count + 1
ret = cmd_exec('ssh',['-i','/root/.ssh/makerspace_hb_close',
if ret != 0:
error = True
eprint('ERROR: could not change the remote state')
else:
eprint('ERROR: Invalid state "'+str(state)+'"\nValid states are "0" and "1"')
error = True