device.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import base64
  4. import urllib
  5. from wechatpy.client.api.base import BaseWeChatAPI
  6. from wechatpy.utils import to_binary, to_text
  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 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. data = urllib.urlencode(data)
  80. data = to_text(base64.b64encode(to_binary(data)))
  81. url = '{base}#{data}'.format(base=url, data=data)
  82. return url
  83. def bind(self, ticket, device_id, user_id):
  84. """
  85. 绑定设备
  86. 详情请参考
  87. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
  88. :param ticket: 绑定操作合法性的凭证(由微信后台生成,第三方H5通过客户端jsapi获得)
  89. :param device_id: 设备id
  90. :param user_id: 用户对应的openid
  91. :return: 返回的 JSON 数据包
  92. """
  93. return self._post(
  94. 'bind',
  95. data={
  96. 'ticket': ticket,
  97. 'device_id': device_id,
  98. 'openid': user_id
  99. }
  100. )
  101. def unbind(self, ticket, device_id, user_id):
  102. """
  103. 解绑设备
  104. 详情请参考
  105. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
  106. :param ticket: 绑定操作合法性的凭证(由微信后台生成,第三方H5通过客户端jsapi获得)
  107. :param device_id: 设备id
  108. :param user_id: 用户对应的openid
  109. :return: 返回的 JSON 数据包
  110. """
  111. return self._post(
  112. 'unbind',
  113. data={
  114. 'ticket': ticket,
  115. 'device_id': device_id,
  116. 'openid': user_id
  117. }
  118. )
  119. def compel_bind(self, device_id, user_id):
  120. """
  121. 强制绑定用户和设备
  122. 详情请参考
  123. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
  124. :param device_id: 设备id
  125. :param user_id: 用户对应的openid
  126. :return: 返回的 JSON 数据包
  127. """
  128. return self._post(
  129. 'compel_bind',
  130. data={
  131. 'device_id': device_id,
  132. 'openid': user_id
  133. }
  134. )
  135. force_bind = compel_bind
  136. def compel_unbind(self, device_id, user_id):
  137. """
  138. 强制解绑用户和设备
  139. 详情请参考
  140. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-7
  141. :param device_id: 设备id
  142. :param user_id: 用户对应的openid
  143. :return: 返回的 JSON 数据包
  144. """
  145. return self._post(
  146. 'compel_unbind',
  147. data={
  148. 'device_id': device_id,
  149. 'openid': user_id
  150. }
  151. )
  152. force_unbind = compel_unbind
  153. def get_stat(self, device_id):
  154. """
  155. 设备状态查询
  156. 详情请参考
  157. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-8
  158. :param device_id: 设备id
  159. :return: 返回的 JSON 数据包
  160. """
  161. return self._get(
  162. 'get_stat',
  163. params={'device_id': device_id}
  164. )
  165. def verify_qrcode(self, ticket):
  166. """
  167. 验证二维码
  168. 详情请参考
  169. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-9
  170. :param ticket: 设备二维码的ticket
  171. :return: 返回的 JSON 数据包
  172. """
  173. return self._post(
  174. 'verify_qrcode',
  175. data={'ticket': ticket}
  176. )
  177. def get_user_id(self, device_type, device_id):
  178. """
  179. 获取设备绑定openID
  180. 详情请参考
  181. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-11
  182. :param device_type: 设备类型,目前为“公众账号原始ID”
  183. :param device_id: 设备id
  184. :return: 返回的 JSON 数据包
  185. """
  186. return self._get(
  187. 'get_openid',
  188. params={
  189. 'device_type': device_type,
  190. 'device_id': device_id
  191. }
  192. )
  193. get_open_id = get_user_id
  194. def get_binded_devices(self, user_id):
  195. """
  196. 通过openid获取用户在当前devicetype下绑定的deviceid列表
  197. 详情请参考
  198. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-12
  199. :param user_id: 要查询的用户的openid
  200. :return: 返回的 JSON 数据包
  201. """
  202. return self._get(
  203. 'get_bind_device',
  204. params={'openid': user_id}
  205. )
  206. get_bind_device = get_binded_devices
  207. def get_qrcode(self, product_id=1):
  208. """
  209. 获取deviceid和二维码
  210. 详情请参考
  211. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-4
  212. :param product_id: 设备的产品编号
  213. :return: 返回的 JSON 数据包
  214. """
  215. if product_id == '1' or product_id == 1:
  216. params = None
  217. else:
  218. params = {'product_id': product_id}
  219. return self._get('getqrcode', params=params)
  220. def authorize(self, devices, op_type=1):
  221. """
  222. 设备授权
  223. 详情请参考
  224. https://iot.weixin.qq.com/wiki/new/index.html?page=3-4-5
  225. :param devices: 设备信息的列表
  226. :param op_type: 请求操作的类型,限定取值为:0:设备授权 1:设备更新
  227. :return: 返回的 JSON 数据包
  228. """
  229. return self._post(
  230. 'authorize_device',
  231. data={
  232. 'device_num': len(devices),
  233. 'device_list': devices,
  234. 'op_type': op_type
  235. }
  236. )