Пример #1
0
def test_end_to_end_floats():
    # Setup
    t = transportMock()
    c = Pytelemetry(t)
    cb = mock.Mock(spec=["topic", "data", "opts"])
    default_cb = mock.Mock(spec=["topic", "data", "opts"])
    c.subscribe('sometopic', cb)
    c.subscribe(None, default_cb)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 0.0, 'float32')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', 0.0, None)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 3.4028234e38, 'float32')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    assert abs(cb.call_args[0][1] - 3.4028234e38) <= max(
        1e-7 * max(abs(cb.call_args[0][1]), abs(3.4028234e38)), 0.0)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', -3.4028234e38, 'float32')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    assert abs(cb.call_args[0][1] - (-3.4028234e38)) <= max(
        1e-7 * max(abs(cb.call_args[0][1]), abs(-3.4028234e38)), 0.0)
Пример #2
0
def test_end_to_end_uints():
    # Setup
    t = transportMock()
    c = Pytelemetry(t)
    cb = mock.Mock(spec=["topic", "data", "opts"])
    default_cb = mock.Mock(spec=["topic", "data", "opts"])
    c.subscribe('sometopic', cb)
    c.subscribe(None, default_cb)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 255, 'uint8')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', 255, None)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 65535, 'uint16')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', 65535, None)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 4294967295, 'uint32')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', 4294967295, None)
Пример #3
0
def main():
    init_logging()

    transport = SerialTransport()
    c = Pytelemetry(transport)

    options = dict()
    options['port'] = "COM20"
    options['baudrate'] = 115200

    transport.connect(options)
    #c.subscribe(None, printer)

    bad_frame = bytearray(b'0700736f6d65746f70696300626f6f7961613ecc')
    c.api._decode_frame(bad_frame)

    c.publish('sometopic', 'booyaa', 'string')

    timeout = time.time() + 3

    while True:
        c.update()
        if time.time() > timeout:
            break

    transport.disconnect()
    print("Done.")
Пример #4
0
def test_unexisting_type():
    # Setup
    t = transportMock()
    c = Pytelemetry(t)

    with pytest.raises(IndexError):
        c.publish('sometopic', 12, 'int323')
    assert t.queue.qsize() == 0
Пример #5
0
def test_serial_stats():
    # Setup
    t = SerialTransport()
    t.driver = driverMock()
    c = Pytelemetry(t)

    stats = t.stats()

    assert stats['rx_bytes'] == 0
    assert stats['rx_chunks'] == 0
    assert stats['tx_bytes'] == 0
    assert stats['tx_chunks'] == 0

    c.publish('foo', 'bar', 'string')

    stats = t.stats()

    assert stats['rx_bytes'] == 0
    assert stats['rx_chunks'] == 0
    assert stats['tx_bytes'] == 13
    assert stats['tx_chunks'] == 1

    c.update()

    stats = t.stats()

    assert stats['rx_bytes'] == 13
    assert stats[
        'rx_chunks'] == 13  # TODO : For now data read byte after byter. To replace by read in bulk
    assert stats['tx_bytes'] == 13
    assert stats['tx_chunks'] == 1

    c.publish('fooqux', -32767, 'int16')

    stats = t.stats()

    assert stats['rx_bytes'] == 13
    assert stats['rx_chunks'] == 13
    assert stats['tx_bytes'] == 13 + 15
    assert stats['tx_chunks'] == 2

    c.update()

    stats = t.stats()

    assert stats['rx_bytes'] == 13 + 15
    assert stats['rx_chunks'] == 13 + 15
    assert stats['tx_bytes'] == 13 + 15
    assert stats['tx_chunks'] == 2

    t.resetStats()

    stats = t.stats()

    assert stats['rx_bytes'] == 0
    assert stats['rx_chunks'] == 0
    assert stats['tx_bytes'] == 0
    assert stats['tx_chunks'] == 0
Пример #6
0
def test_wrong_type():
    # Setup
    t = transportMock()
    c = Pytelemetry(t)

    with pytest.raises(Exception) as excinfo:
        c.publish('sometopic', 12, 'string')
    # TODO : Assert exception
    assert t.queue.qsize() == 0
Пример #7
0
def test_end_to_end_ints():
    # Setup
    t = transportMock()
    c = Pytelemetry(t)
    cb = mock.Mock(spec=["topic", "data", "opts"])
    default_cb = mock.Mock(spec=["topic", "data", "opts"])
    c.subscribe('sometopic', cb)
    c.subscribe(None, default_cb)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 127, 'int8')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', 127, None)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', -127, 'int8')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', -127, None)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 32767, 'int16')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', 32767, None)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', -32767, 'int16')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', -32767, None)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 2147483647, 'int32')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', 2147483647, None)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', -2147483647, 'int32')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_with('sometopic', -2147483647, None)
Пример #8
0
def test_topic_contains_space_both_ends():
    # Setup
    t = transportMock()
    c = Pytelemetry(t)
    cb = mock.Mock(spec=["topic", "data", "opts"])
    c.subscribe(' topicwithspaces ', cb)

    c.publish(' topicwithspaces ', 1234567, 'int32')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_once_with(' topicwithspaces ', 1234567, None)
Пример #9
0
def test_hardcoded():
    t = transportMock()
    c = Pytelemetry(t)
    cb = mock.Mock(spec=["topic", "data"])
    c.subscribe('sometopic ', cb)

    # Apply hardcoded frame directly generated by the c library
    #        SOF  head  sometopic..................................... eol 12457........  crc.....  eof
    t.write([
        247, 6, 0, 115, 111, 109, 101, 116, 111, 112, 105, 99, 32, 0, 169, 48,
        0, 0, 111, 249, 127
    ])
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_once_with('sometopic ', 12457, None)
Пример #10
0
    def __init__(self, transport=None, stdout=None):
        # cmd Initialization and configuration
        cmd.Cmd.__init__(self, stdout=stdout)
        self.intro = 'pytelemetry terminal started.' \
                 + ' (type help for a list of commands.)'
        self.prompt = ':> '
        self.file = None

        # pytelemetry setup
        if not transport:
            self.transport = transports.SerialTransport()
        else:
            self.transport = transport
        self.telemetry = Pytelemetry(self.transport)

        self.topics = Topics()
        self.plots = []
        self.plotsLock = Lock()
        self.runner = Runner(self.transport, self.telemetry, self.plots,
                             self.plotsLock, self.topics)

        self.telemetry.subscribe(None, self.topics.process)

        self.types_lookup = {
            '--s': 'string',
            '--u8': 'uint8',
            '--u16': 'uint16',
            '--u32': 'uint32',
            '--i8': 'int8',
            '--i16': 'int16',
            '--i32': 'int32',
            '--f32': 'float32'
        }
        logger.info("Module path : %s" %
                    os.path.dirname(os.path.realpath(__file__)))
        try:
            logger.info("Module version : %s" % __version__)
        except:
            logger.warning("Module version : not found.")
Пример #11
0
def test_end_to_end_string():
    # Setup
    t = transportMock()
    c = Pytelemetry(t)
    cb = mock.Mock(spec=["topic", "data", "opts"])
    default_cb = mock.Mock(spec=["topic", "data", "opts"])
    c.subscribe('sometopic', cb)
    c.subscribe(None, default_cb)

    # testing callback subscribed to topic
    assert t.queue.qsize() == 0
    c.publish('sometopic', 'someMessage', 'string')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    cb.assert_called_once_with('sometopic', 'someMessage', None)

    # test default callback
    c.publish('othertopic', 'otherMessage', 'string')
    assert t.queue.qsize() > 0
    c.update()
    assert t.queue.qsize() == 0
    default_cb.assert_called_once_with('othertopic', 'otherMessage', None)
Пример #12
0
# --------- Import modules ---------- #
from pytelemetry import Pytelemetry
from pytelemetry.transports.serialtransport import *
import time
import socket
import json

# ---- Global Variables ---- #

# load envVariable.json
with open('./envVariable.json') as f:
    jsonFile = json.load(f)
config = json.loads(json.dumps(jsonFile))
transport = SerialTransport()
c = Pytelemetry(transport)

"""
*
* Function Name: 	init_port_forward
* Input: 		test boolean
* Output: 		None
* Logic: 		Initialises socket server, that communicates with js client
* Example Call:		int_port_forward()
*
"""
def init_port_forward(test=False):
    print("starting port from server")
    HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
    # Port to listen on (non-privileged ports are > 1023)
    PORT = config["telemetrySocketPort"]
Пример #13
0
def test_protocol_stats():
    t = transportMock()
    p = Pytelemetry(t)

    measures = p.api.stats()

    assert measures["rx_decoded_frames"] == 0
    assert measures["rx_corrupted_crc"] == 0
    assert measures["rx_corrupted_header"] == 0
    assert measures["rx_corrupted_eol"] == 0
    assert measures["rx_corrupted_topic"] == 0
    assert measures["rx_corrupted_payload"] == 0
    assert measures["tx_encoded_frames"] == 0

    # Add a frame inside the transport queue
    t.write(bytearray.fromhex("f70700666f6f0062617247027f"))
    # update to read the new frame
    p.update()
    # get measurements
    measures = p.api.stats()

    assert measures["rx_decoded_frames"] == 1
    assert measures["rx_corrupted_crc"] == 0
    assert measures["rx_corrupted_header"] == 0
    assert measures["rx_corrupted_eol"] == 0
    assert measures["rx_corrupted_topic"] == 0
    assert measures["rx_corrupted_payload"] == 0
    assert measures["tx_encoded_frames"] == 0

    # replaced CRC '4702' by '4701' to corrupt crc
    t.write(bytearray.fromhex("f70700666f6f0062617247017f"))
    # update to read the new frame
    p.update()
    # get measurements
    measures = p.api.stats()

    assert measures["rx_decoded_frames"] == 1
    assert measures["rx_corrupted_crc"] == 1
    assert measures["rx_corrupted_header"] == 0
    assert measures["rx_corrupted_eol"] == 0
    assert measures["rx_corrupted_topic"] == 0
    assert measures["rx_corrupted_payload"] == 0
    assert measures["tx_encoded_frames"] == 0

    # replaced byte n3 '00' by '10' to corrupt crc
    t.write(bytearray.fromhex("f70710666f6f0062617247027f"))
    # update to read the new frame
    p.update()
    # get measurements
    measures = p.api.stats()

    assert measures["rx_decoded_frames"] == 1
    assert measures["rx_corrupted_crc"] == 2
    assert measures["rx_corrupted_header"] == 0
    assert measures["rx_corrupted_eol"] == 0
    assert measures["rx_corrupted_topic"] == 0
    assert measures["rx_corrupted_payload"] == 0
    assert measures["tx_encoded_frames"] == 0

    #crc = crc16(bytearray.fromhex("0900666f6f00626172"))
    #print(crc)
    #crc = struct.pack("<H",crc)
    #print(crc.hex())

    # Replaced header from 0700 to 0900 to corrupt it. Crc is valid to not discard
    t.write(bytearray.fromhex("f70900666f6f006261725fc57f"))
    # update to read the new frame
    p.update()
    # get measurements
    measures = p.api.stats()

    assert measures["rx_decoded_frames"] == 1
    assert measures["rx_corrupted_crc"] == 2
    assert measures["rx_corrupted_header"] == 1
    assert measures["rx_corrupted_eol"] == 0
    assert measures["rx_corrupted_topic"] == 0
    assert measures["rx_corrupted_payload"] == 0
    assert measures["tx_encoded_frames"] == 0

    #crc = crc16(bytearray.fromhex("0700666f6f01626172"))
    #print(crc)
    #crc = struct.pack("<H",crc)
    #print(crc.hex())

    # Removed EOL. Crc is valid to not discard
    t.write(bytearray.fromhex("f70700666f6f0162617224417f"))
    # update to read the new frame
    p.update()
    # get measurements
    measures = p.api.stats()

    assert measures["rx_decoded_frames"] == 1
    assert measures["rx_corrupted_crc"] == 2
    assert measures["rx_corrupted_header"] == 1
    assert measures["rx_corrupted_eol"] == 1
    assert measures["rx_corrupted_topic"] == 0
    assert measures["rx_corrupted_payload"] == 0
    assert measures["tx_encoded_frames"] == 0

    # Impossible to detect corrupted payload of type string because length is unkown. One more reason to store framesize inside frame

    # Use a frame of type u32 instead
    #crc = crc16(bytearray.fromhex("03006b6c6d6f707100ffffff"))
    #crc = struct.pack("<H",crc)
    #print(crc.hex())

    # Corruped payload by removing third & fourth hex number from end. Crc will pass
    #
    t.write(bytearray.fromhex("f703006b6c6d6f707100fffffff2dd7f"))
    # update to read the new frame
    p.update()
    # get measurements
    measures = p.api.stats()

    assert measures["rx_decoded_frames"] == 1
    assert measures["rx_corrupted_crc"] == 2
    assert measures["rx_corrupted_header"] == 1
    assert measures["rx_corrupted_eol"] == 1
    assert measures["rx_corrupted_topic"] == 0
    assert measures["rx_corrupted_payload"] == 1
    assert measures["tx_encoded_frames"] == 0

    topmeasures = p.stats()

    assert measures["rx_decoded_frames"] == topmeasures['protocol'][
        "rx_decoded_frames"]
    assert measures["rx_corrupted_crc"] == topmeasures['protocol'][
        "rx_corrupted_crc"]
    assert measures["rx_corrupted_header"] == topmeasures['protocol'][
        "rx_corrupted_header"]
    assert measures["rx_corrupted_eol"] == topmeasures['protocol'][
        "rx_corrupted_eol"]
    assert measures["rx_corrupted_topic"] == topmeasures['protocol'][
        "rx_corrupted_topic"]
    assert measures["rx_corrupted_payload"] == topmeasures['protocol'][
        "rx_corrupted_payload"]
    assert measures["tx_encoded_frames"] == topmeasures['protocol'][
        "tx_encoded_frames"]

    p.publish("boo", 123, "uint8")

    # get measurements
    measures = p.stats()

    assert measures['protocol']["rx_decoded_frames"] == 1
    assert measures['protocol']["rx_corrupted_crc"] == 2
    assert measures['protocol']["rx_corrupted_header"] == 1
    assert measures['protocol']["rx_corrupted_eol"] == 1
    assert measures['protocol']["rx_corrupted_topic"] == 0
    assert measures['protocol']["rx_corrupted_payload"] == 1
    assert measures['protocol']["tx_encoded_frames"] == 1

    measures = p.api.delimiter.stats()

    assert measures["rx_processed_bytes"] > 0
    assert measures["rx_discarded_bytes"] == 0
    assert measures["rx_escaped_bytes"] == 0
    assert measures["rx_complete_frames"] > 0
    assert measures["rx_uncomplete_frames"] == 0
    assert measures["tx_processed_bytes"] > 0
    assert measures["tx_encoded_frames"] > 0
    assert measures["tx_escaped_bytes"] == 0

    p.resetStats()
    measures = p.stats()

    assert measures['protocol']["rx_decoded_frames"] == 0
    assert measures['protocol']["rx_corrupted_crc"] == 0
    assert measures['protocol']["rx_corrupted_header"] == 0
    assert measures['protocol']["rx_corrupted_eol"] == 0
    assert measures['protocol']["rx_corrupted_topic"] == 0
    assert measures['protocol']["rx_corrupted_payload"] == 0
    assert measures['protocol']["tx_encoded_frames"] == 0

    assert measures['framing']["rx_processed_bytes"] == 0
    assert measures['framing']["rx_discarded_bytes"] == 0
    assert measures['framing']["rx_escaped_bytes"] == 0
    assert measures['framing']["rx_complete_frames"] == 0
    assert measures['framing']["rx_uncomplete_frames"] == 0
    assert measures['framing']["tx_processed_bytes"] == 0
    assert measures['framing']["tx_encoded_frames"] == 0
    assert measures['framing']["tx_escaped_bytes"] == 0