Few questions on how to handle errors

Supporting Browser-based Javascript and Node.js
Post Reply
sdeblois-itesmedia
Fresh meat
Posts: 4
Joined: Fri Jun 18, 2021 11:49 am
Contact:

Few questions on how to handle errors

Post by sdeblois-itesmedia »

Hello, I have a few use cases when an error occurs that I'm not sure how to handle and would like to know if there are any official recommendations.

Here is the code I'm using:

Code: Select all

	function runExample() {
		console.log('Running runExample');
		var ch = new phidget22.VoltageInput();
		
		ch.setIsHubPortDevice(true);
		ch.setHubPort(0);

		ch.onError = function (code, description) {
			console.log("Error " + code + ": " + description);
		};

		ch.onAttach = () => {
			console.log('attach');
			/*ch.onSensorChange = (sensorValue, sensorUnit) => {
				console.log(sensorValue);
			}*/
		};
		
		ch.onSensorChange = (sensorValue, sensorUnit) => {
			console.log(sensorValue);
		}
		
		ch.open().then(() => {
			ch.setSensorType(phidget22.VoltageSensorType.PN_MOT2002_LOW);
		}).catch(function (err) {
			console.error(err);
		});
	}

	var conn = new phidget22.Connection(8989, 'localhost');
	conn.connect().then(runExample).catch(function (err) {
		console.log('This function never runs');
		console.error('Error during connect:', err);
	});
1. If I start the code while the server is not running, neither of the then or catch methods of conn.connect() are called and nothings happens, no retries are taking place or anything.
2. While everything is working correctly and the connection is lost by closing the server I can see that it's automatically retrying and eventually succeeds after starting the server again but the onSensorChange is no longer called.
3. Every time the connection is established, I get a bunch of "Error 4103: Sensor value is ouside the valid range for this sensor." and onSensorChange is not being called. After around 10 seconds or so, sometime more, the errors stops and onSensorChange is called correctly.

Am I doing something wrong? What should I do in these situations if want the connection to recover?

Thanks
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Few questions on how to handle errors

Post by mparadis »

1. I would have expected an error to be caught by your catch statement on connect(), but maybe you can try setting the onError handler for conn and see if it's caught there.

2. Try setting the onSensorChange handler (and ch's other properties) inside conn's onConnect handler, so that it re-initializes ch whenever the server reconnects.

3. I think this behaviour might be solved by the solution above in #2. It could be caused by sensorChange events coming in before the sensortype gets set, but 10s sounds like way too long for that to be the case.


Here's an example of how to use the onError and onConnect functions:

Code: Select all

// NOTE: This shows importing phidget22 in Node.JS. In-Browser JavaScript import is different
var phidget22 = require("phidget22");

var conn = new phidget22.Connection({
	hostname: "localhost",
	port: 5661,
	name: "Phidget Server Connection",
	passwd: "",
	onAuthenticationNeeded: function() { return "password"; },
	onError: function(code, msg) { console.error("Connection Error:", msg); },
	onConnect: function() { console.log("Connected"); },
	onDisconnect: function() { console.log("Disconnected"); }
});

conn.connect().catch(function(err) {
	console.error("Error during connect:", err);
});
sdeblois-itesmedia
Fresh meat
Posts: 4
Joined: Fri Jun 18, 2021 11:49 am
Contact:

Re: Few questions on how to handle errors

Post by sdeblois-itesmedia »

Thanks for the quick reply.
1. The catch method is correctly called on node.js but not on a browser, I have tried onError in a browser and it is not called either.
2 and 3. I have tried to set onSensorChange again after a reconnection and it's still not called.

Here's the latest code that can be run on both a browser or node.js if you use the correct port:

Code: Select all

var ch = null;
function runExample() {
	console.log('Running runExample');
	var isNew = ch == null;
	if(isNew){
		ch = new phidget22.VoltageInput();
	}
	ch.setIsHubPortDevice(true);
	ch.setHubPort(0);

	ch.onError = function (code, description) {
		console.log("Error " + code + ": " + description);
	};

	ch.onAttach = () => {
		console.log('attach');
		/*ch.onSensorChange = (sensorValue, sensorUnit) => {
			console.log(sensorValue);
		}*/
	};
	
	ch.onSensorChange = (sensorValue, sensorUnit) => {
		console.log(sensorValue);
	}
	
	if(isNew){
		ch.open().then(() => {
			ch.setSensorType(phidget22.VoltageSensorType.PN_MOT2002_LOW);
		}).catch(function (err) {
			console.error(err);
		});
	}
}

var conn = new phidget22.Connection({
   hostname: "localhost",
   port: 8989,
   name: "Phidget Server Connection",
   passwd: "",
   onAuthenticationNeeded: function() { return "password"; },
   onError: function(code, msg) { console.error("Connection Error:", msg); },
   onConnect: function() {
		runExample();
   },
   onDisconnect: function() { console.log("Disconnected"); }
});
conn.connect().then(() => console.log('then')).catch(function (err) {
	console.log('This function never runs');
	console.error('Error during connect:', err);
});
Side note, I asked my colleague to try on his side with his own device in a different location and on his side when the device is set to PN_MOT2002_LOW, the Error 4103 seems to simply never stop, setting it to PN_MOT2002_MED seems to have helped. The results for 1 and 2 are the same on his side once the Error 4103 stops.

I have also noticed that I get the exact same error in the control panel. See attached screenshot.
Attachments
2021-06-18.png
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Few questions on how to handle errors

Post by mparadis »

I took a look into how the sensor type works for the MOT2002, and it looks like it needs a stretch of 25 samples of very little movement in order to establish a baseline. It is able to use the samples before you select the sensor type, but since the default data rate is 250ms, the odds that you have 25 samples with no movement are very slim. You'll find if you set the data interval to something below 60ms before selecting the sensor type in the control panel, you won't get the errors. In your program, you should set the data interval to a low value and then throw in a sleep statement to ensure it has time to fill the buffer before changing the sensor type. Alternatively, you could ignore the errors, since they'll always go away once the buffer is filled.

The reason your colleague was seeing the errors continuously is probably because there was so much movement being picked up that the filter was never able to establish a baseline.
sdeblois-itesmedia
Fresh meat
Posts: 4
Joined: Fri Jun 18, 2021 11:49 am
Contact:

Re: Few questions on how to handle errors

Post by sdeblois-itesmedia »

I changed the data interval in my code to 50ms and added a sleep of 5s. This is enough if there is no movement in front of the device.
But I was able to prevent the device from establishing its baseline by walking around the room, it was still throwing errors after 2 minutes and only stopped once I had left the room for a few seconds.

Should I be concerned about this happening in production or is this expected behaviour and negligible in real world scenarios?
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Few questions on how to handle errors

Post by mparadis »

You may find it settles easier if you set the data interval to the minimum value (1ms if you're using a VINT Hub).

This is expected behaviour, and whether or not it's negligible really depends on how you're using it. This issue only occurs when the channel is first opened, so as long as there a period of stillness between startup and when you need the data, you'll be fine. If this doesn't work in your application, you could just use the raw voltage value without setting the sensorType. If you do this you'd have to come up with a way to decide based on the data whether there has been enough motion to trigger the sensor or not.
sdeblois-itesmedia
Fresh meat
Posts: 4
Joined: Fri Jun 18, 2021 11:49 am
Contact:

Re: Few questions on how to handle errors

Post by sdeblois-itesmedia »

Thanks for the details, I have validated the other two issues again and I seem to have found working solutions for both.

The first issue from my original post seems to be fixed by using the onError method instead of catch, this seems like a bug considering the different results between node.js and a browser but I can work with that.

And the second issue seems to be fixed by calling setSensorType inside of onAttach instead of inside then() of the channel open method. This makes it so that it's called after each reconnect and therefore fixes the issue.

Thanks again
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest