Skip to content

Instantly share code, notes, and snippets.

@JoelBender
Created September 18, 2024 02:14
Show Gist options
  • Save JoelBender/e7cf8d5f9d681c9698d5191b33400fb9 to your computer and use it in GitHub Desktop.
Save JoelBender/e7cf8d5f9d681c9698d5191b33400fb9 to your computer and use it in GitHub Desktop.
Lots of Objects
"""
An example with lots of local objects.
"""
import asyncio
from random import random
from bacpypes3.debugging import ModuleLogger
from bacpypes3.argparse import SimpleArgumentParser
from bacpypes3.ipv4.app import Application
from bacpypes3.local.analog import AnalogValueObject
# some debugging
_debug = 0
_log = ModuleLogger(globals())
async def main() -> None:
try:
app = None
args = SimpleArgumentParser().parse_args()
if _debug:
_log.debug("args: %r", args)
# build an application
app = Application.from_args(args)
if _debug:
_log.debug("app: %r", app)
# create many objects
for i in range(1, 4000):
analog_value_object = AnalogValueObject(
objectIdentifier=("analog-value", i),
objectName=f"Analog Value {i}",
presentValue=random() * 100,
)
if _debug:
_log.debug("analog_value_object: %r", analog_value_object)
app.add_object(analog_value_object)
await asyncio.Future()
finally:
if app:
app.close()
if __name__ == "__main__":
try:
asyncio.run(main())
except KeyboardInterrupt:
pass
#!/usr/bin/python
"""
Console hack that uses a JSON settings file for configuring the local device
object and then turns it over to the CmdShell.
"""
import sys
import asyncio
from bacpypes3.settings import settings
from bacpypes3.debugging import ModuleLogger
from bacpypes3.argparse import JSONArgumentParser
from bacpypes3.console import Console
from bacpypes3.comm import bind
from bacpypes3.app import Application
import bacpypes3.__main__
# some debugging
_debug = 0
_log = ModuleLogger(globals())
async def main() -> None:
try:
console = None
parser = JSONArgumentParser()
args = parser.parse_args()
if _debug:
_log.debug("args: %r", args)
_log.debug("settings: %r", settings)
# build a very small stack
console = Console()
cmd = bacpypes3.__main__.CmdShell()
bind(console, cmd)
# build an application and -- hack alert -- make it available to the CmdShell
bacpypes3.__main__.app = app = Application.from_json(
settings.json["application"]
)
if _debug:
_log.debug("app: %r", app)
# wait until the user is done
await console.fini.wait()
finally:
if console and console.exit_status:
sys.exit(console.exit_status)
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment