device.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # -*- coding: utf-8 -*-
  2. # !/usr/bin/env python
  3. from __future__ import absolute_import, unicode_literals
  4. import base64
  5. from library.wechatpy.client.api.base import BaseWeChatAPI
  6. from library import to_text, to_binary
  7. class WeChatDevice(BaseWeChatAPI):
  8. API_BASE_URL = 'https://api.weixin.qq.com/device/'
  9. def send_message(self, device_type, device_id, user_id, content):
  10. """
  11. 主动发送消息给设备
  12. 详情请参考
  13. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-3
  14. :param device_type: 设备类型,目前为“公众账号原始ID”
  15. :param device_id: 设备ID
  16. :param user_id: 微信用户账号的openid
  17. :param content: 消息内容,BASE64编码
  18. :return: 返回的 JSON 数据包
  19. """
  20. content = to_text(base64.b64encode(to_binary(content)))
  21. return self._post(
  22. 'transmsg',
  23. data={
  24. 'device_type': device_type,
  25. 'device_id': device_id,
  26. 'open_id': user_id,
  27. 'content': content
  28. }
  29. )
  30. def send_status_message(self, device_type, device_id, user_id, msg_type, device_status):
  31. """
  32. 第三方主动发送设备状态消息给微信终端
  33. 详情请参考
  34. https://iot.weixin.qq.com/wiki/document-2_10.html
  35. :param device_type: 设备类型,目前为“公众账号原始ID”
  36. :param device_id: 设备ID
  37. :param user_id: 微信用户账号的openid
  38. :param msg_type: 消息类型:2--设备状态消息
  39. :param device_status: 设备状态:0--未连接, 1--已连接
  40. :return: 返回的 JSON 数据包
  41. """
  42. return self._post(
  43. 'transmsg',
  44. data={
  45. 'device_type': device_type,
  46. 'device_id': device_id,
  47. 'open_id': user_id,
  48. 'msg_type': msg_type,
  49. 'device_status': device_status,
  50. }
  51. )
  52. def create_qrcode(self, device_ids):
  53. """
  54. 获取设备二维码
  55. 详情请参考
  56. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-4
  57. :param device_ids: 设备id的列表
  58. :return: 返回的 JSON 数据包
  59. """
  60. return self._post(
  61. 'create_qrcode',
  62. data={
  63. 'device_num': len(device_ids),
  64. 'device_id_list': device_ids
  65. }
  66. )
  67. def get_qrcode_url(self, ticket, data=None):
  68. """
  69. 通过 ticket 换取二维码地址
  70. 详情请参考
  71. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-4
  72. :param ticket: 二维码 ticket
  73. :param data: 额外数据
  74. :return: 二维码地址
  75. """
  76. url = 'https://we.qq.com/d/{ticket}'.format(ticket=ticket)
  77. if data:
  78. if isinstance(data, (dict, tuple, list)):
  79. from six.moves.urllib import parse
  80. data = parse.urlencode(data)
  81. data = to_text(base64.b64encode(to_binary(data)))
  82. url = '{base}#{data}'.format(base=url, data=data)
  83. return url
  84. def bind(self, ticket, device_id, user_id):
  85. """
  86. 绑定设备
  87. 详情请参考
  88. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
  89. :param ticket: 绑定操作合法性的凭证(由微信后台生成,第三方H5通过客户端jsapi获得)
  90. :param device_id: 设备id
  91. :param user_id: 用户对应的openid
  92. :return: 返回的 JSON 数据包
  93. """
  94. return self._post(
  95. 'bind',
  96. data={
  97. 'ticket': ticket,
  98. 'device_id': device_id,
  99. 'openid': user_id
  100. }
  101. )
  102. def unbind(self, ticket, device_id, user_id):
  103. """
  104. 解绑设备
  105. 详情请参考
  106. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
  107. :param ticket: 绑定操作合法性的凭证(由微信后台生成,第三方H5通过客户端jsapi获得)
  108. :param device_id: 设备id
  109. :param user_id: 用户对应的openid
  110. :return: 返回的 JSON 数据包
  111. """
  112. return self._post(
  113. 'unbind',
  114. data={
  115. 'ticket': ticket,
  116. 'device_id': device_id,
  117. 'openid': user_id
  118. }
  119. )
  120. def compel_bind(self, device_id, user_id):
  121. """
  122. 强制绑定用户和设备
  123. 详情请参考
  124. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
  125. :param device_id: 设备id
  126. :param user_id: 用户对应的openid
  127. :return: 返回的 JSON 数据包
  128. """
  129. return self._post(
  130. 'compel_bind',
  131. data={
  132. 'device_id': device_id,
  133. 'openid': user_id
  134. }
  135. )
  136. force_bind = compel_bind
  137. def compel_unbind(self, device_id, user_id):
  138. """
  139. 强制解绑用户和设备
  140. 详情请参考
  141. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
  142. :param device_id: 设备id
  143. :param user_id: 用户对应的openid
  144. :return: 返回的 JSON 数据包
  145. """
  146. return self._post(
  147. 'compel_unbind',
  148. data={
  149. 'device_id': device_id,
  150. 'openid': user_id
  151. }
  152. )
  153. force_unbind = compel_unbind
  154. def get_stat(self, device_id):
  155. """
  156. 设备状态查询
  157. 详情请参考
  158. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-8
  159. :param device_id: 设备id
  160. :return: 返回的 JSON 数据包
  161. """
  162. return self._get(
  163. 'get_stat',
  164. params={'device_id': device_id}
  165. )
  166. def verify_qrcode(self, ticket):
  167. """
  168. 验证二维码
  169. 详情请参考
  170. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-9
  171. :param ticket: 设备二维码的ticket
  172. :return: 返回的 JSON 数据包
  173. """
  174. return self._post(
  175. 'verify_qrcode',
  176. data={'ticket': ticket}
  177. )
  178. def get_user_id(self, device_type, device_id):
  179. """
  180. 获取设备绑定openID
  181. 详情请参考
  182. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-11
  183. :param device_type: 设备类型,目前为“公众账号原始ID”
  184. :param device_id: 设备id
  185. :return: 返回的 JSON 数据包
  186. """
  187. return self._get(
  188. 'get_openid',
  189. params={
  190. 'device_type': device_type,
  191. 'device_id': device_id
  192. }
  193. )
  194. get_open_id = get_user_id
  195. def get_binded_devices(self, user_id):
  196. """
  197. 通过openid获取用户在当前devicetype下绑定的deviceid列表
  198. 详情请参考
  199. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-12
  200. :param user_id: 要查询的用户的openid
  201. :return: 返回的 JSON 数据包
  202. """
  203. return self._get(
  204. 'get_bind_device',
  205. params={'openid': user_id}
  206. )
  207. get_bind_device = get_binded_devices
  208. def get_qrcode(self, product_id=1):
  209. """
  210. 获取deviceid和二维码
  211. 详情请参考
  212. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-4
  213. :param product_id: 设备的产品编号
  214. :return: 返回的 JSON 数据包
  215. """
  216. if product_id == '1' or product_id == 1:
  217. params = None
  218. else:
  219. params = {'product_id': product_id}
  220. return self._get('getqrcode', params=params)
  221. def authorize(self, devices, op_type=1):
  222. """
  223. 设备授权
  224. 详情请参考
  225. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-5
  226. :param devices: 设备信息的列表
  227. :param op_type: 请求操作的类型,限定取值为:0:设备授权 1:设备更新
  228. :return: 返回的 JSON 数据包
  229. """
  230. return self._post(
  231. 'authorize_device',
  232. data={
  233. 'device_num': len(devices),
  234. 'device_list': devices,
  235. 'op_type': op_type
  236. }
  237. )