Subversion Repositories yaws

Rev

Rev 16 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16 ron 1
#!/usr/bin/python3
1 ron 2
# -*- coding: utf-8 -*-
3
#
4
#  database/samples.py
5
#
6
#  Copyright 2020 Ron Wellsted <ron@wellsted.org.uk>
7
#
8
# This program is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 2 of the License, or
11
# (at your option) any later version.
12
#
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with this program; if not, write to the Free Software
20
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
 
22
from sqlalchemy import create_engine
23
 
13 ron 24
engine = create_engine('sqlite:///yaws.sqlite3', echo=True)
1 ron 25
from sqlalchemy.orm import sessionmaker
26
 
27
Session = sessionmaker(bind=engine)
28
session = Session()
29
import datetime
30
from sqlalchemy import Table, Column, DateTime, Boolean, Float
31
from sqlalchemy.ext.declarative import declarative_base
32
 
15 ron 33
def create_connection():
34
    return engine.connect()
35
 
1 ron 36
Base = declarative_base()
37
 
38
class Samples(Base):
39
    __tablename__ = 'samples'
40
    sampled = Column(DateTime, primary_key=True, default=datetime.datetime.utcnow)
41
    airtemp = Column(Float)
42
    gndtemp = Column(Float)
43
    pressure = Column(Float)
44
    humidity = Column(Float)
45
    windspeed = Column(Float)
20 ron 46
    direction = Column(Float)
1 ron 47
    rain = Column(Boolean)
48
    rainfall = Column(Float)
49
    sunlight = Column(Float)
50
    cputemp = Column(Float)
51
 
52
    def __repr__(self):
53
        return {'sampled':self.sampled, 'airtemp':self.airtemp,
54
            'gndtemp':self.gndtemp, 'pressure':self.pressure,
55
            'humidity':self.humidity, 'windspeed':self.windspeed,
56
            'rain':self.rain, 'rainfall':self.rainfall,
57
            'sunlight':self.sunlight, 'cputemp':self.cputemp}
58
 
59
    def __str__(self):
60
        return (f'Sampled={self.sampled}\n'
61
            f'AirTemp={self.airtemp}\n'
62
            f'GndTemp={self.gndtemp}\n'
63
            f'Pressure={self.pressure}\n'
64
            f'Humidity={self.humidity}\n'
65
            f'Windspeed={self.windspeed}\n'
66
            f'Rain={self.rain}\n'
67
            f'Rainfall={self.rainfall}\n'
68
            f'Sunlight={self.sunlight}\n'
69
            f'CPUTemp={self.cputemp}')
70
 
71
Base.metadata.create_all(engine)