

function updateSensorStatus(id, value) {  // update sensor port status as data comes in from backend
    // console.log("updateSensorStatus = " + id + ", " + value);
    for (var i=0; i < sensorPinID.length; i++) {
        if (sensorPinID[i] == id) {
            previousSensorStatus[i] = sensorStatus[i];  // record previous state
            sensorStatus[i] = value;  // record new state
            // console.log('previousSensorStatus[i] = ' + previousSensorStatus[i]);
            // console.log('sensorStatus[i]         = ' + sensorStatus[i]);
            // console.log('');
            if (previousSensorStatus[i] != sensorStatus[i]) {sensorsUpdateDashboard(); sensorUpdateIcon[i] = 1;}  // check if DC port state change, flag to update icon
            break;
        }
    }
}

function sensorsUpdateDashboard() {  // update sensors on dashboard
    // console.log('sensorsUpdateDashboard ----------- ');
    for (var i=0; i < sensorPinID.length; i++)  // 
    {
        // console.log('DRAW ----------- ');
        document.getElementById('dashboardDataBox_' + dashboardDataID).innerHTML = 
            '<div class="probeHolder" id="sensorHolder_' + i + '">\
                <div class="sensorIcon" id="sensorIcon_' + i + '">\
                    <img class="" src="images/icons/probes.png">\
                </div>\
                <div class="probeBox">\
                    <div class="probeName" id="sensorName_' + i + '">' + sensorName[i] + '</div>\
                    <div class="sensorValue" id="sensorValue_' + i + '">\</div>\
                </div>\
            </div>';
            
        dashboardDataID++;
    }
    dashboardDataID = dashboardDataID - (sensorPinID.length);  // subtract number of sensors from dashboardDataID so we redraw in the same box

    // console.log('sensorPinID.length----------- ' + sensorPinID.length);
    for (var i=0; i < sensorPinID.length; i++)  // 
    {
        // console.log('sensorTypeID[i] ----------- ' + sensorTypeID[i]);
        if (sensorTypeID[i] == 6) {
            if (sensorStatus[i] == 0) {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/non-contact1.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Sensing Water';
            }
            else {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/non-contact2.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Not Sensing Water';
            }
        }
        else if (sensorTypeID[i] == 7) {
            if (sensorStatus[i] == 0) {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/optical_down.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Touching Water';
            }
            else {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/optical_up.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Not Touching Water';
            }
        }
        else if (sensorTypeID[i] == 8) {
            if (sensorStatus[i] == 0) {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/float_up.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Touching Water';
            }
            else {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/float_down.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Not Touching Water';
            }
        }
        else if (sensorTypeID[i] == 9) {
            if (sensorStatus[i] == 0) {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/other_sensor1.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Touching Water';
            }
            else {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/other_sensor2.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Not Touching Water';
            }
        }
        else if (sensorTypeID[i] == 14) {
            if (sensorStatus[i] == 0) {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/toggle_off.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Switch Off';
            }
            else {
                document.getElementById('sensorIcon_' + i).innerHTML = '<img class="sensorDashboardIcon" src="images/icons/toggle_on.png">';
                document.getElementById('sensorValue_' + i).innerHTML = 'Switch On';
            }
        }
    }
}