forked from kizniche/Mycodo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add set_custom_option/get_custom_option to Conditionals (kizniche#901)…
…, fix Conditional sample_rate not being set from Config
- Loading branch information
Showing
11 changed files
with
173 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
33 changes: 33 additions & 0 deletions
33
databases/alembic/versions/706a32b64ba2_add_custom_options_to_function_tables.py
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Add custom_options to function tables | ||
Revision ID: 706a32b64ba2 | ||
Revises: 313a6fb99082 | ||
Create Date: 2020-12-22 15:11:42.441917 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = '706a32b64ba2' | ||
down_revision = '313a6fb99082' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
with op.batch_alter_table("conditional") as batch_op: | ||
batch_op.add_column(sa.Column('custom_options', sa.Text)) | ||
|
||
op.execute( | ||
''' | ||
UPDATE conditional | ||
SET custom_options='' | ||
''' | ||
) | ||
|
||
|
||
def downgrade(): | ||
with op.batch_alter_table("conditional") as batch_op: | ||
batch_op.drop_column('custom_options') |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# coding=utf-8 | ||
""" | ||
This module contains the AbstractConditional Class which acts as a template | ||
for all Conditionals. It is not to be used directly. The AbstractConditional Class | ||
ensures that certain methods and instance variables are included in each | ||
Conditional. | ||
All Conditionals should inherit from this class and overwrite methods that raise | ||
NotImplementedErrors | ||
""" | ||
import json | ||
|
||
from mycodo.mycodo_client import DaemonControl | ||
|
||
control = DaemonControl() | ||
from mycodo.config import SQL_DATABASE_MYCODO | ||
from mycodo.databases.models import Conditional | ||
from mycodo.databases.utils import session_scope | ||
from mycodo.utils.database import db_retrieve_table_daemon | ||
|
||
MYCODO_DB_PATH = 'sqlite:///' + SQL_DATABASE_MYCODO | ||
|
||
|
||
class AbstractConditional: | ||
""" | ||
Base Conditional class that ensures certain methods and values are present | ||
in Conditionals. | ||
""" | ||
def __init__(self, logger, function_id, message): | ||
self.logger = logger | ||
self.function_id = function_id | ||
self.variables = {} | ||
self.message = message | ||
self.running = True | ||
|
||
def run_all_actions(self, message=None): | ||
if message is None: | ||
message = self.message | ||
self.message = control.trigger_all_actions(self.function_id, message=message) | ||
|
||
def run_action(self, action_id, message=None): | ||
if message is None: | ||
message = self.message | ||
self.message = control.trigger_action(action_id, message=message, single_action=True) | ||
|
||
@staticmethod | ||
def condition(condition_id): | ||
return control.get_condition_measurement(condition_id) | ||
|
||
@staticmethod | ||
def condition_dict(condition_id): | ||
string_sets = control.get_condition_measurement_dict(condition_id) | ||
if string_sets: | ||
list_ts_values = [] | ||
for each_set in string_sets.split(';'): | ||
ts_value = each_set.split(',') | ||
list_ts_values.append({'time': ts_value[0], 'value': float(ts_value[1])}) | ||
return list_ts_values | ||
return None | ||
|
||
def stop_conditional(self): | ||
self.running = False | ||
|
||
def set_custom_option(self, option, value): | ||
try: | ||
with session_scope(MYCODO_DB_PATH) as new_session: | ||
mod_cond = new_session.query(Conditional).filter( | ||
Conditional.unique_id == self.function_id).first() | ||
try: | ||
dict_custom_options = json.loads(mod_cond.custom_options) | ||
except: | ||
dict_custom_options = {} | ||
dict_custom_options[option] = value | ||
mod_cond.custom_options = json.dumps(dict_custom_options) | ||
new_session.commit() | ||
except Exception: | ||
self.logger.exception("set_custom_option") | ||
|
||
def get_custom_option(self, option): | ||
conditional = db_retrieve_table_daemon(Conditional, unique_id=self.function_id) | ||
try: | ||
dict_custom_options = json.loads(conditional.custom_options) | ||
except: | ||
dict_custom_options = {} | ||
if option in dict_custom_options: | ||
return dict_custom_options[option] |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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