Skip to content

Commit

Permalink
core - implemented reverse condition handling (Ticket#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Apr 29, 2021
1 parent 260c207 commit ba9a1dd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 29 deletions.
48 changes: 44 additions & 4 deletions code/core/device/output/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@

from core.config.object.device.output import GaOutputDevice, GaOutputModel

from time import sleep


class Go:
SQL_TASK_COMMAND = DEVICE_DICT['task']
REVERSE_KEY_TIME = 'time'
REVERSE_KEY_CONDITION = 'condition'
REVERSE_CONDITION_INTERVAL = 60

def __init__(self, instance):
self.instance = instance
Expand Down Expand Up @@ -57,7 +60,7 @@ def _evaluate(self, condition_result) -> None:
else:
self.logger.write(f"Conditions for \"{self.instance.name}\" were not met", level=3)

def _process(self, task_dict: dict, reverse=False) -> None:
def _process(self, task_dict: dict, reverse=False) -> bool:
device = task_dict['device']
self.logger.write(f"Processing device instance: \"{device.__dict__}\"", level=7)

Expand All @@ -69,18 +72,32 @@ def _process(self, task_dict: dict, reverse=False) -> None:

if result is None:
self.logger.write(f"Processing of output-device \"{device.name}\" failed", level=3)
return False

elif result is False:
self.logger.write(f"Device \"{device.name}\" is in fail-sleep", level=4)
return False

else:
self.logger.write(f"Processing of output \"{device.name}\" succeeded", level=7)

self.logger.write(f"Checking device \"{device.name}\" for reversion: reversible - {device.reverse}, active - {device.active}, "
f"reverse-type - {device.reverse_type}={self.REVERSE_KEY_TIME}", level=7)

if device.reverse == 1 and device.active and device.reverse_type == self.REVERSE_KEY_TIME:
self._reverse_timer(task_dict=task_dict)
if device.reverse == 1 and device.active:
# todo: write state to database => a service restart MUST NOT keep devices running

if device.reverse_type == self.REVERSE_KEY_TIME:
self._reverse_timer(task_dict=task_dict)

elif device.reverse_type == self.REVERSE_KEY_CONDITION:
self._reverse_condition(task_dict=task_dict)

elif reverse and not device.active:
# todo: write state (inactive) to database => a service restart MUST NOT keep devices running
pass

return True

def _condition_members(self) -> dict:
output_dict = {}
Expand All @@ -105,10 +122,33 @@ def _reverse_timer(self, task_dict: dict) -> None:
sleep_time=int(device.reverse_type_data),
thread_data=task_dict,
once=True,
description=device.name,
description=f"Timed reversing for '{device.name}'",
)
def thread_task(data):
self._process(task_dict=data, reverse=True)
self.logger.write(f"Reversing of device \"{device.name}\" finished", level=6)
thread.stop_thread(description=device.name)

thread.start()

def _reverse_condition(self, task_dict: dict) -> None:
device = task_dict['device']
self.logger.write(f"Entering reverse-condition loop for output-device \"{device.name}\"", level=6)

thread = Thread()

@thread.thread(
sleep_time=int(1),
thread_data=task_dict,
once=True,
description=f"Conditional reversing for '{device.name}'",
)
def thread_task(data):
while not self._process(task_dict=data, reverse=True):
self.logger.write(f"Reversing of device \"{device.name}\" continues", level=8)
sleep(self.REVERSE_CONDITION_INTERVAL)

self.logger.write(f"Reversing of device \"{device.name}\" finished", level=6)
thread.stop_thread(description=device.name)

thread.start()
63 changes: 38 additions & 25 deletions code/core/device/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,7 @@ def _fail_check(self):
return True

def _execute(self):
if isinstance(self.instance, (GaInputDevice, GaOutputDevice)):
config_dict = {
'connection': self.instance.connection,
}

elif isinstance(self.instance, GaConnectionDevice):
try:
try:
connection = json_loads(self.instance.connection)

except (TypeError, JSONDecodeError):
connection_json = self.instance.connection.replace("'", "\"")
connection = json_loads(connection_json)

except (TypeError, JSONDecodeError):
connection = self.instance.connection

config_dict = {
'connection': connection,
'downlink_pin': self.nested_instance.connection
}

else:
config_dict = {}

config_dict = self._get_config()
params_dict = self._get_script_params()

if params_dict is None or not self._reverse_check:
Expand All @@ -118,6 +94,43 @@ def _execute(self):

return result

def _get_config(self) -> dict:
if self.instance.connection.find('ga_json') != -1:
raw = self.instance.connection.split('[', 1)[1].rsplit(']', 1)[0]
if raw.find(',') != -1:
raw_list = raw.split(',')
raw_dict = {}

for raw_item in raw_list:
key, value = raw_item.split('=')
raw_dict[key] = value

connection = raw_dict

else:
connection = raw

else:
try:
try:
connection = json_loads(self.instance.connection)

except (TypeError, JSONDecodeError):
connection_json = self.instance.connection.replace("'", "\"")
connection = json_loads(connection_json)

except (TypeError, JSONDecodeError):
connection = self.instance.connection

config_dict = {
'connection': connection,
}

if isinstance(self.instance, GaConnectionDevice):
config_dict['downlink_pin'] = self.nested_instance.connection

return config_dict

def _get_script_params(self) -> (None, dict):
bad_values = [None, '', 'None']
params_dict = {'script': self.instance.script, 'bin': self.instance.script_bin, 'arg': self.instance.script_arg}
Expand Down

0 comments on commit ba9a1dd

Please sign in to comment.