#!/usr/bin/env python3
#-*- coding: utf-8 -*-

""" When button pressed, then switch LED of neighbour group"""

import threading

import gpiozero
import paho.mqtt.client as mqtt

HOST = "192.168.1.200"
PORT = 1883

GPIO_BUTTON = 12
GROUP_ID_NEIGHBOUR = 'B'

def callback_pressed():
    print('Button is pressed')
    mqttc.publish( f"tutorial/{GROUP_ID_NEIGHBOUR}/BUTTON", "ON")

def callback_released():
    print('Button is released')
    mqttc.publish( f"tutorial/{GROUP_ID_NEIGHBOUR}/BUTTON", "OFF")
    
mqttc = mqtt.Client()
mqttc.connect(HOST, PORT, keepalive=60)
mqttc.loop_start()

# default Button, pull_up=True
button = gpiozero.Button(GPIO_BUTTON)
# add rising edge detection on a channel
button.when_pressed = callback_pressed
button.when_released = callback_released


wait = threading.Event()
try:
    wait.wait()
except KeyboardInterrupt:
    pass
    

mqttc.loop_stop()
mqttc.disconnect()
    
print("finished")
