Skip to content

Commit

Permalink
cleanup ServiceUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaquu committed Jun 18, 2024
1 parent 189e54e commit c061760
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
12 changes: 5 additions & 7 deletions src/lib/utils/ServiceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ module.exports = function (node: HAPServiceNodeType) {
}

const onInput = function (msg: Record<string, any>) {
if (msg.hasOwnProperty('payload')) {
if (msg.payload) {
// payload must be an object
const type = typeof msg.payload

Expand All @@ -241,7 +241,7 @@ module.exports = function (node: HAPServiceNodeType) {
return
}

const topic = node.config.topic ? node.config.topic : node.name
const topic = node.config.topic ?? node.name
if (node.config.filter && msg.topic !== topic) {
log.debug(
"msg.topic doesn't match configured value and filter is enabled. Dropping message."
Expand All @@ -250,15 +250,13 @@ module.exports = function (node: HAPServiceNodeType) {
}

let context: any = null
if (msg.payload.hasOwnProperty('Context')) {
if (msg.payload.Context) {
context = msg.payload.Context
delete msg.payload.Context
}

node.topic_in = msg.topic ? msg.topic : ''
node.topic_in = msg.topic ?? ''

// iterate over characteristics to be written
// eslint-disable-next-line no-unused-vars
Object.keys(msg.payload).map((key: string) => {
if (node.supported.indexOf(key) < 0) {
log.error(
Expand All @@ -277,7 +275,7 @@ module.exports = function (node: HAPServiceNodeType) {
)

if (context !== null) {
characteristic.setValue(value, () => {}, context)
characteristic.setValue(value, undefined, context)
} else {
characteristic.setValue(value)
}
Expand Down
6 changes: 2 additions & 4 deletions src/lib/utils/ServiceUtils2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ module.exports = function (node: HAPService2NodeType) {
return
}

const topic = node.config.topic ? node.config.topic : node.name
const topic = node.config.topic ?? node.name
if (node.config.filter && msg.topic !== topic) {
log.debug(
"msg.topic doesn't match configured value and filter is enabled. Dropping message."
Expand All @@ -292,8 +292,6 @@ module.exports = function (node: HAPService2NodeType) {

node.topic_in = msg.topic ?? ''

// iterate over characteristics to be written
// eslint-disable-next-line no-unused-vars
Object.keys(msg.payload).map((key: string) => {
if (node.supported.indexOf(key) < 0) {
if (
Expand Down Expand Up @@ -330,7 +328,7 @@ module.exports = function (node: HAPService2NodeType) {
)

if (context !== null) {
characteristic.setValue(value, () => {}, context)
characteristic.setValue(value, undefined, context)
} else {
characteristic.setValue(value)
}
Expand Down

0 comments on commit c061760

Please sign in to comment.