这算是做一个应用端吧,把之前没有写好的补上,华为家的名字也是奇奇怪怪,反正就是自己的python连接华为云读取设备
尽量写的不要太难,就是跟着一步步做就好啦 分为:得到产品,获取数据,下发数据
token就是当一个网站有了你的token之后,不用post进行请求,直接get就好啦
找到我们需要的------>进这个网页华为token认证
"username”即IAM用户名、“password”即登录华为云密码、“domainname”即账号名,“projectname”项目
密码应该是登录华为云的密码
import requests
import json
url = "https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens"
HEADERS = { "Content-Type": "application/json;charset=UTF-8","User-Agent": "API Explorer","X-Auth-Token": "***", }
FormData = {
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"domain": {
"name": "hw05803451"
},
"name": "hw05803451",
"password": "登录华为的密码"
}
}
}
}
}
res = requests.post(url=url,data=json.dumps(FormData),headers=HEADERS)
print(res.text)
token成功返回
我日了,调试了这么久,发现直接去设备影子查询,就可以查所有的数据,也不知道是不是数据接收是因为要数据时时上报才能开启数据接收的
没关系,我们看历史的最新数据一样可以达到要求
得到ak 和sk
上面的网站拿到
调试着调试着,发现好像sdk原来是这样用的,改sdk算了,反正不是有接口嘛,哈哈哈哈,拿着就行了
根据可以取出数组的每一个值,来读取参数,这个是都有引号的版本,但是华为发过来,第一个数据没有引号
# coding: utf-8
### 查询所有信息
import re
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcore.http.http_config import HttpConfig
from huaweicloudsdkiotda.v5 import *
def find_device_message():
ak = "4MSDTDVTPSTEDCYFDXOS"
sk = "kLluTIa57HabASGp4bxrfytKUwCSWbbYlCLHoYm1"
endpoint = "https://iotda.cn-north-4.myhuaweicloud.com"
project_id = "08d0cfa57b80f5d32f54c019654ee2eb"
config = HttpConfig.get_default_config()
config.timeout = 3
credentials = BasicCredentials(ak, sk, project_id)
client = IoTDAClient.new_builder(IoTDAClient) \
.with_http_config(config) \
.with_credentials(credentials) \
.with_endpoint(endpoint) \
.build()
try:
request = ShowDeviceShadowRequest()
request.device_id = "5f26687704feea02bac7dd35_11111111"
response = client.show_device_shadow(request)
print("ok\n")
print(response)
return(str(response))
except exceptions.ClientRequestException as e:
print(e.status_code)
print("************")
print(e.request_id)
print("************")
print(e.error_code)
print("************")
print(e.error_msg)
### 如果都有引号,就都能返回
def dis_str(content):
str = re.findall(r"(\n[\s\S]*\n)", content, re.DOTALL)
# str1=re.findall(r"\n([\s\S]*)\n",str[0])
str1 = str[0].replace('\n', '')
str2 = str1.replace(' ', '')
str3 = re.findall(r"'properties':{(.+?)}},", str2)
str4 = re.findall(r"'(.+?)'", str3[0])
str5 = re.findall(r"'(.+?)'", str3[1])
str6 = re.findall(r"[0-9][0-9]", str3[1])
str4.append(str5[0])
str4.append(str6[0])
print(str4[2])
return str4
if __name__ == '__main__':
contnet = find_device_message()
dis_str(contnet)
我们不用搞得这么复杂,只要有东西发过去,我们就成功了
我用的是createmessage用这个调试
# coding: utf-8
#### 命令下发
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcore.http.http_config import HttpConfig
from huaweicloudsdkiotda.v5 import *
if __name__ == "__main__":
ak = "4MSDTDVTPSTEDCYFDXOS"
sk = "kLluTIa57HabASGp4bxrfytKUwCSWbbYlCLHoYm1"
endpoint = "https://iotda.cn-north-4.myhuaweicloud.com"
project_id = "08d0cfa57b80f5d32f54c019654ee2eb"
config = HttpConfig.get_default_config()
config.timeout = 3
credentials = BasicCredentials(ak, sk, project_id)
client = IoTDAClient.new_builder(IoTDAClient) \
.with_http_config(config) \
.with_credentials(credentials) \
.with_endpoint(endpoint) \
.build()
try:
request = CreateMessageRequest()
request.device_id = "5f26687704feea02bac7dd35_11111111"
request.body = DeviceMessageRequest(
message="hello",
name="wodename"
)
response = client.create_message(request)
print(response)
except exceptions.ClientRequestException as e:
print(e.status_code)
print(e.request_id)
print(e.error_code)
print(e.error_msg)