使用swagger API和python在Thingsboard中添加实体关系 [英] Adding Entity relations in Thingsboard using swagger API and python

查看:192
本文介绍了使用swagger API和python在Thingsboard中添加实体关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动执行Thingsboard节点的设置. (2.0.2)

I am trying to automatize the setup of a Thingsboard node. (2.0.2)

我设法使用 oss-thingsboard-backend创建资产和设备-示例.

但是,我没有成功建立关系.

However, I did not succeed in creating relations.

我正在使用的代码是

from tb_api_client import swagger_client
from tb_api_client.swagger_client import ApiClient, Configuration,Device,AssetControllerApi,AssetSearchQuery,Asset,EntityRelation,EntityRelationControllerApi
from tb_api_client.swagger_client.rest import ApiException

tojson = lambda x: json.loads(str(x).replace("None","'None'").replace("'",'"').replace("True","true").replace("False","false"))


# get the token. 
headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' }

token_response = requests.post('http://127.0.0.1:8080/api/auth/login',data=login,headers=headers)
token = json.loads(token_response.text)

        # set up the api-client. 

api_client_config                   = Configuration()
api_client_config.host              = '127.0.0.1:8080'
api_client_config.api_key['X-Authorization']    = 'Bearer %s' % token['token']
api_client                      = ApiClient(api_client_config)

device_controller_api       = swagger_client.DeviceControllerApi(api_client=api_client)
device_api_controller_api       = swagger_client.DeviceApiControllerApi(api_client=api_client)
asset_controller_api        = swagger_client.AssetControllerApi(api_client=api_client)
entity_relation_controller_api  = swagger_client.EntityRelationControllerApi(api_client=api_client)

dv = Device(name="A",type="Sensor1")            
newdevice_res = device_controller_api.save_device_using_post(dv)
devicedata = tojson(newdevice_res) 

# Get the new device id.
deviceid = devicedata['id']['id']

newAsset = Asset(name="B",type="Home") 
R,res,header = asset_controller_api.save_asset_using_post_with_http_info(newAsset)          
asset_res = tojson(R)
assetid = asset_res['id']['id']

到目前为止,一切都很好.此代码创建资产和设备,然后我 有他们的ID.

So far so good. This code creates the asset and the device and I have their IDs.

现在,我正在尝试在它们之间建立关系:

Now, I am trying to create a relation between them:

newrelation = EntityRelation(_from=assetid,to=deviceid,type='Contains',type_group='COMMON')
print(newrelation)

关系是

{'_ from':u'd4472320-73ca-11e8-9297-b93b0883b039', 'additional_info':无, '至':u'4fe6c710-7312-11e8-9297-b93b0883b039', 'type':'包含', 'type_group':'COMMON'}

{'_from': u'd4472320-73ca-11e8-9297-b93b0883b039', 'additional_info': None, 'to': u'4fe6c710-7312-11e8-9297-b93b0883b039', 'type': 'Contains', 'type_group': 'COMMON'}

但是当我尝试保存它时:

But when I try to save it:

R,res,header = entity_relation_controller_api.save_relation_using_post(newrelation)
print(R)

我得到

Traceback (most recent call last):
  File "python/bin/LoadScenario.py", line 122, in <module>
    R,res,header = entity_relation_controller_api.save_relation_using_post(newrelation)
  File "/home/yehudaa/Projects/2018/GeshemBeito/python/tb_api_client/swagger_client/apis/entity_relation_controller_api.py", line 835, in save_relation_using_post
    (data) = self.save_relation_using_post_with_http_info(relation, **kwargs)
  File "/home/yehudaa/Projects/2018/GeshemBeito/python/tb_api_client/swagger_client/apis/entity_relation_controller_api.py", line 911, in save_relation_using_post_with_http_info
    collection_formats=collection_formats)
  File "/home/yehudaa/Projects/2018/GeshemBeito/python/tb_api_client/swagger_client/api_client.py", line 319, in call_api
    _return_http_data_only, collection_formats, _preload_content, _request_timeout)
  File "/home/yehudaa/Projects/2018/GeshemBeito/python/tb_api_client/swagger_client/api_client.py", line 153, in __call_api
    _request_timeout=_request_timeout)
  File "/home/yehudaa/Projects/2018/GeshemBeito/python/tb_api_client/swagger_client/api_client.py", line 362, in request
    body=body)
  File "/home/yehudaa/Projects/2018/GeshemBeito/python/tb_api_client/swagger_client/rest.py", line 262, in POST
    body=body)
  File "/home/yehudaa/Projects/2018/GeshemBeito/python/tb_api_client/swagger_client/rest.py", line 218, in request
    raise ApiException(http_resp=r)
tb_api_client.swagger_client.rest.ApiException: (400)
Reason: 
HTTP response headers: HTTPHeaderDict({'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Transfer-Encoding': 'chunked', 'Expires': '0', 'Connection': 'close', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate', 'Date': 'Tue, 19 Jun 2018 16:17:15 GMT', 'Content-Type': 'application/json;charset=UTF-8'})
HTTP response body: {"timestamp":1529425035915,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: com.fasterxml.jackson.databind.node.TextNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode (through reference chain: org.thingsboard.server.common.data.relation.EntityRelation[\"to\"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: com.fasterxml.jackson.databind.node.TextNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode (through reference chain: org.thingsboard.server.common.data.relation.EntityRelation[\"to\"])","path":"/api/relation"}

有什么想法吗?

推荐答案

关系主体的形式为:

{
  "from": {
    "id": "eb1a6dd0-73db-11e8-ba6f-5f195a167785",
    "entityType": "ASSET"
  },
  "type": "Contains",
  "to": {
    "entityType": "DEVICE",
    "id": "80131e70-745c-11e8-ba6f-5f195a167785"
  },
  "additionalInfo": null
}

我直接创建了JSON,而不是使用 EntityRelation 来创建JSON,

I created the JSON directly and not by using EntityRelation, and it works.

这篇关于使用swagger API和python在Thingsboard中添加实体关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