Skip to content

Remove onInput and subscribe on startup #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions bacnet.html
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ <h3><code>valueList</code></h3>
</script>

<script type="text/x-red" data-help-name="bacnet-subscribe-property">
<p>The <code>subscribeProperty</code> command subscribes to a specified objects single properties changes. It accepts an input whose device property will override the properties set here.</p>
<p>The <code>subscribeProperty</code> command subscribes to a specified objects single properties changes.</p>
<ul>
<li><code>address</code> <em>[string]</em> - IP address of the target device.</li>
<li><code>objectType</code> <em>[number]</em> - The BACNET object type to read.</li>
Expand All @@ -1152,41 +1152,41 @@ <h3><code>valueList</code></h3>
defaults: {
address: {
value: '',
required: false
required: true
},
objectType: {
value: '',
required: false,
required: true,
validate: RED.validators.number()
},
objectInstance: {
value: '',
required: false,
required: true,
validate: RED.validators.number()
},
propertyId: {
value: '',
required: false,
required: true,
validate: RED.validators.number()
},
arrayIndex: {
value: undefined
},
subscribeId: {
value: '',
required: false,
required: true,
validate: RED.validators.number()
},
issueConfirmedNotifications: {
value: false,
required: false
required: true
},
server: {
value: '',
type: 'bacnet-server'
}
},
inputs: 1,
inputs: 0,
outputs: 1,
icon: 'automation.gif',
paletteLabel: 'SubscribeCOVProperty',
Expand Down Expand Up @@ -1254,7 +1254,7 @@ <h3><code>valueList</code></h3>
</script>

<script type="text/x-red" data-help-name="bacnet-subscribe-cov">
<p>The <code>subscribeCOV</code> command subscribes to a specified objects changes. It accepts an input whose device property will override the properties set here.</p>
<p>The <code>subscribeCOV</code> command subscribes to a specified objects changes.</p>
<ul>
<li><code>address</code> <em>[string]</em> - IP address of the target device.</li>
<li><code>objectType</code> <em>[number]</em> - The BACNET object type to read.</li>
Expand All @@ -1272,38 +1272,38 @@ <h3><code>valueList</code></h3>
defaults: {
address: {
value: '',
required: false
required: true
},
objectType: {
value: '',
required: false,
required: true,
validate: RED.validators.number()
},
objectInstance: {
value: '',
required: false,
required: true,
validate: RED.validators.number()
},
subscribeId: {
value: '',
required: false,
required: true,
validate: RED.validators.number()
},
issueConfirmedNotifications: {
value: false,
required: false,
required: true,
},
lifetime: {
value: 0,
required: false,
required: true,
validate: RED.validators.number()
},
server: {
value: '',
type: 'bacnet-server'
}
},
inputs: 1,
inputs: 0,
outputs: 1,
icon: 'automation.gif',
paletteLabel: 'SubscribeCOV',
Expand Down
59 changes: 24 additions & 35 deletions bacnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,30 +168,25 @@ module.exports = function (RED) {
RED.nodes.createNode(this, config);
this.subscription = null;
this.server = RED.nodes.getNode(config.server);
this.defaults = config;
this.on('input', this.onInput);
this.subscribeProperty(config)

this.on('close', this.unsubscribe);
}

onInput(input) {
const { address, objectType, objectInstance, propertyId, arrayIndex, subscribeId, issueConfirmedNotifications } = Object.assign({}, this.defaults, input.device);
subscribeProperty(config) {
const { address, objectType, objectInstance, propertyId, arrayIndex, subscribeId, issueConfirmedNotifications } = config;

const device = { type: objectType, instance: objectInstance };
const property = { propertyIdentifier: propertyId, propertyArrayIndex: arrayIndex };

// Unsubscribe before subscribing again
this.unsubscribe()
.then(() => this.server.connection.subscribeProperty(
address,
device,
property,
subscribeId,
issueConfirmedNotifications,
(payload) => {
const message = Object.assign({}, input, { payload: payload })
this.send(message);
})
)
this.server.connection.subscribeProperty(
address,
device,
property,
subscribeId,
issueConfirmedNotifications,
(payload) => this.send({ payload: payload })
)
.then(() => this.subscription = {
address: address,
device: device,
Expand Down Expand Up @@ -226,29 +221,23 @@ module.exports = function (RED) {
RED.nodes.createNode(this, config);
this.subscription = null;
this.server = RED.nodes.getNode(config.server);
this.defaults = config;
this.on('input', this.onInput);
this.subscribeCOV(config);

this.on('close', this.unsubscribe);
}

onInput(input) {
const { address, objectType, objectInstance, subscribeId, issueConfirmedNotifications, lifetime } = Object.assign({}, this.defaults, input.device);

subscribeCOV(config) {
const { address, objectType, objectInstance, subscribeId, issueConfirmedNotifications, lifetime } = config;
const device = { type: objectType, instance: objectInstance };

// Unsubscribe before subscribing again
this.unsubscribe()
.then(() => this.server.connection.subscribeCOV(
address,
device,
subscribeId,
issueConfirmedNotifications,
lifetime,
(payload) => {
const message = Object.assign({}, input, { payload: payload })
this.send(message);
})
)
this.server.connection.subscribeCOV(
address,
device,
subscribeId,
issueConfirmedNotifications,
lifetime,
(payload) => this.send({ payload: payload })
)
.then(() => this.subscription = {
address: address,
device: device,
Expand Down