您好,登錄后才能下訂單哦!
Python中怎么獲取OneNet數(shù)據(jù),相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
使用python腳本文件對(duì)OneNet服務(wù)器數(shù)據(jù)進(jìn)行上傳、獲取的操作
上傳數(shù)據(jù)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/2/24 23:01 # @Author : LiShan # @Email : lishan_1997@126.com # @File : OneNet_Post.py # @Note : https://blog.csdn.net/lishan132/article/details/114044902 import urllib.request import json # 設(shè)備ID、Key deviceId = "591972034" APIKey = "ROsfgvwqy2jxn2x93TdDCCbFmL8=" # 上傳函數(shù) def OneNet_post_data(info): url = "https://api.heclouds.com/devices/" + deviceId + '/datapoints' streams = [] for index, element in enumerate(info): streams.append({"id": element[0], "datapoints": [{"value": element[1]}]}) values = {"datastreams": streams} data = json.dumps(values).encode("utf-8") request = urllib.request.Request(url, data) request.add_header('api-key', APIKey) request.get_method = lambda: 'POST' request = urllib.request.urlopen(request) print(json.loads(request.read())) if __name__ == '__main__': upload_data = [ ["road1", 10], ["road2", 20], ["road3", 30], ["road4", 40], ["road5", 50], ["road6", 60], ["road7", 70], ["road8", 80], ["road9", 90], ["road10", 100], ["road11", 110], ["road12", 120], ] OneNet_post_data(upload_data)
獲取數(shù)據(jù)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/2/24 23:01 # @Author : LiShan # @Email : lishan_1997@126.com # @File : OneNet_Get.py # @Note : https://blog.csdn.net/lishan132/article/details/114044902 import urllib.request import json # 設(shè)備ID、Key deviceId = "591972034" APIKey = "ROsfgvwqy2jxn2x93TdDCCbFmL8=" # 獲取函數(shù) def OneNet_get_data(): url = "http://api.heclouds.com/devices/" + deviceId + "/datastreams" request = urllib.request.Request(url) request.add_header('api-key', APIKey) request.get_method = lambda: 'GET' request = urllib.request.urlopen(request) r = json.loads(request.read()) data = r.pop('data') print(r) return data if __name__ == '__main__': load_data = OneNet_get_data() print('參數(shù)' + '\t\t\t\t\t' + '更新時(shí)間' + '\t\t\t\t\t\t' + '數(shù)值') for index, element in enumerate(load_data): a = str(element.get('update_at', '')) b = str(element.get('current_value', '')) if a != "" and b != "": print(str(element['id']) + '\t\t\t\t' + a + '\t\t\t' + b)
整合為一個(gè)文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # pip install prettytable import urllib.request as req import json # 設(shè)備ID、Key deviceId = "591972034" APIKey = "ROsfgvwqy2jxn2x93TdDCCbFmL8=" # 上傳函數(shù) def OneNet_post_data(info): url = "https://api.heclouds.com/devices/" + info[0] + '/datapoints' headers = {'api-key': info[1]} streams = [] for i, e in enumerate(info[2]): streams.append({"id": e[0], "datapoints": [{"value": e[1]}]}) data = json.dumps({"datastreams": streams}).encode("utf-8") request = json.loads((req.urlopen(req.Request(url, data, headers=headers))).read()) print(request) # 獲取函數(shù) def OneNet_get_data(info): url = "http://api.heclouds.com/devices/" + info[0] + "/datastreams" headers = {'api-key': info[1]} request = json.loads((req.urlopen(req.Request(url, headers=headers))).read()) data = request.pop('data') print(request) return data if __name__ == '__main__': # 準(zhǔn)備待上傳數(shù)據(jù) upload_data = [ ["road1", 10], ["road2", 20], ["road3", 30], ["road4", 40], ["road5", 50], ["road6", 60], ["road7", 70], ["road8", 80], ["road9", 90], ["road10", 100], ["road11", 110], ["road12", 120], ] # 上傳數(shù)據(jù) OneNet_post_data([deviceId, APIKey, upload_data]) # 獲取數(shù)據(jù) get_data = OneNet_get_data([deviceId, APIKey]) # noinspection PyBroadException try: # 使用表格美化顯示數(shù)據(jù) import prettytable as pt tb = pt.PrettyTable() tb.field_names = ["id", "update_at", "current_value"] for index, element in enumerate(get_data): ID = str(element.get('id', '')) update_at = str(element.get('update_at', '')) current_value = str(element.get('current_value', '')) tb.add_row([ID, update_at, current_value]) print(tb) except Exception: # 直接顯示數(shù)據(jù) for index, element in enumerate(get_data): ID = str(element.get('id', '')) update_at = str(element.get('update_at', '')) current_value = str(element.get('current_value', '')) print(ID, update_at, current_value)
看完上述內(nèi)容,你們掌握Python中怎么獲取OneNet數(shù)據(jù)的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。