0

I have a rpi that updates my db with its current time every 5 seconds this is saved in the variable cseen and changes every 5 minutes IF the board updates the db successfully. The point of this is if after 5 seconds the values are the same the board must have lost connection.

my code to check if the variable has changed is the problem here and is as follows:

<script>
var lastSeen = "";
var cseen = "";
 setInterval(function(){
    $.getJSON('temp.php',function(data){
        tot = data.length;
        for(var i = 0; tot > i; i++){
            //console.log(data[i][0]);
            cseen = data[i][1];
            
            $("#temp" + i).html(data[i][0]);
            if(cseen != lastSeen){
                console.log("board " + i + "cseen:" + cseen +" lastSeen:"+ lastSeen);
                console.log("board " + i + " Online");
            }else{
                console.log("board " + i + "cseen:" + cseen +" lastSeen:"+ lastSeen);
                console.log("board " + i + " Offline");
            }
            lastSeen = cseen;
        }
        //console.log(data.length);
        //$(".temp").html(data.value);
    })
 },5000)
</script>

the output i receive from console is

(index):50 board 0cseen:1 lastSeen:1
(index):51 board 0 Offline
(index):50 board 1cseen:1 lastSeen:1
(index):51 board 1 Offline
(index):50 board 2cseen:1 lastSeen:1
(index):51 board 2 Offline
(index):50 board 3cseen:1 lastSeen:1
(index):51 board 3 Offline
(index):47 board 4cseen:Wed Feb  7 12:54:21 2018 lastSeen:1
(index):48 board 4 Online
(index):47 board 5cseen:1 lastSeen:Wed Feb  7 12:54:21 2018
(index):48 board 5 Online
(index):50 board 6cseen:1 lastSeen:1
(index):51 board 6 Offline

EDIT: Here is the json data logged to console:

(7) [Array(2), Array(2), Array(2), Array(2), Array(2), Array(2), Array(2)]
(2) ["400'C", "1"]
(2) ["20", "1"]
(2) ["30", "1"]
(2) ["60", "1"]
(2) ["46.2'C↵", "Wed Feb  7 13:00:44 2018"]
(2) ["70", "1"]
(2) [null, "1"]

The only board that is actually online and updating is board 5, please tell me what I am doing wrong, this is a pure logic problem

  • 1
    can we see your JSON data please – ADyson Feb 07 '18 at 12:59
  • edited question to include json – anthonytherockjohnson Feb 07 '18 at 13:03
  • Since you are checking small unit of time (seconds) I suggest you to work with timestamp (which is amount of seconds) and `time()` – Yuri Feb 07 '18 at 13:04
  • This is controlled with python on the PI side, I am limited to the values i have. – anthonytherockjohnson Feb 07 '18 at 13:06
  • You can get timestamp in Python as well https://stackoverflow.com/a/13891070/1151408 – Yuri Feb 07 '18 at 13:06
  • Plus, according to RPi speed and possible distance from router, I would check for 30 seconds delay. Few years ago I developed a weather station and I noticed some big delay in DB timestamps.. – Yuri Feb 07 '18 at 13:08
  • Sorry but how does that help my logic problem in the question I asked? – anthonytherockjohnson Feb 07 '18 at 13:08
  • 1
    I assume a value of "1" means it's not online? It's not clear. And you appear to be comparing the boards to each other, not to any given particular board's previous state – ADyson Feb 07 '18 at 13:09
  • @ADyson agreed, I am just not sure how to compare the particular board at that time. – anthonytherockjohnson Feb 07 '18 at 13:10
  • well you need to start by storing the state of the boards between requests, at the moment you just throw the state information away each time – ADyson Feb 07 '18 at 13:11
  • a value of 1 is just a placeholder, I thought i was saving the current boards last state using lastSeen = cseen;? – anthonytherockjohnson Feb 07 '18 at 13:14
  • no, because you overwrite it each time you loop. So it saves the state of board 0, then the state of board 1, then the state of board 2 etc. So consequently `if(cseen != lastSeen){` compares the state of board 1 to the state of board 0, then 2 to 1, then 3 to 2 etc. If you only had one board, you'd get away with it, but since you have six it doesn't work. – ADyson Feb 07 '18 at 13:29
  • So how would I get around this? – anthonytherockjohnson Feb 07 '18 at 13:35
  • Your "lastSeen" needs to be an array, so it can hold the info for each board separately. Then it will match the structure of your "current" data, and that then makes comparisons easy. You write to the lastSeen array once per ajax request, not after every loop through the data. And in your loop you compare the current data at index i to the lastSeen data at index i. If I have some time later I'll write a proper answer, assuming you haven't figured it out by then. – ADyson Feb 07 '18 at 13:39
  • P.S. If the number of boards, and/or the order in which they are reported in the JSON, may change over time, then you probably want to start outputting a unique, unchanging board ID as part of the JSON, and use that to ensure you compare the correct records against each other. – ADyson Feb 07 '18 at 13:50
  • Did you figure anything out for this? – ADyson Feb 07 '18 at 20:31
  • Unfortunately not, I keep having trouble with saving them to an array could you please give a code sample i can look at? – anthonytherockjohnson Feb 08 '18 at 06:49

1 Answers1

0

This should help you. Obviously I had to create a "fake" example because I can't call your JSON data via ajax, but hopefully this demonstrates what you need to do sufficiently for you to adapt it to your needs. If not, I can write what I think should be the "real" code as well, but obviously will be unable to test it.

What you are doing currently is comparing each of the boards to each other, rather than to their previous state. To enable that you need to store the previous state of each board in an array which is preserved in between the ajax requests, and which you overwrite with the most recent data at the end of each request. Then you make your comparisons between the "current" and "last" state of each separate board.

You can run this snippet and observe the output:

//imagine this was the data written last time getJSON was called. 
//When you make your real code, you should declare it as null to begin with, as you will have no previous data at startup.
var lastState = [
  ["300'C", "1"],
  ["40", "1"],
  ["50", "1"],
  ["30", "1"],
  ["44.2'C", "Wed Feb 7 12:55:44 2018"],
  ["53", "1"],
  [null, "1"]
];

//now imagine this is within the "getJSON" callback, as if it has just come from the server
var data = [
  ["400'C", "1"],
  ["20", "1"],
  ["30", "1"],
  ["60", "1"],
  ["46.2'C", "Wed Feb  7 13:00:44 2018"],
  ["70", "1"],
  [null, "1"]
];

//if there's no previous data, we won't do anything except create it for next time. If there is some previous data, do the comparisons
if (lastState) {

  for (var i = 0; data.length > i; i++) {
    var curState = data[i][1];
    var brdText = "board " + i;
    var statusText = " Offline";

    $("#temp" + i).html(data[i][0]);
    console.log(brdText + " current State:" + curState + " last State:" + lastState[i][1]);

    //compare to the lastState entry for the same index
    if (curState != lastState[i][1]) {
      statusText = " Online";
    }
    console.log(brdText + statusText);
  }
}

lastState = data; //save the state as the "last state" for the next request

//this would be the end of your getJSON callback.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
ADyson
  • 57,178
  • 14
  • 51
  • 63