from GUI import Ui_MainWindow ### Generated by Qt Designer
from PyQt4 import QtCore, QtGui ### needed to get the GUI running
import communication ###simple ascii command to com port
import threading ### used to schedule display update
import time ### only used for two seconds sleep to allow arduino bootloader to load

### This program needs Python 2.7, PyQt, and pySerial installed to run.

class StartQT4(QtGui.QMainWindow): ### generated class
    mcu_com = communication.MCUcomm()

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        ### link the PyQt generated file to a specific method (methods are defined below)###
        ##################<Buttons>#####################################################################
        self.ui.pushButton_connect.clicked.connect(self.connect)
        self.ui.pushButton_disconnect.clicked.connect(self.disconnect)

        self.ui.pushButton_ventilationFanON.clicked.connect(self.ventilationFanON)
        self.ui.pushButton_ventilationFanOFF.clicked.connect(self.ventilationFanOFF)
        self.ui.pushButton_circulationFanON.clicked.connect(self.circulationFanON)
        self.ui.pushButton_circulationFanOFF.clicked.connect(self.circulationFanOFF)
        self.ui.pushButton_panelLightON.clicked.connect(self.panelLightON)
        self.ui.pushButton_panelLightOFF.clicked.connect(self.panelLightOFF)
        self.ui.pushButton_humidifierON.clicked.connect(self.humidifierON)
        self.ui.pushButton_humidifierOFF.clicked.connect(self.humidifierOFF)

        self.ui.pushButton_growLightON.clicked.connect(self.growLightON)
        self.ui.pushButton_growLightOFF.clicked.connect(self.growLightOFF)
        self.ui.pushButton_heaterON.clicked.connect(self.heaterON)
        self.ui.pushButton_heaterOFF.clicked.connect(self.heaterOFF)
        self.ui.pushButton_waterPumpON.clicked.connect(self.waterPumpON)
        self.ui.pushButton_waterPumpOFF.clicked.connect(self.waterPumpOFF)
        self.ui.pushButton_airPumpON.clicked.connect(self.airPumpON)
        self.ui.pushButton_airPumpOFF.clicked.connect(self.airPumpOFF)
        ##################</Buttons>####################################################################

    #############<Methods_for_buttons>####################################################
    def connect(self):
        self.mcu_com.openMCU(str(self.ui.lineEdit.text()))
        time.sleep(2) # wait until arduino's bootloader done loading (arduino tend to auto restart each time new connection is made)
        self.periodic_sampling()

    def disconnect(self):
        self.mcu_com.closeMCU()

    def ventilationFanON(self):
        self.mcu_com.ventilationFanON()

    def ventilationFanOFF(self):
        self.mcu_com.ventilationFanOFF()

    def circulationFanON(self):
        self.mcu_com.circulationFanON()

    def circulationFanOFF(self):
        self.mcu_com.circulationFanOFF()

    def panelLightON(self):
        self.mcu_com.panelLightON()

    def panelLightOFF(self):
        self.mcu_com.panelLightOFF()

    def humidifierON(self):
        self.mcu_com.humidifierON()

    def humidifierOFF(self):
        self.mcu_com.humidifierOFF()

    def growLightON(self):
        self.mcu_com.growLightON()

    def growLightOFF(self):
        self.mcu_com.growLightOFF()

    def heaterON(self):
        self.mcu_com.heaterON()

    def heaterOFF(self):
        self.mcu_com.heaterOFF()

    def waterPumpON(self):
        self.mcu_com.waterPumpON()

    def waterPumpOFF(self):
        self.mcu_com.waterPumpOFF()

    def airPumpON(self):
        self.mcu_com.airPumpON()

    def airPumpOFF(self):
        self.mcu_com.airPumpOFF()
    #############</Methods_for_buttons>####################################################

###<display_stuffs>###
    def updateDisplays(self, a, b, c, d, e, f, g, h, i, j):
        self.ui.lcdNumber_windowSwitch.display(a)
        self.ui.lcdNumber_shellSwitch.display(b)
        self.ui.lcdNumber_waterTemperature.display(c)
        self.ui.lcdNumber_waterConductivity.display(d)
        self.ui.lcdNumber_waterpH.display(e)
        self.ui.lcdNumber_lightIntensity.display(f)
        self.ui.lcdNumber_lightPAR.display(g)
        self.ui.lcdNumber_airTemperature.display(h)
        self.ui.lcdNumber_airHumidity.display(i)
        self.ui.lcdNumber_airCO2.display(j)
###<display_stuffs>###


#######<thread>###
    def periodic_sampling_scheduler(self):
        if self.mcu_com.mcuStatus() == True:
            threading.Timer(1, self.periodic_sampling).start()  # update displays every one second


    def periodic_sampling(self):
        if self.mcu_com.mcuStatus() == True:
            receivedData = self.mcu_com.getAllSensor()
            splitData = receivedData.split(',')
            a = (splitData[0])
            b = (splitData[1])
            c = (splitData[2])
            d = (splitData[3])
            e = (splitData[4])
            f = (splitData[5])
            g = (splitData[6])
            h = (splitData[7])
            i = (splitData[8])
            j = (splitData[9])
            self.updateDisplays(a, b, c, d, e, f, g, h, i, j)
        self.periodic_sampling_scheduler()
#######</thread>###

if __name__ == "__main__": # generated by PyQt Designer
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = StartQT4()
    MainWindow.show()
    sys.exit(app.exec_())