1
0
mirror of https://github.com/pommi/python-itho-wpu.git synced 2024-11-24 14:22:14 +01:00

feat(itho-wpu): move itho interaction to an IthoWPU class

To be able to easily execute multiple actions.
This commit is contained in:
Pim van den Berg 2021-02-12 16:32:45 +01:00
parent 8b34069fc6
commit 0b4b9a3d79

View File

@ -162,6 +162,35 @@ class I2CMaster:
self.i.close() self.i.close()
class IthoWPU():
def __init__(self, master_only, slave_only, slave_timeout):
self.master_only = master_only
self.slave_only = slave_only
self.slave_timeout = slave_timeout
self._q = queue.Queue()
def get(self, action):
reponse = None
if not self.master_only:
slave = I2CSlave(address=0x40, queue=self._q)
slave.set_callback()
if self.slave_only:
time.sleep(self.slave_timeout)
if not self.slave_only:
master = I2CMaster(address=0x41, bus=1, queue=self._q)
if action:
response = master.execute_action(action)
logger.debug(f"Response: {response}")
master.close()
if not self.master_only:
slave.close()
return response
def is_messageclass_valid(action, response): def is_messageclass_valid(action, response):
if int(response[1], 0) != actions[action][0] and int(response[2], 0) != actions[action][1]: if int(response[1], 0) != actions[action][0] and int(response[2], 0) != actions[action][1]:
logger.error(f"Response MessageClass != {actions[action][0]} {actions[action][1]} " logger.error(f"Response MessageClass != {actions[action][0]} {actions[action][1]} "
@ -275,23 +304,7 @@ if __name__ == "__main__":
if args.loglevel: if args.loglevel:
logger.setLevel(args.loglevel.upper()) logger.setLevel(args.loglevel.upper())
q = queue.Queue() wpu = IthoWPU(args.master_only, args.slave_only, args.slave_timeout)
response = wpu.get(args.action)
if not args.master_only: if response is not None:
slave = I2CSlave(address=0x40, queue=q) process_response(args.action, response, args)
slave.set_callback()
if args.slave_only:
time.sleep(args.slave_timeout)
if not args.slave_only:
master = I2CMaster(address=0x41, bus=1, queue=q)
if args.action:
result = master.execute_action(args.action)
logger.debug(f"Response: {result}")
master.close()
if result is not None:
process_response(args.action, result, args)
if not args.master_only:
slave.close()