<reponame>cowo78/pynextion
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import faulthandler
import functools
import logging
import sys

from pynextion import PySerialNex

from pynextion.device import NexDevice, NexEventPoller

from PyQt5 import QtCore

from Mesdan.Exceptions.Hooks.Logger import install_logger_excepthook

def printargs(message, *args, **kwargs):
    logging.info("%s - %s - %s", message, args, kwargs)


if __name__ == '__main__':
    app = QtCore.QCoreApplication([])

    # nexButtonPlus = NexButton(nexSerial, "b0", cid=1)
    # nexButtonMinus = NexButton(nexSerial, "b1", cid=3)
    # nexButtonEnter = NexButton(nexSerial, "b2", cid=4)  # noqa: F841

    # nexButtonPlus.backcolor = NamedColor.GREEN
    # nexButtonMinus.backcolor = NamedColor.RED

    # nexNumber0.value = value
    # value = nexNumber0.value
    # value = 1
    # dt_start = datetime.datetime.utcnow()
    # for i in range(0, 10):
    #     nexText.text = str(value)
    #     value += 1
    #     time.sleep(0.2)
    # new_value = nexNumber0.value
    # if new_value != value:
    #     value = new_value
    #     print(value)
    # if datetime.datetime.utcnow() - dt_start > datetime.timedelta(seconds=delay):
    #     break

    logging.basicConfig(level=logging.DEBUG, filename="qnextion.log")
    install_logger_excepthook("main")
    faulthandler.enable()

    nexSerial = PySerialNex("spy:///dev/ttyUSB0?color", baudrate=9600, timeout=0.1)
    # nexSerial = PySerialNex("/dev/ttyUSB0", timeout=0.1)

    nexDevice = NexDevice(nexSerial, 57600)
    # nexDevice = NexDevice(nexSerial, 115200)

    nexDevice.page_changed.connect(functools.partial(printargs, "Page changed"))

    page_0 = nexDevice.hook_page("home", 0)
    p0t0 = page_0.hook_widget("text", "t0", 1)
    page_1 = nexDevice.hook_page("text_button", 1)

    page_1.hook_widgets((
        ("text", "t7", 1),
        ("button", "b3", 5),
        ("button", "b2", 9),
        ("button", "b0", 8),
        ("slider", "h0", 6),
        ("progressbar", "j0", 7),
    ))
    # page_1.hook_widget("text", "t7", 1)
    # page_1.hook_widget("button", 'b1', 2)
    # p1b3 = page_1.hook_widget("button", 'b3', 5)
    # p1h0 = page_1.hook_widget("slider", "h0", 6)

    page_0["t0"].pressed.connect(functools.partial(nexDevice.select_page, page_1))
    page_1["b3"].released.connect(functools.partial(page_1["h0"].set_value, 50))
    page_1["h0"].value_changed.connect(functools.partial(printargs, "Slider, value changed"))
    page_1["b0"].pressed.connect(page_1["j0"].increase)
    page_1["b2"].pressed.connect(page_1["j0"].decrease)

    # QtCore.QTimer.singleShot(0, functools.partial(nexDevice.select_page, page_0))

    nexDevice.init()
    poller = NexEventPoller(nexDevice, 100)
    app.aboutToQuit.connect(poller.stop)

    # b3.released.connect(nexDevice.reset)

    pool = QtCore.QThreadPool()
    if not pool.tryStart(poller):
        print("Could not start poller")
        sys.exit(1)

    # widget = page1['t7']

    QtCore.QTimer.singleShot(60000, poller.stop)
    QtCore.QTimer.singleShot(61000, app.quit)

    # nexSerial.close()

    sys.exit(app.exec_())
