コード例 #1
0
def example_notification(base):
    def notification_callback(data):
        print("****************************")
        print("* Callback function called *")
        print(json_format.MessageToJson(data))
        print("****************************")

    # Subscribe to ConfigurationChange notifications
    print("Subscribing to ConfigurationChange notifications")
    try:
        notif_handle = base.OnNotificationConfigurationChangeTopic(
            notification_callback, Base_pb2.NotificationOptions())
    except KException as k_ex:
        print("Error occured: {}".format(k_ex))
    except Exception:
        print("Error occured")

    # ... miscellaneous tasks
    time.sleep(3)

    # Create a user profile to trigger a notification
    full_user_profile = Base_pb2.FullUserProfile()
    full_user_profile.user_profile.username = '******'
    full_user_profile.user_profile.firstname = 'Johnny'
    full_user_profile.user_profile.lastname = 'Cash'
    full_user_profile.user_profile.application_data = "Custom Application Stuff"
    full_user_profile.password = "******"

    user_profile_handle = Base_pb2.UserProfileHandle()
    try:
        print("Creating user profile to trigger notification")
        user_profile_handle = base.CreateUserProfile(full_user_profile)
    except KException:
        print("User profile creation failed")

    # Following the creation of the user profile, we should receive the ConfigurationChange notification (notification_callback() should be called)
    print("User {0} created".format(full_user_profile.user_profile.username))

    # Give time for the notification to arrive
    time.sleep(3)

    print("Now unsubscribing from ConfigurationChange notifications")
    base.Unsubscribe(notif_handle)

    try:
        print("Deleting previously created user profile ({0})".format(
            full_user_profile.user_profile.username))
        base.DeleteUserProfile(
            user_profile_handle
        )  # Should not have received notification about this modification

    except KException:
        print("User profile deletion failed")

    # Sleep to confirm that ConfigurationChange notification is not raised anymore after the unsubscribe
    time.sleep(3)
コード例 #2
0
def example_error_management(base):

    try:
        base.CreateUserProfile(Base_pb2.FullUserProfile())

    except KServerException as ex:
        # Get error and sub error codes
        error_code = ex.get_error_code()
        sub_error_code = ex.get_error_sub_code()
        print("Error_code:{0} , Sub_error_code:{1} ".format(error_code, sub_error_code))
        print("Caught expected error: {0}".format(ex))
コード例 #3
0
def example_manipulation_protobuf_object():

    # Messages are the main element in Google Protocol Buffer in the same way classes are to Python. You need a message
    # to make a workable object. A message can contain many kind of elements. We have already
    # covered the scalar value and in this section we are going to cover the message.
    #

    # A message can make a reference to another message to make more comprehensive element.

    # For this example we'll use the FullUserProfile and UserProfile messages.
    # message FullUserProfile {
    #     UserProfile user_profile = 1; //User Profile, which includes username.
    #     string password = 2; //User's password
    # }
    # message UserProfile {
    #     Kinova.Api.Common.UserProfileHandle handle = 1; // User handle (no need to be set)
    #     string username = 2;                            // username, which is used to connect to robot (or via Web App login)
    #     string firstname = 3;                           // user first name
    #     string lastname = 4;                            // user last name
    #     string application_data = 5;                    // other application data (used by Web App)
    # }

    # https://developers.google.com/protocol-buffers/docs/proto3#simple

    full_user_profile = Base_pb2.FullUserProfile()
    # Now we'll add data to the scalar
    full_user_profile.password = "******"

    # Now I want to work with the user profile attribute which is a message itself.
    # Since user profile is a message you can use the '.' to access
    # these attributes.
    full_user_profile.user_profile.username = "******"
    full_user_profile.user_profile.firstname = "Johnny"
    full_user_profile.user_profile.lastname = "Cash"

    # Another basic element is the enum. Enum is directly available from
    # the message - no need to use the enum 'message'.
    # Here's an example:
    # enum LimitationType {
    #     UNSPECIFIED_LIMITATION = 0;  // unspecified limitation
    #     FORCE_LIMITATION = 1;        // force limitation
    #     ACCELERATION_LIMITATION = 2; // acceleration limitation
    #     VELOCITY_LIMITATION = 3;     // velocity limitation
    # }

    # message CartesianLimitation {
    #     LimitationType type = 1;     // limitation type
    # }

    # https://developers.google.com/protocol-buffers/docs/proto3#enum

    cartesianLimitation = Base_pb2.CartesianLimitation
    cartesianLimitation.type = Base_pb2.FORCE_LIMITATION