Refactor communication

Add QoS andd retain flag
Refactor reducer
This commit is contained in:
Thomas Nordquist
2019-01-20 05:30:21 +01:00
parent 1839b551c0
commit f893d5ce60
21 changed files with 433 additions and 166 deletions

View File

@@ -1,7 +1,7 @@
import {
AddMqttConnection,
EventDispatcher,
Message,
MqttMessage,
addMqttConnectionEvent,
backendEvents,
checkForUpdates,
@@ -36,19 +36,20 @@ export class ConnectionManager {
connection.connect(options)
this.handleNewMessagesForConnection(connectionId, connection)
backendEvents.subscribe(makePublishEvent(connectionId), (msg: Message) => {
this.connections[connectionId].publish(msg.topic, msg.payload)
backendEvents.subscribe(makePublishEvent(connectionId), (msg: MqttMessage) => {
this.connections[connectionId].publish(msg)
})
}
private handleNewMessagesForConnection(connectionId: string, connection: MqttSource) {
const messageEvent = makeConnectionMessageEvent(connectionId)
connection.onMessage((topic: string, payload: Buffer) => {
connection.onMessage((topic: string, payload: Buffer, packet: any) => {
let buffer = payload
if (buffer.length > 10000) {
buffer = buffer.slice(0, 10000)
}
backendEvents.emit(messageEvent, { topic, payload: buffer.toString('base64') })
backendEvents.emit(messageEvent, { topic, payload: buffer.toString(), qos: packet.qos, retain: packet.retain })
})
}