import network
import time
import urequests
import requests
from machine import Pin

collection_api = "https://getbuildings-teydd2m6qq-uc.a.run.app";
document_api = "https://getbuilding-teydd2m6qq-uc.a.run.app/?ID=32mWZ7dJQcSf72ih8Lkl";

print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
  print(".", end="")
  time.sleep(0.1)
print(" Connected!")

buildingState = []
stateCount = 0

paramList = {"ID": "32mWZ7dJQcSf72ih8Lkl"}

def init_device():
  response = urequests.request('GET', document_api)
  geyserList = response.json()['geysers']
  print(response.json())

  for x in geyserList:
    print(x['State'])
    p = int(x['Pin'])
    v = bool(x['State'])

    print(p)
    print(v)

    buildingState.append(Pin(p, Pin.OUT, value=v))
    
  print(buildingState)

def toggle_state(id):
  ''' change state '''
  print("State change: ", id)
  p = buildingState[id]
  s = buildingState[id].value()

  p.on() if s == 0 else p.off()

init_device()

time.sleep(5)

toggle_state(1)
toggle_state(0)