Skip to content

Commit

Permalink
Update config
Browse files Browse the repository at this point in the history
Support:
  - fields: $.payload
  • Loading branch information
blavka committed Sep 24, 2018
1 parent b2bae56 commit 99a348a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion mqtt2influxdb/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def port_range(port):
'points': [{
'measurement': And(str, len, Use(str_or_jsonPath)),
'topic': And(str, len),
Optional('fields'): {str: And(str, len, Use(str_or_jsonPath))},
Optional('fields'): Or({str: And(str, len, Use(str_or_jsonPath))}, And(str, len, Use(str_or_jsonPath))),
Optional('tags'): {str: And(str, len, Use(str_or_jsonPath))},
Optional('database'): And(str, len)
}]
Expand Down
19 changes: 11 additions & 8 deletions mqtt2influxdb/mqtt2influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,22 @@ def _on_mqtt_message(self, client, userdata, message):
'fields': {}}

if 'fields' in point:
for key in point['fields']:
val = self._get_value_from_str_or_JSONPath(point['fields'][key], msg)
if val is None:
continue
record['fields'][key] = val
if isinstance(point['fields'], jsonpath_ng.JSONPath):
record['fields'] = self._get_value_from_str_or_JSONPath(point['fields'], msg)
else:
for key in point['fields']:
val = self._get_value_from_str_or_JSONPath(point['fields'][key], msg)
if val is None:
continue
record['fields'][key] = val

if len(record['fields']) != len(point['fields']):
logging.warning('different number of fields')

if not record['fields']:
logging.warning('empty fields')
return

if len(record['fields']) != len(point['fields']):
logging.warning('different number of fields')

if 'tags' in point:
for key in point['tags']:
val = self._get_value_from_str_or_JSONPath(point['tags'][key], msg)
Expand Down

0 comments on commit 99a348a

Please sign in to comment.