Beispiel #1
0
        def success(payload):
            '''
            :return: the response of ``add_link`` api call. This handle return
                *ok* if the ``Link`` record create successfully.
                The sqlite in-memory will be executed in ``serializable``
                ioslation level.  If the writting transaction of ``Link``
                is failed, we will return a *error* state as response.
            '''
            mode = self.get_mode(payload)

            try:
                Link.add(payload['da_id'], payload[mode], mode, topic)
            except ValueError as err:
                payload['state'] = 'error'
                payload['reason'] = 'Link already exists'

            # update esm worker config and (re)start
            func = UserFunction.select(payload['func']) if payload.get('func') else None
            if mode == 'idf':
                conf = self.esm_worker.idf_confs.copy()
                conf.append({'src': func, 'topic': topic})
                self.esm_worker.idf_confs = conf
            elif mode == 'odf':
                conf = self.esm_worker.odf_confs.copy()
                conf.append({'src': func, 'topic': topic})
                self.esm_worker.odf_confs = conf
            self.esm_worker.start()

            return payload
Beispiel #2
0
def test_link_add_conflict(_link):
    assert Link.add('id', 'feature', 'idf', 'topic')
    assert Link.select() == {('id', 'feature', 'idf'): 'topic'}

    with pytest.raises(ValueError) as err:
        Link.add('id', 'feature', 'idf', 'topic')

    assert 'exists' in str(err)
Beispiel #3
0
        def success(payload):
            '''
            If the record of ``Link`` removes failed,
            we will return a error payload.
            '''
            mode = self.get_mode(payload)

            try:
                Link.rm(payload['da_id'], payload[mode], mode)
            except ValueError as err:
                payload['state'] = 'error'
                payload['reason'] = str(err)

            return payload
Beispiel #4
0
    def rm_link(self, payload):
        '''
        This ``rm_link`` command will send a ``DISCONNECT`` control message
        to device application.
        '''
        da_id = payload['da_id']
        msg_id = str(uuid4())  # random
        mode = self.get_mode(payload)
        feature = payload[mode]
        pub = iot_conn_mgr.conns[UUID(payload['da_id'])].ctrl.pub

        try:
            topic = Link.pop(da_id, feature, mode)
        except ValueError as err:
            return self.send_error(payload, reason=str(err))

        iot_conn_mgr.conns[UUID(da_id)].ctrl.add_res_callback(
            msg_id,
            *self.response_func(
                payload,
                on_success=self.rm_link_on_success(),
                on_error=None
            )
        )

        return ctrl.disconnect(msg_id, mode, feature, topic, pub)
Beispiel #5
0
    def add_link(self, payload):
        '''
        This add link command will send a ``CONNECT`` control message
        to device application.

        We will check the device feature lock as first.
        Then, record the link to ``Link`` for future lookup.
        '''
        da_id = UUID(payload['da_id'])
        mode = self.get_mode(payload.keys())
        feature = payload[mode]

        if Link.select(payload['da_id'], feature, mode):
            return self.send_error(payload, reason='Link already exists')
        elif payload.get('func') and not UserFunction.select(payload['func']):
            return self.send_error(
                payload, reason='Function unknown. Please add it first.')
        return self._add_link(da_id, mode, feature, payload)
Beispiel #6
0
def test_link_pop_unknown(_link):
    with pytest.raises(ValueError) as err:
        assert Link.pop('id', 'feature', 'idf')

    return 'unknown' in str(err)
Beispiel #7
0
def test_link_pop_key(_link):
    assert Link.add('id', 'feature', 'idf', 'topic')
    assert Link.select('id', 'feature', 'idf') == 'topic'
    assert Link.pop('id', 'feature', 'idf') == 'topic'
    assert Link.select('id', 'feature', 'idf') is None
Beispiel #8
0
def test_link_rm_none(_link):
    with pytest.raises(ValueError) as err:
        Link.rm('id', 'feature', 'idf')

    assert 'unknown' in str(err)
Beispiel #9
0
def test_link_rm(_link):
    assert Link.add('id', 'feature', 'idf', 'topic')
    assert Link.rm('id', 'feature', 'idf')
Beispiel #10
0
def test_link_add(_link):
    assert Link.add('id', 'feature', 'idf', 'topic')
    assert Link.select() == {('id', 'feature', 'idf'): 'topic'}
Beispiel #11
0
def _link():
    assert Link.clear()
    assert Link.select() == {}
    yield
    assert Link.clear()
    assert Link.select() == {}