Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-existing attribute is returned and causes float conversion error #203

Closed
FWuellhorst opened this issue Oct 4, 2023 · 13 comments · Fixed by #311
Closed

Non-existing attribute is returned and causes float conversion error #203

FWuellhorst opened this issue Oct 4, 2023 · 13 comments · Fixed by #311
Assignees
Labels
bug Something isn't working

Comments

@FWuellhorst
Copy link
Contributor

Describe the bug

For this entity:

{
    _id: {
        id: 'conCenFan_oveAct_u',
        type: 'Actuator',
        servicePath: '/ALPaperStage3'
    },
    attrNames: [
        'conCenFan_oveAct_u_status',
        'conCenFan_oveAct_u_info',
        'TimeInstant'
    ],
    attrs: {
        conCenFan_oveAct_u_status: {
            type: 'commandStatus',
            creDate: 1692798690.8602214,
            modDate: 1693224142.4930723,
            value: 'OK',
            mdNames: [
                'TimeInstant'
            ],
            md: {
                TimeInstant: {
                    type: 'DateTime',
                    value: 1693224142.4899998
                }
            }
        },
        conCenFan_oveAct_u_info: {
            type: 'commandResult',
            creDate: 1692798690.8602214,
            modDate: 1693224142.4930823,
            value: 9.99729039,
            mdNames: [
                'TimeInstant'
            ],
            md: {
                TimeInstant: {
                    type: 'DateTime',
                    value: 1693224142.4899998
                }
            }
        },
        TimeInstant: {
            type: 'DateTime',
            creDate: 1692798690.8602214,
            modDate: 1696261274.039428,
            value: 1696261274.0340004,
            mdNames: []
        }
    },
    creDate: 1692798690.8602214,
    modDate: 1696261274.0394897,
    lastCorrelator: '10a74dbf-2355-4470-be58-a1638b7868b1'
}

I get the following attributes when calling get_attributes in ngsi_v2:

{
  'TimeInstant': {
    'type': 'DateTime',
    'value': '2023-10-02T15:41:14.034Z',
    'metadata': {}
  },
  'conCenFan_oveAct_u_info': {
    'type': 'commandResult',
    'value': 9.99729039,
    'metadata': {
      'TimeInstant': {
        'type': 'DateTime',
        'value': '2023-08-28T12:02:22.490Z'
      }
    }
  },
  'conCenFan_oveAct_u_status': {
    'type': 'commandStatus',
    'value': 'OK',
    'metadata': {
      'TimeInstant': {
        'type': 'DateTime',
        'value': '2023-08-28T12:02:22.490Z'
      }
    }
  },
  'conCenFan_oveAct_u': {
    'type': 'Number',
    'value': '',
    'metadata': {}
  }
}

The attribute conCenFan_oveAct_u does not exist, and as the value is an empty string but the type a Number, I get a float conversion error.

@FWuellhorst FWuellhorst added the bug Something isn't working label Oct 4, 2023
@djs0109
Copy link
Contributor

djs0109 commented Oct 5, 2023

It seems to me that conCenFan_oveAct_u is a command in IoTAent, and the entity attribute is also created by IoTAent.

If that is the case, I will suggest to use command as the type. For example:

  {
    "protocol": "IoTA-JSON",
    "transport": "MQTT",
    "explicitAttrs": false,
    "device_id":  ???,
    "entity_name": "conCenFan_oveAct_u",
    "entity_type": "Actuator",
    "commands": [
      {
        "name": "conCenFan_oveAct_u",
        "type": "command"
      }
    ],
    "attributes": [
      ...
    ],
    "ngsiVersion": "v2"
  }

@FWuellhorst
Copy link
Contributor Author

That is correct. But the type of the command is a Number, not "command". And we never changed this implementation, it was working some months ago.

@FWuellhorst
Copy link
Contributor Author

It works specifying the type="command", but I don't get why this only occurs now. We use the development fiware cluster, did you update the iotagent / orion?

@djs0109
Copy link
Contributor

djs0109 commented Oct 5, 2023

As far as I know, IoTagent will always create such an attribute in Orion. So I think the creation of conCenFan_oveAct_u is not a bug.

