Last active
September 7, 2021 15:04
-
-
Save andrewdoss-bit/87ef1b53a7309f603ffde616f72c1922 to your computer and use it in GitHub Desktop.
Creating a record
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
def create_record(sample, measurement_config, period): | |
"""Create a record for insertion to a database.""" | |
record = {'location': 'outside of house'} | |
record['sensor_id'] = parse_value(sample[0], 'little', [6, 2]) | |
record['datetime'] = str(datetime.utcnow()) | |
for measurement, parse_args in config['measurements'].items(): | |
record[measurement] = sum([parse_value(x, 'little', *parse_args) for x in sample]) | |
record[measurement] = record[measurement] / period | |
return record | |
config = { | |
'pm_2_5': [2, 2, 0.1], | |
'pm_10': [4, 2, 0.1] | |
} | |
num_messages = 300 # 300 seconds = 5 minutes | |
sample = [ser.read(10) for i in range(num_messages)] | |
record = create_record(sample, config, num_messages) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment