import numpy as np
import json
from aplant import APlant
from math import sqrt
from hendiCtrl import HendiCtrl
from max31865 import Max31865

class Plant(APlant):
	def __init__(self):
		self.heater = HendiCtrl('/dev/ttyUSB0', 115200)
		self.tempsensor = Max31865()
		self.P = 0
		self.isOn = False

	def activate(self, enable=True):
		if enable == True:
			self.heater.remoteEnable(enable)
			self.heater.setSwitch(enable)
		else:
			self.heater.setSwitch(enable)
			self.heater.remoteEnable(enable)

	def process(self):
		pass

	def setPower(self, power_w):
		self.P = power_w
		self.heater.setPowerWatts(power_w)
		if power_w < 250:
			if self.isOn:
				self.heater.setSwitch(False)
				self.isOn = False
		elif power_w >= 500:
			if not self.isOn:
				self.heater.setSwitch(True)
				self.isOn = True

	def getPower(self):
		return self.heater.getPowerWatts()

	def getTemperature(self):
		return self.tempsensor.temperature()

