component.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 product = deviceBehaviour.getProductKey(pk)
  105. var pwd = md5(product.productSecret + auth.clientid);
  106. if (pwd.toLocaleLowerCase() != auth.password.toLocaleLowerCase()) {
  107. throw new Error("incorrect password");
  108. }
  109. return {
  110. type: "register",
  111. data: {
  112. productKey: pk,
  113. deviceName: dn,
  114. model: model
  115. }
  116. };
  117. }
  118. function subRegister(topic, parent, payload) {
  119. var params = payload.params;
  120. var reply =
  121. {
  122. productKey: parent.productKey,
  123. deviceName: parent.deviceName,
  124. mid: "0",
  125. content: {
  126. topic: topic.replace("/s/", "/c/") + "_reply",
  127. payload: JSON.stringify({
  128. id: "0",
  129. code: 0,
  130. data: {
  131. "productKey": params.productKey,
  132. "deviceName": params.deviceName
  133. }
  134. })
  135. }
  136. };
  137. return {
  138. type: "register",
  139. data: {
  140. productKey: parent.productKey,
  141. deviceName: parent.deviceName,
  142. subDevices: [{
  143. productKey: params.productKey,
  144. deviceName: params.deviceName,
  145. model: params.model
  146. }]
  147. },
  148. action: {
  149. type: "ack",
  150. content: JSON.stringify(reply)
  151. }
  152. };
  153. }
  154. function deviceStateChange(head, clientId, state) {
  155. var topic = head.topic;
  156. var device = getPkDn(clientId);
  157. var arr = topic.split('/');
  158. if (arr.length < 6) {
  159. throw new Error("incorrect topic")
  160. }
  161. var pk = arr[2];
  162. var dn = arr[3];
  163. return {
  164. type: "state",
  165. data: {
  166. productKey: pk,
  167. deviceName: dn,
  168. state: state,
  169. parent: {
  170. productKey: device.pk,
  171. deviceName: device.dn,
  172. }
  173. }
  174. }
  175. }
  176. function disconnect(clientId) {
  177. var device = getPkDn(clientId);
  178. return {
  179. type: "state",
  180. data: {
  181. productKey: device.pk,
  182. deviceName: device.dn,
  183. state: "offline"
  184. }
  185. }
  186. }
  187. function ota(head, payload) {
  188. payload = JSON.parse(payload);
  189. var topic = head.topic;
  190. var arr = topic.split('/');
  191. if (arr.length < 6) {
  192. throw new Error("incorrect topic")
  193. }
  194. var pk = arr[2];
  195. var dn = arr[3];
  196. return {
  197. type: "ota",
  198. data: {
  199. productKey: pk,
  200. deviceName: dn,
  201. mid: payload.id,
  202. content: {
  203. topic: topic,
  204. payload: payload
  205. }
  206. }
  207. }
  208. }
  209. //必须提供onReceive方法
  210. this.onReceive = function (head, type, payload) {
  211. if (type == 'auth') {
  212. return register(payload);
  213. }
  214. if (type == 'subscribe') {
  215. return deviceStateChange(head, payload, 'online');
  216. }
  217. if (type == 'unsubscribe') {
  218. return deviceStateChange(head, payload, 'offline');
  219. }
  220. if (type == 'disconnect') {
  221. return disconnect(payload);
  222. }
  223. if (type == 'ota') {
  224. return ota(head, payload);
  225. }
  226. var topic = head['topic'];
  227. if (!topic) {
  228. throw new Error("topic is blank")
  229. }
  230. var arr = topic.split('/');
  231. if (arr.length < 6) {
  232. throw new Error("incorrect topic")
  233. }
  234. var pk = arr[2];
  235. var dn = arr[3];
  236. payload = JSON.parse(payload);
  237. //子设备注册
  238. if (topic.endsWith('/register')) {
  239. return subRegister(topic, {productKey: pk, deviceName: dn}, payload);
  240. }
  241. //数据上报
  242. var reply =
  243. {
  244. productKey: pk,
  245. deviceName: dn,
  246. mid: payload.id,
  247. content: {
  248. topic: topic.replace("/s/", "/c/") + "_reply",
  249. payload: JSON.stringify({
  250. id: payload.id,
  251. method: payload.method + "_reply",
  252. code: 0,
  253. })
  254. }
  255. };
  256. var action = {};
  257. if (!topic.endsWith("_reply")) {
  258. //需要回复的消息
  259. action = {
  260. type: "ack",
  261. content: JSON.stringify(reply)
  262. }
  263. }
  264. return {
  265. type: "report",
  266. data: {
  267. productKey: pk,
  268. deviceName: dn,
  269. mid: payload.id,
  270. content: {
  271. topic: topic,
  272. payload: payload
  273. }
  274. },
  275. action: action
  276. }
  277. }
  278. this.onRegistered = function (regInfo, result) {
  279. }