demo-children-device.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * 子设备消息模拟,请在启动脚本start.sh中自行修改scriptFile选项
  3. *
  4. * 在平台中创建对应的设备实例:
  5. *
  6. * 父设备ID使用: gateway-1
  7. * 子设备ID使用: child-device-1
  8. * 型号使用演示型号.
  9. * 协议使用最新的demo-protocol-1.0.jar,源代码地址: https://github.com/jetlinks/demo-protocol
  10. */
  11. var _logger = logger;
  12. //事件类型
  13. var events = {
  14. reportProperty: function (index, session) {
  15. var deviceId = "child-device-1";
  16. var topic = "/children/report-property";
  17. var json = JSON.stringify({
  18. "deviceId": deviceId,
  19. "success": true,
  20. "timestamp": new Date().getTime(),
  21. properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextDouble(20, 40)},
  22. });
  23. session.sendMessage(topic, json)
  24. },
  25. fireAlarm: function (index, session) {
  26. var deviceId = "child-device-1";
  27. var topic = "/children/fire_alarm";
  28. var json = JSON.stringify({
  29. "deviceId": deviceId, // 设备编号 "pid": "TBS-110", // 设备编号
  30. "a_name": "商务大厦", // 区域名称 "bid": 2, // 建筑 ID
  31. "b_name": "C2 栋", // 建筑名称
  32. "l_name": "12-05-201", // 位置名称
  33. "timestamp": new Date().getTime() // 消息时间
  34. });
  35. session.sendMessage(topic, json)
  36. }
  37. };
  38. //事件上报
  39. simulator.onEvent(function (index, session) {
  40. //上报属性
  41. events.reportProperty(index, session);
  42. //上报火警
  43. events.fireAlarm(index, session);
  44. });
  45. simulator.bindHandler("/children/read-property", function (message, session) {
  46. _logger.info("读取子设备属性:[{}]", message);
  47. session.sendMessage("/read-property-reply", JSON.stringify({
  48. messageId: message.messageId,
  49. deviceId: message.deviceId,
  50. timestamp: new Date().getTime(),
  51. properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextDouble(20, 40)},
  52. success: true
  53. }));
  54. });
  55. simulator.onConnect(function (session) {
  56. //模拟子设备上线
  57. session.sendMessage("/children/device_online_status", JSON.stringify({
  58. deviceId: "child-device-1",
  59. timestamp: new Date().getTime(),
  60. status: "1",
  61. success: true
  62. }));
  63. });
  64. simulator.onAuth(function (index, auth) {
  65. //使用网关设备id 连接平台
  66. auth.setClientId("gateway-1" );
  67. auth.setUsername("admin");
  68. auth.setPassword("admin");
  69. });