diff --git a/src/core/pyros_django/agent/Agent.py b/src/core/pyros_django/agent/Agent.py index 1c9bbe6..2e724d9 100755 --- a/src/core/pyros_django/agent/Agent.py +++ b/src/core/pyros_django/agent/Agent.py @@ -1104,6 +1104,8 @@ class Agent: #return (self._current_specific_thread is None) or not self._current_specific_thread.is_alive() return self._current_specific_thread and self._current_specific_thread.is_alive() + def is_in_simulator_mode(self): + return self.SIMULATOR_MODE def kill_running_specific_cmd_if_exists(self, abort_sender): if not self.is_running_specific_cmd(): diff --git a/src/core/pyros_django/agent/AgentDevice.py b/src/core/pyros_django/agent/AgentDevice.py index afe96a5..cafb213 100755 --- a/src/core/pyros_django/agent/AgentDevice.py +++ b/src/core/pyros_django/agent/AgentDevice.py @@ -3,6 +3,7 @@ import sys ##import utils.Logger as L +import threading import time ##from .Agent import Agent @@ -17,6 +18,11 @@ sys.path.append("../../..") class AgentDevice(Agent): + # Default host and port of the device controller + _device_ctrl = None + HOST, PORT = None, None + _thread_device_simulator = None + _agent_device_status = None # FOR TEST ONLY @@ -76,6 +82,8 @@ class AgentDevice(Agent): def __init__(self, name:str, config_filename, RUN_IN_THREAD, device_controller, host, port): if name is None: name = self.__class__.__name__ super().__init__(name, config_filename, RUN_IN_THREAD) + self.HOST, self.PORT = host, port + self._device_ctrl = device_controller # Initialize the device table status # If table is empty, create a default 1st row @@ -96,7 +104,7 @@ class AgentDevice(Agent): #HOST, PORT = "localhost", 11110 #self._device_ctrl = TelescopeControllerGEMINI(host, port, True) ##self._device_ctrl = device_controller(HOST, PORT, True) - self._device_ctrl = device_controller(host, port, True) + ##self._device_ctrl = device_controller(host, port, True) self._log.print(f"init done for {name}") @@ -108,6 +116,16 @@ class AgentDevice(Agent): ##agent_alias = self.__class__.__name__ ##self.set_mode_from_config(agent_alias) + # Start device simulator (device server) if in simulator mode + if self.is_in_simulator_mode() and not self.SIMULATOR_WITH_REAL_DEVICE: + # START device SIMULATOR (in a thread) so that we can connect to it in place of the real device + self.HOST = "localhost" + self._thread_device_simulator = threading.Thread(target=self.device_simulator) + self._thread_device_simulator.start() + + # Create instance of device controller (device client) + self._device_ctrl = self._device_ctrl(self.HOST, self.PORT, True) + # Device socket init # (optional) Only useful for TCP (does nothing for UDP) self._device_ctrl._connect_to_device() @@ -117,6 +135,17 @@ class AgentDevice(Agent): # TODO: + def device_simulator(self): + #HOST, PORT = "localhost", 11110 + #with get_SocketServer_UDP_TCP(HOST, PORT, "UDP") as myserver: + print("Starting device simulator on (host, port): ", self.HOST, self.PORT) + ''' + with get_SocketServer_UDP_TCP(self.HOST, self.PORT, "UDP") as myserver: + myserver.serve_forever() + myserver = get_SocketServer_UDP_TCP(self.HOST, self.PORT, "UDP") + myserver.serve_forever() + ''' + ''' # @override def load_config(self): diff --git a/src/core/pyros_django/agent/AgentDeviceTelescopeGemini.py b/src/core/pyros_django/agent/AgentDeviceTelescopeGemini.py index 3e4cda8..5d0264b 100755 --- a/src/core/pyros_django/agent/AgentDeviceTelescopeGemini.py +++ b/src/core/pyros_django/agent/AgentDeviceTelescopeGemini.py @@ -3,7 +3,7 @@ import sys ##import utils.Logger as L -import threading +##import threading import time ##from .Agent import Agent @@ -49,11 +49,13 @@ class AgentDeviceTelescopeGemini(AgentDevice): # @override def __init__(self, config_filename=None, RUN_IN_THREAD=True): - if not self.SIMULATOR_WITH_REAL_DEVICE: + ''' + if self.is_in_simulator_mode() and not self.SIMULATOR_WITH_REAL_DEVICE: # START device SIMULATOR (in a thread) so that we can connect to it in place of the real device self.HOST = "localhost" thread_device_simulator = threading.Thread(target=self.device_simulator) thread_device_simulator.start() + ''' super().__init__(self.__class__.__name__, config_filename, RUN_IN_THREAD, device_controller=TelescopeControllerGEMINI, host=self.HOST, port=self.PORT) # Initialize the device table status @@ -77,13 +79,12 @@ class AgentDeviceTelescopeGemini(AgentDevice): def init(self): - super().init() - # Telescope (long) init # TODO: def device_simulator(self): + super().device_simulator() #HOST, PORT = "localhost", 11110 #with get_SocketServer_UDP_TCP(HOST, PORT, "UDP") as myserver: print(self.HOST, self.PORT) -- libgit2 0.21.2