# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'tractorpull_main.ui'
#
# Created by: PyQt5 UI code generator 5.11.3

from PyQt5 import QtCore, QtGui, QtWidgets

import sys
import threading
#import mqtt_client_tractorpull

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

lock = threading.Lock()

ip = "172.22.20.174"

#global variables
CURRENTVALUE = 11.3
POWER = 39
DIRECTION = 'N'
#SPEED = 0
OBSTICLE = False
TRAILER = False
START = False

class Ui_tractorpull_main(object):
    def setupUi(self, tractorpull_main):
        tractorpull_main.setObjectName("tractorpull_main")
        tractorpull_main.resize(560, 459)
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(170, 170, 170))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(170, 170, 170))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipText, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(170, 170, 170))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipText, brush)
        tractorpull_main.setPalette(palette)
        self.MAIN = QtWidgets.QWidget(tractorpull_main)
        self.MAIN.setObjectName("MAIN")
        self.Frame_Warning = QtWidgets.QFrame(self.MAIN)
        self.Frame_Warning.setGeometry(QtCore.QRect(60, 140, 441, 251))
        font = QtGui.QFont()
        font.setPointSize(8)
        self.Frame_Warning.setFont(font)
        self.Frame_Warning.setStyleSheet("border-color: rgb(170, 0, 0);\n"
"background-color: rgb(255,255,255);")
        self.Frame_Warning.setFrameShape(QtWidgets.QFrame.Box)
        self.Frame_Warning.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.Frame_Warning.setLineWidth(5)
        self.Frame_Warning.setObjectName("Frame_Warning")
        self.Warning = QtWidgets.QLabel(self.Frame_Warning)
        self.Warning.setGeometry(QtCore.QRect(140, 10, 151, 61))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.Warning.setFont(font)
        self.Warning.setStyleSheet("color: rgb(255, 0, 0);")
        self.Warning.setObjectName("Warning")
        self.Obsticle_Dectection_Warning = QtWidgets.QLabel(self.Frame_Warning)
        self.Obsticle_Dectection_Warning.setGeometry(QtCore.QRect(30, 100, 371, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.Obsticle_Dectection_Warning.setFont(font)
        self.Obsticle_Dectection_Warning.setObjectName("Obsticle_Dectection_Warning")
        self.Trailer_Not_Detected = QtWidgets.QLabel(self.Frame_Warning)
        self.Trailer_Not_Detected.setGeometry(QtCore.QRect(130, 70, 201, 20))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.Trailer_Not_Detected.setFont(font)
        self.Trailer_Not_Detected.setObjectName("Trailer_Not_Detected")
        self.YES = QtWidgets.QPushButton(self.Frame_Warning)
        self.YES.setGeometry(QtCore.QRect(130, 180, 171, 51))
        self.YES.setObjectName("YES")
        self.Trailer_Warning_Hide = QtWidgets.QFrame(self.Frame_Warning)
        self.Trailer_Warning_Hide.setGeometry(QtCore.QRect(70, 60, 291, 31))
        self.Trailer_Warning_Hide.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.Trailer_Warning_Hide.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Trailer_Warning_Hide.setObjectName("Trailer_Warning_Hide")
        self.Obsticle_Warning_Hide = QtWidgets.QFrame(self.Frame_Warning)
        self.Obsticle_Warning_Hide.setGeometry(QtCore.QRect(20, 100, 381, 71))
        self.Obsticle_Warning_Hide.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.Obsticle_Warning_Hide.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Obsticle_Warning_Hide.setObjectName("Obsticle_Warning_Hide")
        self.Obsticle_Removal = QtWidgets.QLabel(self.Obsticle_Warning_Hide)
        self.Obsticle_Removal.setGeometry(QtCore.QRect(160, 40, 91, 21))
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.Obsticle_Removal.setFont(font)
        self.Obsticle_Removal.setObjectName("Obsticle_Removal")
        self.Trailer_Warning_Hide.lower()
        self.Obsticle_Warning_Hide.lower()
        self.Warning.lower()
        self.Obsticle_Dectection_Warning.lower()
        self.Trailer_Not_Detected.lower()
        self.YES.raise_()
        self.Frame_Value = QtWidgets.QFrame(self.MAIN)
        self.Frame_Value.setGeometry(QtCore.QRect(10, 20, 541, 381))
        self.Frame_Value.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.Frame_Value.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.Frame_Value.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Frame_Value.setObjectName("Frame_Value")
        self.Direction = QtWidgets.QLabel(self.Frame_Value)
        self.Direction.setGeometry(QtCore.QRect(40, 70, 91, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.Direction.setFont(font)
        self.Direction.setObjectName("Direction")
        self.Current = QtWidgets.QLabel(self.Frame_Value)
        self.Current.setGeometry(QtCore.QRect(50, 20, 71, 31))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.Current.setFont(font)
        self.Current.setObjectName("Current")
        self.BatteryPower = QtWidgets.QProgressBar(self.Frame_Value)
        self.BatteryPower.setGeometry(QtCore.QRect(380, 20, 141, 21))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(170, 170, 170))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ToolTipText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(170, 170, 170))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ToolTipText, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Light, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Midlight, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Dark, brush)
        brush = QtGui.QBrush(QtGui.QColor(170, 170, 170))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Mid, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.BrightText, brush)
        brush = QtGui.QBrush(QtGui.QColor(127, 127, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Shadow, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.AlternateBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 220))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipBase, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ToolTipText, brush)
        self.BatteryPower.setPalette(palette)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.BatteryPower.setFont(font)
        self.BatteryPower.setProperty("value", 24)
        self.BatteryPower.setObjectName("BatteryPower")
        self.Curent_Value = QtWidgets.QLabel(self.Frame_Value)
        self.Curent_Value.setGeometry(QtCore.QRect(140, 30, 100, 16))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.Curent_Value.setFont(font)
        self.Curent_Value.setObjectName("Curent_Value")
        self.Battery = QtWidgets.QLabel(self.Frame_Value)
        self.Battery.setGeometry(QtCore.QRect(390, 50, 71, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        self.Battery.setFont(font)
        self.Battery.setObjectName("Battery")
        self.Direction_Value = QtWidgets.QLabel(self.Frame_Value)
        self.Direction_Value.setGeometry(QtCore.QRect(140, 70, 161, 21))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.Direction_Value.setFont(font)
        self.Direction_Value.setObjectName("Direction_Value")
        self.Start_Hide_Frame = QtWidgets.QFrame(self.MAIN)
        self.Start_Hide_Frame.setGeometry(QtCore.QRect(0, 0, 561, 421))
        self.Start_Hide_Frame.setStyleSheet("background-color: rgb(0, 0, 0);")
        self.Start_Hide_Frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.Start_Hide_Frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.Start_Hide_Frame.setObjectName("Start_Hide_Frame")
        self.Start_PB = QtWidgets.QPushButton(self.Start_Hide_Frame)
        self.Start_PB.setGeometry(QtCore.QRect(170, 150, 191, 91))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(0, 255, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        self.Start_PB.setPalette(palette)
        font = QtGui.QFont()
        font.setPointSize(22)
        self.Start_PB.setFont(font)
        self.Start_PB.setAutoFillBackground(False)
        self.Start_PB.setStyleSheet("background-color: rgb(0, 255, 0);")
        self.Start_PB.setObjectName("Start_PB")
        self.Start_Hide_Frame.raise_()
        self.Frame_Value.lower()
        self.Frame_Warning.lower()
        tractorpull_main.setCentralWidget(self.MAIN)

        self.retranslateUi(tractorpull_main)
        QtCore.QMetaObject.connectSlotsByName(tractorpull_main)

        #start button event
        self.Start_PB.clicked.connect(self.Start_Pushed)

        #Changing Values
        self.Curent_Value.setText( (f"{CURRENTVALUE} mA") )
        self.Direction_Value.setText( (f"{DIRECTION}") )
        self.BatteryPower.setProperty("value", POWER)

        

    def retranslateUi(self, tractorpull_main):
        _translate = QtCore.QCoreApplication.translate
        tractorpull_main.setWindowTitle(_translate("tractorpull_main", "tractorpull_main"))
        self.Warning.setText(_translate("tractorpull_main", "WARNING"))
        self.Obsticle_Dectection_Warning.setText(_translate("tractorpull_main", "Obsticle Dectect: Manual Removal Needed"))
        self.Trailer_Not_Detected.setText(_translate("tractorpull_main", "Trailer Not Dectected"))
        self.YES.setText(_translate("tractorpull_main", "YES"))
        self.Obsticle_Removal.setText(_translate("tractorpull_main", "Conintue?"))
        self.Direction.setText(_translate("tractorpull_main", "Direction"))
        self.Current.setText(_translate("tractorpull_main", "Current"))
        self.Curent_Value.setText(_translate("tractorpull_main", "Current_Value"))
        self.Battery.setText(_translate("tractorpull_main", "Battery"))
        self.Direction_Value.setText(_translate("tractorpull_main", "Direction_Value"))
        self.Start_PB.setText(_translate("tractorpull_main", "START"))

    def Start_Pushed(self):
        #if received from publish Topic: Start, Msg: True
        print("start pressed\n\r")
        publish.single("Speed", "1", hostname=ip)
        global START
        if START == True:
            print ("starting\n\r")
            self.Start_Hide_Frame.lower()
        else:
            print("waiting for publish\n\r")
         
        

    def Warning(self):
        if (TRAILER == True) or (OBSTICLE == True):
            self.Frame_Warning.raise_()
            
            if (Trailer == True):
                self.Trailer_Not_Detected.raise_()
            else:
                self.Trailer_Not_Detected.lower()
                
            if (Obsticle == True):
                self.Obsticle_Dectection_Warning.raise_()
            else:
                self.Obsticle_Dectection_Warning.lower()
                
        else:
            self.Frame_Warning.lower()
            self.Obsticle_Dectection_Warning.lower()
            self.Trailer_Not_Detected.lower()
            

    def YES_pushed(self):
        if (TRAILER == False) and (OBSTICLE == False):
            publish.single("Speed", "1", hostname=ip)


def initilization_qt_interface():
    lock.acquire()
    
    app = QtWidgets.QApplication(sys.argv)
    tractorpull_main = QtWidgets.QMainWindow()
    ui = Ui_tractorpull_main()
    ui.setupUi(tractorpull_main)
    tractorpull_main.show() 
    sys.exit(app.exec_())

    lock.release()

def on_connect(client, userdata, flags, rc):
     
    # Subscribing in on_connect() - if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("Compass")
    client.subscribe("Current")
    client.subscribe("Obsticle")
    client.subscribe("Trailer")
    client.subscribe("Speed")
    client.subscribe("Start")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print()
    print (msg.topic)
    print( msg.payload.decode() )

    if msg.topic == "Speed":
        #payload either 0, 1, or 2
        global SPEED
        SPEED = msg.payload.decode()
        print(SPEED)
        
    if msg.topic == "Current":
        global CURRENTVALUE
        CURRENTVALUE = msg.payload.decode()
        print(CURRENTVALUE)
        
    if msg.topic == "Compass":
        global DIRECTION
        DIRECTION = msg.payload.decode()
        print(DIRECTION)

    if msg.topic == "Obsticle":
        global OBSTICLE
        #payload either TRUE or FALSE
        OBSTICLE = msg.payload.decode()
        print(OBSTICLE)

    if msg.topic == "Trailer":
        global TRAILER
        #payload either TRUE or FALSE
        TRAILER = msg.payload.decode()
        print(TRAILER)

    if msg.topic == "Start":
        global START
        START = msg.payload.decode()
        print(START)

def client_subscription():
    lock.acquire()
    
    client = mqtt.Client()
    client.on_connect = on_connect
    client.on_message = on_message
 
    client.connect(ip, 1883, 60)
    client.loop_forever()

    lock.release()
    

if __name__ == "__main__":

    #lock.threading
    
    tclient_sub = threading.Thread(target=client_subscription)
    tclient_sub.start()
    print("starting thread client")

    tIni_qt_int = threading.Thread(target=initilization_qt_interface)
    tIni_qt_int.start()
    print("starting thread interface")
    #import sys
    #app = QtWidgets.QApplication(sys.argv)
    #tractorpull_main = QtWidgets.QMainWindow()
    #ui = Ui_tractorpull_main()
    #ui.setupUi(tractorpull_main)
    #tractorpull_main.show() 
    #sys.exit(app.exec_())

    
    tclient_sub.join()
    tIni_qt_int.join()
    


