@@ -82,16 +82,24 @@ const matchings = {
8282 humidity : [ 'rel. luftfeuchte' , 'luftfeuchtigkeit' , 'luftfeuchte' ] ,
8383 pressure : [ 'luftdruck' , 'druck' ] ,
8484 signal : [ 'stärke' , 'signal' ] ,
85+ noise_laeq : [ 'schallpegel' , 'geräuschpegel' ] ,
8586} ;
8687
8788const findSensorId = function findSensorId ( sensors , value_type ) {
8889 // temperature, humidity and pressure values
8990 // are named either directly or with a prefix
9091 // separated by underscores. The last element
9192 // should be the the desired phenomenon
92- let [ vt_sensortype , vt_phenomenon ] = value_type
93- . toLowerCase ( )
94- . split ( '_' ) ;
93+ let [ vt_sensortype , vt_phenomenon ] = [ ] ;
94+ const splitAtIndex = value_type . toLowerCase ( ) . indexOf ( '_' ) ;
95+ if ( splitAtIndex > 0 ) {
96+ [ vt_sensortype , vt_phenomenon ] = [
97+ value_type . toLowerCase ( ) . slice ( 0 , splitAtIndex ) ,
98+ value_type . toLowerCase ( ) . slice ( splitAtIndex + 1 ) ,
99+ ] ;
100+ } else {
101+ [ vt_sensortype , vt_phenomenon ] = value_type . toLowerCase ( ) . split ( '_' ) ;
102+ }
95103
96104 // DHT11 and DHT22 sensors have no underscore prefix
97105 if ( ! vt_phenomenon && [ 'temperature' , 'humidity' ] . includes ( vt_sensortype ) ) {
@@ -110,9 +118,10 @@ const findSensorId = function findSensorId (sensors, value_type) {
110118 const title = sensor . title . toLowerCase ( ) ;
111119 const type = sensor . sensorType . toLowerCase ( ) ;
112120 if (
113- ( title === vt_phenomenon || matchings [ vt_phenomenon ] . includes ( title ) || matchings [ vt_phenomenon ] . some ( alias => title . includes ( alias ) ) )
114- &&
115- ( type . startsWith ( vt_sensortype ) )
121+ ( title === vt_phenomenon ||
122+ matchings [ vt_phenomenon ] . includes ( title ) ||
123+ matchings [ vt_phenomenon ] . some ( ( alias ) => title . includes ( alias ) ) ) &&
124+ type . startsWith ( vt_sensortype )
116125 ) {
117126 sensorId = sensor . _id . toString ( ) ;
118127 break ;
@@ -137,7 +146,6 @@ const transformLuftdatenJson = function transformLuftdatenJson (json, sensors) {
137146 return outArray ;
138147} ;
139148
140-
141149module . exports = {
142150 decodeMessage : function ( message , { sensors } = { } ) {
143151 if ( ! sensors ) {
@@ -159,7 +167,9 @@ module.exports = {
159167 }
160168
161169 if ( ! json . sensordatavalues ) {
162- return Promise . reject ( new Error ( 'Invalid luftdaten json. Missing `sensordatavalues`' ) ) ;
170+ return Promise . reject (
171+ new Error ( 'Invalid luftdaten json. Missing `sensordatavalues`' )
172+ ) ;
163173 }
164174
165175 const transformedMeasurements = transformLuftdatenJson ( json , sensors ) ;
@@ -170,6 +180,8 @@ module.exports = {
170180 return transformAndValidateMeasurements ( transformedMeasurements ) ;
171181 }
172182
173- return Promise . reject ( new Error ( 'Cannot decode empty message (luftdaten decoder)' ) ) ;
174- }
183+ return Promise . reject (
184+ new Error ( 'Cannot decode empty message (luftdaten decoder)' )
185+ ) ;
186+ } ,
175187} ;
0 commit comments