Skip to content

Commit

Permalink
core - implemented basic socket server for web-to-core communications
Browse files Browse the repository at this point in the history
  • Loading branch information
superstes committed Sep 19, 2021
1 parent 915dea6 commit 373797b
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 11 deletions.
20 changes: 19 additions & 1 deletion code/core/config/object/core/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class GaTaskDevice(GaBase):
setting_list = ['timer', 'enabled', 'interval', 'target', 'interval']
setting_list = ['timer', 'enabled', 'interval', 'target']

def __init__(self, setting_dict: dict, **kwargs):
# inheritance from superclasses
Expand All @@ -29,3 +29,21 @@ def __init__(self, setting_dict: dict, **kwargs):
# # specific vars
# self.member_list = member_list
# self.setting_dict = setting_dict


class SystemTask(GaBase):
setting_list = ['timer']

def __init__(self, setting_dict: dict, execute, **kwargs):
# inheritance from superclasses
super().__init__(**kwargs)
# specific vars
self.setting_dict = setting_dict
self.execute = execute
# device instance vars
set_attribute(
setting_dict=self.setting_dict,
setting_list=self.setting_list,
instance=self,
obj=SystemTask
)
1 change: 1 addition & 0 deletions code/core/config/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ def init():

# todo: set as controller setting
PYTHON_VENV = '/home/ga_core/venv/bin'
SOCKET_SHUFFLE = None # if unset SYSTEM.security will be evaluated
38 changes: 38 additions & 0 deletions code/core/config/socket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# the addressing of functionality should be done like this:
# we'll use a domain-like addressing to parse the requests
#
# per example:
# path: ga.core.device.output.3
# action: start
#
# the socket connection will enable these internal api-calls to be called from any external program (p.e. directly from a shell) [for external source shuffle might need to be turned off]
# the main reason to implement this is the django (web) to core communication
# we need to be able to start device-actions via the core from the webUI
#

from core.config import shared as shared_var

PATH_CORE = 'ga.core'
SUB_PATH_DEVICE = 'device'
PATH_DEVICE_TYPES = ['input', 'output', 'connection']
MATCH_DEVICE = f'{PATH_CORE}{SUB_PATH_DEVICE}.([0-9]{{1,10}}).(.*?)$'

PATH_WEB = 'ga.web'

SUB_PATHS = [SUB_PATH_DEVICE]

SOCKET_PORT = 2048
SOCKET_PUBLIC = False # if not => the socket server will only be reachable locally
SOCKET_SERVER_NAME = 'InterconnectionServer'
SOCKET_BANNER_CORE = f'### GrowAutomation {SOCKET_SERVER_NAME} ###'
PACKAGE_START = '+GA+'
PACKAGE_STOP = '-GA-'
PACKAGE_PATH_SEPARATOR = '_GA_'
PACKAGE_SIZE = 1024

if shared_var.SOCKET_SHUFFLE or shared_var.SYSTEM.security:
SHUFFLE = True
else:
SHUFFLE = False

SHUFFLE_DATA = '80e7540fe29c6b88314a8083acf7110c'.encode('utf-8')
5 changes: 4 additions & 1 deletion code/core/service/decision.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from core.config.object.device.input import GaInputDevice
from core.config.object.device.input import GaInputModel
from core.config.object.core.task import GaTaskDevice
from core.config.object.core.task import GaTaskDevice, SystemTask
from core.config.object.setting.condition import GaConditionGroup

from core.utils.debug import log
Expand All @@ -22,6 +22,9 @@ def start(self):
elif isinstance(self.instance, GaTaskDevice):
self._core_timer()

elif isinstance(self.instance, SystemTask):
self.instance.execute()

else:
log(f"Service could not find a matching decision for instance: '{self.instance}'", level=5)

Expand Down
4 changes: 2 additions & 2 deletions code/core/service/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(self):
signal.signal(signal.SIGINT, self.stop)
self.CONFIG, self.current_config_dict = factory()
self._init_shared_vars()
self.timer_list, self.custom_timer_list = get_timer(config_dict=self.CONFIG)
self.timer_list = get_timer(config_dict=self.CONFIG)
self.CONFIG_FILE = GaDataFile()
self._update_config_file()
self.THREAD = Thread()
Expand Down Expand Up @@ -99,7 +99,7 @@ def reload(self, signum=None, stack=None):
self._update_config_file()
self._init_shared_vars()
# re-create the list of possible timers
self.timer_list, self.custom_timer_list = get_timer(config_dict=self.CONFIG)
self.timer_list = get_timer(config_dict=self.CONFIG)
# stop and reset all current threads
self.THREAD.stop()
self.THREAD.jobs = []
Expand Down
16 changes: 16 additions & 0 deletions code/core/service/system_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from core.config.object.core.task import SystemTask

from core.sock.connect import Server as SocketServer


def get_tasks() -> list:
return [
SystemTask(
name='Socket Server',
description='Server to receive commands from other clients or parts of the software',
execute=SocketServer().run,
setting_dict={'timer': 10},
object_id=1,
),
]

12 changes: 5 additions & 7 deletions code/core/service/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from core.config.object.setting.condition import GaConditionGroup
from core.config.object.core.task import GaTaskDevice
from core.utils.debug import log
from core.service.system_tasks import get_tasks


ALLOWED_OBJECT_TUPLE = (
Expand All @@ -18,9 +19,8 @@
)


def get(config_dict: dict) -> tuple:
timer_list = []
custom_list = []
def get(config_dict: dict) -> list:
timer_list = get_tasks()

for category, obj_list in config_dict.items():
for obj in obj_list:
Expand All @@ -29,7 +29,7 @@ def get(config_dict: dict) -> tuple:

if isinstance(obj, GaInputDevice):
if obj.timer is not None and obj.timer != obj.parent_instance.timer:
custom_list.append(obj)
timer_list.append(obj)

elif isinstance(obj, (GaInputModel, GaConditionGroup)):
timer_list.append(obj)
Expand All @@ -40,6 +40,4 @@ def get(config_dict: dict) -> tuple:
else:
log(f"Is not allowed: \"{obj}\"", level=7)

timer_list.extend(custom_list)

return timer_list, custom_list
return timer_list

0 comments on commit 373797b

Please sign in to comment.