Skip to content

Commit

Permalink
Merge pull request #7 from djjudas21/influxdb
Browse files Browse the repository at this point in the history
Convert data to Int or Float before sending to InfluxDB
  • Loading branch information
djjudas21 authored Jul 10, 2023
2 parents 0c5c9ee + 1eb0d38 commit 3da8df9
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion ecowitt_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ def log_request_info():
app.logger.debug('Body: %s', request.get_data())


def is_integer(element: any) -> bool:
"""
Test if a string can be represent as an integer
"""
if element is None:
return False
try:
int(element)
return True
except ValueError:
return False


def is_float(element: any) -> bool:
"""
Test if a string can be represent as a float
"""
if element is None:
return False
try:
float(element)
return True
except ValueError:
return False


def numify(value):
"""
Convert a string to an integer or float if possible
"""
if is_integer(value):
return int(value)
elif is_float(value):
return float(value)
else:
return value


@app.route('/report', methods=['POST'])
def logecowitt():

Expand Down Expand Up @@ -116,17 +154,20 @@ def logecowitt():
# Send the data to the Prometheus exporter
if prometheus:
metrics[key].set(value)
app.logger.debug("Set Prometheus metric %s: %s", key, value)

# Build an array of points to send to InfluxDB
if influxdb:
point = Point("weather").tag("station_id", station_id).field(key, value)
point = Point("weather").tag("station_id", station_id).field(key, numify(value))
app.logger.debug("Created InfluxDB point %s: %s", key, value)
points.append(point)

# Send the data to InfluxDB
if influxdb:
with InfluxDBClient(url=influxdb_url, token=influxdb_token, org=influxdb_org) as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
write_api.write(bucket=influxdb_bucket, record=points)
app.logger.debug("Submitted InfluxDB points to server")

# Return a 200 to the weather station
response = app.response_class(
Expand Down

0 comments on commit 3da8df9

Please sign in to comment.