#!/usr/bin/env python3
import hal
import time
import subprocess

h = hal.component("driveTemp")
h.newpin("xLogicTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("xPowerTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("xEncoderTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("xFan", hal.HAL_S32, hal.HAL_OUT)
h.newpin("zLogicTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("zPowerTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("zEncoderTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("zFan", hal.HAL_S32, hal.HAL_OUT)
h.newpin("cLogicTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("cPowerTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("cEncoderTemperature", hal.HAL_S32, hal.HAL_OUT)
h.newpin("cFan", hal.HAL_S32, hal.HAL_OUT)
h.ready()

delay = 1

while True:
	r = subprocess.run("ethercat upload -p0 -t int16 0x260B 0", encoding='utf-8', capture_output=True, shell=True)
	h['xPowerTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p0 -t int16 0x260C 0", encoding='utf-8', capture_output=True, shell=True)
	h['xLogicTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p0 -t int16 0x260D 0", encoding='utf-8', capture_output=True, shell=True)
	h['xEncoderTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p0 -t int32 0x262A 0", encoding='utf-8', capture_output=True, shell=True)
	h['xFan'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p1 -t int16 0x260B 0", encoding='utf-8', capture_output=True, shell=True)
	h['zPowerTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p1 -t int16 0x260C 0", encoding='utf-8', capture_output=True, shell=True)
	h['zLogicTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p1 -t int16 0x260D 0", encoding='utf-8', capture_output=True, shell=True)
	h['zEncoderTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p1 -t int32 0x262A 0", encoding='utf-8', capture_output=True, shell=True)
	h['zFan'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p2 -t int16 0x260B 0", encoding='utf-8', capture_output=True, shell=True)
	h['cPowerTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p2 -t int16 0x260C 0", encoding='utf-8', capture_output=True, shell=True)
	h['cLogicTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p2 -t int16 0x260D 0", encoding='utf-8', capture_output=True, shell=True)
	h['cEncoderTemperature'] = int(r.stdout.split(" ")[1])
	time.sleep(delay)
	
	r = subprocess.run("ethercat upload -p2 -t int32 0x262A 0", encoding='utf-8', capture_output=True, shell=True)
	h['cFan'] = int(r.stdout.split(" ")[1])
	time.sleep(48)

#except KeyboardInterrupt:
#	exit()
