<commit_before>#!/usr/bin/env python
# encoding=utf8

import json

import requests
from flexbe_core import EventState


class WonderlandEntityNameByID(EventState):
    '''
    Read the position of a room in a json string
    ># id                int     id of the room

    #> entityClass       string   of the entity
    #> entityName        string   of the entity
    #> entityCategory    string   of the entity
    #> entityColor       string   of the entity
    #> entityWeight      string   of the entity
    #> entitySize        string   of the entity
    #> entityContainer   string   of the entity

    #> entityPosX        float   position of the entity
    #> entityPosY        float   position of the entity
    #> entityPosZ        float   position of the entity
    #> entityPosYaw      float   position of the entity
    #> entityPosPitch    float   position of the entity
    #> entityPosRoll     float   position of the entity

    #> entityWaypointX   float   position of the position to reach for have the object
    #> entityWaypointY   float   position of the position to reach for have the object
    #> entityWaypointYaw float   position of the position to reach for have the object

    #> depth_waypoint    float   position of the room
    #> depth_position    float   position of the room

    <= done              return when at least one entity exist
    <= none              return when no entity have the selected name
    <= error             return when error reading data
    '''

    def __init__(self):
        # See example_state.py for basic explanations.
        super(WonderlandEntityNameByID, self).__init__(outcomes=['done', 'none', 'error'],
                                                       input_keys=['id'],
                                                       output_keys=['entityClass', 'entityName', 'entityCategory',
                                                                    'entityColor', 'entityWeight', 'entitySize',
                                                                    'entityContainer', 'entityPosX', 'entityPosY',
                                                                    'entityPosZ', 'entityPosYaw', 'entityPosPitch',
                                                                    'entityPosRoll', 'entityWaypointX',
                                                                    'entityWaypointY', 'entityWaypointYaw'])

    def execute(self, userdata):
        # Generate URL to contact
        url = "http://wonderland:8000/api/entity?entityId="+str(userdata.id)

        print("This")
        print("Is")
        print("A")
        print("Test")

        # try the request
        try:
            response = requests.get(url)
            print(response)
        except requests.exceptions.RequestException as e:
            print(e)
            return 'error'

        # parse parameter json data
        data = json.loads(response.content)

        if not 'entityClass' in data:
            self.none = 'none'
            return self.none

        # write return data
        userdata.entityClass = data['entityClass']
        userdata.entityName = data['entityName']
        userdata.entityCategory = data['entityCategory']
        userdata.entityColor = data['entityColor']
        userdata.entityWeight = data['entityWeight']
        userdata.entitySize = data['entitySize']
        userdata.entityContainer = data['entityContainer']

        userdata.entityPosX = data['entityPosX']
        userdata.entityPosY = data['entityPosY']
        userdata.entityPosZ = data['entityPosZ']
        userdata.entityPosYaw = data['entityPosYaw']
        userdata.entityPosPitch = data['entityPosPitch']
        userdata.entityPosRoll = data['entityPosRoll']

        userdata.entityWaypointX = data['entityWaypointX']
        userdata.entityWaypointY = data['entityWaypointY']
        userdata.entityWaypointYaw = data['entityWaypointYaw']

        userdata.depth_waypoint = data['depth_waypoint']
        userdata.depth_position = data['depth_position']

        return 'done'<commit_msg>Change state to use an entity message.<commit_after>#!/usr/bin/env python
# encoding=utf8

import json

import requests
from flexbe_core import EventState
from sara_msgs.msg import Entity


class WonderlandEntityNameByID(EventState):
    '''
    Read the position of a room in a json string
    ># id               int     The id of the entity in the database
    #> entity           Entity  The entity following the `sara_msgs/entity.msg` structure
    #> depth_position   int     The accuracy of the position (Number of chained container for have value)
    #> depth_waypoint   int     The accuracy of the waypoint (Number of chained container for have value)

    <= found            returned when at least one entity exist
    <= not_found        returned when no entity have the selected name
    <= error            returned when am error happen during the HTTP request
    '''

    def __init__(self):
        # See example_state.py for basic explanations.
        super(WonderlandEntityNameByID, self).__init__(outcomes=['found', 'not_found', 'error'],
                                                       input_keys=['id'],
                                                       output_keys=['entity', 'depth_position', 'depth_waypoint'])

    def execute(self, userdata):
        # Generate URL to contact
        url = "http://wonderland:8000/api/entity?entityId=" + str(userdata.id)

        # try the request
        try:
            response = requests.get(url)
            print(response)
        except requests.exceptions.RequestException as e:
            print(e)
            return 'error'

        # parse parameter json data
        data = json.loads(response.content)

        if 'entityId' not in data:
            return 'not_found'

        entity = Entity()

        entity.wonderlandId = data['entityId']
        entity.aliases.append(data['entityName'])

        # Description of the object:
        entity.name = data['entityClass']
        entity.category = data['entityCategory']
        entity.color = data['entityColor']

        # Physical description of the object:
        entity.weight = data['entityWeight']
        entity.size = data['entitySize']

        # Location of the object
        entity.position.x = data['entityPosX']
        entity.position.y = data['entityPosY']
        entity.position.z = data['entityPosZ']
        # Add if needed: 'entityPosYaw' ; 'entityPosPitch' ; 'entityPosRoll'

        entity.waypoint.x = data['entityWaypointX']
        entity.waypoint.y = data['entityWaypointY']
        entity.waypoint.theta = data['entityWaypointYaw']

        entity.containerId = data['entityContainer']

        userdata.entity = entity
        userdata.depth_position = data['depth_position']
        userdata.depth_waypoint = data['depth_waypoint']

        return 'done'
{'fraction_non_alphanumeric': 0.08304448295782785, 'fraction_numerical': 0.0014442518775274408, 'mean_word_length': 1.9682811830261466, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '139861', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1699, 'n_words': 495}