-
Notifications
You must be signed in to change notification settings - Fork 11
/
bacnet.js
161 lines (149 loc) · 5.46 KB
/
bacnet.js
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
module.exports = function (RED) {
const Bacnet = require('./lib/bacnet');
/**
* Backnet Server Node
*/
class BacnetServerNode {
constructor(config) {
RED.nodes.createNode(this, config);
this.connection = new Bacnet(config);
this.on('close', () => this.connection.onDestroy());
}
}
RED.nodes.registerType('bacnet-server', BacnetServerNode);
/**
* BacnetDiscovery
* The whoIs command discovers all BACnet
* devices in the network.
*/
class BacnetDiscovery {
constructor(config) {
RED.nodes.createNode(this, config);
this.status({fill:'green', shape: 'dot', text: 'connected'});
const { lowLimit, highLimit, address } = config;
const server = RED.nodes.getNode(config.server);
server.connection.whoIs(lowLimit, highLimit, address, (device) => this.send({ device: device }));
}
}
RED.nodes.registerType('bacnet-discovery', BacnetDiscovery);
/**
* Bacnet Read Property
*
* The readProperty command reads a single
* property of an object from a device.
*/
class BacnetReadProperty {
constructor(config) {
RED.nodes.createNode(this, config);
this.server = RED.nodes.getNode(config.server);
this.defaults = config;
this.on('input', this.onInput);
}
onInput(input) {
const { address, objectType, objectInstance, propertyId, arrayIndex } = Object.assign({}, this.defaults, input.device);
this.server.connection.readProperty(
address,
objectType,
objectInstance,
propertyId,
arrayIndex
)
.then((payload) => {
const message = Object.assign({}, input, { payload: payload });
this.send(message);
})
.catch((error) => this.error({ error: error }));
};
}
RED.nodes.registerType('bacnet-read', BacnetReadProperty);
/**
* Write Property
*
* The writeProperty command writes a single
* property of an object to a device.
*/
class BacnetWriteProperty {
constructor(config) {
RED.nodes.createNode(this, config);
this.server = RED.nodes.getNode(config.server);
this.defaults = config;
this.on('input', this.onInput);
}
onInput(input) {
const valueList = [{
type: +(input.payload.applicationTag || this.defaults.applicationTag),
value: (input.payload.value || this.defaults.value) + Math.random() * 10000
}];
const { address, objectType, objectInstance, propertyId, priority } = Object.assign({}, this.defaults, input.device, input.payload);
console.log('write', valueList)
this.server.connection.writeProperty(
address,
objectType,
objectInstance,
propertyId,
priority,
valueList
)
.then(() => this.send(input))
.catch((error) => this.error({ error: error }));
}
}
RED.nodes.registerType('bacnet-write', BacnetWriteProperty);
/**
* Bacnet Read Property Multiple
*
* The readPropertyMultiple command reads multiple properties from multiple device objects
*/
class BacnetReadPropertyMultiple {
constructor(config) {
RED.nodes.createNode(this, config);
this.server = RED.nodes.getNode(config.server);
this.defaults = config;
this.on('input', this.onInput);
}
onInput(input) {
if (!input.requestArray) {
this.error('requestArray not provided for Bacnet read multiple.')
return;
}
const { address } = Object.assign({}, this.defaults, input.device);
this.server.connection.readPropertyMultiple(
address,
input.requestArray,
)
.then((payload) => {
const message = Object.assign({}, input, { payload: payload });
this.send(message);
})
.catch((error) => this.error({ error: error }));
};
}
RED.nodes.registerType('bacnet-read-multiple', BacnetReadPropertyMultiple);
/**
* Bacnet Write Property Multiple
*
* The writePropertyMultiple command writes multiple properties to multiple device objects
*/
class BacnetWritePropertyMultiple {
constructor(config) {
RED.nodes.createNode(this, config);
this.server = RED.nodes.getNode(config.server);
this.defaults = config;
this.on('input', this.onInput);
}
onInput(input) {
if (!input.valueList) {
this.error('valueList not provided for Bacnet write multiple.');
return;
}
const { address } = Object.assign({}, this.defaults, input.device);
this.server.connection.writePropertyMultiple(
address,
input.valueList,
)
.then(() => this.send(input))
.catch((error) => this.error({ error: error }));
};
}
RED.nodes.registerType('bacnet-write-multiple', BacnetWritePropertyMultiple);
};