-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathLocalScheduleObject2.py
188 lines (149 loc) · 5.51 KB
/
LocalScheduleObject2.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
#!/usr/bin/env python
"""
This application creates a Local Schedule Objects and then prompts
to test dates and times.
"""
from time import localtime as _localtime
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ConfigArgumentParser
from bacpypes.consolecmd import ConsoleCmd
from bacpypes.core import run
from bacpypes.primitivedata import Null, Real, Date, Time
from bacpypes.constructeddata import ArrayOf, ListOf
from bacpypes.basetypes import (
CalendarEntry,
DailySchedule,
DateRange,
DeviceObjectPropertyReference,
SpecialEvent,
SpecialEventPeriod,
TimeValue,
)
from bacpypes.object import register_object_type, WritableProperty, AnalogValueObject
from bacpypes.app import BIPSimpleApplication
from bacpypes.local.device import LocalDeviceObject
from bacpypes.local.object import CurrentPropertyListMixIn
from bacpypes.local.schedule import LocalScheduleObject
# some debugging
_debug = 0
_log = ModuleLogger(globals())
# globals
test_analog_value = None
test_schedule = None
@register_object_type(vendor_id=999)
class WritableAnalogValueObject(CurrentPropertyListMixIn, AnalogValueObject):
properties = [WritableProperty("presentValue", Real)]
@bacpypes_debugging
def analog_value_changed(old_value, new_value):
if _debug:
TestConsoleCmd._debug("analog_value_changed %r %r", old_value, new_value)
print("analog value changed from {!r} to {!r}".format(old_value, new_value))
@bacpypes_debugging
class TestConsoleCmd(ConsoleCmd):
def do_test(self, args):
"""test <date> <time>"""
args = args.split()
if _debug:
TestConsoleCmd._debug("do_test %r", args)
date_string, time_string = args
test_date = Date(date_string).value
test_time = Time(time_string).value
v, t = test_schedule._task.eval(test_date, test_time)
print(
test_schedule.objectName + ", " + repr(v and v.value) + " until " + str(t)
)
def do_except(self, args):
"""except <date> <start> <stop>"""
args = args.split()
if _debug:
TestConsoleCmd._debug("do_except %r", args)
date_string, start_string, stop_string = args
except_date = Date(date_string).value
start_time = Time(start_string).value
stop_time = Time(stop_string).value
exception_schedule = ArrayOf(SpecialEvent)([
SpecialEvent(
period=SpecialEventPeriod(
calendarEntry=CalendarEntry(date=except_date)
),
listOfTimeValues=[
TimeValue(time=start_time, value=Real(999.0)),
TimeValue(time=stop_time, value=Null()),
],
eventPriority=1,
)
])
if _debug:
TestConsoleCmd._debug(" - exception_schedule: %r", exception_schedule)
# new exception
test_schedule.exceptionSchedule = exception_schedule
def do_now(self, args):
"""now"""
args = args.split()
if _debug:
TestConsoleCmd._debug("do_now %r", args)
print("test analog value: %r" % (test_analog_value.presentValue,))
print("test schedule: %r" % (test_schedule.presentValue.value,))
def main():
global args, test_analog_value, test_schedule
# parse the command line arguments
parser = ConfigArgumentParser(description=__doc__)
# parse the command line arguments
args = parser.parse_args()
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 sample application
this_application = BIPSimpleApplication(this_device, args.ini.address)
# create a writeable analog value object
test_analog_value = WritableAnalogValueObject(
objectIdentifier=("analogValue", 1),
objectName="Test Analog Value",
presentValue=0.0,
)
_log.debug(" - test_analog_value: %r", test_analog_value)
this_application.add_object(test_analog_value)
# print when the value changes
test_analog_value._property_monitors["presentValue"].append(analog_value_changed)
#
# Simple daily schedule (actually a weekly schedule with every day
# being identical.
#
test_schedule = LocalScheduleObject(
objectIdentifier=("schedule", 1),
objectName="Test Schedule",
presentValue=Real(8.0),
effectivePeriod=DateRange(startDate=(0, 1, 1, 1), endDate=(254, 12, 31, 2)),
weeklySchedule=ArrayOf(DailySchedule, 7)([
DailySchedule(
daySchedule=[
TimeValue(time=(8, 0, 0, 0), value=Real(8.0)),
TimeValue(time=(14, 0, 0, 0), value=Null()),
TimeValue(time=(17, 0, 0, 0), value=Real(42.0)),
]
)
]
* 7),
listOfObjectPropertyReferences=ListOf(DeviceObjectPropertyReference)(
[
DeviceObjectPropertyReference(
objectIdentifier=("analogValue", 1),
propertyIdentifier="presentValue",
)
]
),
scheduleDefault=Real(0.0),
)
_log.debug(" - test_schedule: %r", test_schedule)
this_application.add_object(test_schedule)
TestConsoleCmd()
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()