Skip to content

Commit

Permalink
Observatory Dependency Injection (#195)
Browse files Browse the repository at this point in the history
* Create `Observatory` outside `POCS` class and pass in as param
* Adjust pocs_shell to create observatory
  • Loading branch information
wtgee authored and jamessynge committed Dec 16, 2017
1 parent 9ef5987 commit 41dc689
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
4 changes: 3 additions & 1 deletion bin/pocs_shell
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ from astropy.io import fits
from astropy.utils import console

from pocs import POCS
from pocs.observatory import Observatory
from pocs.scheduler.field import Field
from pocs.scheduler.observation import Observation
from pocs.utils import current_time
Expand Down Expand Up @@ -125,7 +126,8 @@ class PocsShell(Cmd):
simulator = []

try:
self.pocs = POCS(simulator=simulator, messaging=True)
observatory = Observatory(simulator=simulator)
self.pocs = POCS(observatory, messaging=True)
self.pocs.initialize()
except error.PanError:
pass
Expand Down
11 changes: 8 additions & 3 deletions pocs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from astropy import units as u

from . import PanBase
from .observatory import Observatory
from .state.machine import PanStateMachine
from .utils import current_time
from .utils import get_free_space
Expand All @@ -25,6 +24,7 @@ class POCS(PanStateMachine, PanBase):
the `get_ready()` method the transition that is responsible for moving to the initial state.
Args:
observatory(Observatory): An instance of a `pocs.observatory.Observatory` class
state_machine_file(str): Filename of the state machine to use, defaults to
'simple_state_table'
messaging(bool): If messaging should be included, defaults to False
Expand All @@ -38,7 +38,12 @@ class POCS(PanStateMachine, PanBase):
"""

def __init__(self, state_machine_file='simple_state_table', messaging=False, **kwargs):
def __init__(
self,
observatory,
state_machine_file='simple_state_table',
messaging=False,
**kwargs):

# Explicitly call the base classes in the order we want
PanBase.__init__(self, **kwargs)
Expand All @@ -61,7 +66,7 @@ def __init__(self, state_machine_file='simple_state_table', messaging=False, **k
PanStateMachine.__init__(self, state_machine_file, **kwargs)

# Create our observatory, which does the bulk of the work
self.observatory = Observatory(**kwargs)
self.observatory = observatory

self._connected = True
self._initialized = False
Expand Down
28 changes: 20 additions & 8 deletions pocs/tests/test_pocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
from pocs import POCS
from pocs import _check_config
from pocs import _check_environment
from pocs.observatory import Observatory
from pocs.utils import error
from pocs.utils.messaging import PanMessaging


@pytest.fixture
def pocs(config):
def observatory():
observatory = Observatory(simulator=['all'])

yield observatory


@pytest.fixture
def pocs(config, observatory):
os.environ['POCSTIME'] = '2016-08-13 13:00:00'
pocs = POCS(simulator=['all'], run_once=True,

pocs = POCS(observatory,
run_once=True,
config=config,
ignore_local_config=True, db='panoptes_testing')

Expand Down Expand Up @@ -177,11 +187,13 @@ def test_is_weather_safe_no_simulator(pocs, db):
assert pocs.is_weather_safe() is False


def test_run_wait_until_safe(db):
def test_run_wait_until_safe(db, observatory):
os.environ['POCSTIME'] = '2016-08-13 23:00:00'

def start_pocs():
pocs = POCS(simulator=['camera', 'mount', 'night'],
observatory.config['simulator'] = ['camera', 'mount', 'night']

pocs = POCS(observatory,
messaging=True, safe_delay=15)
pocs.db.current.remove({})
pocs.initialize()
Expand Down Expand Up @@ -286,9 +298,9 @@ def test_run(pocs):
assert pocs.state == 'sleeping'


def test_run_interrupt_with_reschedule_of_target():
def test_run_interrupt_with_reschedule_of_target(observatory):
def start_pocs():
pocs = POCS(simulator=['all'], messaging=True)
pocs = POCS(observatory, messaging=True)
pocs.logger.info('Before initialize')
pocs.initialize()
pocs.logger.info('POCS initialized, back in test')
Expand Down Expand Up @@ -321,9 +333,9 @@ def start_pocs():
assert pocs_process.is_alive() is False


def test_run_power_down_interrupt():
def test_run_power_down_interrupt(observatory):
def start_pocs():
pocs = POCS(simulator=['all'], messaging=True)
pocs = POCS(observatory, messaging=True)
pocs.initialize()
pocs.observatory.scheduler.fields_list = [{'name': 'KIC 8462852',
'position': '20h06m15.4536s +44d27m24.75s',
Expand Down

0 comments on commit 41dc689

Please sign in to comment.