# Este código es una modificaión del archivo original main.py.
# Solamente se agragaron lineas de código para utilizar el tópico
# "notification" con distintos valores que permitan encender, apagar
# y hacer parpadear  un led conectado en el pin 2 del ESP32
import machine
import time

ninguno = {0:0, 14:0, 2:0, 13:0, 4:0, 5:0, 12:0}
cero =    {0:1, 14:1, 2:0, 13:1, 4:1, 5:1, 12:1}
uno =     {0:0, 14:0, 2:0, 13:1, 4:0, 5:1, 12:0}
dos =     {0:0, 14:1, 2:1, 13:0, 4:1, 5:1, 12:1}
tres =    {0:0, 14:0, 2:1, 13:1, 4:1, 5:1, 12:1}
cuatro =  {0:1, 14:0, 2:1, 13:1, 4:0, 5:1, 12:0}
cinco =   {0:1, 14:0, 2:1, 13:1, 4:1, 5:0, 12:1}
seis =    {0:1, 14:1, 2:1, 13:1, 4:1, 5:0, 12:1}
siete =   {0:0, 14:0, 2:0, 13:1, 4:1, 5:1, 12:0}
ocho =    {0:1, 14:1, 2:1, 13:1, 4:1, 5:1, 12:1}
nueve =   {0:1, 14:0, 2:1, 13:1, 4:1, 5:1, 12:1}
osup  =   {0:1, 14:0, 2:1, 13:0, 4:1, 5:1, 12:0}
oinf =    {0:0, 14:1, 2:1, 13:1, 4:0, 5:0, 12:1}


def numero(lista):
    for i in lista:
        if lista[i]==1:
            pin=machine.Pin(i,machine.Pin.OUT)
            pin.on()
        else:
            pin=machine.Pin(i,machine.Pin.OUT)
            pin.off()


# Poniendo todo en OFF

numero(ninguno)
led=machine.Pin(15,machine.Pin.OUT) # Pin 15 punto decimal 
led.value(0)



def sub_cb(topic, msg):
  print((topic, msg))
  if topic == b'notification' and msg == b'received':
    print('ESP received hello message')

###########################################################
# Código agregado al main.py original
##########################################################

  if topic == b'notification' and msg == b'punto ON':
    led.value(1)
  if topic == b'notification' and msg == b'punto OFF':
    led.value(0)
  if topic == b'notification' and msg == b'punto BLINK':
    for vez in [1,2,3,4,5]:
        led.value(1)
        time.sleep(0.250)
        led.value(0)
        time.sleep(0.250)
  if topic == b'notification' and msg == b'ninguno':
    numero(ninguno)
  if topic == b'notification' and msg == b'cero':
    numero(cero)
  if topic == b'notification' and msg == b'uno':
    numero(uno)
  if topic == b'notification' and msg == b'dos':
    numero(dos)
  if topic == b'notification' and msg == b'tres':
    numero(tres)
  if topic == b'notification' and msg == b'cuatro':
    numero(cuatro)
  if topic == b'notification' and msg == b'cinco':
    numero(cinco)
  if topic == b'notification' and msg == b'seis':
    numero(seis)
  if topic == b'notification' and msg == b'siete':
    numero(siete)
  if topic == b'notification' and msg == b'ocho':
    numero(ocho)
  if topic == b'notification' and msg == b'nueve':
    numero(nueve)
  if topic == b'notification' and msg == b'osup':
    numero(osup)
  if topic == b'notification' and msg == b'oinf':
    numero(oinf)
###########################################################
def connect_and_subscribe():
  global client_id, mqtt_server, topic_sub
  client = MQTTClient(client_id, mqtt_server)
  client.set_callback(sub_cb)
  client.connect()
  client.subscribe(topic_sub)
  for v in [1,2,3,4,5]:
      numero(oinf)
      time.sleep(0.250)
      numero(osup)
      time.sleep(0.250)
  numero(ninguno)
  print('Conectado al broker MQTT %s, suscrito al tópico %s' % (mqtt_server, topic_sub))
  return client

def restart_and_reconnect():
  print('Falla al conectar al broker MQTT. Reconectando...')
  time.sleep(10)
  machine.reset()

try:
  client = connect_and_subscribe()
except OSError as e:
  restart_and_reconnect()

while True:
  try:
    client.check_msg()
    if (time.time() - last_message) > message_interval:
      msg = b'Hello #%d' % counter
      client.publish(topic_pub, msg)
      last_message = time.time()
      counter += 1
  except OSError as e:
    restart_and_reconnect()
