import time
import can
import canTest
import struct

bustype = "socketcan"
channel = "can0"


def getTempOverCan():
    bus = can.Bus(channel=channel, interface=bustype)
    message = can.Message(
        arbitration_id=0x002, data=[3, 0, 0, 0, 0, 0, 0], is_extended_id=False
    )
    bus.send(message)
    voltages = []
    runtime = 2
    begin = 0

    for msg in bus:
        print(msg.data)
        voltages.append(msg.data)
        print("begin {}".format(begin))
        begin = begin + 1
        if begin > runtime:
            return voltages


def verifyNumbers(numberListList):
    zeroCount = 0
    max = 0
    min = 1000000
    for numberList in numberListList:
        print("Temperatures {}".format(numberList))
        for number in numberList:
            if number < min:
                min = number

            elif number > max:
                max = number

    print("Temperatures {}".format())
    print("The biggest difference tmep was max{} and min{}".format((max), (min)))


def tempTest():
    while True:
        print("====================")
        print("starting temperature test ")
        canTest.waitForUserInput(True)
        Temps = getTempOverCan()
        allTemp = []
        changed = []

        for temp in Temps:
            allTemp += struct.unpack("<HHHH", temp)  # Format anpassen

        print("Raw temperatures:")
        print(allTemp)

        for te in allTemp:
            changed.append(((te >> 4) * 0.0625))
        print("Interpreted temperatures:")
        print(changed)
        if canTest.waitForUserInput(True, "Repeat temperature test?") != 0:
            break
    print("I am done with this shit")
