server

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package server implements a BACnet/IP server device.

The server package provides a complete BACnet server implementation that can:

  • Respond to WhoIs requests with IAm messages
  • Handle ReadProperty, WriteProperty, ReadPropertyMultiple, WritePropertyMultiple
  • Handle SubscribeCOV and emit Confirmed/Unconfirmed COV Notifications
  • Manage BACnet objects and their properties in a thread-safe object store
  • Send proper BACnet error / abort responses for invalid or unsupported requests

Basic usage:

cfg := server.DefaultDeviceConfig()
srv, err := server.NewServer(cfg)
if err != nil {
    log.Fatal(err)
}
defer srv.Close()

// Add some objects
srv.AddObject(btypes.Object{
    ID: btypes.ObjectID{
        Type:     btypes.AnalogInput,
        Instance: 1,
    },
})

// Start the server
srv.Serve() // Blocks until Close() is called

Note: DeviceID 0 is a valid BACnet instance and is preserved. Use DefaultDeviceConfig() (or a nil cfg) when you want the demo default of 1000.

中文说明:server 包实现了 BACnet/IP 服务端设备,支持 WhoIs→IAm、 读写属性、SubscribeCOV/COV 通知、对象管理和错误/Abort 响应。 DeviceID=0 为合法实例,不会被静默改写为 1000。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DeviceConfig

type DeviceConfig struct {
	// DeviceID is the BACnet device instance (0..4194302). Zero is a valid
	// instance ID; it is NOT rewritten. Use DefaultDeviceConfig() or a nil
	// cfg to get the conventional demo default of 1000.
	// DeviceID 为 BACnet 设备实例号(0..4194302)。0 是合法实例,不会被静默改写。
	DeviceID     btypes.ObjectInstance
	DeviceName   string                     // BACnet device name
	VendorID     uint32                     // BACnet vendor identifier
	Interface    string                     // Network interface name (e.g., "eth0")
	Ip           string                     // IP address to bind to
	Port         int                        // BACnet port (default: 47808)
	SubnetCIDR   int                        // Subnet CIDR (e.g., 24 for /24)
	MaxPDU       uint16                     // Maximum PDU size (default: 1476)
	MaxSegments  uint                       // Maximum segments accepted (default: 0 = no segmentation)
	Segmentation segmentation.SegmentedType // Segmentation support (default: noSegmentation)
}

DeviceConfig contains the configuration for creating a BACnet server device.

中文说明:DeviceConfig 包含创建 BACnet 服务端设备的配置。

func DefaultDeviceConfig

func DefaultDeviceConfig() *DeviceConfig

DefaultDeviceConfig returns a DeviceConfig with sensible defaults.

中文说明:DefaultDeviceConfig 返回具有合理默认值的 DeviceConfig。

type ObjectStore

type ObjectStore struct {
	// contains filtered or unexported fields
}

ObjectStore provides thread-safe storage for BACnet device objects and their properties. It manages the complete object hierarchy: device → object type → object instance → properties.

中文说明:ObjectStore 提供线程安全的 BACnet 设备对象及其属性存储。 管理完整的对象层次结构:设备 → 对象类型 → 对象实例 → 属性。

func NewObjectStore

func NewObjectStore(deviceID btypes.ObjectInstance, deviceName string, vendorID uint32) *ObjectStore

NewObjectStore creates a new ObjectStore with the given device configuration.

中文说明:NewObjectStore 使用给定的设备配置创建新的 ObjectStore。

func (*ObjectStore) AddObject

func (s *ObjectStore) AddObject(obj btypes.Object) error

AddObject adds a new object to the store. Returns error if the object already exists.

中文说明:AddObject 向存储中添加新对象。如果对象已存在则返回错误。

func (*ObjectStore) DevicePropertyExists

func (s *ObjectStore) DevicePropertyExists(instance btypes.ObjectInstance) bool

DevicePropertyExists reports whether the device instance is known to this store.

func (*ObjectStore) GetAllObjects

func (s *ObjectStore) GetAllObjects() map[btypes.ObjectType]map[btypes.ObjectInstance]*btypes.Object

GetAllObjects returns all objects in the store.

中文说明:GetAllObjects 返回存储中的所有对象。

func (*ObjectStore) GetDeviceID

func (s *ObjectStore) GetDeviceID() btypes.ObjectInstance

GetDeviceID returns the device instance ID.

func (*ObjectStore) GetDeviceName

func (s *ObjectStore) GetDeviceName() string

GetDeviceName returns the device name.

func (*ObjectStore) GetObject

func (s *ObjectStore) GetObject(objType btypes.ObjectType, instance btypes.ObjectInstance) (*btypes.Object, bool)

GetObject retrieves an object by type and instance.

中文说明:GetObject 按类型和实例检索对象。

func (*ObjectStore) GetObjectList

func (s *ObjectStore) GetObjectList() []btypes.ObjectID

GetObjectList returns all user objects in the store (excludes the Device object).

中文说明:GetObjectList 返回存储中的所有用户对象(不含 Device 对象)。

func (*ObjectStore) GetProperty

func (s *ObjectStore) GetProperty(objType btypes.ObjectType, instance btypes.ObjectInstance, propType btypes.PropertyType) (interface{}, bool)

GetProperty retrieves a specific property from an object.

