from homie.device import HomieDevice
from machine import Pin, I2C
import settings

# import uasyncio as asyncio
# from time import ticks_ms, ticks_add, ticks_diff

from plant_node import PlantNode
from bmp280_node import BMP280Node
from plant_box_control_panel import PlantBoxControlPanel
from waterlevel_sensor import WaterLevelSensor
import bme280


def main():
    global plantNode
    i2c = I2C(scl=Pin(18), sda=Pin(19))

    wls = WaterLevelSensor(
        power_pin=Pin(26),
        adc_pin=Pin(35),)
    bmp280 = bme280.BME280(i2c=i2c)

    plantNode = PlantNode(
        id="pflanze1",
        name="Planze",
        pin_watering=Pin(13, Pin.OUT, value=0),
        pin_moisture=Pin(34),
        pin_water_tank=Pin(23),
        waterlevel_sensor=wls)

    bmp280Node = BMP280Node(
        id="bmp280",
        name="Enviroment-Sensor",
        bmp280=bmp280)

    controlNode = PlantBoxControlPanel(
        id="control", name="Benutzeroberfläche",
        i2c=i2c, waterlevel_sensor=wls, bmp280=bmp280,
        pin_clk=Pin(15), pin_dt=Pin(16), pin_sw=Pin(17))

    # Homie device setup
    homie = HomieDevice(settings)
    homie.add_node(plantNode)
    homie.add_node(bmp280Node)
    homie.add_node(controlNode)

    # run forever
    homie.run_forever()


if __name__ == "__main__":
    main()
