converter.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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("/event/") > 0) {
  38. //事件上报
  39. return {
  40. mid: msg.mid,
  41. productKey: msg.productKey,
  42. deviceName: msg.deviceName,
  43. type:"event",
  44. identifier: identifier,
  45. occur: new Date().getTime(),
  46. time: new Date().getTime(),
  47. data: payload.params,
  48. };
  49. }else if(topic.endsWith("/service/property/set_reply")){
  50. //属性设置回复
  51. return {
  52. mid: msg.mid,
  53. productKey: msg.productKey,
  54. deviceName: msg.deviceName,
  55. type:"property",
  56. identifier: identifier,
  57. occur: new Date().getTime(),
  58. time: new Date().getTime(),
  59. code: payload.code
  60. };
  61. }else if(topic.endsWith("/config/set_reply")){
  62. //设备配置设置回复
  63. return {
  64. mid: msg.mid,
  65. productKey: msg.productKey,
  66. deviceName: msg.deviceName,
  67. type:"config",
  68. identifier: "set_reply",
  69. occur: new Date().getTime(),
  70. time: new Date().getTime(),
  71. code: payload.code
  72. };
  73. }else if(topic.endsWith("/config/get")){
  74. //设备配置获取
  75. return {
  76. mid: msg.mid,
  77. productKey: msg.productKey,
  78. deviceName: msg.deviceName,
  79. type:"config",
  80. identifier: "get",
  81. occur: new Date().getTime(),
  82. time: new Date().getTime(),
  83. data: {},
  84. };
  85. } else if (topic.endsWith("_reply")) {
  86. //服务回复
  87. return {
  88. mid: msg.mid,
  89. productKey: msg.productKey,
  90. deviceName: msg.deviceName,
  91. type:"service",
  92. identifier: identifier,
  93. occur: new Date().getTime(),
  94. time: new Date().getTime(),
  95. code: payload.code,
  96. data: payload.data,
  97. };
  98. }
  99. return null;
  100. };
  101. this.encode = function (service,device) {
  102. var deviceMid=getMid();
  103. var method="thing.service.";
  104. var topic="/sys/"+service.productKey+"/"+service.deviceName+"/c/service/";
  105. var params={};
  106. //透传下发
  107. if(device.transparent){
  108. var rst=component.transparentEncode(service,device);
  109. topic="/sys/"+rst.productKey+"/"+rst.deviceName+"/c/service/rawSend";
  110. params.model=rst.content.model;
  111. params.deviceName=rst.content.deviceName;
  112. params.data=rst.content.data;
  113. return {
  114. productKey:rst.productKey,
  115. deviceName:rst.deviceName,
  116. mid:rst.mid,
  117. content:{
  118. topic:topic,
  119. payload:JSON.stringify({
  120. id:rst.mid,
  121. method:method+"rawSend",
  122. params:params
  123. })
  124. }
  125. }
  126. }
  127. var type=service.type;
  128. var identifier=service.identifier;
  129. if(type=="property"){
  130. method+="property."+identifier;
  131. topic+="property/"+identifier;
  132. }else if(type=="service"){
  133. method+=identifier;
  134. topic+=identifier;
  135. }else if(type=="config"){
  136. //设备配置下发
  137. method+=identifier;
  138. topic="/sys/"+service.productKey+"/"+service.deviceName+"/c/config/"+identifier;
  139. }else if(type="lifetime"){
  140. //子设备注销下发
  141. method+=identifier;
  142. topic="/sys/"+service.productKey+"/"+service.deviceName+"/c/deregister";
  143. }
  144. for(var p in service.params){
  145. params[p]=service.params[p];
  146. }
  147. return {
  148. productKey:service.productKey,
  149. deviceName:service.deviceName,
  150. mid:deviceMid,
  151. content:{
  152. topic:topic,
  153. payload:JSON.stringify({
  154. id:deviceMid,
  155. method:method,
  156. params:params
  157. })
  158. }
  159. }
  160. };