-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_weather_data.py
62 lines (53 loc) · 2.38 KB
/
import_weather_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
import configparser
import datetime
import json
import os
import sqlite3
directory = '/home/crassmann/transfer/'
config = configparser.ConfigParser()
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
config.read(os.path.join(credential_dir, 'weather_upload'))
class SensorData:
def __init__(self, data):
self.time = datetime.datetime.strptime(data['time'], '%Y-%m-%d %H:%M:%S')
self.model = data['model']
self.sensor_id = data['id']
self.channel = ""
self.temperature_C = ""
self.temperature_C_dec = None
self.humidity = ""
self.humidity_dec = None
if 'channel' in data.keys():
self.channel = data['channel']
if 'temperature_C' in data.keys():
self.temperature_C = str(data['temperature_C'])
self.temperature_C_dec = data['temperature_C']
if 'humidity' in data.keys():
self.humidity = str(data['humidity'])
self.humidity_dec = data['humidity']
if 'temperature_F' in data.keys():
self.temperature_C = (data['temperature_F'] - 32) * 5 / 9
self.temperature_C_dec = float((data['temperature_F'] - 32) * 5 / 9)
def __str__(self):
return "time: %s, model: %s, sensor_id: %s, channel: %s, temperature_c: %s, temperature_c_dec: %s, humidity: %s, humidity_dec: %s" % (
self.time, self.model, self.sensor_id, self.channel, self.temperature_C, self.temperature_C_dec, self.humidity, self.humidity_dec)
def write_sensor_data(self):
sql = """
INSERT INTO sensors('time','model','sensor_id','channel','temperature_C','temperature_C_dec','humidity','humidity_dec') VALUES (?,?,?,?,?,?,?,?)
"""
with sqlite3.connect(config.get('Database', 'path', 1)) as con:
cur = con.cursor()
cur.execute(sql,
(self.time, self.model, self.sensor_id, self.channel, self.temperature_C, self.temperature_C_dec, self.humidity, self.humidity_dec))
con.commit()
for filename in os.listdir(directory):
if filename.endswith(".json"):
file = os.path.join(directory, filename)
lines = tuple(open(file, 'r'))
for line in lines:
data = json.loads(line)
sensor_data = SensorData(data)
sensor_data.write_sensor_data()
os.remove(file)