Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Global variable store that can be used by devices.
- const store = {};
- const ictcg1 = (model, msg, publish, options, action) => {
- const deviceID = msg.endpoints[0].device.ieeeAddr;
- const payload = {};
- if (!store[deviceID]) {
- //const _publish = debounce((msg) => publish(msg), 10);
- store[deviceID] = {since: false, direction: false, value: 255, publish: publish};
- }
- const s = store[deviceID];
- if (action === 'move') {
- s.since = Date.now();
- const direction = msg.data.data.movemode === 1 ? 'left' : 'right';
- s.direction = direction;
- payload.action = `rotate_${direction}`;
- } else if (action === 'stop' || action === 'level') {
- if (action === 'level') {
- s.value = msg.data.data.level;
- const direction = s.value === 0 ? 'left' : 'right';
- payload.action = `rotate_${direction}_quick`;
- payload.brightness = s.value;
- } else {
- const duration = Date.now() - s.since;
- const delta = Math.round((duration / 10) * (s.direction === 'left' ? -1 : 1));
- const newValue = s.value + delta;
- if (newValue >= 0 && newValue <= 255) {
- s.value = newValue;
- }
- payload.action = 'rotate_stop';
- payload.brightness = s.value;
- }
- }
- if (s.timerId) {
- clearInterval(s.timerId);
- s.timerId = false;
- }
- if (action === 'move') {
- s.timerId = setInterval(() => {
- const duration = Date.now() - s.since;
- const delta = Math.round((duration / 10) * (s.direction === 'left' ? -1 : 1));
- const newValue = s.value + delta;
- if (newValue >= 0 && newValue <= 255) {
- s.value = newValue;
- }
- payload.brightness = s.value;
- s.since = Date.now();
- s.publish(payload);
- }, 10);
- }
- s.publish(payload);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement