Skip to content

Commit

Permalink
Merge 50b331e into 9474aeb
Browse files Browse the repository at this point in the history
  • Loading branch information
joyja committed May 19, 2021
2 parents 9474aeb + 50b331e commit c43b1dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,19 @@ MqttSource.prototype.log = async function (scanClassId) {
return false
}
})
// TO-DO check how long it has been since last change, if it's been a while, log the previous value before out of deadband was detected.
const preDeadbandExitPoints = tags
.filter((tag) => {
const secondsSinceChange = tag.lastChangeOn - tag.prevChangeOn
return secondsSinceChange >= (tag.scanClass.rate / 1000.0) * 3
})
.map((tag) => {
return {
id: tag.id,
value: tag.prevValue,
timestamp: tag.prevChangeOn,
}
})
// The following is to collect realtime history of events to be published without isHistorical
if (tags.length > 0) {
this.rtHistory = [
Expand All @@ -502,6 +515,7 @@ MqttSource.prototype.log = async function (scanClassId) {
timestamp: getUnixTime(new Date()),
}
}),
...preDeadbandExitPoints,
]
}
// The following is to collect history in the event of a primaryHost going offline
Expand Down
10 changes: 9 additions & 1 deletion src/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class Tag extends Model {
this._units = result.units
this._deadband = result.deadband
this.prevValue = null
this.lastChangeOn = getUnixTime(new Date())
this.prevChangeWithinDeadband = false
this.prevChangeOn = getUnixTime(new Date())
}
get name() {
this.checkInit()
Expand Down Expand Up @@ -98,20 +100,26 @@ class Tag extends Model {
}
async setValue(value, write = true) {
this.checkInit()
const lastValue = this.value
const lastChangeOn = this.lastChangeOn
if (!this.prevChangeWithinDeadband) {
this.prevValue = lastValue
this.prevChangeOn = lastChangeOn
}
if (this.source && write) {
this.source.write(value)
}
const result = this.update(this.id, 'value', value, Tag).then((result) => {
this._value = result
this.pubsub.publish('tagUpdate', { tagUpdate: this })
})
this.lastChangeOn = getUnixTime(new Date())
this.prevChangeWithinDeadband =
this.datatype === 'BOOLEAN'
? this.value === this.prevValue
: Math.abs(this.value - this.prevValue) < this.deadband
if (!this.prevChangeWithinDeadband) {
this.prevChangeWithinDeadband = false
this.prevValue = this.value
}
return result
}
Expand Down

0 comments on commit c43b1dc

Please sign in to comment.