#!/usr/bin/python3
import RPi.GPIO as GPIO
import requests, time
from bs4 import BeautifulSoup

global soup
soup=''
#yellow=37
#green=36

GPIO.setmode(GPIO.BOARD)
GPIO.setup(37,GPIO.OUT)
GPIO.setup(36,GPIO.OUT)

#garage=requests.get('http://garage.local')

def readData():
    global soup
    garage=requests.get('http://garage.local')
    soup=BeautifulSoup((garage.text),features='html.parser')
    return soup

def check():
    global soup
    dstate=soup.find(id='dstate')
    lstate=soup.find(id='lstate')
    try:
       if dstate.text=='CLOSED':
           GPIO.output(36,False)
       if dstate.text=='OPEN':
           GPIO.output(36,True)
       if lstate.text=='OFF':
           GPIO.output(37,False)
       if lstate.text=='ON':
           GPIO.output(37,True)
    except AttributeError:
       print('oops')
       pass

while True:
      readData()
      check()
      time.sleep(1)
