demo-device.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * 烟感设备模拟器
  3. */
  4. var _logger = logger;
  5. //设备实例id前缀
  6. var devicePrefix = "demo-";
  7. var eventId = Math.ceil(Math.random() * 1000);
  8. //事件类型
  9. var events = {
  10. reportProperty: function (index, session) {
  11. var deviceId = devicePrefix + index;
  12. var topic = "/report-property";
  13. var json = JSON.stringify({
  14. "deviceId": deviceId,
  15. "success": true,
  16. "timestamp":new Date().getTime(),
  17. properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextInt(20, 30)},
  18. });
  19. session.sendMessage(topic, json)
  20. },
  21. fireAlarm: function (index, session) {
  22. var deviceId = devicePrefix + index;
  23. var topic = "/fire_alarm/department/1/area/1/dev/" + deviceId;
  24. var json = JSON.stringify({
  25. "deviceId": deviceId, // 设备编号 "pid": "TBS-110", // 设备编号
  26. "a_name": "商务大厦", // 区域名称 "bid": 2, // 建筑 ID
  27. "b_name": "C2 栋", // 建筑名称
  28. "l_name": "12-05-201", // 位置名称
  29. "timestamp": new Date().getTime() // 消息时间
  30. });
  31. session.sendMessage(topic,json )
  32. }
  33. };
  34. //事件上报
  35. simulator.onEvent(function (index, session) {
  36. //上报属性
  37. events.reportProperty(index, session);
  38. //上报火警
  39. events.fireAlarm(index,session);
  40. });
  41. simulator.bindHandler("/read-property", function (message, session) {
  42. _logger.info("读取属性:[{}]", message);
  43. session.sendMessage("/read-property-reply", JSON.stringify({
  44. messageId: message.messageId,
  45. deviceId: message.deviceId,
  46. timestamp: new Date().getTime(),
  47. properties: {"temperature": java.util.concurrent.ThreadLocalRandom.current().nextInt(20, 30)},
  48. success: true
  49. }));
  50. });
  51. simulator.onConnect(function (session) {
  52. // _logger.info("[{}]:连接成功", session.auth.clientId);
  53. });
  54. simulator.onAuth(function (index, auth) {
  55. auth.setClientId(devicePrefix + index);
  56. auth.setUsername("admin");
  57. auth.setPassword("admin");
  58. });