Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

double notifications #208

Closed
realjax opened this issue Jul 3, 2019 · 10 comments
Closed

double notifications #208

realjax opened this issue Jul 3, 2019 · 10 comments
Assignees
Labels

Comments

@realjax
Copy link

realjax commented Jul 3, 2019

Hello,

When I do something like this:

const light = lightbulbs[65537].lightList[0];
light.turnOff();

I receive two notifications through observeDevices() where I would expect one. What could be causing this?

@AlCalzone
Copy link
Owner

Can you please post the payload of those notifications?

@realjax
Copy link
Author

realjax commented Jul 3, 2019

edit:
-- objects removed --

note:
There were originally two objects in this post that were identical

@realjax
Copy link
Author

realjax commented Jul 3, 2019

This was for the turnOn() command btw..

@AlCalzone
Copy link
Owner

Hmmm.. aside from all that noise I should probably strip from the event object, they are completely identical. I need to look into this. Could you provide a minimal repro where this happens?

@AlCalzone AlCalzone self-assigned this Jul 3, 2019
@AlCalzone AlCalzone added the bug label Jul 3, 2019
@realjax
Copy link
Author

realjax commented Jul 3, 2019

Thanks.

I am integrating the node-trafri-client into a Node Red custom node, so I don't have a plain vanilla example I'm afraid.

btw, are you ok with me deleting ( or editing) my post with the objects? :)

@AlCalzone
Copy link
Owner

Ok. I'll try to reproduce myself.
Feel free to edit your post.

@realjax
Copy link
Author

realjax commented Jul 6, 2019

Not sure if it has anything to do with this but when setting the colorTemp of a lamp, I often receive multiple events with the only difference being in color values. The first event has the color set to "0" and the next has it set to, for instance, "f1e0b5". Sometimes I receive a multitude of this sequence, resulting in for instance 6 or 8 messages.

I also see a small fluctuation in colorX and colorY values after changing the colorTemp but this might be expected, not sure.

@AlCalzone
Copy link
Owner

So I finally had time to look into the issue. I can reproduce it if I execute this test script twice in a couple of seconds:

// @ts-check

const { TradfriClient, discoverGateway } = require("../../build");
const { wait } = require("alcalzone-shared/async");

async function main() {
	const gwinfo = await discoverGateway();

	const tradfri = new TradfriClient(
		gwinfo.addresses[0],
		(msg, sev) => sev !== "silly" && console.error(msg)
	);

	await tradfri.connect( /* redacted */);

	await tradfri.observeDevices();

	tradfri
		// @ts-ignore
		.on("device updated", ({ client, ...dev }) => {
			const stack = new Error().stack;
			console.log(stack);
			// console.log(new Date(gw.utcNowUnixTimestamp * 1000).toISOString());
			console.dir(dev);
			// console.log(JSON.stringify(gw, null, 4))
		});

	await wait(1000);

	console.error();
	console.error("SWITCHING");
	console.error();

	await tradfri.operateLight(tradfri.devices[65538], {
		dimmer: Math.random() * 100
	});

	await wait(1000);

	tradfri.destroy();
}
main();

The problem is that the gateway does not "forget" the connection quick enough, so it thinks I'm connected twice. It seems to me that it matches the observers by client ID, so this library actually receives two (or more) updates from the gateway:
grafik

I fear there's nothing I can do here, since the messages from the gateway are not duplicated (different MIDs).

How is the connection handling of your app? Do you keep a connection open or do you reconnect often?

@realjax
Copy link
Author

realjax commented Jul 22, 2019

Thanks for testing. I keep a connection open which works fine. There is a connection loss every now and then which seems to occur more often over time. Or when the gateway is 'flooded' with requests. And I think the gateway get's flooded quite easily :)
Anyways, a reboot fixes connection issues and/or slow responding of the gateway.

In order to minimise having to handle (more or less)double messages I've written a comparator which discards similar messages. This approach works ok for me.

@AlCalzone
Copy link
Owner

I'll try to add such functionality when I find the time. Closing in favor of #224

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants