-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGarageDoor.py
63 lines (55 loc) · 1.98 KB
/
GarageDoor.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
import RPi.GPIO as GPIO
import json
from pprint import pprint
import urllib.request
import random
import time
import sys
class GarageDoor:
base_url = 'https://devops-tutorial-1-jploewen-1945.mybluemix.net/'
previousState = None
desiredState = None
currentState = None
# Ignore the sapce id for now
space_id = None
def __init__(self, space_id=None):
self.space_id = space_id
# self.userid = "extuser"
# self.passwd = "DRiving4AWorking/sYstem2016"
self.headers = {
'Content-type': 'application/json',
# 'X-TenantID': self.space_id,
}
def print_key(self):
print(self.space_id)
print(self.base_url)
def getCloudConfig(self):
# Need to get the current desired configuration from the cloud app
# Retrieve the JSON file
# i = 6211 + random.randint(1,4)
response = urllib.request.urlopen("http://devops-tutorial-1-jploewen-1945.mybluemix.net/garages/6213")
# Decode response with proper charset
output = response.read().decode('utf-8')
# Load output string into JSON
jsondata = json.loads(output)
self.desiredState = jsondata["desiredState"]
def getPreviousState(self):
# Reads the saved file to retrieve data about previous session
pass
def getCurrentState(self):
# Reads the GPIO to determine the current status of the garage door
# Get the current status of the door
reading = GPIO.input(27)
# If status is GPIO.LOW, magnet is close so door is closed
# Else, magnet is not close, door is open
if reading == GPIO.LOW:
self.currentState = 'closed'
elif reading == GPIO.HIGH:
self.currentState = 'open'
else:
self.currentState = None
def activate(self):
# Activate relay (simulate garage door button press)
GPIO.output(17, GPIO.LOW)
time.sleep(0.4)
GPIO.output(17, GPIO.HIGH)