converter.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. var mid = 1;
  2. function getMid() {
  3. mid++;
  4. if (mid > 10000) {
  5. mid = 1;
  6. }
  7. return mid + "";
  8. }
  9. this.decode = function (msg) {
  10. //对msg进行解析,并返回物模型数据
  11. var content = msg.content;
  12. var topic = content.topic;
  13. var payload = content.payload;
  14. var identifier = topic.substring(topic.lastIndexOf("/") + 1);
  15. //透传上报
  16. if (topic.endsWith("/event/rawReport")) {
  17. var rst = component.transparentDecode(payload.params);
  18. if (!rst) {
  19. return null;
  20. }
  21. rst.occur = new Date().getTime();
  22. rst.time = new Date().getTime();
  23. return rst;
  24. }
  25. if (topic.endsWith("/property/post")) {
  26. //属性上报
  27. return {
  28. mid: msg.mid,
  29. productKey: msg.productKey,
  30. deviceName: msg.deviceName,
  31. type: "property",
  32. identifier: "report", //属性上报
  33. occur: new Date().getTime(), //时间戳,设备上的事件或数据产生的本地时间
  34. time: new Date().getTime(), //时间戳,消息上报时间
  35. data: payload.params,
  36. };
  37. } else if (topic.indexOf("/ota/") >= 0) {
  38. //事件上报
  39. return {
  40. mid: msg.mid,
  41. productKey: msg.productKey,
  42. deviceName: msg.deviceName,
  43. type: "ota",
  44. identifier: "ota",
  45. occur: new Date().getTime(),
  46. time: new Date().getTime(),
  47. data: payload.params,
  48. };
  49. } else if (topic.indexOf("/event/") > 0) {
  50. //事件上报
  51. return {
  52. mid: msg.mid,
  53. productKey: msg.productKey,
  54. deviceName: msg.deviceName,
  55. type: "event",
  56. identifier: identifier,
  57. occur: new Date().getTime(),
  58. time: new Date().getTime(),
  59. data: payload.params,
  60. };
  61. } else if (topic.endsWith("/service/property/set_reply")) {
  62. //属性设置回复
  63. return {
  64. mid: msg.mid,
  65. productKey: msg.productKey,
  66. deviceName: msg.deviceName,
  67. type: "property",
  68. identifier: identifier,
  69. occur: new Date().getTime(),
  70. time: new Date().getTime(),
  71. code: payload.code
  72. };
  73. } else if (topic.endsWith("/config/set_reply")) {
  74. //设备配置设置回复
  75. return {
  76. mid: msg.mid,
  77. productKey: msg.productKey,
  78. deviceName: msg.deviceName,
  79. type: "config",
  80. identifier: "set_reply",
  81. occur: new Date().getTime(),
  82. time: new Date().getTime(),
  83. code: payload.code
  84. };
  85. } else if (topic.endsWith("/config/get")) {
  86. //设备配置获取
  87. return {
  88. mid: msg.mid,
  89. productKey: msg.productKey,
  90. deviceName: msg.deviceName,
  91. type: "config",
  92. identifier: "get",
  93. occur: new Date().getTime(),
  94. time: new Date().getTime(),
  95. data: {},
  96. };
  97. } else if (topic.endsWith("_reply")) {
  98. //服务回复
  99. return {
  100. mid: msg.mid,
  101. productKey: msg.productKey,
  102. deviceName: msg.deviceName,
  103. type: "service",
  104. identifier: identifier,
  105. occur: new Date().getTime(),
  106. time: new Date().getTime(),
  107. code: payload.code,
  108. data: payload.data,
  109. };
  110. }
  111. return null;
  112. };
  113. this.encode = function (service, device) {
  114. var deviceMid = getMid();
  115. var method = "thing.service.";
  116. var topic = "/sys/" + service.productKey + "/" + service.deviceName + "/c/service/";
  117. var params = {};
  118. //透传下发
  119. if (device.transparent) {
  120. var rst = component.transparentEncode(service, device);
  121. topic = "/sys/" + rst.productKey + "/" + rst.deviceName + "/c/service/rawSend";
  122. params.model = rst.content.model;
  123. params.deviceName = rst.content.deviceName;
  124. params.data = rst.content.data;
  125. return {
  126. productKey: rst.productKey,
  127. deviceName: rst.deviceName,
  128. mid: rst.mid,
  129. content: {
  130. topic: topic,
  131. payload: JSON.stringify({
  132. id: rst.mid,
  133. method: method + "rawSend",
  134. params: params
  135. })
  136. }
  137. }
  138. }
  139. var type = service.type;
  140. var identifier = service.identifier;
  141. if (type == "property") {
  142. method += "property." + identifier;
  143. topic += "property/" + identifier;
  144. } else if (type == "service") {
  145. method += identifier;
  146. topic += identifier;
  147. } else if (type == "ota") {
  148. method += identifier;
  149. topic = "/ota/device/upgrade/" + service.productKey + "/" + service.deviceName;
  150. } else if (type == "config") {
  151. //设备配置下发
  152. method += identifier;
  153. topic = "/sys/" + service.productKey + "/" + service.deviceName + "/c/config/" + identifier;
  154. } else if (type = "lifetime") {
  155. //子设备注销下发
  156. method += identifier;
  157. topic = "/sys/" + service.productKey + "/" + service.deviceName + "/c/deregister";
  158. }
  159. if (type == "property" && identifier == "get") {
  160. var listParams = []
  161. for (var p in service.params) {
  162. listParams.push(service.params[p]);
  163. }
  164. return {
  165. productKey: service.productKey,
  166. deviceName: service.deviceName,
  167. mid: deviceMid,
  168. content: {
  169. topic: topic,
  170. payload: JSON.stringify({
  171. id: deviceMid,
  172. method: method,
  173. params: listParams
  174. })
  175. }
  176. }
  177. } else {
  178. for (var p in service.params) {
  179. params[p] = service.params[p];
  180. }
  181. return {
  182. productKey: service.productKey,
  183. deviceName: service.deviceName,
  184. mid: deviceMid,
  185. content: {
  186. topic: topic,
  187. payload: JSON.stringify({
  188. id: deviceMid,
  189. method: method,
  190. params: params
  191. })
  192. }
  193. }
  194. }
  195. };