python实现阿里云物联网平台历史数据查询

物联网平台爱好者阅读

阿里云官方文档提供物联网平台访问历史数据的API,但是操作起来不方便,最多显示100行数据,利用一个下午的时间,自己写了一份代码,控制台数据查询数据时间段。权当让大家偷懒的工具,多余的不说,直接上代码。代码由两部分组成,时间戳日期互换模块和主函数代码:

时间戳日期互换模块 代码片.

import time'''
时间戳转换成日期格式
1)将时间时间戳转换成时间元组
2)通过strftime()将时间元组转换成格式化时间
'''
def timestamp_to_format(timestamp=None, format='%Y-%m-%d %H:%M:%S'):# try:if timestamp:time_tuple = time.localtime(timestamp)#print('time_tuple:', time_tuple)# print('type(time_tuple):',type(time_tuple))res = time.strftime(format, time_tuple)else:res = time.strftime(format)return res
'''
将格式化时间转换为时间戳
1)将日期转换成时间元组
2)转换成时间戳
'''
def timeformat_to_timestamp(timeformat=None,format = '%Y-%m-%d %H:%M:%S'):# try:if timeformat:time_tuple = time.strptime(timeformat,format)res = time.mktime(time_tuple) #转成时间戳else:res = time.time()        #获取当前时间戳return int(res)#print("timeformat_to_timestamp('2018-04-12 09:09:45')的时间戳:",timeformat_to_timestamp('2018-04-12 09:09:45'))if __name__ == '__main__':ac = timestamp_to_format(1615811334)# print(ac,'\t',at)print(ac)format = input("请输入时间:")at = timeformat_to_timestamp(format)print(at)# print('时间戳1234567转换成日期格式为:',timestamp_to_format(1234567)#以下为主函数部分
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkiot.request.v20180120.QueryDevicePropertiesDataRequest import QueryDevicePropertiesDataRequest
import json
import time_transform
import time
"""
{"NextValid":true,"NextTime":1616414594866,"RequestId":"4F83BDAF-A66C-4B53-828B-BC721C4B2209","PropertyDataInfos":{"PropertyDataInfo":[{"Identifier":"Distance","List":{"PropertyInfo":[{"Value":"65.56","Time":1616083267025},{"Value":"65.10","Time":1616083335520},{"Value":"65.10","Time":1616083404606},{"Value":"65.45","Time":1616083473543},{"Value":"67.92","Time":1616086865662},{"Value":"69.39","Time":1616086934633},{"Value":"70.33","Time":1616087003654},{"Value":"69.55","Time":1616087072515},{"Value":"70.13","Time":1616090465884},]}}]},"Code":"","Success":true
}"""def check(StartTime, EedTime):#阿里云控制台可以查到AccessKey ID和AccessKey Secretclient = AcsClient('AccessKey ID','AccessKey Secret', 'cn-shanghai')request = QueryDevicePropertiesDataRequest()request.set_accept_format("json")request.set_DeviceName("YAN76-5")""" print("起始时间应早于终止时间,时间格式:xxxx-xx-xx xx:xx:xx")format = input("请输入起始时间:")ad = time_transform.timeformat_to_timestamp(format)*1000print(ad)request.set_StartTime(ad)# request.set_EndTime(1618230534000)# print("起始时间应早于终止时间")format = input("请输入终止时间:")ap = time_transform.timeformat_to_timestamp(format)*1000print(ap)request.set_EndTime(ap) """request.set_StartTime(StartTime)request.set_EndTime(EedTime)request.set_Asc("1")request.set_PageSize("10")request.set_Identifiers(["Distance"])# request.set_Identifiers(["Distance","Distance1"])request.set_ProductKey("a1gMuOnqrr7")# request.set_IotId("b01a617629f73a72d8867f9232633ae0")response = client.do_action_with_exception(request)# python2:  print(response)# print(response[0], response[1], response[2], response[3],# response[4], response[5], response[6], response[7])# print('\n')byte = responsestr = byte.decode()# print(type(str))# print(len(str))# print(str)# print(type(str))# print(str[10], str[11], str[12], str[13], str[14], str[15])# print('\n')json_str = json.loads(str)# print(json_str)# print(type(json_str))# print(json_str['PropertyDataInfos'])# print('\n')# print(type(json_str),print(json_str['NextValid']))if json_str['NextValid'] == True:# json_str['NextTime'] = time_transform.timestamp_to_format((json_str['NextTime'])/1000)# print(time_transform.timestamp_to_format(#    (json_str['NextTime'])/1000))# print(json_str['NextTime'])a = json_str['PropertyDataInfos']# print('\n')# print(a['PropertyDataInfo'], type(a['PropertyDataInfo']))# print('\n')b = a['PropertyDataInfo']# print(b[0], type(b[0]))# print('\n')c = b[0]# print(c['List'], type(c['List']))d = c['List']# print('\n')# print(d['PropertyInfo'], type(d['PropertyInfo']))e = d['PropertyInfo']# f=e[0]# g=time_transform.timestamp_to_format((f['Time'])/1000)# print(g)i = 0while i < 10:# print(i, '\t', e[i], time_transform.timestamp_to_format(print(e[i], time_transform.timestamp_to_format((e[i]['Time'])/1000))i += 1# print('\n')return json_str['NextTime']if json_str['NextValid'] == False:print("查询结束")return Noneif __name__ == '__main__':print("起始时间应早于终止时间,时间格式:xxxx-xx-xx xx:xx:xx")format = input("请输入起始时间:")ad = time_transform.timeformat_to_timestamp(format)*1000#ad = 1617638400000print(ad)# request.set_StartTime(ad)# request.set_EndTime(1618230534000)# print("起始时间应早于终止时间")format = input("请输入终止时间:")ap = time_transform.timeformat_to_timestamp(format)*1000#ap = 1618070400000print(ap)# request.set_EndTime(ap)ac = ap-adprint(ac)while ac > 600000:an = check(ad, ap)# time.sleep(0.5)# print(an)ad = anif ad == None:print("执行完毕")breakinput("等待指令")# return ad

