python開發華為雲應用側進行裝置接入

鴨鴨打瞌睡發表於2020-09-25

用python接入華為雲,最自己的IOT裝置進行監督

這算是做一個應用端吧,把之前沒有寫好的補上,華為家的名字也是奇奇怪怪,反正就是自己的python連線華為雲讀取裝置
儘量寫的不要太難,就是跟著一步步做就好啦 分為:得到產品,獲取資料,下發資料

發現一個除錯小工具—>好像只能除錯token
新增連結描述
在這裡插入圖片描述

得到產品

建立token

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)

相關文章