def test_some_frames():
    t = Telemetry(None,None)
    assert t._decode_frame(bytearray.fromhex("0100626f7574746f6e300000b92c")) == ('boutton0',0)
    assert t._decode_frame(bytearray.fromhex("0100626f7574746f6e3100005f2a")) == ('boutton1',0)
    assert t._decode_frame(bytearray.fromhex("0300636d70740029000000e867")) == ('cmpt', 41)
    assert t._decode_frame(bytearray.fromhex("0300636d70740006000000d269")) == ('cmpt', 6)
    assert t._decode_frame(bytearray(b"\x03\x00cmpt\x00\xc7\x00\x00\x00\x83e")) == ('cmpt', 199)
def test_special_characters():
    t = Telemetry(None,None)

    tests = [ ('foo', 'bar é$à', 'string'),
              ('çé', 2**30, 'int32'),
              ('yolo', 32768, 'uint16') ]

    for topic, data, typ in tests:
        frame = bytes(t._encode_frame(topic, data, typ))
        decoded = t._decode_frame(frame)
        assert decoded == (topic, data), '%s != %s' % (decoded, (topic, data))
Example #3
0
    def __init__(self, transport):
        """
            Creates a new instance of the Pytelemetry class.

            :param transport: A transport-compliant class. See Pytelemetry class
            documentation for more information
        """

        self.callbacks = dict()
        self.default_callback = None

        if _telemetry_use_c_api:
            self.api = TelemetryCBinding(transport,self._on_frame)
        else:
            self.api = Telemetry(transport,self._on_frame)
Example #4
0
    def __init__(self, transport):
        """
            Creates a new instance of the Pytelemetry class.

            :param transport: A transport-compliant class. See Pytelemetry class
            documentation for more information
        """

        self.callbacks = dict()
        self.default_callback = None

        if _telemetry_use_c_api:
            self.api = TelemetryCBinding(transport, self._on_frame)
        else:
            self.api = Telemetry(transport, self._on_frame)
def test_reference_vector():
    t = Telemetry(None,None)
    d = Delimiter(cb)

    d.decode(bytearray.fromhex("f70700666f6f0062617247027f"))
    assert t._decode_frame(Cache.frame) == ('foo','bar')

    d.decode(bytearray.fromhex("f70700666f6f2077697468207370616365730062617220776974682073706163657317dc7f"))
    assert t._decode_frame(Cache.frame) == ('foo with spaces','bar with spaces')

    d.decode(bytearray.fromhex("f7070030313233343536373839003031323334353637383973c07f"))
    assert t._decode_frame(Cache.frame) == ('0123456789', '0123456789')


    d.decode(bytearray.fromhex("f703006b6c6d6f707100ffffffff107b7f"))
    assert t._decode_frame(Cache.frame) == ('klmopq', 4294967295)
Example #6
0
class Pytelemetry:
    """
        Main class for the pytelemetry library.

        If you intend to implement a new transport, it needs to implement these
        four methods:

        >>> read(bytesAmount)
        Expects: Amount of bytes to read. Will return less bytes if
        bytes amount is bigger than immediately available bytes amount
        Returns: A bytes instance containing read bytes

        >>> readable()
        Returns: Amount of immediately available bytes

        >>> write(data)
        Expects: a bytes instance containing the data to write
        Returns: True

        >>> writeable()
        Returns: True if transport is available for writing,
                 False otherwise

    """
    def __init__(self, transport):
        """
            Creates a new instance of the Pytelemetry class.

            :param transport: A transport-compliant class. See Pytelemetry class
            documentation for more information
        """

        self.callbacks = dict()
        self.default_callback = None

        if _telemetry_use_c_api:
            self.api = TelemetryCBinding(transport,self._on_frame)
        else:
            self.api = Telemetry(transport,self._on_frame)

    def resetStats(self):
        """
Resets all counters that monitor transport and protocol to 0.
        """
        self.api.delimiter.resetStats()
        self.api.resetStats()

    def stats(self):
        """
Returns a dictionnary of dictionnary that contains critical information
about the transport and protocol behavior, such as:
   * amount of received frames
   * amount of badly delimited frames
   * amount of correctly delimited but still corrupted frames
   * etc
        """
        d = dict()
        d['framing'] = self.api.delimiter.stats()
        d['protocol'] = self.api.stats()

        return d

    def publish(self, topic, data, datatype):
        """

        """
        self.api.publish(topic,data,datatype)

    # subscribe a callback to topic
    # Subscribing to None will call that function for any unsubscribed topic
    def subscribe(self, topic, cb):
        if topic:
            self.callbacks[topic] = cb
        else:
            self.default_callback = cb

    def update(self):
        self.api.update()

    def _on_frame(self, topic, payload):
        cb = None
        # Search if topic has a registered callback
        if topic in self.callbacks:
            cb = self.callbacks[topic]
        # else pick default callback
        else:
            cb = self.default_callback

        # Extract eventual indexing and grouping data from topic
        topic, opts = translate(topic)

        # check callback is valid and call
        if cb:
            cb(topic,payload, opts)
Example #7
0
class Pytelemetry:
    """
        Main class for the pytelemetry library.

        If you intend to implement a new transport, it needs to implement these
        four methods:

        >>> read(bytesAmount)
        Expects: Amount of bytes to read. Will return less bytes if
        bytes amount is bigger than immediately available bytes amount
        Returns: A bytes instance containing read bytes

        >>> readable()
        Returns: Amount of immediately available bytes

        >>> write(data)
        Expects: a bytes instance containing the data to write
        Returns: True

        >>> writeable()
        Returns: True if transport is available for writing,
                 False otherwise

    """
    def __init__(self, transport):
        """
            Creates a new instance of the Pytelemetry class.

            :param transport: A transport-compliant class. See Pytelemetry class
            documentation for more information
        """

        self.callbacks = dict()
        self.default_callback = None

        if _telemetry_use_c_api:
            self.api = TelemetryCBinding(transport, self._on_frame)
        else:
            self.api = Telemetry(transport, self._on_frame)

    def resetStats(self):
        """
Resets all counters that monitor transport and protocol to 0.
        """
        self.api.delimiter.resetStats()
        self.api.resetStats()

    def stats(self):
        """
Returns a dictionnary of dictionnary that contains critical information
about the transport and protocol behavior, such as:
   * amount of received frames
   * amount of badly delimited frames
   * amount of correctly delimited but still corrupted frames
   * etc
        """
        d = dict()
        d['framing'] = self.api.delimiter.stats()
        d['protocol'] = self.api.stats()

        return d

    def publish(self, topic, data, datatype):
        """

        """
        self.api.publish(topic, data, datatype)

    # subscribe a callback to topic
    # Subscribing to None will call that function for any unsubscribed topic
    def subscribe(self, topic, cb):
        if topic:
            self.callbacks[topic] = cb
        else:
            self.default_callback = cb

    def update(self):
        self.api.update()

    def _on_frame(self, topic, payload):
        cb = None
        # Search if topic has a registered callback
        if topic in self.callbacks:
            cb = self.callbacks[topic]
        # else pick default callback
        else:
            cb = self.default_callback

        # Extract eventual indexing and grouping data from topic
        topic, opts = translate(topic)

        # check callback is valid and call
        if cb:
            cb(topic, payload, opts)