中文说明:GetProperty 从对象中检索特定属性。

func (*ObjectStore) GetPropertyAt

func (s *ObjectStore) GetPropertyAt(objType btypes.ObjectType, instance btypes.ObjectInstance, propType btypes.PropertyType, arrayIndex uint32) (interface{}, bool)

GetPropertyAt retrieves a property, honoring BACnet array indexes where applicable. For ObjectList: index 0 returns the array size, index N returns the Nth element (1-based).

中文说明:GetPropertyAt 检索属性,并支持 BACnet 数组索引。 对 ObjectList:索引 0 返回数组长度,索引 N 返回第 N 个元素(从 1 开始)。

func (*ObjectStore) GetVendorID

func (s *ObjectStore) GetVendorID() uint32

GetVendorID returns the vendor ID.

func (*ObjectStore) ListDeviceProperties

func (s *ObjectStore) ListDeviceProperties() []btypes.Property

ListDeviceProperties returns all readable device properties for PROP_ALL / REQUIRED. Object_List is included as the full BACnetARRAY of Object Identifiers.

func (*ObjectStore) RemoveObject

func (s *ObjectStore) RemoveObject(objType btypes.ObjectType, instance btypes.ObjectInstance) error

RemoveObject removes an object from the store.

中文说明:RemoveObject 从存储中移除对象。

func (*ObjectStore) SetDeviceProperty

func (s *ObjectStore) SetDeviceProperty(propType btypes.PropertyType, data interface{})

SetDeviceProperty sets or replaces a device-object property (object type Device). Useful for tuning Model_Name / Description before clients browse the device.

中文说明:SetDeviceProperty 设置或替换 Device 对象属性(如 Model_Name、Description)。

func (*ObjectStore) SetProperty

func (s *ObjectStore) SetProperty(objType btypes.ObjectType, instance btypes.ObjectInstance, propType btypes.PropertyType, data interface{}) error

SetProperty sets a specific property on an object.

中文说明:SetProperty 设置对象上的特定属性。

type Server

type Server interface {
	// Serve starts the server message loop. This method blocks until the server is stopped.
	// It should be called in a goroutine for non-blocking operation.
	// Serve 启动服务端消息循环。此方法阻塞直到服务端停止。
	// 应在 goroutine 中调用以实现非阻塞操作。
	Serve() error

	// Close stops the server and releases all resources.
	// Close 停止服务端并释放所有资源。
	Close() error

	// IsRunning returns true if the server message loop is running.
	// IsRunning 返回服务端消息循环是否正在运行。
	IsRunning() bool

	// AddObject adds a new object to the server's object store.
	// AddObject 向服务端对象存储中添加新对象。
	AddObject(obj btypes.Object) error

	// RemoveObject removes an object from the server's object store.
	// RemoveObject 从服务端对象存储中移除对象。
	RemoveObject(objType btypes.ObjectType, instance btypes.ObjectInstance) error

	// GetObject retrieves an object by type and instance.
	// GetObject 按类型和实例检索对象。
	GetObject(objType btypes.ObjectType, instance btypes.ObjectInstance) (*btypes.Object, bool)

	// SetProperty sets a specific property value on an object.
	// SetProperty 设置对象上的特定属性值。
	SetProperty(objType btypes.ObjectType, instance btypes.ObjectInstance, propType btypes.PropertyType, data interface{}) error

	// GetProperty retrieves a specific property value from an object.
	// GetProperty 从对象中检索特定属性值。
	GetProperty(objType btypes.ObjectType, instance btypes.ObjectInstance, propType btypes.PropertyType) (interface{}, bool)

	// GetObjectStore returns the underlying object store for direct access.
	// GetObjectStore 返回底层对象存储以供直接访问。
	GetObjectStore() *ObjectStore

	// GetDeviceID returns the server's device instance ID.
	// GetDeviceID 返回服务端的设备实例 ID。
	GetDeviceID() btypes.ObjectInstance
}

Server defines the interface for a BACnet server device. A BACnet server responds to WhoIs requests with IAm, and handles ReadProperty, WriteProperty, ReadPropertyMultiple, WritePropertyMultiple, and SubscribeCOV requests from BACnet clients.

中文说明:Server 定义了 BACnet 服务端设备接口。 BACnet 服务端响应 WhoIs 请求并回复 IAm,处理客户端的 ReadProperty、 WriteProperty、ReadPropertyMultiple、WritePropertyMultiple 和 SubscribeCOV 请求。

func NewServer

func NewServer(cfg *DeviceConfig) (Server, error)

NewServer creates a new BACnet server with the given configuration. It initializes the data link layer, creates the object store, and prepares the server for handling BACnet requests.

Parameters:

cfg - DeviceConfig containing the server configuration

Returns:

A new Server instance and any error encountered during initialization.

中文说明:NewServer 使用给定的配置创建新的 BACnet 服务端。 初始化数据链路层,创建对象存储,并准备服务端以处理 BACnet 请求。

func NewServerWithDataLink(cfg *DeviceConfig, dl datalink.DataLink) (Server, error)

NewServerWithDataLink creates a new BACnet server with an existing data link. This is primarily useful for testing with mock data links.

中文说明:NewServerWithDataLink 使用已有的数据链路创建新的 BACnet 服务端。 主要用于使用 mock 数据链路进行测试。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL