2021-02-16 20:37:51 +01:00
|
|
|
from cbpi.api.dataclasses import Actor
|
|
|
|
from cbpi.controller.basic_controller2 import BasicController
|
2018-12-29 00:27:19 +01:00
|
|
|
import logging
|
2021-01-22 23:25:20 +01:00
|
|
|
from tabulate import tabulate
|
|
|
|
class ActorController(BasicController):
|
2018-11-01 21:25:42 +01:00
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
def __init__(self, cbpi):
|
2021-02-16 20:37:51 +01:00
|
|
|
super(ActorController, self).__init__(cbpi, Actor,"actor.json")
|
2021-01-24 22:14:57 +01:00
|
|
|
self.update_key = "actorupdate"
|
2022-04-23 20:32:32 +02:00
|
|
|
self.sorting=True
|
2021-02-16 20:37:51 +01:00
|
|
|
|
2024-08-07 18:42:57 +02:00
|
|
|
async def on(self, id, power=None, output=None):
|
2019-01-04 09:29:09 +01:00
|
|
|
try:
|
2021-01-22 23:25:20 +01:00
|
|
|
item = self.find_by_id(id)
|
2021-12-02 14:48:39 +01:00
|
|
|
if power is None:
|
|
|
|
logging.info("Power is none")
|
|
|
|
if item.power:
|
|
|
|
power = item.power
|
|
|
|
else:
|
|
|
|
power = 100
|
2024-08-07 18:42:57 +02:00
|
|
|
|
|
|
|
if output is None:
|
|
|
|
logging.info("Output is none")
|
|
|
|
if item.output:
|
|
|
|
output = item.output
|
|
|
|
else:
|
|
|
|
output = 100
|
2021-02-16 20:37:51 +01:00
|
|
|
if item.instance.state is False:
|
2024-08-11 18:03:31 +02:00
|
|
|
try:
|
|
|
|
await item.instance.on(power, output)
|
|
|
|
except:
|
|
|
|
await item.instance.on(power)
|
2022-02-03 17:00:12 +01:00
|
|
|
#await self.push_udpate()
|
2022-04-23 20:32:32 +02:00
|
|
|
self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting)
|
2022-01-03 12:47:20 +01:00
|
|
|
self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict(), True)
|
2021-11-11 11:22:13 +01:00
|
|
|
else:
|
|
|
|
await self.set_power(id, power)
|
2024-08-07 18:42:57 +02:00
|
|
|
await self.set_output(id, output)
|
|
|
|
|
2019-01-04 09:29:09 +01:00
|
|
|
except Exception as e:
|
2021-11-06 15:15:11 +01:00
|
|
|
logging.error("Failed to switch on Actor {} {}".format(id, e))
|
2019-01-28 22:21:31 +01:00
|
|
|
|
2021-01-22 23:25:20 +01:00
|
|
|
async def off(self, id):
|
|
|
|
try:
|
|
|
|
item = self.find_by_id(id)
|
2021-02-16 20:37:51 +01:00
|
|
|
if item.instance.state is True:
|
|
|
|
await item.instance.off()
|
2022-02-03 17:00:12 +01:00
|
|
|
#await self.push_udpate()
|
2022-04-23 20:32:32 +02:00
|
|
|
self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting)
|
2022-01-03 12:47:20 +01:00
|
|
|
self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict())
|
2021-01-22 23:25:20 +01:00
|
|
|
except Exception as e:
|
2021-11-06 15:15:11 +01:00
|
|
|
logging.error("Failed to switch on Actor {} {}".format(id, e), True)
|
2018-12-13 21:45:33 +01:00
|
|
|
|
2021-01-22 23:25:20 +01:00
|
|
|
async def toogle(self, id):
|
|
|
|
try:
|
|
|
|
item = self.find_by_id(id)
|
|
|
|
instance = item.get("instance")
|
|
|
|
await instance.toggle()
|
2022-04-23 20:32:32 +02:00
|
|
|
self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting)
|
2022-01-03 12:47:20 +01:00
|
|
|
self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict())
|
2021-01-22 23:25:20 +01:00
|
|
|
except Exception as e:
|
2021-11-06 15:15:11 +01:00
|
|
|
logging.error("Failed to toggle Actor {} {}".format(id, e))
|
|
|
|
|
|
|
|
async def set_power(self, id, power):
|
|
|
|
try:
|
|
|
|
item = self.find_by_id(id)
|
2021-11-15 20:18:33 +01:00
|
|
|
await item.instance.set_power(power)
|
2024-08-11 18:03:31 +02:00
|
|
|
output = round(item.maxoutput*power/100)
|
|
|
|
if item.output != output:
|
|
|
|
item.output = output
|
|
|
|
|
2021-11-15 20:18:33 +01:00
|
|
|
except Exception as e:
|
|
|
|
logging.error("Failed to set power {} {}".format(id, e))
|
|
|
|
|
2024-08-07 18:42:57 +02:00
|
|
|
async def set_output(self, id, output):
|
|
|
|
try:
|
|
|
|
item = self.find_by_id(id)
|
|
|
|
await item.instance.set_output(output)
|
2024-08-11 18:03:31 +02:00
|
|
|
if item.output != output:
|
|
|
|
item.output=output
|
|
|
|
power=round(output/item.maxoutput*100)
|
|
|
|
if item.power != power:
|
|
|
|
await item.instance.set_power(power)
|
2024-08-07 18:42:57 +02:00
|
|
|
except Exception as e:
|
|
|
|
logging.error("Failed to set output {} {}".format(id, e))
|
|
|
|
|
2024-08-11 18:03:31 +02:00
|
|
|
async def actor_update(self, id, power, output=None, maxoutput=None):
|
2021-11-15 20:18:33 +01:00
|
|
|
try:
|
|
|
|
item = self.find_by_id(id)
|
2024-08-11 18:03:31 +02:00
|
|
|
if maxoutput:
|
|
|
|
item.maxoutput=maxoutput
|
2021-11-15 20:18:33 +01:00
|
|
|
item.power = round(power)
|
2024-08-11 18:03:31 +02:00
|
|
|
if output:
|
|
|
|
item.output = round(output)
|
2022-02-03 17:00:12 +01:00
|
|
|
#await self.push_udpate()
|
2022-04-23 20:32:32 +02:00
|
|
|
self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))),self.sorting)
|
2022-01-03 12:47:20 +01:00
|
|
|
self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict())
|
2021-11-06 15:15:11 +01:00
|
|
|
except Exception as e:
|
2021-11-15 20:18:33 +01:00
|
|
|
logging.error("Failed to update Actor {} {}".format(id, e))
|
2022-08-31 20:46:27 +02:00
|
|
|
|
2024-04-27 17:15:06 +02:00
|
|
|
async def timeractor_update(self, id, timer):
|
|
|
|
try:
|
|
|
|
item = self.find_by_id(id)
|
|
|
|
item.timer = round(timer)
|
|
|
|
#await self.push_udpate()
|
|
|
|
self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data))))
|
|
|
|
self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict())
|
|
|
|
except Exception as e:
|
|
|
|
logging.error("Failed to update Actor {} {}".format(id, e))
|
|
|
|
|
2022-08-31 20:46:27 +02:00
|
|
|
async def ws_actor_update(self):
|
|
|
|
try:
|
|
|
|
#await self.push_udpate()
|
|
|
|
self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda x: x.to_dict(), self.data))),self.sorting)
|
|
|
|
# self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict())
|
|
|
|
except Exception as e:
|
|
|
|
logging.error("Failed to update Actors {}".format(e))
|