And still, I recommend using command as the data type, because it will be then easier to recognize the command in context broker, and this kind of attribute will not have any meaningful value anyway. The value is stored in conCenFan_oveAct_u_info

Apart from that, are you expecting filip to ignore such a data type error?

{
  "conCenFan_oveAct_u": {
    "type": "Number",
    "value": ""
  }
}

@FWuellhorst
Copy link
Contributor Author

ok, thanks! Yes, filip should avoid this error. The attr is not even listed in mongo-express. As the error only occurs due to the conversion from "" to float, this should be catched using some sort of float(value) if value else None

@djs0109
Copy link
Contributor

djs0109 commented Oct 5, 2023

I agree. I will have a look

@djs0109 djs0109 self-assigned this Oct 5, 2023
@github-actions
Copy link

github-actions bot commented Oct 5, 2023

@tstorek
Copy link
Collaborator

tstorek commented Oct 17, 2023

@FWuellhorst @djs0109 I just faced the exact same issue. I looked into it and the trouble occurs in the ContextAttribute creation and validation.

Let's start at the beginning:

  1. You cannot see the attribute in the database because does not live in the ContextBroker database. It comes from a registraion that the IoTA-Agent creates for it. For more details check on the registration docs
  2. When calling the entity the contextbroker fowards the request to the iotagent and receives the data. Maybe they changed the default value, if the command is empty.
  3. ToFix: We need to adjust filip.models.ngsi_v2.base.BaseValueAttribute.validate_value_type to handle EmptyStrings. Otherwise we need to use the the command type so far, which I probably would recommend anyways. If you want to add additional info about the type maybe the metadata field works by now for commands. The latter would require to adjust the command model which would be a minor issue filip.models.ngsi_v2.iot.DeviceCommand. Let's check on the docs! Here we go: https://iotagent-node-lib.readthedocs.io/en/latest/api.html
  4. From the docs commands should always have the type command. Probably I followed that in the original implementation but forgot to fix the type in the CommandModel.

I'm happy to help out here :) It is true that this is annoying. However, I will not be free until November. Maybe @FWuellhorst @djs0109 you can already try to realize the second option.

@tstorek
Copy link
Collaborator

tstorek commented Oct 17, 2023

Oh, btw @FWuellhorst. If you use command you can access them directly using the api of the ContextEntity ;) same for relationships :)

@djs0109
Copy link
Contributor

djs0109 commented Oct 18, 2023

@tstorek Thanks for your suggestions! I saw that if the data type is command, the value will not be validated in filip.models.ngsi_v2.base.BaseValueAttribute.validate_value_type. That is to say, if using command as type, empty string will not cause any problem.

For adding the metadata in DeviceModel, I check the docs. It is actually contradictory... some part suggests that only attribute can have metadata, and the other part says that command can also have metadata.
So I test it with LibVersion 3.3, JSON-version 2.3:

Metadata in command will cause syntax error

    "commands": [
      {
        "name": "com1",
        "type": "command",
        "metadata": {
            "unitCode": { "type": "Text", "value": "CAL" }
            }
      }
    ]

But metadata in attributes can work

    "attributes": [
      {
        "name": "attr1",
        "type": "Integer",
        "metadata": {
            "unitCode": { "type": "Text", "value": "CAL" }
            }
      }  
    ],

@tstorek
Copy link
Collaborator

tstorek commented Oct 18, 2023

@djs0109 @FWuellhorst According to the previous test, we should do the two things:

  1. Raise an issue in IoTA-Json for metadata
  2. Agree on a conversation-convention in filip

What Do you think? Shall we allow any types favoring the missing metadata object? Mostly likely it would be useful also for the other conversions, right?

@FWuellhorst
Copy link
Contributor Author

Okay, thanks for checking! I would suggest to fix the validate_value_type function for this issue, the rest would be another issue, or?

@djs0109
Copy link
Contributor

djs0109 commented Nov 6, 2023

I think I would add an if statement to every data type, i.e. to explicitly allow missing values in the form of None, and "". float/int/str(value) if value else None will be too risky, because it will ignore for example 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants