Skip to content

Commit

Permalink
Update app
Browse files Browse the repository at this point in the history
  • Loading branch information
blavka committed Jun 28, 2019
1 parent bd884b3 commit a50fedd
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions cp2influxdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'COOPER to InfluxDB'

import click
import click_log
import datetime
import influxdb
import json
Expand All @@ -14,18 +15,18 @@

MEASUREMENTS = [
'altitude',
'co2-conc',
'co2_conc',
'humidity',
'illuminance',
'motion-count',
'motion_count',
'orientation',
'press-count',
'press_count',
'pressure',
'rssi',
'sequence',
'sound-level',
'sound_level',
'temperature',
'voc-conc',
'voc_conc',
'voltage',
]

Expand All @@ -34,6 +35,7 @@
@click.option('--config', '-c', 'config_file', type=click.File('r'),
required=True, help='Configuration file.')
@click.option('--test', is_flag=True, help='Test configuration file.')
@click_log.simple_verbosity_option(default='INFO')
@click.version_option(version=__version__)
def main(config_file, test=False):
try:
Expand Down Expand Up @@ -73,19 +75,40 @@ def server(config):
message = sock.recv_json()
time = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
points = []
for k, v in message.items():
if k in MEASUREMENTS and v is not None:
points.append({
'measurement': k,
'time': time,
'tags': {
'id': message['id'],
'gw': message['gw'],
},
'fields': {
'value': v
}
})

mtype = message.get('type', 'beacon')

if mtype == 'beacon':
for k, v in message.items():
if k in MEASUREMENTS and v is not None:
points.append({
'measurement': k,
'time': time,
'tags': {
'id': message['id'],
'gw': message['gw'],
},
'fields': {
'value': v
}
})
elif mtype == 'sound':
points.append({
'measurement': 'sound',
'time': time,
'tags': {
'id': message['id'],
'gw': message['gw'],
},
'fields': {
'min': message['min'],
'max': message['max'],
}
})
else:
logging.error('Unknown message type: %s' % mtype)
return

db.write_points(points)
except zmq.error.Again as e:
logging.error('ZeroMQ error: %s' % e)
Expand Down

0 comments on commit a50fedd

Please sign in to comment.