Exemplo n.º 1
0
 def finalizeInitialization(self):
     ## Replace lights strings with their handlers.
     handlers = []
     for name in self.lights:
         h = depot.getHandler(name, depot.LIGHT_TOGGLE)
         if h:
             handlers.append(h)
     self.lights = handlers
     # Register this shutter with the LightHandler class.
     LightHandler.addShutter(self, self.lights)
Exemplo n.º 2
0
 def __init__(self, name: str, config: typing.Mapping[str, str]) -> None:
     super().__init__(name, config)
     self._state = {'time': 100.0}
     callbacks = {
         'getExposureTime': self._getExposureTime,
         'setExposureTime': self._setExposureTime,
         'setEnabled': self._setEnabled,
     } # type: typing.Dict[str, typing.Callable]
     self._handlers = [LightHandler('Ambient', 'ambient', callbacks,
                                    wavelength=0, exposureTime=100.0)]
Exemplo n.º 3
0
    def getHandlers(self):
        self.handlers = []
        trigsource = self.config.get('triggersource', None)
        trigline = self.config.get('triggerline', None)
        if trigsource:
            trighandler = cockpit.depot.getHandler(trigsource,
                                                   cockpit.depot.EXECUTOR)
        else:
            trighandler = None
        self._exposureTime = 100
        self.handlers.append(LightHandler(
            self.name,
            self.name + ' light source',
            {'setEnabled': lambda name, on: time.sleep(0.5),
             'setExposureTime': lambda name, value: setattr(self, '_exposureTime', value),
             'getExposureTime': lambda name: self._exposureTime},
            self.config.get('wavelength', None),
            100,
            trighandler,
            trigline))

        return self.handlers
Exemplo n.º 4
0
    def initialize(self, config):
        ## TODO: we will want to remove this print statements when
        ## we're done refactoring the location of the log and config
        ## files (issue #320)
        print("Cockpit is running from %s" %
              os.path.split(os.path.abspath(__file__))[0])

        # Create our server
        from cockpit.devices.server import CockpitServer

        ## TODO remove special case by having fallback empty section?
        ## Or fallback to the defaults in the class?
        if config.has_section('server'):
            sconf = dict(config.items('server'))
        else:
            sconf = {}
        self.nameToDevice['server'] = CockpitServer('server', sconf)

        # Parse config to create device instances.
        for name in config.sections():
            if name in SKIP_CONFIG:
                continue
            try:
                cls = config.gettype(name, 'type')
            except configparser.NoOptionError:
                raise RuntimeError("Missing 'type' key for device '%s'" % name)

            device_config = dict(config.items(name))
            try:
                device = cls(name, device_config)
            except Exception as e:
                raise RuntimeError("Failed to construct device '%s'" % name, e)
            self.nameToDevice[name] = device

        # Initialize devices in order of dependence
        # Convert to list - python3 dict_values has no pop method.
        devices = list(self.nameToDevice.values())
        done = []
        while devices:
            # TODO - catch circular dependencies.
            d = devices.pop(0)
            depends = []
            for dependency in ['triggersource', 'analogsource']:
                other = d.config.get(dependency)
                if other:
                    if other not in self.nameToDevice:
                        raise Exception(
                            "Device %s depends on non-existent device %s." %
                            (d, other))
                    depends.append(other)

            if any([other not in done for other in depends]):
                devices.append(d)
                continue
            yield d.name
            self.initDevice(d)
            done.append(d.name)

        # Add dummy devices as required.
        dummies = []
        # Dummy objectives
        if not getHandlersOfType(OBJECTIVE):
            from cockpit.devices.objective import ObjectiveDevice

            if config.has_section('objectives'):
                objs = dict(config.items('objectives'))
            else:
                objs = {}
            dummies.append(ObjectiveDevice('objectives', objs))
        # Dummy stages
        axes = self.getSortedStageMovers().keys()
        if 2 not in axes:
            from cockpit.devices.dummyZStage import DummyZStage
            dummies.append(DummyZStage())
        if (0 not in axes) or (1 not in axes):
            from cockpit.devices.dummyXYStage import DummyMover
            dummies.append(DummyMover())
        # Cameras
        if not getHandlersOfType(CAMERA):
            from cockpit.devices.dummyCamera import DummyCamera
            dummies.append(DummyCamera())
        # Dummy imager
        if not getHandlersOfType(IMAGER):
            from cockpit.devices.imager import DummyImagerDevice
            dummies.append(DummyImagerDevice())
        # Initialise dummies.
        for d in dummies:
            self.nameToDevice[d.name] = d
            self.initDevice(d)
        # Ambient light source
        from cockpit.handlers.lightSource import LightHandler
        ambient = {'t': 100}
        h = LightHandler(
            'ambient', 'ambient', {
                'setExposureTime': lambda null, t: ambient.__setitem__('t', t),
                'getExposureTime': lambda null: ambient['t'],
                'setEnabled': lambda null, state: True,
            }, 0, 100)
        self.addHandler(h)
        self.finalizeInitialization()
        yield 'dummy-devices'