Exemple #1
0
 async def handle_event(center, event):
     """Listen for events and call trigger when data matches."""
     if match_event(event, **event_data):
         _LOGGER.debug("Trigger matched for event %s", event_type)
         # pass variables from trigger with event
         await trigger_func(
             {CONF_TRIGGER: {
                 CONF_TYPE: CONF_EVENT,
                 ATTR_EVENT: event
             }})
        async def send_cam_job(center, event):
            """Run on well event."""
            next_well_x, next_well_y = next_well_xy(center.samples.leica,
                                                    PLATE_NAME)

            if (not match_event(event, event_type=CAMACQ_START_EVENT)
                    and not match_event(
                        event,
                        field_x=self.x_fields - 1,
                        field_y=self.y_fields - 1,
                        well_img_ok=True,
                    ) or next_well_x is None
                    or (next_well_x, next_well_y) not in self.wells_left):
                return

            if center.samples.leica.images:
                await center.actions.command.stop_imaging()
            await self.send_gain_jobs(
                next_well_x,
                next_well_y,
            )
            self.wells_left.remove((next_well_x, next_well_y))
    async def set_sample_img_ok(self, center, event):
        """Set sample field img ok."""
        if not match_event(event, job_id=self.exp_job_ids[-1]):
            return

        await center.actions.sample.set_sample(
            name="field",
            plate_name=event.plate_name,
            well_x=event.well_x,
            well_y=event.well_y,
            field_x=event.field_x,
            field_y=event.field_y,
            values={"img_ok": True},
        )
        async def stop_imaging(center, event):
            """Run to stop the experiment."""
            match = match_event(
                event,
                field_x=self.x_fields - 1,
                field_y=self.y_fields - 1,
                well_img_ok=True,
            )

            if not match or self.wells_left:
                return

            await center.actions.command.stop_imaging()

            _LOGGER.info("Congratulations, experiment is finished!")
        async def calc_gain(center, event):
            """Calculate correct gain."""
            field_x, field_y = get_last_gain_coords(self.x_fields,
                                                    self.y_fields)
            channel_id = self.gain_job_channels - 1
            if not match_event(
                    event,
                    field_x=field_x,
                    field_y=field_y,
                    job_id=self.gain_job_id,
                    channel_id=channel_id,
            ):
                return

            await center.actions.command.stop_imaging()
            await center.actions.gain.calc_gain(
                plate_name=event.plate_name,
                well_x=event.well_x,
                well_y=event.well_y,
            )
        async def add_cam_job(center, event):
            """Add an experiment job to the cam list."""
            last_channel = self.channels[-1]
            channels = get_matched_samples(
                center.samples.leica,
                "channel",
                {
                    "plate_name": event.plate_name,
                    "well_x": event.well_x,
                    "well_y": event.well_y,
                },
            )
            if not match_event(event, channel_name=last_channel[CONF_CHANNEL]
                               ) or len(channels) != len(self.channels):
                return

            commands = []
            for field_x in range(self.x_fields):
                for field_y in range(self.y_fields):
                    cmd = cam_com(
                        self.exp_pattern,
                        event.well_x,
                        event.well_y,
                        field_x,
                        field_y,
                        0,
                        0,
                    )
                    commands.append(cmd)

            await center.actions.command.send(command=del_com())
            await center.actions.command.send_many(commands=commands)

            if self._remove_handle_exp_image is None:
                self._remove_handle_exp_image = self.handle_exp_image()

            await center.actions.command.start_imaging()
            await center.actions.command.send(command="/cmd:startcamscan")