import time
import abc


class AStirrer(abc.ABC):
	def __init__(self, dt):
		self.dt = dt
		self.speed = 0
		self.cycleTime = 1
		self.dutyCycle = 1
		self.cycleCounter = 0
		self.isOn = 1

	def log(self, s):
		print("{:.2f}: {}".format(time.time(), s))

	def process(self):
		isOn = self.isOn
		self.cycleCounter += self.dt
		if self.cycleCounter >= self.cycleTime:
			self.cycleCounter -= self.cycleTime
			isOn = 0

		if self.cycleCounter >= (self.cycleTime * (1-self.dutyCycle)):
			if not isOn:
				isOn = 1

		if self.isOn != isOn:
			if isOn:
				self.onSetSpeed(self.speed)
			else:
				self.onSetSpeed(0)

		self.isOn = isOn

	def setSpeed(self, speed):
		self.speed = speed
		if self.isOn or self.dutyCycle == 1:
			self.onSetSpeed(speed)

	def setCycleTime(self, time):
		self.cycleTime = time

	def setDutyCycle(self, dutyCycle):
		self.dutyCycle = dutyCycle

	@abc.abstractmethod
	def getSpeed(self):
		return None

	@abc.abstractmethod
	def activate(self):
		pass

	@abc.abstractmethod
	def deactivate(self):
		pass
