Advertisement
Guest User

fromZigbee.js

a guest
Sep 15th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. // Global variable store that can be used by devices.
  2. const store = {};
  3.  
  4. const ictcg1 = (model, msg, publish, options, action) => {
  5. const deviceID = msg.endpoints[0].device.ieeeAddr;
  6. const payload = {};
  7.  
  8. if (!store[deviceID]) {
  9. //const _publish = debounce((msg) => publish(msg), 10);
  10. store[deviceID] = {since: false, direction: false, value: 255, publish: publish};
  11. }
  12.  
  13. const s = store[deviceID];
  14.  
  15. if (action === 'move') {
  16. s.since = Date.now();
  17. const direction = msg.data.data.movemode === 1 ? 'left' : 'right';
  18. s.direction = direction;
  19. payload.action = `rotate_${direction}`;
  20. } else if (action === 'stop' || action === 'level') {
  21. if (action === 'level') {
  22. s.value = msg.data.data.level;
  23. const direction = s.value === 0 ? 'left' : 'right';
  24. payload.action = `rotate_${direction}_quick`;
  25. payload.brightness = s.value;
  26. } else {
  27. const duration = Date.now() - s.since;
  28. const delta = Math.round((duration / 10) * (s.direction === 'left' ? -1 : 1));
  29. const newValue = s.value + delta;
  30. if (newValue >= 0 && newValue <= 255) {
  31. s.value = newValue;
  32. }
  33. payload.action = 'rotate_stop';
  34. payload.brightness = s.value;
  35. }
  36. }
  37. if (s.timerId) {
  38. clearInterval(s.timerId);
  39. s.timerId = false;
  40. }
  41. if (action === 'move') {
  42. s.timerId = setInterval(() => {
  43. const duration = Date.now() - s.since;
  44. const delta = Math.round((duration / 10) * (s.direction === 'left' ? -1 : 1));
  45. const newValue = s.value + delta;
  46. if (newValue >= 0 && newValue <= 255) {
  47. s.value = newValue;
  48. }
  49. payload.brightness = s.value;
  50. s.since = Date.now();
  51. s.publish(payload);
  52. }, 10);
  53. }
  54. s.publish(payload);
  55. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement