示例#1
0
def create_sequence(router):
    print("Creating Action for Sequence")
    angular_action = create_angular_action()
    cartesian_action = create_cartesian_action()

    print("Creating Sequence")
    sequence = Base_pb2.Sequence()
    sequence.name = "Example sequence"

    print("Appending Actions to Sequence")
    task_1 = sequence.tasks.add()
    task_1.group_identifier = 0
    task_1.action.CopyFrom(angular_action)

    task_2 = sequence.tasks.add()
    task_2.group_identifier = 1  # Sequence element with same group_id are played at the same time
    task_2.action.CopyFrom(cartesian_action)

    print("Create sequence on device and execute it")
    base_client_service = BaseClient(router)
    handle_sequence = base_client_service.CreateSequence(sequence)
    base_client_service.PlaySequence(handle_sequence)

    print("Waiting 30 seconds to finish movement...")
    time.sleep(30)
def example_manipulation_protobuf_list():

    # In protobuf, list are called repeated once they are affected to a python
    # variable they can be used as a normal list.

    # Take note that a repeated message field doesn't have an append() function, it has an
    # add() function that create the new message object.

    # For this example we will use those two messages
    # message Sequence {
    #     SequenceHandle handle = 1
    #     string name = 2
    #     string application_data = 3
    #     repeated SequenceTask tasks = 4
    # }

    # message SequenceTask {
    #     uint32 group_identifier = 1;
    #     Action action = 2;
    #     string application_data = 3;
    # }

    # To keep a clearer example the attribute action in SequenceTask message will be keep to default value

    # Here's 2 way to add to a repeated message field

    # Create the parent message
    sequence = Base_pb2.Sequence()
    sequence.name = "Sequence"

    # The extend way
    sequence_task_1 = Base_pb2.SequenceTask()
    sequence_task_1.group_identifier = 10
    action = sequence_task_1.action
    action = Base_pb2.Action()  # Using Action default constructor
    sequence.tasks.extend([sequence_task_1])  # Extend expect an iterable

    # Created for the add() function unique to repeated message field
    sequence_task_2 = sequence.tasks.add()
    sequence_task_2.group_identifier = 20
    action = sequence_task_2.action
    action = Base_pb2.Action()  # Using Action default constructor

    # Since sequence.task is a list we can use all the python toolset to
    # loop, iterate, interogate and print element in that list
    for i in range(len(sequence.tasks)):
        print("sequence ID with index : {0}".format(
            sequence.tasks[i].group_identifier))

    # The list still have the iterator proprety of python list so you can directly iterate
    # throught element without creating a iterator like previous example
    for task in sequence.tasks:
        print("sequence ID with object iterator : {0}".format(
            task.group_identifier))
def example_manipulation_protobuf_list():

    # In Google Protocol Buffer, 'repeated' is used to designate a list of indeterminate length. Once affected to a Python
    # variable they can be used in the same way as a standard list.

    # Note that a repeated message field doesn't have an append() function, it has an
    # add() function that create the new message object.

    # For this example we will use the following two messages
    # message Sequence {
    #     SequenceHandle handle = 1
    #     string name = 2
    #     string application_data = 3
    #     repeated SequenceTask tasks = 4
    # }

    # message SequenceTask {
    #     uint32 group_identifier = 1;
    #     Action action = 2;
    #     string application_data = 3;
    # }

    # For a clearer example the attribute action in SequenceTask message will be kept to default value

    # Here's two ways to add to a repeated message field

    # Create the parent message
    sequence = Base_pb2.Sequence()
    sequence.name = "Sequence"

    # The 'extend' way
    sequence_task_1 = Base_pb2.SequenceTask()
    sequence_task_1.group_identifier = 10
    action = sequence_task_1.action
    action = Base_pb2.Action()  # Using Action default constructor
    sequence.tasks.extend([sequence_task_1])  # Extend expect an iterable

    # Created for the add() function unique to repeated message field
    sequence_task_2 = sequence.tasks.add()
    sequence_task_2.group_identifier = 20
    action = sequence_task_2.action
    action = Base_pb2.Action()  # Using Action default constructor

    # Since sequence.task is a list we can use all the Python features to
    # loop, iterate, interogate and print elements in that list.
    for i in range(len(sequence.tasks)):
        print("sequence ID with index : {0}".format(
            sequence.tasks[i].group_identifier))

    # Lists have the iterator property of a Python list, so you can directly iterate
    # throught element without creating a iterator as in the previous example
    for task in sequence.tasks:
        print("sequence ID with object iterator : {0}".format(
            task.group_identifier))
def example_manipulation_protobuf_helpers():

    # Google offers some helpers with protobuf
    # We will cover some of them in this section

    # All the module google.protobuf documentation is available here:
    # https://developers.google.com/protocol-buffers/docs/reference/python/

    # First, the function include with message instance. We will cover the next function
    #     Clear
    #     MergeFrom
    #     CopyFrom

    # The Clear function is strainght forward it clear all the message attribute.

    # MergeFrom and CopyFrom have the same purpose, To duplicate data into another object.
    # The difference between them is CopyFrom will do a Clear before a MergeFrom.

    # For it's part MergeFrom will merge data if the new field is not empty.
    # In case of repeated, the content receive in parameter will be append

    # For this example, I'll used the Ssid message
    # message Ssid {
    #     string identifier = 1;
    # }

    # First I'll create 4 of them with unique string
    ssid_1 = Base_pb2.Ssid()
    ssid_1.identifier = ""

    ssid_2 = Base_pb2.Ssid()
    ssid_2.identifier = "123"

    ssid_3 = Base_pb2.Ssid()
    ssid_3.identifier = "@#$"

    # Now I'll Merge ssid_1 in ssid_2 and print the identifier of ssid_2
    ssid_2.MergeFrom(ssid_1)
    print("Content ssid_2: {0}".format(ssid_2.identifier))
    # output : Content ssid_2: 123

    # Now I'll Copy ssid_1 in ssid_3 and print the identifier of ssid_3
    ssid_3.CopyFrom(ssid_1)
    print("Content ssid_3: {0}".format(ssid_3.identifier))
    # output : Content ssid_3:

    # For more function visit the Class Message documentation
    # https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.message.Message-class

    # From the protobuf library you can used the json_format module.
    # One useful function is the MessageToJson.
    # This function serialize the protobuf message to a json object it help alot when you want to print large object in
    # a human readable way.

    # Here's an example with the following two message
    # message Sequence {
    #     SequenceHandle handle = 1
    #     string name = 2
    #     string application_data = 3
    #     repeated SequenceTask tasks = 4
    # }

    # message SequenceTask {
    #     uint32 group_identifier = 1;
    #     Action action = 2;
    #     string application_data = 3;
    # }

    # I quickly populate the message
    sequence = Base_pb2.Sequence()
    sequence.name = "A Name"

    for i in range(5):
        sequence_task = sequence.tasks.add()
        sequence_task.group_identifier = 10 * (
            i + 1)  # Some further function doesn't print if value = 0
        action = sequence_task.action
        action = Base_pb2.Action()  # Using Action default constructor

    # We need to import the module
    from google.protobuf import json_format

    # Now to get the json object
    json_object = json_format.MessageToJson(sequence)

    # Now you can print it
    print("Json object")
    print(json_object)
    # output:
    # Json object
    # {
    #   "name": "A Name",
    #   "tasks": [
    #     {
    #       "groupIdentifier": 10
    #     },
    #     {
    #       "groupIdentifier": 20
    #     },
    #     {
    #       "groupIdentifier": 30
    #     },
    #     {
    #       "groupIdentifier": 40
    #     },
    #     {
    #       "groupIdentifier": 50
    #     }
    #   ]
    # }

    # From the protobuf library you can used the text_format module
    # A useful function is the MessageToString. It has the same purpose of
    # MessageToJson convert the message to a human readable format

    # For the example I'll reuse the sequence object created in previous example

    # First import the module
    from google.protobuf import text_format

    # Now print
    print("Text format")
    print(text_format.MessageToString(sequence))