component.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var mid=1;
  2. var gatewayPk="BRD3x4fkKxkaxXFt"
  3. var smartMeterPk="PwMfpXmp4ZWkGahn"
  4. function getMid(){
  5. mid++;
  6. if(mid>10000){
  7. mid=1;
  8. }
  9. return mid;
  10. };
  11. function register(head){
  12. var mac= head.mac;
  13. return {
  14. type:"register",
  15. data:{
  16. productKey:gatewayPk,
  17. deviceName:mac,
  18. model:""
  19. }
  20. };
  21. }
  22. function deviceStateChange(head,type){
  23. var mac=head.mac;
  24. return {
  25. type:"state",
  26. data:{
  27. productKey:gatewayPk,
  28. deviceName:mac,
  29. state:type
  30. }
  31. }
  32. }
  33. function dltHandle(payload){
  34. var dltData= JSON.parse(payload);
  35. var identify= dltData.identify;
  36. var content={};
  37. content[identify]=dltData.data;
  38. return {
  39. type:"report",
  40. data:{
  41. productKey:smartMeterPk,
  42. deviceName:dltData.deviceAddress,
  43. mid:getMid(),
  44. content:{
  45. type:"property",
  46. identifier: "report", //属性上报
  47. occur: new Date().getTime(), //时间戳,设备上的事件或数据产生的本地时间
  48. time: new Date().getTime(), //时间戳,消息上报时间
  49. data: content
  50. }
  51. },
  52. }
  53. }
  54. //必须提供onReceive方法
  55. this.onReceive=function(head,type,payload){
  56. if("register"==type){
  57. return register(head);
  58. }else if("online"==type){
  59. return deviceStateChange(head,type);
  60. }else if("offline"==type){
  61. return deviceStateChange(head,type);
  62. }else if("dlt"==type){
  63. return dltHandle(payload);
  64. }
  65. };
  66. this.onRegistered=function (data,status) {
  67. apiTool.log("onRegistered调用");
  68. }