示例#1
0
def test_calculate_circular_drive_chains():
    blocks = [
        Block(id='block-1',
              type='test',
              data={'ptr1': blox_link('block-2', 'test', True)}),
        Block(id='block-2',
              type='test',
              data={'ptr2': blox_link('block-3', 'test', True)}),
        Block(id='block-3',
              type='test',
              data={'ptr3': blox_link('block-1', 'test', True)}),
    ]
    result = block_analysis.calculate_drive_chains(blocks)
    result = sorted(result, key=lambda v: v['target'])
    assert result == [
        {
            'target': 'block-1',
            'source': 'block-1',
            'intermediate': ['block-3', 'block-2']
        },
        {
            'target': 'block-2',
            'source': 'block-2',
            'intermediate': ['block-1', 'block-3']
        },
        {
            'target': 'block-3',
            'source': 'block-3',
            'intermediate': ['block-2', 'block-1']
        },
    ]
示例#2
0
    async def run(self):
        try:
            await asyncio.sleep(self.interval)
            synched = await service_status.wait_synchronized(self.app,
                                                             wait=False)

            status_data = service_status.desc_dict(self.app)
            blocks = []

            try:
                if synched:
                    api = BlocksApi(self.app)
                    blocks, logged_blocks = await api.read_all_broadcast()

                    # Convert list to key/value format suitable for history
                    history_data = {
                        block['id']: block['data']
                        for block in logged_blocks if
                        not block['id'].startswith(const.GENERATED_ID_PREFIX)
                    }

                    await mqtt.publish(self.app,
                                       self.history_topic,
                                       err=False,
                                       message={
                                           'key': self.name,
                                           'data': history_data,
                                       })

            finally:
                # State event is always published
                await mqtt.publish(self.app,
                                   self.state_topic,
                                   err=False,
                                   retain=True,
                                   message={
                                       'key': self.name,
                                       'type': 'Spark.state',
                                       'data': {
                                           'status':
                                           status_data,
                                           'blocks':
                                           blocks,
                                           'relations':
                                           calculate_relations(blocks),
                                           'drive_chains':
                                           calculate_drive_chains(blocks),
                                       },
                                   })

        except exceptions.ConnectionPaused:
            LOGGER.debug(f'{self} interrupted: connection paused')

        except Exception as ex:
            LOGGER.debug(f'{self} exception: {strex(ex)}')
            raise
def test_calculate_drive_chains():
    blocks = make_blocks()
    result = block_analysis.calculate_drive_chains(blocks)
    result = sorted(result, key=lambda v: v['target'] + ' ' + v['source'])
    assert result == [
        {'target': 'Cool Actuator', 'source': 'Cool PID', 'intermediate': ['Cool PWM']},
        {'target': 'Cool PWM', 'source': 'Cool PID', 'intermediate': []},
        {'target': 'Heat Actuator', 'source': 'Heat PID', 'intermediate': ['Heat PWM']},
        {'target': 'Heat PWM', 'source': 'Heat PID', 'intermediate': []},
        {'target': 'Spark Pins', 'source': 'Cool PID', 'intermediate': ['Cool Actuator', 'Cool PWM']},
        {'target': 'Spark Pins', 'source': 'Heat PID', 'intermediate': ['Heat Actuator', 'Heat PWM']},
    ]