component.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. !function (n) {
  2. "use strict";
  3. function d(n, t) {
  4. var r = (65535 & n) + (65535 & t);
  5. return (n >> 16) + (t >> 16) + (r >> 16) << 16 | 65535 & r
  6. }
  7. function f(n, t, r, e, o, u) {
  8. return d((u = d(d(t, n), d(e, u))) << o | u >>> 32 - o, r)
  9. }
  10. function l(n, t, r, e, o, u, c) {
  11. return f(t & r | ~t & e, n, t, o, u, c)
  12. }
  13. function g(n, t, r, e, o, u, c) {
  14. return f(t & e | r & ~e, n, t, o, u, c)
  15. }
  16. function v(n, t, r, e, o, u, c) {
  17. return f(t ^ r ^ e, n, t, o, u, c)
  18. }
  19. function m(n, t, r, e, o, u, c) {
  20. return f(r ^ (t | ~e), n, t, o, u, c)
  21. }
  22. function c(n, t) {
  23. var r, e, o, u;
  24. n[t >> 5] |= 128 << t % 32, n[14 + (t + 64 >>> 9 << 4)] = t;
  25. for (var c = 1732584193, f = -271733879, i = -1732584194, a = 271733878, h = 0; h < n.length; h += 16) c = l(r = c, e = f, o = i, u = a, n[h], 7, -680876936), a = l(a, c, f, i, n[h + 1], 12, -389564586), i = l(i, a, c, f, n[h + 2], 17, 606105819), f = l(f, i, a, c, n[h + 3], 22, -1044525330), c = l(c, f, i, a, n[h + 4], 7, -176418897), a = l(a, c, f, i, n[h + 5], 12, 1200080426), i = l(i, a, c, f, n[h + 6], 17, -1473231341), f = l(f, i, a, c, n[h + 7], 22, -45705983), c = l(c, f, i, a, n[h + 8], 7, 1770035416), a = l(a, c, f, i, n[h + 9], 12, -1958414417), i = l(i, a, c, f, n[h + 10], 17, -42063), f = l(f, i, a, c, n[h + 11], 22, -1990404162), c = l(c, f, i, a, n[h + 12], 7, 1804603682), a = l(a, c, f, i, n[h + 13], 12, -40341101), i = l(i, a, c, f, n[h + 14], 17, -1502002290), c = g(c, f = l(f, i, a, c, n[h + 15], 22, 1236535329), i, a, n[h + 1], 5, -165796510), a = g(a, c, f, i, n[h + 6], 9, -1069501632), i = g(i, a, c, f, n[h + 11], 14, 643717713), f = g(f, i, a, c, n[h], 20, -373897302), c = g(c, f, i, a, n[h + 5], 5, -701558691), a = g(a, c, f, i, n[h + 10], 9, 38016083), i = g(i, a, c, f, n[h + 15], 14, -660478335), f = g(f, i, a, c, n[h + 4], 20, -405537848), c = g(c, f, i, a, n[h + 9], 5, 568446438), a = g(a, c, f, i, n[h + 14], 9, -1019803690), i = g(i, a, c, f, n[h + 3], 14, -187363961), f = g(f, i, a, c, n[h + 8], 20, 1163531501), c = g(c, f, i, a, n[h + 13], 5, -1444681467), a = g(a, c, f, i, n[h + 2], 9, -51403784), i = g(i, a, c, f, n[h + 7], 14, 1735328473), c = v(c, f = g(f, i, a, c, n[h + 12], 20, -1926607734), i, a, n[h + 5], 4, -378558), a = v(a, c, f, i, n[h + 8], 11, -2022574463), i = v(i, a, c, f, n[h + 11], 16, 1839030562), f = v(f, i, a, c, n[h + 14], 23, -35309556), c = v(c, f, i, a, n[h + 1], 4, -1530992060), a = v(a, c, f, i, n[h + 4], 11, 1272893353), i = v(i, a, c, f, n[h + 7], 16, -155497632), f = v(f, i, a, c, n[h + 10], 23, -1094730640), c = v(c, f, i, a, n[h + 13], 4, 681279174), a = v(a, c, f, i, n[h], 11, -358537222), i = v(i, a, c, f, n[h + 3], 16, -722521979), f = v(f, i, a, c, n[h + 6], 23, 76029189), c = v(c, f, i, a, n[h + 9], 4, -640364487), a = v(a, c, f, i, n[h + 12], 11, -421815835), i = v(i, a, c, f, n[h + 15], 16, 530742520), c = m(c, f = v(f, i, a, c, n[h + 2], 23, -995338651), i, a, n[h], 6, -198630844), a = m(a, c, f, i, n[h + 7], 10, 1126891415), i = m(i, a, c, f, n[h + 14], 15, -1416354905), f = m(f, i, a, c, n[h + 5], 21, -57434055), c = m(c, f, i, a, n[h + 12], 6, 1700485571), a = m(a, c, f, i, n[h + 3], 10, -1894986606), i = m(i, a, c, f, n[h + 10], 15, -1051523), f = m(f, i, a, c, n[h + 1], 21, -2054922799), c = m(c, f, i, a, n[h + 8], 6, 1873313359), a = m(a, c, f, i, n[h + 15], 10, -30611744), i = m(i, a, c, f, n[h + 6], 15, -1560198380), f = m(f, i, a, c, n[h + 13], 21, 1309151649), c = m(c, f, i, a, n[h + 4], 6, -145523070), a = m(a, c, f, i, n[h + 11], 10, -1120210379), i = m(i, a, c, f, n[h + 2], 15, 718787259), f = m(f, i, a, c, n[h + 9], 21, -343485551), c = d(c, r), f = d(f, e), i = d(i, o), a = d(a, u);
  26. return [c, f, i, a]
  27. }
  28. function i(n) {
  29. for (var t = "", r = 32 * n.length, e = 0; e < r; e += 8) t += String.fromCharCode(n[e >> 5] >>> e % 32 & 255);
  30. return t
  31. }
  32. function a(n) {
  33. var t = [];
  34. for (t[(n.length >> 2) - 1] = void 0, e = 0; e < t.length; e += 1) t[e] = 0;
  35. for (var r = 8 * n.length, e = 0; e < r; e += 8) t[e >> 5] |= (255 & n.charCodeAt(e / 8)) << e % 32;
  36. return t
  37. }
  38. function e(n) {
  39. for (var t, r = "0123456789abcdef", e = "", o = 0; o < n.length; o += 1) t = n.charCodeAt(o), e += r.charAt(t >>> 4 & 15) + r.charAt(15 & t);
  40. return e
  41. }
  42. function r(n) {
  43. return unescape(encodeURIComponent(n))
  44. }
  45. function o(n) {
  46. return i(c(a(n = r(n)), 8 * n.length))
  47. }
  48. function u(n, t) {
  49. return function (n, t) {
  50. var r, e = a(n), o = [], u = [];
  51. for (o[15] = u[15] = void 0, 16 < e.length && (e = c(e, 8 * n.length)), r = 0; r < 16; r += 1) o[r] = 909522486 ^ e[r], u[r] = 1549556828 ^ e[r];
  52. return t = c(o.concat(a(t)), 512 + 8 * t.length), i(c(u.concat(t), 640))
  53. }(r(n), r(t))
  54. }
  55. function t(n, t, r) {
  56. return t ? r ? u(t, n) : e(u(t, n)) : r ? o(n) : e(o(n))
  57. }
  58. "function" == typeof define && define.amd ? define(function () {
  59. return t
  60. }) : "object" == typeof module && module.exports ? module.exports = t : n.md5 = t
  61. }(this);
  62. var md5 = this.md5;
  63. /**
  64. 网关上线
  65. 订阅:/sys/hbtgIA0SuVw9lxjB/AA:BB:CC:DD:10/c/#
  66. 子设备注册:
  67. /sys/hbtgIA0SuVw9lxjB/AA:BB:CC:DD:10/s/register
  68. {
  69. "id": "6",
  70. "params":{
  71. "productKey":"Rf4QSjbm65X45753",
  72. "deviceName":"ABC12400001",
  73. "model":"S1"
  74. }
  75. }
  76. 子设备上线
  77. 订阅:/sys/Rf4QSjbm65X45753/ABC12400001/c/#
  78. 数据上报:
  79. /sys/Rf4QSjbm65X45753/ABC12400001/s/event/property/post
  80. {
  81. "id": "6",
  82. "params":{
  83. "powerstate": 1
  84. }
  85. }
  86. */
  87. var registered = {};
  88. function getPkDn(clientId) {
  89. var arr = clientId.split("_");
  90. return {
  91. pk: arr[0],
  92. dn: arr[1]
  93. };
  94. }
  95. function register(payload) {
  96. var auth = JSON.parse(payload);
  97. var arr = auth.clientid.split("_");
  98. if (arr.length < 3) {
  99. throw new Error("incorrect clientid");
  100. }
  101. var pk = arr[0];
  102. var dn = arr[1];
  103. var model = arr[2];
  104. var pwd = md5("xdkKUymrEGSCYWswqCvSPyRSFvH5j7CU" + auth.clientid);
  105. if (pwd.toLocaleLowerCase() != auth.password.toLocaleLowerCase()) {
  106. throw new Error("incorrect password");
  107. }
  108. return {
  109. type: "register",
  110. data: {
  111. productKey: pk,
  112. deviceName: dn,
  113. model: model
  114. }
  115. };
  116. }
  117. function subRegister(topic, parent, payload) {
  118. var params = payload.params;
  119. var reply =
  120. {
  121. productKey: parent.productKey,
  122. deviceName: parent.deviceName,
  123. mid: "0",
  124. content: {
  125. topic: topic.replace("/s/", "/c/") + "_reply",
  126. payload: JSON.stringify({
  127. id: "0",
  128. code: 0,
  129. data: {
  130. "productKey": params.productKey,
  131. "deviceName": params.deviceName
  132. }
  133. })
  134. }
  135. };
  136. return {
  137. type: "register",
  138. data: {
  139. productKey: parent.productKey,
  140. deviceName: parent.deviceName,
  141. subDevices: [{
  142. productKey: params.productKey,
  143. deviceName: params.deviceName,
  144. model: params.model
  145. }]
  146. },
  147. action: {
  148. type: "ack",
  149. content: JSON.stringify(reply)
  150. }
  151. };
  152. }
  153. function deviceStateChange(head, clientId, state) {
  154. var topic = head.topic;
  155. var device = getPkDn(clientId);
  156. var arr = topic.split('/');
  157. if (arr.length < 6) {
  158. throw new Error("incorrect topic")
  159. }
  160. var pk = arr[2];
  161. var dn = arr[3];
  162. return {
  163. type: "state",
  164. data: {
  165. productKey: pk,
  166. deviceName: dn,
  167. state: state,
  168. parent: {
  169. productKey: device.pk,
  170. deviceName: device.dn,
  171. }
  172. }
  173. }
  174. }
  175. function disconnect(clientId) {
  176. var device = getPkDn(clientId);
  177. return {
  178. type: "state",
  179. data: {
  180. productKey: device.pk,
  181. deviceName: device.dn,
  182. state: "offline"
  183. }
  184. }
  185. }
  186. function ota(head, payload) {
  187. payload = JSON.parse(payload);
  188. var topic = head.topic;
  189. var arr = topic.split('/');
  190. if (arr.length < 6) {
  191. throw new Error("incorrect topic")
  192. }
  193. var pk = arr[2];
  194. var dn = arr[3];
  195. return {
  196. type: "ota",
  197. data: {
  198. productKey: pk,
  199. deviceName: dn,
  200. mid: payload.id,
  201. content: {
  202. topic: topic,
  203. payload: payload
  204. }
  205. }
  206. }
  207. }
  208. //必须提供onReceive方法
  209. this.onReceive = function (head, type, payload) {
  210. if (type == 'auth') {
  211. return register(payload);
  212. }
  213. if (type == 'subscribe') {
  214. return deviceStateChange(head, payload, 'online');
  215. }
  216. if (type == 'unsubscribe') {
  217. return deviceStateChange(head, payload, 'offline');
  218. }
  219. if (type == 'disconnect') {
  220. return disconnect(payload);
  221. }
  222. if (type == 'ota') {
  223. return ota(head, payload);
  224. }
  225. var topic = head['topic'];
  226. if (!topic) {
  227. throw new Error("topic is blank")
  228. }
  229. var arr = topic.split('/');
  230. if (arr.length < 6) {
  231. throw new Error("incorrect topic")
  232. }
  233. var pk = arr[2];
  234. var dn = arr[3];
  235. payload = JSON.parse(payload);
  236. //子设备注册
  237. if (topic.endsWith('/register')) {
  238. return subRegister(topic, {productKey: pk, deviceName: dn}, payload);
  239. }
  240. //数据上报
  241. var reply =
  242. {
  243. productKey: pk,
  244. deviceName: dn,
  245. mid: payload.id,
  246. content: {
  247. topic: topic.replace("/s/", "/c/") + "_reply",
  248. payload: JSON.stringify({
  249. id: payload.id,
  250. method: payload.method + "_reply",
  251. code: 0,
  252. })
  253. }
  254. };
  255. var action = {};
  256. if (!topic.endsWith("_reply")) {
  257. //需要回复的消息
  258. action = {
  259. type: "ack",
  260. content: JSON.stringify(reply)
  261. }
  262. }
  263. return {
  264. type: "report",
  265. data: {
  266. productKey: pk,
  267. deviceName: dn,
  268. mid: payload.id,
  269. content: {
  270. topic: topic,
  271. payload: payload
  272. }
  273. },
  274. action: action
  275. }
  276. }
  277. this.onRegistered = function (regInfo, result) {
  278. }