python实现阿里云物联网平台历史数据查询相关推荐

  1. 利用PYTHON连接阿里云物联网平台

    语言:python 3.7 环境:windows 10 实例:公共实例(免费) 阿里云的官方文档只有C语言和Linux环境,因此自我探索出利用PYTHON连接阿里云物联网平台的方法和步骤. 概述 - ...

  2. 使用Python连接阿里云物联网

    最近在做物联网相关的项目,想着用python模拟一个mqtt客户端,向云端发送数据以及从云端接收数据. 参考文章 <mqtt实战-Python接入阿里云物联网平台> 1 安装阿里云物联网平 ...

  3. Python模拟智能开关设备MQTT接入阿里云物联网平台 - PyCharm paho.mqtt

    概要 Python 使用 paho.mqtt 库,利用阿里云物联网平台的设备证书:productKey.deviceName.deviceSecret,自动合成 userName.passWord.以 ...

  4. 阿里云物联网平台python开发手册_阿里云物联网平台体验(树莓派+Python篇)

    虽然对阿里云物联网平台比较熟悉了,从一开始就有幸参与了飞凤平台(Link Develop 一站式开发平台的前身)的一些偏硬件接入的工作.但是同时也见证了阿里云物联网团队从几十人到数百人的迅速扩张,其物 ...

  5. 阿里云物联网平台体验(树莓派+Python篇)

    阿里云物联网平台体验(树莓派+Python篇) 虽然对阿里云物联网平台比较熟悉了,从一开始就有幸参与了飞凤平台(Link Develop 一站式开发平台的前身)的一些偏硬件接入的工作.但是同时也见证了 ...

  6. 阿里云物联网平台数据解析(python)

    阿里云物联网平台数据解析(python) DTU上传设备返回报文消息,通过数据解析后显示各功能数值,这里以智能电表DLT645规约为例进行解析 因为是做光伏的,所以对电表的需求比较多,之前查找了好多文 ...

  7. 阿里云物联网平台体验(树莓派+Python篇) 1

    2018年12月04日 11:35:08 叶帆 阅读数:349 虽然对阿里云物联网平台比较熟悉了,从一开始就有幸参与了飞凤平台(Link Develop 一站式开发平台的前身)的一些偏硬件接入的工作. ...

  8. 树莓派CM4 Sensing(包含485接口)+python+继电器+水质仪+阿里云物联网平台ito实现实时检测水质并上传数据到阿里云ito和远程控制灯光

    一.硬件设备 1.树莓派CM4 Sensing(包含485接口) 2.带485接口的继电器 继电器的mdobus指令 3.水质仪 水质仪线路需要改造一下 下图中1是水质仪自带的线,2是给水质仪提供电源 ...

  9. 使用ESP8266(基于官方SDK)接入阿里云物联网平台

    引言 作为物联网开发者,ESP8266应该一点都不陌生了.只需十几块钱淘宝一个小开发板,就可以连上Wi-Fi接入互联网,尽情享受从手机端或Web端控制设备的乐趣.ESP8266接入Wi-Fi是没问题, ...

最新文章

  1. 堆和栈的差别(转过无数次的文章)
  2. android universal image loader 缓冲原理详解
  3. CodeForces 703C Chris and Road
  4. WebApp匯入資料之From Excel
  5. nvidia控制面板点了没反应win7_win7系统Nvidia控制面板怎么设置?
  6. iOS 11 适配集锦
  7. Word邮件合并技巧四则
  8. C++ 工程实践(3):采用有利于版本管理的代码格式
  9. 计算机网络自顶向下方法 第四章 作业习题答案
  10. 上海链家网二手房成交数据爬取
  11. nacos注册服务地址为内网地址解决方法
  12. 游戏对战平台原理终结篇(转自)
  13. 焦作大学计算机专业分数线,焦作大学历年分数线 2021焦作大学录取分数线
  14. 文件服务器mfs,分布式文件系统MFS(moosefs)实现存储共享
  15. SecureCRT连接服务器报错Failed to open the host key database file解决方法
  16. html如何使mp4成为背景,如何让MP4 video视频背景色变成透明?
  17. 逻辑漏洞之越权、支付漏洞
  18. 苹果怎么设置铃声?设置自己喜欢的歌曲作为铃声,一招搞定!
  19. 5G时代金融服务如何升级?网易云信助力银行数字化建设
  20. 低格格式化过程及与高级格式化的区别

热门文章

  1. Motorcad 外转子式42极36槽 永磁同步电机,直流无刷电机设计
  2. Hive 核心知识点灵魂 16 问(转)
  3. 【InstallShield】入门介绍
  4. soxr重采样实现变速
  5. 如何抹掉 Mac 并重新安装 macOS
  6. 我的世界粘土服务器怎么注册a,我的世界粘土服务器怎么进 | 手游网游页游攻略大全...
  7. 五十六、Fluent空化模型理论
  8. 基于vlc的ActiveX流媒体播放器的二次开发流程
  9. Pi 3B+安装官方推荐系统
  10. WEKA使用(基础配置+垃圾邮件过滤+聚类分析+关联挖掘)