MrRoundRobin / homeassistant-teams

HomeAssistant integration for Microsoft Teams Meetings
https://robin.ms
MIT License
19 stars 1 forks source link

Sending a command twice leads to crashing #1

Open jalmeroth opened 1 year ago

jalmeroth commented 1 year ago

Hi there 👋,

thanks for this awesome project! I really like the idea. While tinkering around with it, I discovered that the npm process is crashing if you send the same mqtt command twice:

homeassistant-teams % npm start

> homeassistant-teams@1.0.0 start /Users/jan/workspace/homeassistant-teams
> node dist/index.js

/Users/jan/workspace/homeassistant-teams/dist/index.js:237
    mqtt.publish('teams/state/isMuted', message.meetingUpdate.meetingState.isMuted ? 'ON' : 'OFF', { retain: true });
                                                              ^

TypeError: Cannot read property 'meetingState' of undefined
    at WebSocket.message (/Users/jan/workspace/homeassistant-teams/dist/index.js:237:63)
    at WebSocket.emit (events.js:400:28)
    at Receiver.receiverOnMessage (/Users/jan/workspace/homeassistant-teams/node_modules/ws/lib/websocket.js:1180:20)
    at Receiver.emit (events.js:400:28)
    at Receiver.dataMessage (/Users/jan/workspace/homeassistant-teams/node_modules/ws/lib/receiver.js:541:14)
    at Receiver.getData (/Users/jan/workspace/homeassistant-teams/node_modules/ws/lib/receiver.js:459:17)
    at Receiver.startLoop (/Users/jan/workspace/homeassistant-teams/node_modules/ws/lib/receiver.js:158:22)
    at Receiver._write (/Users/jan/workspace/homeassistant-teams/node_modules/ws/lib/receiver.js:84:10)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at Receiver.Writable.write (internal/streams/writable.js:303:10)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! homeassistant-teams@1.0.0 start: `node dist/index.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the homeassistant-teams@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
mosquitto_pub -h "<mqtt-server>" -t "teams/state/isMuted/command" -m "OFF"
mosquitto_pub -h "<mqtt-server>" -t "teams/state/isMuted/command" -m "OFF"

This is probably due to the fact that the current state is not respected.

jalmeroth commented 1 year ago

It is because the server message looks like this, which is not parsed into ServerMessage-type:

{
  apiVersion: '1.0.0',
  errorMsg: 'toggle mute error: Error: Cannot unmute when already locally and server unmuted'
}

I could resolve the crasher by applying this to WS on-message handler:

    const message: ServerMessage = JSON.parse(data.toString())

    if (!message.hasOwnProperty("meetingUpdate")) {
        console.warn(message)
        return
    }

    ...