-
Notifications
You must be signed in to change notification settings - Fork 0
/
vBACdevice.py
212 lines (165 loc) · 5.91 KB
/
vBACdevice.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/python
# -*- coding: utf-8 -*-
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ConfigArgumentParser
from bacpypes.core import run
from bacpypes.basetypes import BinaryPV
from bacpypes.object import BinaryInputObject, BinaryOutputObject, Property, register_object_type
from bacpypes.errors import ExecutionError
from bacpypes.app import BIPSimpleApplication
from bacpypes.local.device import LocalDeviceObject
from misc import VBIdev, VBOdev
DI_devs = [i for i in range(1, 10)]
DO_devs = [i for i in range(11, 20)]
# some debugging
_debug = 0
_log = ModuleLogger(globals())
@bacpypes_debugging
class DIPresentValue(Property):
def __init__(self, identifier):
if _debug:
DIPresentValue._debug("__init__ %r", identifier)
Property.__init__(
self, identifier, BinaryPV, default=BinaryPV("inactive"), optional=False, mutable=False
)
def ReadProperty(self, obj, arrayIndex=None):
if _debug:
DIPresentValue._debug("ReadProperty %r arrayIndex=%r", obj, arrayIndex)
# access an array
if arrayIndex is not None:
raise ExecutionError(
errorClass="property", errorCode="propertyIsNotAnArray"
)
if _debug:
DIPresentValue._debug(" - read button: %r", obj._dev.read())
if obj._dev.read() == 'active':
return "active"
else:
return "inactive"
def WriteProperty(self, obj, value, arrayIndex=None, priority=None, direct=False):
if _debug:
DIPresentValue._debug(
"WriteProperty %r %r arrayIndex=%r priority=%r direct=%r",
obj,
value,
arrayIndex,
priority,
direct,
)
raise ExecutionError(errorClass="property", errorCode="writeAccessDenied")
@bacpypes_debugging
@register_object_type
class DIBinaryInput(BinaryInputObject):
properties = [DIPresentValue('presentValue')]
def __init__(self, dev, **kwargs):
if _debug:
DIBinaryInput._debug("__init__ %r %r", dev, kwargs)
BinaryInputObject.__init__(self, **kwargs)
# create a button object
self._dev = dev
@bacpypes_debugging
class DOPresentValue(Property):
def __init__(self, identifier):
if _debug:
DOPresentValue._debug("__init__ %r", identifier)
Property.__init__(
self, identifier, BinaryPV, default=BinaryPV("inactive"), optional=False, mutable=True
)
def ReadProperty(self, obj, arrayIndex=None):
if _debug:
DOPresentValue._debug("ReadProperty %r arrayIndex=%r", obj, arrayIndex)
# access an array
if arrayIndex is not None:
raise ExecutionError(
errorClass="property", errorCode="propertyIsNotAnArray"
)
###TODO: obj._led is the LED object
if _debug:
DOPresentValue._debug(" - read dev: %r", obj._dev.read())
if obj._dev.read() == "active":
return "active"
else:
return "inactive"
def WriteProperty(self, obj, value, arrayIndex=None, priority=None, direct=False):
if _debug:
DOPresentValue._debug(
"WriteProperty %r %r arrayIndex=%r priority=%r direct=%r",
obj,
value,
arrayIndex,
priority,
direct,
)
# access an array
if arrayIndex is not None:
raise ExecutionError(
errorClass="property", errorCode="propertyIsNotAnArray"
)
###TODO: obj._button is the Button object
if _debug:
DOPresentValue._debug(" - write dev: %r", obj._dev)
# raise ExecutionError(errorClass="property", errorCode="writeAccessDenied")
if value == "active":
obj._dev.write("active")
elif value == "inactive":
obj._dev.write("inactive")
else:
### TODO: insert correct value error. Below is a placeholder.
print("invalid value for led. Use 'active' to turn on or 'inactive' to turn off.")
#
# RPiBinaryOutput
#
@bacpypes_debugging
@register_object_type
class DOBinaryOutput(BinaryOutputObject):
properties = [DOPresentValue("presentValue")]
def __init__(self, dev, **kwargs):
if _debug:
DOBinaryOutput._debug("__init__ %r %r", dev, kwargs)
BinaryOutputObject.__init__(self, **kwargs)
self._dev = dev
#
# __main__
#
def main():
# parse the command line arguments
args = ConfigArgumentParser(description=__doc__).parse_args()
if _debug:
_log.debug("initialization")
if _debug:
_log.debug(" - args: %r", args)
# make a device object
this_device = LocalDeviceObject(
objectName=args.ini.objectname,
objectIdentifier=int(args.ini.objectidentifier),
maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted),
segmentationSupported=args.ini.segmentationsupported,
vendorIdentifier=int(args.ini.vendoridentifier),
)
# make a sample application
this_application = BIPSimpleApplication(this_device, args.ini.address)
# make the buttons
for id in DI_devs:
dev = VBIdev(id)
bio = DIBinaryInput(
dev,
objectIdentifier=(DIBinaryInput.objectType, id),
objectName=dev.file,
)
_log.debug(" - bio: %r", bio)
this_application.add_object(bio)
# make the LEDs
for id in DO_devs:
dev = VBOdev(id)
boo = DOBinaryOutput(
dev,
objectIdentifier=(DOBinaryOutput.objectType, id),
objectName=dev.file,
)
_log.debug(" - boo: %r", boo)
this_application.add_object(boo)
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()