SequentMicrosystems_API/src/modules/Rel4HVI4/service.py

87 lines
2.1 KiB
Python
Raw Normal View History

import lib4relind, os
from typing import Dict
stacks = [int(h) for h in os.getenv("MODULE_REL4HVI4_LEVELS", "")]
if not stacks:
print("MODULE_Rel4HVI4_LEVELS is not set or invalid")
for i in stacks:
print(f"Configured Rel4HVI4 card: {i}")
def set_relay(stack: int, relay: int, value: bool):
"""
Set specified relay to requested state
"""
if stack in stacks and 1 <= relay <= 4:
lib4relind.set_relay(stack=stack, relay=relay, value=int(value))
def set_relay_stack(stack: int, value: bool):
"""
Set all relays to requested state on specified stack/card
"""
(set_relay(stack=stack, relay=ch, value=value) for ch in range(1, 5))
def set_relay_all(value: bool):
"""
Set all relays to requested state
"""
(set_relay_stack(stack=stack, value=value) for stack in stacks)
def read_relay(stack: int, relay: int):
"""
Read specified relay state
"""
if stack in stacks and 1 <= relay <= 4:
try:
if lib4relind.get_relay(stack=stack, relay=relay) == 1:
return True
else:
return False
except:
return False
return False
def read_relay_stack(stack: int):
"""
Read all relays state on specified stack/card
"""
return {relay: read_relay(stack=stack, relay=relay) for relay in range(1, 5)}
def read_relay_all():
"""
Read all relays state
"""
return {stack: read_relay_stack(stack=stack) for stack in stacks}
def read_opto(stack: int, opto: int):
"""
Read specified opto input state
"""
if stack in stacks and 1 <= opto <= 4:
try:
if lib4relind.get_opto(stack=stack, channel=opto) == 1:
return True
else:
return False
except:
return False
return False
def read_opto_stack(stack: int):
"""
Read all opto inputs state on specified stack/card
"""
return {opto: read_opto(stack=stack, opto=opto) for opto in range(1, 5)}
def read_opto_all():
"""
Read all opto inputs state
"""
return {stack: read_opto_stack(stack=stack) for stack in stacks}