September 2019 Update

New! We're moving to the new forum!

ALL new Blog posts will be on the forum.

Please join us there.

This Blog will no longer be updated...

Thursday, September 5, 2019

Happy, happy, happy... It's working again!

Posted: 2:55am

I'm so happy right now! 😊

I finally found, and fixed, the problem with reading the sensor configurations when doing a sensor scan. If you've been following along, I had to close the database after each query or I was getting strange results. Generally it would load the first of two parts for the first sensor, and then all the rest would fail.  By closing the database after each query, it all worked like magic.

In my investigations, which have been going on for a week now, I once noticed that when I did the query for part 2 of the config (there are too many fields to do it in one query due to memory) it would come back with a row, but none of my fields, and when I printed out the field names, they were left over from the part 1 query.  I chalked this up to some sort of memory issue, something I must have done wrong, but it could be anywhere...

So, I started over again, I took the code from my rewrite of the switch module, because it was working, and had all the basic infrastructure., I only had to add the sensor module specific code, which for the most part, I just cut and paste. Each time I would add a tiny bit of code, compile it, upload it, and test it.  Everything was fine until I added the code to read part 1 of the sensor config, which it did, the first time, then failed after that....

That's when I discovered the problem...

This sample query is not the one which caused the problem, this is just an example of a pretty standard sql query, and I've highlighted two lines I had intentionally removed from my query code when loading the sensor configuration.  The do ... while loop loops through the resulting records from the query, however, like this query, I was only expecting a single record back so saw no reason to spend the extra time looping when there were no more records.

Once I added these two lines back into my queries (there were three altogether where I had removed them) I can remove the database close after each query, and everything is working again!

In retrospect, the fact I was seeing leftover field names from the previous query could have been a clue. I haven't looked at the library code enough to check, but it appears that a query returns at least one null row at the end of valid rows, the do while look ensures you have all the records.

By skipping the loop, and only grabbing the first record, somehow the next query first had to read the null row (with the field names from the first query) and this is what caused the problem?  That's my working theory...

I'm going to leave it at that, I now know what I did wrong, every example for the library shows the do...while loop.  I've gone back to my original rewrite code and fixed it, and I can move on, thankfully...

Because I don't have to close the database between queries, it runs much faster now too :)

No comments:

Post a Comment

Any comments deemed off topic or offensive will be removed