import asyncio
from tasks import ATask
from ws.message import MsgIo
from utils.value import ChangedFloat
from components import ATemperatureSensor


class TempSensorTask(ATask):
	def __init__(self, sensor_device: ATemperatureSensor, interval, msg_handler: MsgIo):
		ATask.__init__(self, interval)

		self.msg_handler = msg_handler
		msg_handler.set_recv_handler(self.recv)
		self.sensor = sensor_device
		self.temp = self.sensor.temperature()
		self.set_on_changed("temp", ChangedFloat(self.on_temp_changed, prec=1).set)

	def on_temp_changed(self, value):
		asyncio.create_task(self.send({'Temp': value}))

	async def recv(self, data):
		pass

	async def send(self, data):
		await self.msg_handler.send(data)

	async def on_process(self):
		while True:
			self.temp = self.sensor.temperature()
			await asyncio.sleep(self.interval)
