from components.aplant import APlant
from components.plant.heat_diffusion import HeatDiffusion


class Pot(APlant):
	def __init__(self, dt, params, theta_amb=20):
		APlant.__init__(self)
		self.dt = dt

		self.e = 0
		self.x = 0

		self.gain = params['gain']
		self.C = params['C']
		self.M = params['M']
		self.L = params['L']
		self.Td = params['Td']
		self.kn = params['kn']
		self.temp = params['theta']
		self.temp_intermediate = params['theta']

		self.theta_amb = theta_amb

		self.power_set = 0
		self.delay = HeatDiffusion(self.dt, self.Td, params['theta'])

	def initial(self, temp):
		self.temp_intermediate = temp
		self.temp = temp
		self.delay.initial(temp)

	def activate(self, enable):
		pass

	def process(self):
		power = self.gain * self.power_set
		leak = 1/self.M * self.L * (self.theta_amb - self.temp)/self.theta_amb
		self.temp_intermediate += (power / (self.M * self.C) + leak) * self.dt

		# Delay
		self.temp = self.delay.process(self.temp_intermediate)

	def is_activated(self):
		return True

	def set_power(self, power):
		self.power_set = power

	def get_power(self):
		return round(self.power_set, 1)

	def get_temperature(self):
		return self.temp

	def get_temperature_intermediate(self):
		return self.temp_intermediate
