物联网数据分析(IoTA)-资产属性管理
引导式阅读
Python
物联网数据分析(IoTA)-资产属性管理
作者
C***
上架时间
2023-11-13 02:11:21

0. 版本说明

本示例基于华为云Python SDK开发

1. 简介

基于华为云Python SDK, 与资产属性值的相关操作

2. 开发前准备

已注册华为云, 并完成实名认证; 已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK); Python3及其以上版本

3. 安装SDK

pip install huaweicloudsdkiotanalytics

4. 开始使用

4.1 导入依赖模块

# 用户身份认证 from huaweicloudsdkcore.auth.credentials import BasicCredentials # 请求异常类 from huaweicloudsdkcore.exceptions.exceptions import ServiceResponseException # 导入相应产品的 {Service}Client from huaweicloudsdkiotanalytics.v1.iotanalytics_client import IoTAnalyticsClient # 导入待请求接口的类 from huaweicloudsdkcore.http.http_config import HttpConfig from huaweicloudsdkiotanalytics.v1.model.dt_transform_metrics import DTTransformMetrics from huaweicloudsdkiotanalytics.v1.model.get_metrics_value import GetMetricsValue from huaweicloudsdkiotanalytics.v1.model.last_asset_property_value_request import LastAssetPropertyValueRequest from huaweicloudsdkiotanalytics.v1.model.metric_input import MetricInput from huaweicloudsdkiotanalytics.v1.model.raw_request import RawRequest from huaweicloudsdkiotanalytics.v1.model.show_last_property_value_request import ShowLastPropertyValueRequest from huaweicloudsdkiotanalytics.v1.model.show_metric_value_request import ShowMetricValueRequest from huaweicloudsdkiotanalytics.v1.model.show_property_raw_value_request import ShowPropertyRawValueRequest from huaweicloudsdkiotanalytics.v1.model.time_span_dt import TimeSpanDT from huaweicloudsdkiotanalytics.v1.model.transform_metrics_request import TransformMetricsRequest import os

4.2.1 配置客户端属性

config = HttpConfig.get_default_config() config.ignore_ssl_verification = True config.proxy_protocol = "http"

4.2.2 初始化认证信息

auth = BasicCredentials( ak=your_ak, sk=your_sk, project_id=project_id )

4.2.3 初始化客户端

io_t_analytics_client = IoTAnalyticsClient.new_builder() \ .with_http_config(config=config) \ .with_credentials(credentials=auth) \ .with_endpoint(endpoint=endpoint) \ .build()

5. 示例代码

使用如下代码对资产属性值进行相关查询操作,调用前请根据实际情况替换如下变量: 通用参数: {your ak string}、{your sk string} , 以及各个方法中包含的参数。

package com.huawei.iotanalytics; class PropertyValueApplication: def __init__(self): pass @staticmethod def main(args): # 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; # 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 ak = os.environ["HUAWEICLOUD_SDK_AK"] sk = os.environ["HUAWEICLOUD_SDK_SK"] endpoint = "{your endpoint string}" project_id = "{your project id}" # 配置客户端属性 config = HttpConfig.get_default_config() # 根据需要配置是否跳过SSL证书验证 config.ignore_ssl_verification = True config.proxy_protocol = "http" # 创建认证 auth = BasicCredentials( ak=ak, sk=sk, project_id=project_id ) # 创建IoTAnalyticsClient实例并初始化 io_t_analytics_client = IoTAnalyticsClient.new_builder() \ .with_http_config(config=config) \ .with_credentials(credentials=auth) \ .with_endpoint(endpoint=endpoint) \ .build() # 通过SDK,调用获取资产属性最新值接口 PropertyValueApplication.show_last_property_value(io_t_analytics_client) # 通过SDK,调用获取资产属性聚合值接口 PropertyValueApplication.show_metric_value(io_t_analytics_client) # 通过SDK,获取资产属性历史值接口 PropertyValueApplication.show_property_raw_value(io_t_analytics_client) @staticmethod def show_last_property_value(client): """ 获取资产属性最新值 """ try: show_last_property_value_request = ShowLastPropertyValueRequest() last_asset_property_value_request = LastAssetPropertyValueRequest() show_last_property_value_request.body = last_asset_property_value_request show_last_property_value_request.asset_id = "your assetId" show_last_property_value_response = client.show_last_property_value(show_last_property_value_request) # 输出json格式的字符串响应 print(show_last_property_value_response) except ServiceResponseException as e: print("HttpStatusCode: "+e.status_code) print("RequestId: "+e.request_id) print("ErrorCode: "+e.error_code) print("ErrorMsg: "+e.error_msg) @staticmethod def show_metric_value(client): """ 获取资产属性聚合值 """ try: # 构造请求参数 show_metric_value_request = PropertyValueApplication.__get_show_metric_value_request() show_metric_value_response = client.show_metric_value(show_metric_value_request) # 输出json格式的字符串响应 print(show_metric_value_response) except ServiceResponseException as e: print("HttpStatusCode: "+e.status_code) print("RequestId: "+e.request_id) print("ErrorCode: "+e.error_code) print("ErrorMsg: "+e.error_msg) @staticmethod def show_property_raw_value(client): """ 获取资产属性历史值 """ try: # 构造请求参数 show_property_raw_value_request = ShowPropertyRawValueRequest() show_property_raw_value_request.asset_id = "your assetId" raw_request = RawRequest() raw_request.limit = 2000 property_names = [] property_names.append("p2") raw_request.property_names = property_names time_span = TimeSpanDT() # 设置起止时间,UTC格式字符串 time_span._from = "2020-11-21T10:45:32Z" time_span.to = "2020-11-27T10:45:32Z" raw_request.time_span = time_span show_property_raw_value_request.body = raw_request show_property_raw_value_response = client.show_property_raw_value(show_property_raw_value_request) # 输出json格式的字符串响应 print(show_property_raw_value_response) except ServiceResponseException as e: print("HttpStatusCode: "+e.status_code) print("RequestId: "+e.request_id) print("ErrorCode: "+e.error_code) print("ErrorMsg: "+e.error_msg) @staticmethod def __get_show_metric_value_request(): """ 构造获取资产属性聚合值请求参数 """ show_metric_value_request = ShowMetricValueRequest() show_metric_value_request.asset_id = "your assetId" get_metrics_value = GetMetricsValue() # 设置查询类型,经过转换计算的序列值(transform)、经过聚合计算的序列值(aggregate) get_metrics_value.type = "transform" transform_metrics_request = TransformMetricsRequest() transform_metrics_request.limit = 50 time_span = TimeSpanDT() # 设置起止时间,UTC格式字符串 time_span._from = "2020-12-25T00:00:00Z" time_span.to = "2020-12-27T00:00:00Z" transform_metrics_request.time_span = time_span metrics = [] dt_transform_metrics = DTTransformMetrics() dt_transform_metrics.expression = "floor(y)" dt_transform_metrics.metric_name = "y2" inputs = [] metric_input = MetricInput() metric_input.name = "y" metric_input.property_name = "p2" inputs.append(metric_input) dt_transform_metrics.inputs = inputs metrics.append(dt_transform_metrics) transform_metrics_request.metrics = metrics get_metrics_value.transform = transform_metrics_request show_metric_value_request.body = get_metrics_value return show_metric_value_request

6. 参考

详细了解数据分析,请参考IoT数据分析

更多关于SDK的调用,请参考华为云开发者 Python 软件开发工具包(Python SDK)

7. 修订记录

发布日期 文档版本 修订说明
2021-12-22 1.0 文档首次发布