-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathRecurringWriteProperty.py
executable file
·161 lines (119 loc) · 4.32 KB
/
RecurringWriteProperty.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
#!/usr/bin/env python
"""
This application demonstrates writing a series of values at a regular interval.
$ python RecurringWriteProperty.py 1.2.3.4 analogValue:1 \
presentValue 1.2 3.4 5.6
"""
import sys
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ConfigArgumentParser
from bacpypes.core import run, deferred
from bacpypes.task import RecurringTask
from bacpypes.iocb import IOCB
from bacpypes.pdu import Address
from bacpypes.primitivedata import Real, ObjectIdentifier
from bacpypes.constructeddata import Any
from bacpypes.basetypes import PropertyIdentifier
from bacpypes.apdu import WritePropertyRequest, SimpleAckPDU
from bacpypes.app import BIPSimpleApplication
from bacpypes.local.device import LocalDeviceObject
# some debugging
_debug = 0
_log = ModuleLogger(globals())
# globals
args = None
this_application = None
@bacpypes_debugging
class PrairieDog(RecurringTask):
"""
An instance of this class pops up out of the ground every once in a
while and write out the next value.
"""
def __init__(self, interval):
if _debug:
PrairieDog._debug("__init__ %r", interval)
RecurringTask.__init__(self, interval)
# install it
self.install_task()
def process_task(self):
if _debug:
PrairieDog._debug("process_task")
global args, this_application
if _debug:
PrairieDog._debug(" - args.values: %r", args.values)
# pick up the next value
value = args.values.pop(0)
args.values.append(value)
# make a primitive value out of it
value = Real(float(value))
# build a request
request = WritePropertyRequest(
destination=args.daddr,
objectIdentifier=args.objid,
propertyIdentifier=args.propid,
)
# save the value, application tagged
request.propertyValue = Any()
request.propertyValue.cast_in(value)
if _debug:
PrairieDog._debug(" - request: %r", request)
# make an IOCB
iocb = IOCB(request)
iocb.add_callback(self.write_complete)
if _debug:
PrairieDog._debug(" - iocb: %r", iocb)
# give it to the application to process
deferred(this_application.request_io, iocb)
def write_complete(self, iocb):
if _debug:
PrairieDog._debug("write_complete %r", iocb)
# do something for success
if iocb.ioResponse:
# should be an ack
if not isinstance(iocb.ioResponse, SimpleAckPDU):
if _debug:
PrairieDog._debug(" - not an ack")
else:
sys.stdout.write("ack\n")
# do something for error/reject/abort
elif iocb.ioError:
sys.stdout.write(str(iocb.ioError) + "\n")
def main():
global args, this_application
# parse the command line arguments
parser = ConfigArgumentParser(description=__doc__)
# add an argument for seconds per dog
parser.add_argument("daddr", help="destination address")
parser.add_argument("objid", help="object identifier")
parser.add_argument("propid", help="property identifier")
# list of values to write
parser.add_argument("values", metavar="N", nargs="+", help="values to write")
# add an argument for seconds between writes
parser.add_argument(
"--delay", type=float, help="delay between writes in seconds", default=5.0
)
# now parse the arguments
args = parser.parse_args()
# convert the parameters
args.daddr = Address(args.daddr)
args.objid = ObjectIdentifier(args.objid).value
args.propid = PropertyIdentifier(args.propid).value
if _debug:
_log.debug("initialization")
if _debug:
_log.debug(" - args: %r", args)
# make a device object
this_device = LocalDeviceObject(ini=args.ini)
if _debug:
_log.debug(" - this_device: %r", this_device)
# make a simple application
this_application = BIPSimpleApplication(this_device, args.ini.address)
# make a dog, task scheduling is in milliseconds
dog = PrairieDog(args.delay * 1000)
if _debug:
_log.debug(" - dog: %r", dog)
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()