-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay_board.py
49 lines (39 loc) · 1.41 KB
/
relay_board.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from dae_py_relay_controller.dae_RelayBoard import dae_RelayBoard_Common
import dae_py_relay_controller.dae_RelayBoard as dae_RelayBoard
class NO: pass
class NC: pass
class DeviceNotConnected(Exception): pass
class RelayArray():
def __init__(self):
pass
def connect(self):
try:
self.dr = dae_RelayBoard.DAE_RelayBoard(dae_RelayBoard.DAE_RELAYBOARD_TYPE_8)
self.dr.initialise()
except dae_RelayBoard.dae_RelayBoard_Common.Denkovi_Exception as e:
if hasattr(e, 'message') and e.message == 'FTD2XXWindows device not initialised':
raise DeviceNotConnected
@property
def is_connected(self):
if not hasattr(self, 'dr'):
return False
try:
self.status
except dae_RelayBoard.dae_RelayBoard_Common.Denkovi_Exception as e:
return False
return True
@property
def status(self):
return list(self.dr.getStates().values())
def set(self, relay_number, state):
if state is NO:
self.dr.setState(relay_number, True)
elif state is NC:
self.dr.setState(relay_number, False)
else:
raise Exception
def toggle(self, relay_number):
if self.status[relay_number - 1]:
self.set(relay_number, NC)
else:
self.set(relay_number, NO)