Skip to content

Commit

Permalink
Move to using one database, refactor Flask database management to use…
Browse files Browse the repository at this point in the history
… flask_sqlalchemy (#115)
  • Loading branch information
kizniche committed Feb 8, 2017
1 parent 162715c commit a3d3abf
Show file tree
Hide file tree
Showing 23 changed files with 1,402 additions and 1,537 deletions.
6 changes: 6 additions & 0 deletions mycodo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

# SQLite3 databases that hold users and settings
DATABASE_PATH = os.path.join(INSTALL_DIRECTORY, 'databases')
SQL_DATABASE_MYCODO_5 = os.path.join(DATABASE_PATH, 'mycodo_5.db')
SQL_DATABASE_MYCODO = os.path.join(DATABASE_PATH, 'mycodo.db')
SQL_DATABASE_USER = os.path.join(DATABASE_PATH, 'users.db')
SQL_DATABASE_NOTE = os.path.join(DATABASE_PATH, 'notes.db')
Expand Down Expand Up @@ -103,13 +104,18 @@

class ProdConfig(object):
""" Production Configuration """
SQL_DATABASE_MYCODO_5 = os.path.join(DATABASE_PATH, 'mycodo_5.db')
SQL_DATABASE_MYCODO = os.path.join(DATABASE_PATH, 'mycodo.db')
SQL_DATABASE_USER = os.path.join(DATABASE_PATH, 'users.db')
SQL_DATABASE_NOTE = os.path.join(DATABASE_PATH, 'notes.db')

MYCODO_DB_PATH = 'sqlite:///' + SQL_DATABASE_MYCODO
USER_DB_PATH = 'sqlite:///' + SQL_DATABASE_USER

SQLALCHEMY_DATABASE_URI = 'sqlite:///' + SQL_DATABASE_MYCODO_5

SQLALCHEMY_TRACK_MODIFICATIONS = False


class TestConfig(object):
""" Testing Configuration """
Expand Down
42 changes: 16 additions & 26 deletions mycodo/controller_lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
import datetime

# Classes
from databases.mycodo_db.models import (
from databases.mycodo_db.models_5 import (
LCD,
PID,
Relay,
Expand All @@ -68,17 +68,18 @@
from devices.tca9548a import TCA9548A

# Functions
from utils.database import db_retrieve_table
from utils.database import db_retrieve_table_daemon
from utils.influx import read_last_influxdb

# Config
from config import (
SQL_DATABASE_MYCODO_5,
SQL_DATABASE_MYCODO,
MEASUREMENT_UNITS,
MYCODO_VERSION
)

MYCODO_DB_PATH = 'sqlite:///' + SQL_DATABASE_MYCODO
MYCODO_DB_PATH = 'sqlite:///' + SQL_DATABASE_MYCODO_5


class LCDController(threading.Thread):
Expand All @@ -100,8 +101,7 @@ def __init__(self, ready, lcd_id):
self.lcd_id = lcd_id

try:
lcd = db_retrieve_table(MYCODO_DB_PATH,
LCD,
lcd = db_retrieve_table_daemon(LCD,
device_id=self.lcd_id)

self.lcd_name = lcd.name
Expand Down Expand Up @@ -143,10 +143,8 @@ def __init__(self, ready, lcd_id):
table = PID
elif lcd.line_1_measurement in list_relays:
table = Relay
sensor_line_1 = db_retrieve_table(
MYCODO_DB_PATH,
table,
device_id=lcd.line_1_sensor_id)
sensor_line_1 = db_retrieve_table_daemon(
table, device_id=lcd.line_1_sensor_id)
self.lcd_line[1]['name'] = sensor_line_1.name
if 'time' in lcd.line_1_measurement:
self.lcd_line[1]['measurement'] = 'time'
Expand All @@ -161,10 +159,8 @@ def __init__(self, ready, lcd_id):
table = PID
elif lcd.line_2_measurement in list_relays:
table = Relay
sensor_line_2 = db_retrieve_table(
MYCODO_DB_PATH,
table,
device_id=lcd.line_2_sensor_id)
sensor_line_2 = db_retrieve_table_daemon(
table, device_id=lcd.line_2_sensor_id)
self.lcd_line[2]['name'] = sensor_line_2.name
if 'time' in lcd.line_2_measurement:
self.lcd_line[2]['measurement'] = 'time'
Expand All @@ -180,10 +176,8 @@ def __init__(self, ready, lcd_id):
table = PID
elif lcd.line_3_measurement in list_relays:
table = Relay
sensor_line_3 = db_retrieve_table(
MYCODO_DB_PATH,
table,
device_id=lcd.line_3_sensor_id)
sensor_line_3 = db_retrieve_table_daemon(
table, device_id=lcd.line_3_sensor_id)
self.lcd_line[3]['name'] = sensor_line_3.name
if 'time' in lcd.line_3_measurement:
self.lcd_line[3]['measurement'] = 'time'
Expand All @@ -198,10 +192,8 @@ def __init__(self, ready, lcd_id):
table = PID
elif lcd.line_4_measurement in list_relays:
table = Relay
sensor_line_4 = db_retrieve_table(
MYCODO_DB_PATH,
table,
device_id=lcd.line_4_sensor_id)
sensor_line_4 = db_retrieve_table_daemon(
table, device_id=lcd.line_4_sensor_id)
self.lcd_line[4]['name'] = sensor_line_4.name
if 'time' in lcd.line_4_measurement:
self.lcd_line[4]['measurement'] = 'time'
Expand Down Expand Up @@ -350,10 +342,8 @@ def get_lcd_strings(self):
# Determine if the LCD output will have a value unit
measurement = ''
if self.lcd_line[i]['measurement'] == 'setpoint':
pid = db_retrieve_table(
MYCODO_DB_PATH,
PID,
device_id=self.lcd_line[i]['id'])
pid = db_retrieve_table_daemon(
PID, device_id=self.lcd_line[i]['id'])
measurement = pid.measure_type
elif self.lcd_line[i]['measurement'] in [
'temperature',
Expand Down Expand Up @@ -406,7 +396,7 @@ def get_lcd_strings(self):

@staticmethod
def relay_state(relay_id):
relay = db_retrieve_table(MYCODO_DB_PATH, Relay, device_id=relay_id)
relay = db_retrieve_table_daemon(Relay, device_id=relay_id)
GPIO.setmode(GPIO.BCM)
if GPIO.input(relay.pin) == relay.trigger:
gpio_state = 'On'
Expand Down
28 changes: 12 additions & 16 deletions mycodo/controller_pid.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import timeit

# Classes
from databases.mycodo_db.models import (
from databases.mycodo_db.models_5 import (
Method,
PID,
Relay,
Expand All @@ -48,7 +48,7 @@

# Functions
from databases.utils import session_scope
from utils.database import db_retrieve_table
from utils.database import db_retrieve_table_daemon
from utils.influx import (
read_last_influxdb,
write_influxdb_setpoint
Expand All @@ -59,9 +59,9 @@
)

# Config
from config import SQL_DATABASE_MYCODO
from config import SQL_DATABASE_MYCODO_5

MYCODO_DB_PATH = 'sqlite:///' + SQL_DATABASE_MYCODO
MYCODO_DB_PATH = 'sqlite:///' + SQL_DATABASE_MYCODO_5


class PIDController(threading.Thread):
Expand Down Expand Up @@ -99,7 +99,7 @@ def __init__(self, ready, pid_id):

# Check if a method is set for this PID
if self.method_id:
method = db_retrieve_table(MYCODO_DB_PATH, Method)
method = Method.query
method = method.filter(Method.method_id == self.method_id)
method = method.filter(Method.method_order == 0).first()
self.method_type = method.method_type
Expand Down Expand Up @@ -175,8 +175,8 @@ def run(self):

def initialize_values(self):
"""Set PID parameters"""
pid = db_retrieve_table(MYCODO_DB_PATH, PID, device_id=self.pid_id)
sensor = db_retrieve_table(MYCODO_DB_PATH, Sensor, device_id=pid.sensor_id)
pid = db_retrieve_table_daemon(PID, device_id=self.pid_id)
sensor = db_retrieve_table_daemon(Sensor, device_id=pid.sensor_id)
self.activated = pid.activated # 0=inactive, 1=active, 2=paused
self.sensor_id = pid.sensor_id
self.measure_type = pid.measure_type
Expand Down Expand Up @@ -308,10 +308,8 @@ def manipulate_relays(self):

# Turn off lower_relay if active, because we're now raising
if self.lower_relay_id:
relay = db_retrieve_table(
MYCODO_DB_PATH,
Relay,
device_id=self.lower_relay_id)
relay = db_retrieve_table_daemon(
Relay, device_id=self.lower_relay_id)
if relay.is_on():
self.control.relay_off(self.lower_relay_id)

Expand Down Expand Up @@ -343,10 +341,8 @@ def manipulate_relays(self):

# Turn off raise_relay if active, because we're now lowering
if self.raise_relay_id:
relay = db_retrieve_table(
MYCODO_DB_PATH,
Relay,
device_id=self.raise_relay_id)
relay = db_retrieve_table_daemon(
Relay, device_id=self.raise_relay_id)
if relay.is_on():
self.control.relay_off(self.raise_relay_id)

Expand All @@ -369,7 +365,7 @@ def manipulate_relays(self):
self.control.relay_off(self.lower_relay_id)

def calculate_method_setpoint(self, method_id):
method = db_retrieve_table(MYCODO_DB_PATH, Method)
method = Method.query

method_key = method.filter(Method.method_id == method_id)
method_key = method_key.filter(Method.method_order == 0).first()
Expand Down
17 changes: 8 additions & 9 deletions mycodo/controller_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import timeit

# Classes
from databases.mycodo_db.models import (
from databases.mycodo_db.models_5 import (
Relay,
RelayConditional,
SMTP
)
from mycodo_client import DaemonControl

# Functions
from utils.database import db_retrieve_table
from utils.database import db_retrieve_table_daemon
from utils.influx import write_influxdb_value
from utils.send_data import send_email
from utils.system_pi import cmd_output
Expand Down Expand Up @@ -63,14 +63,15 @@ def __init__(self):

self.logger.debug("Initializing Relays")
try:
smtp = db_retrieve_table(MYCODO_DB_PATH, SMTP, entry='first')

smtp = db_retrieve_table_daemon(SMTP, entry='first')
self.smtp_max_count = smtp.hourly_max
self.smtp_wait_time = time.time() + 3600
self.smtp_timer = time.time()
self.email_count = 0
self.allowed_to_send_notice = True

relays = db_retrieve_table(MYCODO_DB_PATH, Relay, entry='all')
relays = db_retrieve_table_daemon(Relay, entry='all')
self.all_relays_initialize(relays)
# Turn all relays off
self.all_relays_off()
Expand Down Expand Up @@ -264,7 +265,7 @@ def relay_on_off(self, relay_id, state, duration=0.0,
self.check_conditionals(relay_id, duration)

def check_conditionals(self, relay_id, on_duration):
conditionals = db_retrieve_table(MYCODO_DB_PATH, RelayConditional)
conditionals = db_retrieve_table_daemon(RelayConditional)

conditionals = conditionals.filter(RelayConditional.if_relay_id == relay_id)
conditionals = conditionals.filter(RelayConditional.activated == True)
Expand Down Expand Up @@ -328,8 +329,7 @@ def check_conditionals(self, relay_id, on_duration):
message += "Notify {}.".format(
each_conditional.email_notify)

smtp = db_retrieve_table(
MYCODO_DB_PATH,SMTP, entry='first')
smtp = db_retrieve_table_daemon(SMTP, entry='first')
send_email(
smtp.host, smtp.ssl, smtp.port, smtp.user,
smtp.passw, smtp.email_from,
Expand Down Expand Up @@ -401,8 +401,7 @@ def add_mod_relay(self, relay_id, do_setup_pin=False):
:type do_setup_pin: bool
"""
try:
relay = db_retrieve_table(
MYCODO_DB_PATH, Relay, device_id=relay_id)
relay = db_retrieve_table(Relay, device_id=relay_id)
self.relay_id[relay_id] = relay.id
self.relay_name[relay_id] = relay.name
self.relay_pin[relay_id] = relay.pin
Expand Down

0 comments on commit a3d3abf

Please sign in to comment.