bacnet-device
Version:
A TypeScript library for implementing BACnet IP devices in Node.js.
84 lines • 2.78 kB
JavaScript
import { ErrorCode, ErrorClass, } from '@innovation-system/node-bacnet';
import { BDError } from '../errors.js';
import { AsyncEventEmitter, } from '../events.js';
import { TaskQueue } from '../taskqueue.js';
/**
* Enumerates the types of properties that can be defined.
*/
export var BDPropertyType;
(function (BDPropertyType) {
/** A property whose data consists of a single value. */
BDPropertyType[BDPropertyType["SINGLET"] = 0] = "SINGLET";
/** A property whose data consists of an array of values. */
BDPropertyType[BDPropertyType["ARRAY"] = 1] = "ARRAY";
})(BDPropertyType || (BDPropertyType = {}));
const shared_task_queue = new TaskQueue();
/**
* Abstract base class for all types of properties.
*/
export class BDAbstractProperty extends AsyncEventEmitter {
/**
* This property's BACnet identifier.
*/
identifier;
/**
* Indicates whether this property can be written to by other devices in
* the BACnet network.
*/
#writable;
#queue;
#data;
constructor(identifier, writable, data) {
super();
this.#data = data;
this.identifier = identifier;
this.#writable = typeof data !== 'function' && writable;
this.#queue = shared_task_queue;
}
/**
* Returns true if the property is writable (commandable) from other devices
* on the BACnet network, false otherwise.
*/
isWritable() {
return this.#writable;
}
/**
* Sets whether this property can be written to from other devices on the
* BACnet network.
*/
setWritable(writable) {
this.#writable = writable;
}
getData() {
return typeof this.#data === 'function' ? this.#data({ date: new Date() }) : this.#data;
}
async setData(data) {
await this.#queue.run(() => this.___updateData(data));
}
/**
* Allows the object to which this property is added to share its own task
* queue, so that calls to `setData` may be managed transactionally
* with requests to access the property's data coming from the BACnet
* network.
* @see {@link setData}
* @internal
*/
___setQueue(queue) {
this.#queue = queue;
}
/**
* Used by subclasses to update this property's data.
* @see {@link ___readData}
* @see {@link ___writeData}
* @internal
*/
async ___updateData(data) {
if (typeof this.#data === 'function') {
throw new BDError('polled property', ErrorCode.WRITE_ACCESS_DENIED, ErrorClass.PROPERTY);
}
await this.___asyncEmitSeries(true, 'beforecov', this, data);
this.#data = data;
await this.___asyncEmitSeries(false, 'aftercov', this, data);
}
}
//# sourceMappingURL=abstract.js.map