bitfocus / companion-module-qsys-remote-control

MIT License
5 stars 3 forks source link

Error messages for Control.set #14

Closed btmeadows1 closed 1 year ago

btmeadows1 commented 1 year ago

I'm getting this error repeatedly on my qsys module:

error: undefined:1 "String":"false","Value":0.0,"Position":0.0},{"Name":"z2_source_4","String":"false","Value":0.0,"Position":0.0},{"Name":"z2_source_5","String":"false","Value":0.0,"Position":0.0},{"Name":"z6_source_1","String":"false","Value":0.0,"Position":0.0},{"Name":"z6_source_2","String":"true","Value":1.0,"Position":1.0},{"Name":"z6_source_3","String":"false","Value":0.0,"Position":0.0},{"Name":"z6_source_4","String":"false","Value":0.0,"Position":0.0},{"Name":"z6_source_6","String":"false","Value":0.0,"Position":0.0}],"id":1} ^

SyntaxError: Unexpected token : in JSON at position 8 at JSON.parse () at /Applications/Companion.app/Contents/Resources/bundled-modules/qsys-remote-control/main.js:2:218120 at Array.forEach () at i.processResponse (/Applications/Companion.app/Contents/Resources/bundled-modules/qsys-remote-control/main.js:2:218094) at o. (/Applications/Companion.app/Contents/Resources/bundled-modules/qsys-remote-control/main.js:2:218011) at a.emit (/Applications/Companion.app/Contents/Resources/bundled-modules/qsys-remote-control/main.js:2:158054) at Socket. (/Applications/Companion.app/Contents/Resources/bundled-modules/qsys-remote-control/main.js:2:180142) at Socket.emit (node:events:513:28) at Socket.emit (node:domain:489:12) at addChunk (node:internal/streams/readable:324:12)

Node.js v18.16.0

Julusian commented 1 year ago

A quick look at the code finds https://github.com/bitfocus/companion-module-qsys-remote-control/blob/master/index.js#L124-L126, which looks like you are assuming that each TCP packet will contain one or more whole json blobs. But because TCP is a stream of data, a json blob can span multiple packets, which will result in what is being seen here.

Instead I would recommend modifying this strategy to do something like (in psuedo code):

this.receiveBuffer += response
while there is a '\x00' in this.receiveBuffer:
  const message = this.receiveBuffer.slice(0, index-of-the-\x00)
  this.receiveBuffer = this.receiveBuffer.slice(index-of-the-\x00)

  try {
    do processing of message
  } catch (e) {
    this.log(`Process mesage failed: ${e}`) // this is so that one failed message doesnt cause issues
  }
onfire4g05 commented 1 year ago

I'll try to get this fixed next week, but it may not be until the following week when I have time.