Dear community,
I have been developing a system for RFID activation of equipment. It works flawless for 1-2 days but after that I stop receiving events from the particles (actually I use the old spark core, but should upgrade to photons). I can see the events in the console and the mobile app (particle app), and I can also send data to the particles, so they are in fact connected to the cloud.
I have tracked down the problem to
particle.getEventStream({ deviceId: 'HIDDEN', name: 'TagAccess', auth: token }).then(function(stream) {
console.log("Setup tag stream listener");
stream.on('event', function(e) {
var data = JSON.parse(e.data);
// Do other stuff
});
});
That stops receiving calls to the callback in stream.on().
I have searched the community and found these relevant topics:
And this git issue:
But they are all concerning the old spark API. In the old API the getEventStream() seems to return a request object where you can do the on('end', callback) call. But what do the new particle getEventStream() return?
For me it seems like you get the stream object in a callback, so I tested the following code:
var openStream = function() {
//Get your event stream
particle.getEventStream({ deviceId: 'HIDDEN', name: 'TagAccess', auth: token }).then(function(stream) {
console.log("Setup tag stream listener");
stream.on('event', function(e) {
var data = JSON.parse(e.data);
// Do some cool stuff
});
stream.on('end', function() {
console.log("Spark event stream ended! re-opening in 3 seconds...");
setTimeout(openStream, 3 * 1000);
});
});
};
openStream();
But today I noticed that the system stopped getting callbacks again, just as before. So I suspect this is not correct?
How am I supposed to do it?
Why do I even need this restart code? For me it seems like a design error somewhere. It should be able to get the events regardless of what happens?
FYI, code is running on a Raspberry 3. All other code is still active and up-and-running. Just this callback stops getting called.
Best Regards,
Fredrik Löfgren