2020import { ProtocolClient , Content , DefaultContent , createLoggers , ContentSerdes } from "@node-wot/core" ;
2121import * as TD from "@node-wot/td-tools" ;
2222import * as mqtt from "mqtt" ;
23- import { MqttClientConfig , MqttForm , MqttQoS } from "./mqtt" ;
23+ import { MqttClientConfig , MqttForm } from "./mqtt" ;
2424import * as url from "url" ;
2525import { Subscription } from "rxjs/Subscription" ;
2626import { Readable } from "stream" ;
27- import { IClientPublishOptions } from "mqtt" ;
2827import MQTTMessagePool from "./mqtt-message-pool" ;
28+ import { mapQoS } from "./util" ;
2929
3030const { debug, warn } = createLoggers ( "binding-mqtt" , "mqtt-client" ) ;
3131
@@ -130,7 +130,7 @@ export default class MqttClient implements ProtocolClient {
130130 const buffer = content === undefined ? Buffer . from ( "" ) : await content . toBuffer ( ) ;
131131 await pool . publish ( topic , buffer , {
132132 retain : form [ "mqv:retain" ] ,
133- qos : this . mapQoS ( form [ "mqv:qos" ] ) ,
133+ qos : mapQoS ( form [ "mqv:qos" ] ) ,
134134 } ) ;
135135 }
136136
@@ -152,7 +152,7 @@ export default class MqttClient implements ProtocolClient {
152152 const buffer = content === undefined ? Buffer . from ( "" ) : await content . toBuffer ( ) ;
153153 await pool . publish ( topic , buffer , {
154154 retain : form [ "mqv:retain" ] ,
155- qos : this . mapQoS ( form [ "mqv:qos" ] ) ,
155+ qos : mapQoS ( form [ "mqv:qos" ] ) ,
156156 } ) ;
157157 // there will be no response
158158 return new DefaultContent ( Readable . from ( [ ] ) ) ;
@@ -206,21 +206,4 @@ export default class MqttClient implements ProtocolClient {
206206 }
207207 return true ;
208208 }
209-
210- private mapQoS ( qos : MqttQoS | undefined ) : Required < IClientPublishOptions > [ "qos" ] {
211- switch ( qos ) {
212- case "0" :
213- return 0 ;
214- case "1" :
215- return 1 ;
216- case "2" :
217- return 2 ;
218- case undefined :
219- return 0 ;
220- default :
221- warn ( `MqttClient received unsupported QoS level '${ qos } '` ) ;
222- warn ( `MqttClient falling back to QoS level '0'` ) ;
223- return 0 ;
224- }
225- }
226209}
0 commit comments