-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyipxVirtuals.py
128 lines (100 loc) · 3.39 KB
/
pyipxVirtuals.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# -*- coding: utf-8 -*-
import collections
import time
from threading import Lock
from pyipx800 import pyipxBase
class VirtualInput(pyipxBase.pyipxBase):
"""Representing an IPX800 VirtualInput."""
_mutex = Lock()
_updatets = time.time()
def __init__(self, ipx, id: int):
super().__init__(ipx, id)
def update(self):
with VirtualInput._mutex:
if (self._content==None or (time.time() - VirtualInput._updatets >= pyipxBase.pyipxBase.scan_interval)):
self._content = self._ipx.request({"Get": "VI"})
VirtualInput._updatets = time.time()
return self._content
@staticmethod
def len(ipx):
return len(ipx.request({"Get": "VI"}))
@property
def state(self) -> bool:
"""Return the current VirtualInput status."""
self.update()
return self._content[f"VI{self.id}"] == 1
def on(self) -> bool:
"""Turn on a VI and return True if it was successful."""
params = {"SetVI": self.id}
self._ipx.request(params)
return True
def off(self) -> bool:
"""Turn off a VI and return True if it was successful."""
params = {"ClearVI": self.id}
self._ipx.request(params)
return True
def toggle(self) -> bool:
"""Toggle a VI and return True if it was successful."""
params = {"ToggleVI": self.id}
self._ipx.request(params)
return True
class VirtualOutput(pyipxBase.pyipxBase):
"""Representing an IPX800 VirtualOutput."""
_mutex = Lock()
_updatets = time.time()
def __init__(self, ipx, id: int):
super().__init__(ipx, id)
def update(self):
with VirtualOutput._mutex:
if (self._content==None or (time.time() - VirtualOutput._updatets >= pyipxBase.pyipxBase.scan_interval)):
self._content = self._ipx.request({"Get": "VO"})
VirtualOutput._updatets = time.time()
return self._content
@staticmethod
def len(ipx):
return len(ipx.request({"Get": "VO"}))
@property
def state(self) -> bool:
"""Return the current VirtualInput status."""
self.update()
return self._content[f"VO{self.id}"] == 1
def on(self) -> bool:
"""Turn on a VO and return True if it was successful."""
params = {"SetVO": self.id}
self._ipx.request(params)
return True
def off(self) -> bool:
"""Turn off a VO and return True if it was successful."""
params = {"ClearVO": self.id}
self._ipx.request(params)
return True
def toggle(self) -> bool:
"""Toggle a VO and return True if it was successful."""
params = {"ToggleVO": self.id}
self._ipx.request(params)
return True
class VirtualAnalog(pyipxBase.pyipxBase):
"""Representing an IPX800 VirtualAnalog."""
_mutex = Lock()
_updatets = time.time()
def __init__(self, ipx, id: int):
super().__init__(ipx, id)
def update(self):
with VirtualAnalog._mutex:
if (self._content==None or (time.time() - VirtualAnalog._updatets >= pyipxBase.pyipxBase.scan_interval)):
self._content = self._ipx.request({"Get": "VA"})
VirtualAnalog._updatets = time.time()
return self._content
@staticmethod
def len(ipx):
return len(ipx.request({"Get": "VA"}))
@property
def state(self) -> int:
"""Return the current VirtualAnalog value."""
self.update()
return self._content[f"VA{self.id}"]
def set(self, value) -> bool:
"""Change VA value and return True if it was successful."""
params = { "SetVA{:02d}".format(self.id): value}
self._ipx.request(params)
return True