use csv DictReader for dynamic fields

This commit is contained in:
Thomas Ruoff
2019-09-06 01:02:33 +02:00
parent 1c8c5282ad
commit c184f7aaa6

View File

@@ -15,62 +15,70 @@
# CSV file specs # CSV file specs
# #
# | DB Field | DB field | CSV Column | CSV Format # | DB Field | DB field | CSV Column | CSV Format
# |-------------|----------|-----------------------|-------------------------- # |--------------------|----------|--------------------|--------------------------
# | time | time | 1 Time | 2017/05/19 00:00:11 (UTC) # | time | time | Time | 2017/05/19 00:00:11 (UTC)
# | | | 2 durP1 | # | ? | ? | durP1 |
# | | | 3 ratioP1 | # | ? | ? | ratioP1 |
# | | | 4 P1 | # | ? | ? | P1 |
# | | | 5 durP2 | # | ? | ? | durP2 |
# | | | 6 ratioP2 | # | ? | ? | ratioP2 |
# | | | 7 P2 | # | ? | ? | P2 |
# | SDS_P1 | field | 8 SDS_P1 | 10.36 # | SDS_P1 | field | SDS_P1 | 10.36
# | SDS_P2 | field | 9 SDS_P2 | 9.50 # | SDS_P2 | field | SDS_P2 | 9.50
# | temperature | field | 10 Temp | 16.00 # | PMS_P1 | field | PMS_P1 |
# | humidity | field | 11 Humidity | 79.00 # | PMS_P2 | field | PMS_P2 |
# | | | 12 BMP_temperature | # | temperature | field | Temp | 16.00
# | | | 13 BMP_pressure | # | humidity | field | Humidity | 79.00
# | | | 14 BME280_temperature | # | BMP_temperature | field | BMP_temperature |
# | | | 15 BME280_humidity | # | BMP_pressure | field | BMP_pressure |
# | | | 16 BME280_pressure | # | BME280_temperature | field | BME280_temperature |
# | samples | field | 17 Samples | 828799 # | BME280_humidity | field | BME280_humidity |
# | min_micro | field | 18 Min_cycle | 172 # | BME280_pressure | field | BME280_pressure |
# | max_micro | field | 19 Max_cycle | 25198 # | samples | field | Samples | 828799
# | | | 20 Signal | -91 # | min_micro | field | Min_cycle | 172
# | node | tag | -- -- | e.g. esp8266-16229960 # | max_micro | field | Max_cycle | 25198
# # | signal | field | Signal | -91
# | node | tag | N/A filename? | e.g. esp8266-16229960
import os import os
import sys import sys
import csv import csv
from datetime import datetime, timedelta from datetime import datetime, timedelta
def getTimestamp(timestr): def get_timestamp(timestr):
naiveDt = datetime.strptime(timestr, '%Y/%m/%d %H:%M:%S'); naive_dt = datetime.strptime(timestr, '%Y/%m/%d %H:%M:%S');
utcTimestamp = (naiveDt - datetime(1970, 1, 1)) / timedelta(seconds=1) utc = (naive_dt - datetime(1970, 1, 1)) / timedelta(seconds=1)
return int(utcTimestamp) return int(utc)
sensor_id = os.environ.get('SENSOR_ID', '16229960') SENSOR_ID = os.environ.get('SENSOR_ID', '16229960')
database = os.environ.get('INFLUXDB_DATABASE', 'feinstaub') DATABASE = os.environ.get('INFLUXDB_DATABASE', 'sensors')
node = 'esp8266-' + sensor_id NODE = 'esp8266-' + SENSOR_ID
outline = '{database},node={node} SDS_P1={sds_p1},SDS_P2={sds_p2},humidity={humidity},min_micro={min_micro},max_micro={max_micro},samples={samples},temperature={temperature} {timestamp}'
reader = csv.reader(sys.stdin, delimiter=';') NAME_MAP = {
for row in reader: 'Humidity': 'humidity',
if (row[0] == 'Time'): 'Max_cycle': 'max_micro',
continue 'Samples': 'samples',
'Min_cycle': 'min_micro',
'Signal': 'signal',
'Temp': 'temperature'
}
values = {} READER = csv.DictReader(sys.stdin, delimiter=';')
values['database'] = database for row in READER:
values['node'] = node measurements = []
values['timestamp'] = getTimestamp(row[0]) for header, value in row.items():
values['sds_p1'] = row[7] if header == 'Time' or not value:
values['sds_p2'] = row[8] continue
values['temperature'] = row[9] measurements.append('{0}={1}'.format(NAME_MAP.get(header, header), value))
values['humidity'] = row[10]
values['samples'] = row[16]
values['min_micro'] = row[17]
values['max_micro'] = row[18]
print(outline.format(**values)) values = {
'database': DATABASE,
'node': NODE,
'measurements': ','.join(measurements),
'time': get_timestamp(row['Time'])
}
print('{database},node={node} {measurements} {time}'.format(**values))