Digimat BACnet BAC0 Wrapper
Project description
Python digimat.bac0
digimat.bac0 is a Python 3 module basically providing a wrapper around the excellent BAC0 module, itself using BACpypes, allowing to access and browse BACnet/IP networks. The main goal of this module is to provide a super simple way to discover/browse/interact with BACnet/IP devices on a network, especially from a terminal session. This module can be used interactively or within an application. Install this module with a pip install -U digimat.bac0 command.
>>> from digimat.bac0 import BAC
>>> bacnet=BAC(network='192.168.0.84/24')
Congratulations ! You just have launched a BACnet/IP node binded to your LAN card (ip 192.168.0.84, netmask 255.255.255.0). This short Youtube Screencast shows a small interactive usage of this module.You have to provide the ip (“ip/networkSize”) that will be used to send/receive every BACnet UDP messages. Under Linux/MacOS hosts, this can be done automagically if you have installed the netifaces module (install it with pip install -U netifaces). If so, you can ommit the network parameter (the module will try to guess the ip address of your network card)
>>> bacnet=BAC() # return a BAC object
>>> bacnet
<BAC:192.168.0.84/24(0 devices)>
If needed, you can give a bbmd router (‘ipAddress[:port]’) to the constructor
>>> bacnet=BAC(router='192.168.2.1')
Now, assuming that your network settings are correct, you can start discovering the network
>>> bacnet.whois() # return a list of devices detected on the network
[('192.168.0.15', 8015), ('192.168.0.80', 4000)]
>>> bacnet.whois('*:*') # by default, send the request on all networks
>>> bacnet.whois('2001:*') # search devices on the 2001 network only
The whois() method returns a list of remote BACnet/address servers detected on the network (address/deviceId). You can then can add to your node thoses remote devices (BACnet servers)
>>> device=bacnet.declareDevice(8015, '192.168.0.15')
>>> device=bacnet.declareDevice(did=8015, address='192.168.0.15', poll=60) # with polling time specified (default is to poll it every 15s)
In fact, if the device is detected by the whois(), you can declare a node based on it’s id or it’s address. A whois() request is sent to get the associated device address/id value
>>> device=bacnet.declareDeviceFromId(8015) # return a BACDevice object
>>> device=bacnet.declareDeviceFromAddress('192.168.0.15') # equivalent
>>> device
<BACDevice:8015:0(Digimat:Digimat3_CPU_Bridge), operational, 66 points)>
>>> bacnet.discover() # this will magically declare every device reported by whois()
The underlying BAC0 module has started a thread managing the remote bacnet nodes (understand remote “BACnet servers”) you will declare to your node. Every BAC* object has a .dump() method, very useful in interactive sessions
>>> bacnet.dump()
+---+---------+------+--------------+---------+---------------------+-------------+-------------+---------+
| # | name | id | address | vendor | model | status | description | #points |
+---+---------+------+--------------+---------+---------------------+-------------+-------------+---------+
| 0 | s_112_1 | 8015 | 192.168.0.15 | Digimat | Digimat3_CPU_Bridge | operational | S+T | 66 |
+---+---------+------+--------------+---------+---------------------+-------------+-------------+---------+
>>> device.dump()
+-----------------------+--------------+
| property | value |
+-----------------------+--------------+
| ip | 192.168.0.15 |
| address | 8015 |
| name | s_112_1 |
| description | S+T |
| systemStatus | operational |
| vendorName | Digimat |
| vendorIdentifier | 892 |
| points | 66 |
| segmentationSupported | True |
| analogInput | 16 |
| analogOutput | 8 |
| binaryInput | 31 |
| binaryOutput | 11 |
+-----------------------+--------------+
Once a device has been declared, you can retrieve a reference to the corresponding BACDevice object it with bacnet.device(…) or directly with a bacnet[…] request. You can use either the index (0), the name (s_112_1), the id (8105) or the address (192.168.0.15) to retrieve your device from the BAC object. If you redeclare a device already existing, it will be simply returns the existing one (no duplication).
You will have to dig a bit into the BAC and BACDevice objects to find avalaible methods and properties. It’s now time to access to the points (variables) of our device, all provided by the device.points property, returning a BACPoints object
>>> points=device.points
>>> points
<BACPoints(66 points)>
>>> points.dump()
+----+---------------------+-------------------------------------------------------------------------+-------------------+---------+--------------+-----+-------+-------+
| # | name | description | descriptor | value | label | age | COV | OoS |
+----+---------------------+-------------------------------------------------------------------------+-------------------+---------+--------------+-----+-------+-------+
| 0 | r_112_1_cio_13056_0 | sonde exterieure | analogInput13056 | 25.5573 | C | 4s | False | False |
| 1 | r_112_1_cio_13057_0 | sonde depart chaudiere | analogInput13057 | 25.0004 | C | 4s | False | False |
| 2 | r_112_1_cio_13058_0 | sonde depart radiateurs | analogInput13058 | 29.1211 | C | 4s | False | False |
| 3 | r_112_1_cio_13059_0 | sonde depart chauffage de sol | analogInput13059 | 25.2445 | C | 4s | False | False |
| 4 | r_112_1_cio_13060_0 | pot.physique consigne depart chauffage de sol (-10;+10C) | analogInput13060 | 4.91234 | C | 4s | False | False |
| 5 | r_112_1_cio_13061_0 | pot.physique consigne depart radiateurs (-10;+10C) | analogInput13061 | 2.925 | C | 4s | False | False |
| 6 | r_112_1_cio_13062_0 | sonde ambiance bureau direction rez | analogInput13062 | 24.434 | C | 4s | False | False |
...
>>> device.points.dump('sonde') # output can be filtered (by part of names or descriptions)
+----+---------------------+-----------------------------------------------------+------------------+---------+-------+-----+-------+-------+
| # | name | description | descriptor | value | label | age | COV | OoS |
+----+---------------------+-----------------------------------------------------+------------------+---------+-------+-----+-------+-------+
| 0 | r_112_1_cio_13056_0 | sonde exterieure | analogInput13056 | 25.572 | C | 11s | False | False |
| 1 | r_112_1_cio_13057_0 | sonde depart chaudiere | analogInput13057 | 25.0248 | C | 11s | False | False |
| 2 | r_112_1_cio_13058_0 | sonde depart radiateurs | analogInput13058 | 29.1211 | C | 11s | False | False |
| 3 | r_112_1_cio_13059_0 | sonde depart chauffage de sol | analogInput13059 | 25.2689 | C | 11s | False | False |
| 6 | r_112_1_cio_13062_0 | sonde ambiance bureau direction rez | analogInput13062 | 24.4437 | C | 11s | False | False |
| 8 | r_112_1_cio_13064_0 | sonde ambiance bureau direction cote hall rez | analogInput13064 | 23.9457 | C | 11s | False | False |
| 10 | r_112_1_cio_13066_0 | sonde ambiance salle de conferences | analogInput13066 | 24.307 | C | 11s | False | False |
| 11 | r_112_1_cio_13067_0 | sonde ambiance temperature bureau comptabilite rez | analogInput13067 | 23.2328 | C | 11s | False | False |
| 12 | r_112_1_cio_13068_0 | sonde ambiance bureau schematique s-sol | analogInput13068 | 22.5492 | C | 11s | False | False |
| 14 | r_112_1_cio_13070_0 | sonde ambiance bureau individuel s-sol | analogInput13070 | 23.4086 | C | 11s | False | False |
+----+---------------------+-----------------------------------------------------+------------------+---------+-------+-----+-------+-------+
Each point of the BACPoints object is accessible by it’s index, type or a part of something belonging to it
>>> point=points[8]
>>> point
<BACPointAnalogInput(r_112_1_cio_13064_0:analogInput#13064=26.51 degreesCelsius)>
>>> point.dump()
+--------------+-----------------------------------------------+
| property | value |
+--------------+-----------------------------------------------+
| class | BACPointAnalogInput |
| name | r_112_1_cio_13064_0 |
| description | sonde ambiance bureau direction cote hall rez |
| type | analogInput |
| address | 13064 |
| value | 26.57267189025879 |
| state | 26.57 |
| unit | degreesCelsius (C) |
| COV | False |
| OutOfService | False |
| index | 8 |
+--------------+-----------------------------------------------+
>>> point=device.points.analogInput(13064)
>>> point=bacnet[8015].points.analogOuput(18181)
>>> points['sonde hall'] # return the first object matching to this
<BACPointAnalogInput(r_112_1_cio_13064_0:analogInput#13064=26.55 degreesCelsius)>
>>> point=point['r_112_1_cio_13064_0']
>>> point=point['13064']
>>> point=point['analogInput13064']
Points are exposed through BACPoint objects (generic class), derived in BACPointBinaryInput, BACPointBinaryOutput, BACPointAnalogInput, BACPointAnalogOutput, BACPointBinaryValue, BACPointAnalogValue, BACPointMultiStateInput, BACPointMultiStateOutput, BACPointMultiStateValue objects, each providing specialized BACPoint extensions. You will have to dig a bit into theses objects to learn what helper they provide. Using bpython interactive interpreter with it’s autocompletion feature is a very convenient way to discover thoses object (with the actual lack of documentation)
>>> point.
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ activePriority address bacnetProperty bacnetproperties celciusToFahrenheit cov │
│ covCancel description digDecimals digUnit digUnitStr dump │
│ fahrenheitToCelcius index isAnalog isBinary isCOV isMultiState │
│ isOutOfService isWritable label match name onInit │
│ poll pollStop priority properties read refresh │
│ reloadBacnetProperties state toCelcius type unit unitNumber │
│ value │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
>>> point.value
26.699626922607422
>>> point.unit
'degreesCelsius'
>>> point.value=12.0 # if a point is writable, this will change it's value
>>> point.write(12.0, priority=8)
>>> point.write(12.0, prop='presentValue', priority=8)
# for binary values
>>> point.on()
>>> point.off()
>>> point.toggle()
>>> point.isOn()
>>> point.isOff()
>>> point.label
>>> point.labels
>>> point.value=1
>>> point.value='on'
# for multiState values
>>> point.state
>>> point.label
>>> point.labels
>>> point.value
>>> point.value=2
A device automatically refresh it’s points every 15s (the device’s polling time could be specified at object creation/declaration). You can stop this with device.pollStop() or adjust the polling period with device.poll(60). This is the device polling global setting. Every point may also be polled individually with point.poll(10) and point.pollStop(). Of course you may wish to set an individual poll for each point of the device with device.points.poll(60). But a global device.poll() is a more efficient way to do it. Refresh may also be done throug COV (Change Of Value) mechanism. By default, COV is not enabled on a device. You can enable COV subscriptions on a point with point.cov(), and disable it with point.covCancel(). This can also be done on each points with device.points.cov(). By default, the COV timeout is set to 300s. The poll and/or COV mechanism ensure the autorefresh of the points values. If needed, a point can be refreshed manually with point.refresh(). As suspected, the device.refresh() or device.points.refresh() does this globally.
If a BACPoint object doesn’t expose something that would be useful, either ask it (we will try to add this support) or use the underlying ._bac0point object which is the BAC0’s Point object associated to this point. If a BACDevice object doesn’t expose something that would be useful, you can use the underlying ._bac0device BAC0 device object. If the BAC object doesn’t expose something that would be useful, you can use the underlying ._bac0 BAC0 application object.
Integrated Node
The module provide a simple BACnet browser application you can start with “python -i -m digimat.bac0 [–ip “192.168.0.84/24”] [–router x.x.x.x] [–debug]”. This will launch the following application
parser=argparse.ArgumentParser(description='BACnet/IP browser')
parser.add_argument('--router', dest='router', type=str, help='BBMD router address')
parser.add_argument('--network', dest='network', type=str, help='optional ip/netsize of the BACnet/IP interface')
parser.add_argument('--debug', dest='debug', action='store_true', help='enable debug/verbose mode')
args=parser.parse_args()
bacnet=BAC(network=args.network, router=args.router)
if args.debug:
bacnet.BAC0LogDebug()
if bacnet.discover():
bacnet.dump()
When launched interactively (-i), you’ll have a working bacnet variable (a BAC object) ready to be used in just one command line.
Todo
We will try to add objects and methods docstring as soon as possible to help the use of theses objects. Please let us know (fhess@st-sa.ch) is this is useful for someone (for us it is).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file digimat.bac0-0.0.56.tar.gz
.
File metadata
- Download URL: digimat.bac0-0.0.56.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/65.6.3 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af08f32cc5fb8fe9238e5a8366dfe26f8f236ee9c0eafc36734cd586d491f1a9 |
|
MD5 | 4f6e67d187b16ff4798853d7dfc3cb3f |
|
BLAKE2b-256 | 283fb20f320b0e62584bea1342be616e5009d5172a9836f6690fe9b0efb659fa |