ipcRenderer.on('data-message' , function(event , data) {
    updateMainUnit(data);
});

ipcRenderer.on('control-message' , function(event , data) {
    var action = data.substr(0,1);
    var type = data.substr(1,3);
    var com = data.substr(4,5);

    if (type == 'PE0') {
        if (action == 'O') {
            document.getElementById("comm-secondary-1").innerText = com;
            document.getElementById("comm-secondary-1").className = 'ai-cell ai-cell-success';
        } else if (action == 'C') {
            document.getElementById("comm-secondary-1").innerText = '--';
            document.getElementById("comm-secondary-1").className = 'ai-cell ai-cell-info-1';
        } else if (action == 'T') {
            document.getElementById("comm-secondary-1").innerText = com;
            document.getElementById("comm-secondary-1").className = 'ai-cell ai-cell-warning';
        }
    } else if (type == 'PE1') {
        if (action == 'O') {
            document.getElementById("comm-secondary-2").innerText = com;
            document.getElementById("comm-secondary-2").className = 'ai-cell ai-cell-success';
        } else if (action == 'C') {
            document.getElementById("comm-secondary-2").innerText = '--';
            document.getElementById("comm-secondary-2").className = 'ai-cell ai-cell-info-1';
        } else if (action == 'T') {
            document.getElementById("comm-secondary-2").innerText = com;
            document.getElementById("comm-secondary-2").className = 'ai-cell ai-cell-warning';
        }
    } else if (type == 'RAI') {
        if (action == 'O') {
            document.getElementById("comm-rain-meter").innerText = com;
            document.getElementById("comm-rain-meter").className = 'ai-cell ai-cell-success';
        } else if (action == 'C') {
            document.getElementById("comm-rain-meter").innerText = '--';
            document.getElementById("comm-rain-meter").className = 'ai-cell ai-cell-info-1';
        } else if (action == 'T') {
            document.getElementById("comm-rain-meter").innerText = com;
            document.getElementById("comm-rain-meter").className = 'ai-cell ai-cell-warning';
        }
    } else if (type == 'MAI') {
        if (action == 'O') {
            document.getElementById("comm-main-unit").innerText = com;
            document.getElementById("comm-main-unit").className = 'ai-cell ai-cell-success';
        } else if (action == 'C') {
            document.getElementById("comm-main-unit").innerText = '--';
            document.getElementById("comm-main-unit").className = 'ai-cell ai-cell-info-1';
        } else if (action == 'T') {
            document.getElementById("comm-main-unit").innerText = com;
            document.getElementById("comm-main-unit").className = 'ai-cell ai-cell-warning';
        }
    } else if (type == 'CAN') {
        if (action == 'O') {
            document.getElementById("comm-can-bus").innerText = com;
            document.getElementById("comm-can-bus").className = 'ai-cell ai-cell-success';
        } else if (action == 'C') {
            document.getElementById("comm-can-bus").innerText = '--';
            document.getElementById("comm-can-bus").className = 'ai-cell ai-cell-info-1';
        } else if (action == 'T') {
            document.getElementById("comm-can-bus").innerText = com;
            document.getElementById("comm-can-bus").className = 'ai-cell ai-cell-warning';
        }
    } else if (type == 'GPS') {
        if (action == 'O') {
            document.getElementById("comm-gps-external").innerText = com;
            document.getElementById("comm-gps-external").className = 'ai-cell ai-cell-success';
        } else if (action == 'C') {
            document.getElementById("comm-gps-external").innerText = '--';
            document.getElementById("comm-gps-external").className = 'ai-cell ai-cell-info-1';
        } else if (action == 'T') {
            document.getElementById("comm-gps-external").innerText = com;
            document.getElementById("comm-gps-external").className = 'ai-cell ai-cell-warning';
        }
    }
});

var lastType = 0xFF;
var gps_x = 0;
var gps_y = 0;

var updateMainUnit = function(data)
{
    switch(data[2]) {
        case 0:
            updateMainUnit_0(data);
            break;
        case 1:
            updateMainUnit_1(data);
            break;
        case 2:
            updateMainUnit_2(data);
            break;
        case 3:
            updateMainUnit_3(data);
            break;
        case 4:
            updateMainUnit_4(data);
            break;
        case 5:
            updateMainUnit_5(data);
            break;
        case 6:
            updateMainUnit_6(data);
            break;
        case 7:
            updateMainUnit_7(data);
            break;
        case 8:
            updateMainUnit_8(data);
            break;
        case 9:
            updateMainUnit_9_49(data, 0);
            break;
        case 10:
            updateMainUnit_9_49(data, 1);
            break;
        case 11:
            updateMainUnit_9_49(data, 2);
            break;
        case 12:
            updateMainUnit_9_49(data, 3);
            break;
        case 13:
            updateMainUnit_9_49(data, 4);
            break;
        case 14:
            updateMainUnit_9_49(data, 5);
            break;
        case 15:
            updateMainUnit_9_49(data, 6);
            break;
        case 16:
            updateMainUnit_9_49(data, 7);
            break;
        case 17:
            updateMainUnit_9_49(data, 8);
            break;
        case 18:
            updateMainUnit_9_49(data, 9);
            break;
        case 19:
            updateMainUnit_9_49(data, 10);
            break;
        case 20:
            updateMainUnit_9_49(data, 11);
            break;
        case 21:
            updateMainUnit_9_49(data, 12);
            break;
        case 22:
            updateMainUnit_9_49(data, 13);
            break;
        case 23:
            updateMainUnit_9_49(data, 14);
            break;
        case 24:
            updateMainUnit_9_49(data, 15);
            break;
        case 25:
            updateMainUnit_9_49(data, 16);
            break;
        case 26:
            updateMainUnit_9_49(data, 17);
            break;
        case 27:
            updateMainUnit_9_49(data, 18);
            break;
        case 28:
            updateMainUnit_9_49(data, 19);
            break;
        case 29:
            updateMainUnit_9_49(data, 20);
            break;
        case 30:
            updateMainUnit_9_49(data, 21);
            break;
        case 31:
            updateMainUnit_9_49(data, 22);
            break;
        case 32:
            updateMainUnit_9_49(data, 23);
            break;
        case 33:
            updateMainUnit_9_49(data, 24);
            break;
        case 34:
            updateMainUnit_9_49(data, 25);
            break;
        case 35:
            updateMainUnit_9_49(data, 26);
            break;
        case 36:
            updateMainUnit_9_49(data, 27);
            break;
        case 0x99:
            updateMainUnit_Ping(data);
            break;
        case 0xDA:
            updateMainUnit_Diagnostics(data);
            break;
    }
}

var updateMainUnit_0 = function(data)
{
    //General flags
    updateFlagObject("flags-general-0", Math.floor(data[4])%2, "warning");
    updateFlagObject("flags-general-1", Math.floor(data[4]/2)%2, "warning");
    updateFlagObject("flags-general-2", Math.floor(data[4]/4)%2, "warning");
    updateFlagObject("flags-general-3", Math.floor(data[4]/8)%2, "warning");
    updateFlagObject("flags-general-4", Math.floor(data[4]/16)%2, "warning");
    updateFlagObject("flags-general-5", Math.floor(data[4]/32)%2, "warning");

    //Init flags
    updateFlagObject("flags-init-0", Math.floor(data[7])%2, "warning");
    updateFlagObject("flags-init-1", Math.floor(data[7]/2)%2, "warning");
    updateFlagObject("flags-init-2", Math.floor(data[7]/4)%2, "warning");
    updateFlagObject("flags-init-3", Math.floor(data[7]/8)%2, "warning");
    updateFlagObject("flags-init-4", Math.floor(data[7]/16)%2, "warning");
    updateFlagObject("flags-init-5", Math.floor(data[7]/32)%2, "warning");
    updateFlagObject("flags-init-6", Math.floor(data[7]/64)%2, "warning");
    updateFlagObject("flags-init-7", Math.floor(data[7]/128)%2, "warning");
    updateFlagObject("flags-init-8", Math.floor(data[8])%2, "warning");
    updateFlagObject("flags-init-9", Math.floor(data[8]/2)%2, "warning");
    updateFlagObject("flags-init-10", Math.floor(data[8]/4)%2, "warning");
    updateFlagObject("flags-init-11", Math.floor(data[8]/8)%2, "warning");
    updateFlagObject("flags-init-12", Math.floor(data[8]/16)%2, "warning");

    //Source flags
    updateFlagObject("flags-source-0-1", (Math.floor(data[5])%4 == 0), "warning");
    updateFlagObject("flags-source-0-2", (Math.floor(data[5])%4 == 1), "warning");
    updateFlagObject("flags-source-0-3", (Math.floor(data[5])%4 == 2), "warning");
    updateFlagObject("flags-source-0-4", (Math.floor(data[5])%4 == 3), "warning");
    updateFlagObject("flags-source-2", Math.floor(data[5]/4)%2, "warning");
    updateFlagObject("flags-source-3", Math.floor(data[5]/8)%2, "warning");
    updateFlagObject("flags-source-4", Math.floor(data[5]/16)%2, "warning");
    updateFlagObject("flags-source-5", Math.floor(data[5]/32)%2, "warning");
    updateFlagObject("flags-source-6", Math.floor(data[5]/64)%2, "warning");
    updateFlagObject("flags-source-7", Math.floor(data[5]/128)%2, "warning");
    updateFlagObject("flags-source-8", Math.floor(data[6])%2, "warning");
    updateFlagObject("flags-source-9", Math.floor(data[6]/2)%2, "warning");
    updateFlagObject("flags-source-10", Math.floor(data[6]/4)%2, "warning");
    updateFlagObject("flags-source-11", Math.floor(data[6]/8)%2, "warning");
    updateFlagObject("flags-source-12", Math.floor(data[6]/16)%2, "warning");
    updateFlagObject("flags-source-13", Math.floor(data[6]/32)%2, "warning");
    updateFlagObject("flags-source-14", Math.floor(data[6]/64)%2, "warning");

    //Alarm flags
    updateFlagObject("flags-alarms-con-0", Math.floor(data[9])%2, "danger");
    updateFlagObject("flags-alarms-con-1", Math.floor(data[9]/2)%2, "danger");
    updateFlagObject("flags-alarms-con-2", Math.floor(data[9]/4)%2, "danger");
    updateFlagObject("flags-alarms-con-3", Math.floor(data[9]/8)%2, "danger");
    updateFlagObject("flags-alarms-con-4", Math.floor(data[9]/16)%2, "danger");
    updateFlagObject("flags-alarms-con-5", Math.floor(data[9]/32)%2, "danger");
    updateFlagObject("flags-alarms-con-6", Math.floor(data[9]/64)%2, "danger");
    updateFlagObject("flags-alarms-con-7", Math.floor(data[9]/128)%2, "danger");
    updateFlagObject("flags-alarms-con-8", Math.floor(data[10])%2, "danger");
    
    updateFlagObject("flags-alarms-0", Math.floor(data[11])%2, "danger");
    updateFlagObject("flags-alarms-1", Math.floor(data[11]/2)%2, "danger");
    updateFlagObject("flags-alarms-2", Math.floor(data[12])%2, "danger");
    updateFlagObject("flags-alarms-3", Math.floor(data[12]/2)%2, "danger");
    document.getElementById("flags-alarms-second").innerText = Math.floor(data[11]/4)%64;
    
    //DO MORSE ALARM HERE
    
    updateFlagObject("devices-flow-flag", Math.floor(data[13])%2, "warning");
    updateFlagObject("devices-fc-flag", Math.floor(data[13]/2)%2, "warning");
    updateFlagObject("devices-hydraulic-flag", Math.floor(data[13]/4)%2, "warning");
    updateFlagObject("devices-new-flag", Math.floor(data[13]/8)%2, "warning");
    updateFlagObject("devices-flow-disabled", Math.floor(data[13]/16)%2, "warning");
    updateFlagObject("devices-fc-disabled", Math.floor(data[13]/32)%2, "warning");
    updateFlagObject("devices-hydraulic-disabled", Math.floor(data[13]/64)%2, "warning");

    //CAN flags
    updateFlagObject("flags-can-config-mode", Math.floor(data[14])%2, "warning");
    updateFlagObject("flags-can-boot-mode", Math.floor(data[14]/2)%2, "warning");
    updateFlagObject("flags-can-sent-config", Math.floor(data[14]/4)%2, "warning");
    updateFlagObject("flags-can-sent-base", Math.floor(data[14]/8)%2, "danger");
    updateFlagObject("flags-can-sent-unconfig", Math.floor(data[14]/16)%2, "danger");
    updateFlagObject("flags-can-config-correct", Math.floor(data[14]/32)%2, "danger");
    updateFlagObject("flags-can-check-valid-config", Math.floor(data[14]/64)%2, "danger");
    updateFlagObject("flags-can-received-unconfigured", Math.floor(data[14]/128)%2, "danger");
    updateFlagObject("flags-can-received-response", Math.floor(data[15])%2, "danger");
    updateFlagObject("flags-can-device-not-responding", Math.floor(data[15]/2)%2, "danger");
    
    var GPSSpeed = (data[16] + data[17]*256)/10;
    var GPSSpeedKnots = (data[18] + data[19]*256)/10;
    var PickupSpeed = (data[20] + data[21]*256)/10;
    var SimulatedSpeed = (data[22] + data[23]*256)/10;
    var CurrentSpeed = (data[24] + data[25]*256)/10;
    var RadialSpeed = (data[26] + data[27]*256)/10;
    if (RadialSpeed > 32767) RadialSpeed = (-65536) + RadialSpeed;

    meterBars[0].updateLevel(GPSSpeed);
    meterBars[1].updateLevel(GPSSpeedKnots);
    meterBars[2].updateLevel(PickupSpeed);
    meterBars[3].updateLevel(SimulatedSpeed);
    meterBars[4].updateLevel(RadialSpeed);
    meterBars[5].updateLevel(CurrentSpeed);

    //CAN counters
    document.getElementById("count-can-tx").innerText = (data[28]);
    document.getElementById("count-can-rx").innerText = (data[29]);
    document.getElementById("count-can-ov").innerText = (data[30]);
    document.getElementById("count-can-c1").innerText = (data[31]);
    document.getElementById("count-can-er").innerText = (data[32]);

    //App flags
    updateFlagObject("flags-app-2-per", Math.floor(data[33])%2, "danger");
    updateFlagObject("flags-app-5-per", Math.floor(data[33]/2)%2, "danger");
    updateFlagObject("flags-app-10-per", Math.floor(data[33]/4)%2, "danger");
    updateFlagObject("flags-app-25-per", Math.floor(data[33]/8)%2, "danger");

    //USB
    updateFlagObject("flags-usb-dev-detected", Math.floor(data[34])%2, "warning");
    updateFlagObject("flags-usb-end-of-data", Math.floor(data[34]/2)%2, "warning");
    updateFlagObject("flags-usb-file-open", Math.floor(data[34]/4)%2, "warning");
    updateFlagObject("flags-usb-return-page", Math.floor(data[34]/8)%4, "warning");
    updateFlagObject("flags-usb-prog-type", Math.floor(data[34]/32)%2, "warning");
    updateFlagObject("flags-usb-com-fail", Math.floor(data[34]/64)%2, "warning");
    updateFlagObject("flags-usb-buff-over", Math.floor(data[34]/128)%2, "warning");

    var main = (data[36])%16;
    for (var i = 0; i < 10; i++) {
        updateFlagObject("usb-main-action-" + i, (main == i), "warning");
    }
    var base = Math.floor(data[36]/16)%8;
    for (var i = 0; i < 6; i++) {
        updateFlagObject("usb-base-action-" + i, (base == i), "warning");
    }
    var command = (data[37])%8;
    for (var i = 0; i < 6; i++) {
        updateFlagObject("usb-command-action-" + i, (command == i), "warning");
    }
}

var updateMainUnit_1 = function(data)
{
    document.getElementById("app-main-app-target").innerText = (data[4] + data[5]*256);
    document.getElementById("app-main-flow-target").innerText = (data[6] + data[7]*256);
    document.getElementById("app-default-app-target").innerText = (data[8] + data[9]*256);
    document.getElementById("app-main-app-actual").innerText = (data[10] + data[11]*256);
    document.getElementById("app-main-flow-actual").innerText = (data[12] + data[13]*256);
    document.getElementById("app-total-app-target").innerText = (data[14] + data[15]*256);
    document.getElementById("app-total-flow-target").innerText = (data[16] + data[17]*256);
    document.getElementById("app-total-app-actual").innerText = (data[18] + data[19]*256);
    document.getElementById("app-total-flow-actual").innerText = (data[20] + data[21]*256);

    //GPS
    document.getElementById("gps-control-int-receive").innerText = (data[22] + data[23]*256);
    document.getElementById("gps-control-ext-receive").innerText = (data[24] + data[25]*256);
    document.getElementById("gps-control-acc-count").innerText = (data[26]);

    //Screen
    document.getElementById("gui-control-screen").innerText = (data[27]);
    document.getElementById("gui-control-tab").innerText = (data[28]);
    document.getElementById("gui-control-type").innerText = (data[29]);
    document.getElementById("gui-control-planting").innerText = (data[30]);
    document.getElementById("gui-control-selected-device").innerText = (data[31]);

    //Log
    var logging = (data[32] + data[33]*256 + data[34]*256*256 + data[35]*256*256*256);
    document.getElementById("logging-log-pointer").innerText = logging.toString(16);
    var log_string = "Bytes";
    if (logging > 1000000) {
        logging = Math.floor(logging/1000000);
        log_string = "MB";
    }
    if (logging > 1000) {
        logging = Math.floor(logging/1000);
        log_string = "KB";
    }
    document.getElementById("logging-log-size").innerText = logging + " " + log_string;

    //USB
    updateFlagObject("flags-usb-vis-all", Math.floor(data[36])%2, "warning");
    updateFlagObject("flags-usb-vis-planting", Math.floor(data[36]/2)%2, "warning");
    updateFlagObject("flags-usb-vis-config", Math.floor(data[36]/4)%2, "warning");
    updateFlagObject("flags-usb-vis-program", Math.floor(data[36]/8)%2, "warning");
    updateFlagObject("flags-usb-boot-mode", Math.floor(data[36]/64)%2, "warning");
    document.getElementById("counter-usb-directory-level").innerText = (data[37]);
    
    //GPS
    gps_y = (data[44]*256*256*256 + data[45]*256*256 + data[46]*256 + data[47]);
    gps_x = (data[40]*256*256*256 + data[41]*256*256 + data[42]*256 + data[43]);
    document.getElementById("gps-control-y").innerText = gps_y.toString(16);
    document.getElementById("gps-control-x").innerText = gps_x.toString(16);
    type = "";
    for (var i = 0; i < 2; i++) {
        type += String.fromCharCode(data[48 + i]);
    }
    document.getElementById("gps-control-status").innerText = type;
}

var updateMainUnit_2 = function(data)
{
    var dev = (data[4] + data[5]*256);
    if (dev > 32767) dev = (-65536) + dev;
    document.getElementById("app-main-app-deviation").innerText = dev;
    dev = (data[6] + data[7]*256);
    if (dev > 32767) dev = (-65536) + dev;
    document.getElementById("app-main-flow-deviation").innerText = dev;
    var per = (data[8] + data[9]*256);
    if (per > 32767) per = (-65536) + per;
    document.getElementById("app-main-app-deviation-per").innerText = per;
    per = (data[10] + data[11]*256);
    if (per > 32767) per = (-65536) + per;
    document.getElementById("app-main-flow-deviation-per").innerText = per;
    dev = (data[12] + data[13]*256);
    if (dev > 32767) dev = (-65536) + dev;
    document.getElementById("app-total-app-deviation").innerText = dev;
    dev = (data[14] + data[15]*256);
    if (dev > 32767) dev = (-65536) + dev;
    document.getElementById("app-total-flow-deviation").innerText = dev;

    document.getElementById("fert-density").innerText = (data[16] + data[17]*256)/100;
    document.getElementById("prec-plant-flow").innerText = (data[18] + data[19]*256);
    document.getElementById("prec-plant-index").innerText = (data[20] + data[21]*256);
    document.getElementById("prec-plant-x0").innerText = (data[22] + data[23]*256 + data[24]*256*256 + data[25]*256*256*256).toString(16);
    document.getElementById("prec-plant-x1").innerText = (data[26] + data[27]*256 + data[28]*256*256 + data[29]*256*256*256).toString(16);
    document.getElementById("prec-plant-y0").innerText = (data[30] + data[31]*256 + data[32]*256*256 + data[33]*256*256*256).toString(16);
    document.getElementById("prec-plant-y1").innerText = (data[34] + data[35]*256 + data[36]*256*256 + data[37]*256*256*256).toString(16);
}

var updateMainUnit_3 = function(data)
{
    document.getElementById("golden").innerText = (data[4]*256*256*256*256 + data[5]*256*256*256 + data[6]*256*256 + data[7]*256 + data[8]).toString(16);
    document.getElementById("impl-row-width").innerText = ((data[9])/100).toFixed(2);
    document.getElementById("impl-rows-per-hectar").innerText = (data[10] + data[11]*256).toFixed(2);
    document.getElementById("impl-leftmost-row").innerText = data[12];
    document.getElementById("impl-rightmost-row").innerText = data[13];
    document.getElementById("impl-rows").innerText = data[14];
    document.getElementById("impl-active-rows").innerText = data[15];
    document.getElementById("impl-hectares-left").innerText = data[16] + data[17]*256;

    /*for (var i = 1; i < data[32]; i++) {
        if (i >= data[12] && i <= data[13]) {
            document.getElementById("dev-cell-D-ACT-" + i).innerText = "NO";
            document.getElementById("dev-cell-D-ACT-" + i).className = "ai-cell ai-cell-danger";
        } else {
            document.getElementById("dev-cell-D-ACT-" + i).innerText = "YES";
            document.getElementById("dev-cell-D-ACT-" + i).className = "ai-cell ai-cell-success";
        }
    }*/

    var type = "";
    for (var i = 0; i < 10; i++) {
        type += String.fromCharCode(data[18 + i]);
    }
    document.getElementById("other-seed-type").innerText = type;
    type = "";
    for (var i = 0; i < 2; i++) {
        type += String.fromCharCode(data[28 + i]);
    }
    document.getElementById("other-impl-type").innerText = type;
    document.getElementById("pickup-mm-per-pulse").innerText = (data[30] + data[31]*256);

    document.getElementById("device-counters-total").innerText = data[32];
    devices_update_number(data[32]);
    document.getElementById("device-counters-flow").innerText = data[33];
    document.getElementById("device-counters-fc").innerText = data[34];
    document.getElementById("device-counters-hydr").innerText = data[35];

    for (var i = 0; i < 2; i++) {
        updateFlagObject('flags-messcan-sendfcengage-' + (i + 1), (Math.floor(data[36 + i])%2), 'danger');
        updateFlagObject('flags-messcan-sendfcpressuretarget-' + (i + 1), (Math.floor(data[36 + i]/2)%2), 'danger');
        updateFlagObject('flags-messcan-sendfcstatusrequest-' + (i + 1), (Math.floor(data[36 + i]/4)%2), 'danger');
        
        updateFlagObject('flags-messcan-sendhydrengage-' + (i + 1), (Math.floor(data[38 + i])%2), 'danger');
        updateFlagObject('flags-messcan-sendhydrflowtarget-' + (i + 1), (Math.floor(data[38 + i]/2)%2), 'danger');
        updateFlagObject('flags-messcan-sendhydrvolumetarget-' + (i + 1), (Math.floor(data[38 + i]/4)%2), 'danger');
        updateFlagObject('flags-messcan-sendhydrstatusrequest-' + (i + 1), (Math.floor(data[38 + i]/8)%2), 'danger');
    }
}

var updateMainUnit_4 = function(data)
{
    document.getElementById("hydr-unit-volume").innerText = (data[4] + data[5]*256);
    document.getElementById("hydr-number-units").innerText = (data[6] + data[7]*256);
    document.getElementById("hydr-total-volume").innerText = (data[8] + data[9]*256);
    document.getElementById("hydr-flow-target").innerText = (data[10] + data[11]*256 + data[12]*256*256);

    document.getElementById("daily-time-value").innerText = (data[13] + data[14]/60).toFixed(2);
    document.getElementById("daily-volume-value").innerText = ((data[15] + data[16]*256 + data[17]*256 + data[18]*256)/1000).toFixed(3);
    document.getElementById("daily-weight-value").innerText = ((data[19] + data[20]*256 + data[21]*256 + data[22]*256)/1000).toFixed(3);
    document.getElementById("daily-area-value").innerText = ((data[23] + data[24]*256 + data[25]*256 + data[26]*256)/100000000).toFixed(3);

    document.getElementById("grand-time-value").innerText = (data[27] + data[28]/60).toFixed(2);
    document.getElementById("grand-volume-value").innerText = ((data[29] + data[30]*256 + data[31]*256 + data[32]*256)/1000).toFixed(3);
    document.getElementById("grand-weight-value").innerText = ((data[33] + data[34]*256 + data[35]*256 + data[36]*256)/1000).toFixed(3);
    document.getElementById("grand-area-value").innerText = ((data[37] + data[38]*256 + data[39]*256 + data[40]*256)/100000000).toFixed(3);

    for (var i = 0; i < 32; i++) {
        updateFlagObject('flags-messcan-sendflowengage-' + (i + 1), (Math.floor(data[41 + i])%2), 'danger');
        updateFlagObject('flags-messcan-sendflowtarget-' + (i + 1), (Math.floor(data[41 + i]/2)%2), 'danger');
        updateFlagObject('flags-messcan-sendflowstatusrequest-' + (i + 1), (Math.floor(data[41 + i]/4)%2), 'danger');
        updateFlagObject('flags-messcan-sendflowgolden-' + (i + 1), (Math.floor(data[41 + i]/8)%2), 'danger');
    }
}

var updateMainUnit_5 = function(data)
{
    document.getElementById("daily-distance-value").innerText = (data[4] + data[5]*256 + data[6]*256*256 + data[7]*256*256*256);
    document.getElementById("grand-distance-value").innerText = (data[8] + data[9]*256 + data[10]*256*256 + data[11]*256*256*256);
    meterBars[6].updateLevel(data[12] + data[13]*256);
    meterBars[6].updateLimits((data[14] + data[15]*256), (data[16] + data[17]*256));
    meterBars[7].updateLevel((data[22] + data[23]*256)/100);

    updateFlagObject('flags-messcan-sendbasenumber', (Math.floor(data[24])%2), 'danger');
    updateFlagObject('flags-messcan-sendconfigmodeall', (Math.floor(data[24]/2)%2), 'danger');
    updateFlagObject('flags-messcan-sendconfigmodenotinit', (Math.floor(data[24]/4)%2), 'danger');
    updateFlagObject('flags-messcan-sendexitconfigmode', (Math.floor(data[24]/8)%2), 'danger');
    updateFlagObject('flags-messcan-sendnotinitresponse', (Math.floor(data[24]/16)%2), 'danger');
    updateFlagObject('flags-messcan-sendnewenvelopetx', (Math.floor(data[24]/32)%2), 'danger');
    updateFlagObject('flags-messcan-sendnewenveloperx', (Math.floor(data[24]/64)%2), 'danger');
    updateFlagObject('flags-messcan-sendnewdeviceid', (Math.floor(data[24]/128)%2), 'danger');

    updateFlagObject('flags-messcan-resetIDMemory', (Math.floor(data[25])%2), 'danger');
    updateFlagObject('flags-messcan-bootstartflowfc', (Math.floor(data[25]/2)%2), 'danger');
    updateFlagObject('flags-messcan-bootstarthydr', (Math.floor(data[25]/4)%2), 'danger');
    updateFlagObject('flags-messcan-bootend', (Math.floor(data[25]/8)%2), 'danger');
    updateFlagObject('flags-messcan-sendengageflowall', (Math.floor(data[25]/16)%2), 'danger');
    updateFlagObject('flags-messcan-sendflowtargetall', (Math.floor(data[25]/32)%2), 'danger');
    updateFlagObject('flags-messcan-sendmanualcalibrateall', (Math.floor(data[25]/64)%2), 'danger');
    updateFlagObject('flags-messcan-sendstatusrequestallflow', (Math.floor(data[25]/128)%2), 'danger');

    updateFlagObject('flags-messcan-sendengagefcall', (Math.floor(data[26])%2), 'danger');
    updateFlagObject('flags-messcan-sendfcpressuretargetall', (Math.floor(data[26]/2)%2), 'danger');
    updateFlagObject('flags-messcan-sendstatusrequestallfc', (Math.floor(data[26]/4)%2), 'danger');
    updateFlagObject('flags-messcan-sendengagehydall', (Math.floor(data[26]/8)%2), 'danger');
    updateFlagObject('flags-messcan-sendhydflowtargetall', (Math.floor(data[26]/16)%2), 'danger');
    updateFlagObject('flags-messcan-sendhydvolumetargetall', (Math.floor(data[26]/32)%2), 'danger');
    updateFlagObject('flags-messcan-sendhydmanualtargetall', (Math.floor(data[26]/64)%2), 'danger');
    updateFlagObject('flags-messcan-sendhydmanualpwm', (Math.floor(data[26]/128)%2), 'danger');

    updateFlagObject('flags-messcan-sendstatusrequestallhyd', (Math.floor(data[27])%2), 'danger');

    for (var i = 0; i < 40; i++) {
        updateFlagObject('flags-messcan-sendenterconfigmode-' + (i + 1), (Math.floor(data[28 + i])%2), 'danger');
        updateFlagObject('flags-messcan-sendreset-' + (i + 1), (Math.floor(data[28 + i]/2)%2), 'danger');
    }
}

var updateMainUnit_9_49 = function(data, idx)
{
    devices_update_device_main(idx, data[6], data);
}

var updateMainUnit_6 = function(data)
{
    for (var i = 0; i < 4; i++) {
        if ((data[4 + (18*i)]*256*256*256 + data[5 + (18*i)]*256*256 + data[6 + (18*i)]*256 + data[7 + (18*i)]) == 0) {
            document.getElementById("prec-search-y0-" + (i + 1)).innerText = "--";
            document.getElementById("prec-search-y1-" + (i + 1)).innerText = "--";
            document.getElementById("prec-search-x0-" + (i + 1)).innerText = "--";
            document.getElementById("prec-search-x1-" + (i + 1)).innerText = "--";
            document.getElementById("prec-search-idx-" + (i + 1)).innerText = "--";
            
            document.getElementById("prec-search-y0-" + (i + 1)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-y1-" + (i + 1)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-x0-" + (i + 1)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-x1-" + (i + 1)).className = "ai-cell ai-cell-info-2";
        } else {
            var x0 = (data[4 + (18*i)]*256*256*256 + data[5 + (18*i)]*256*256 + data[6 + (18*i)]*256 + data[7 + (18*i)]);
            var x1 = (data[8 + (18*i)]*256*256*256 + data[9 + (18*i)]*256*256 + data[10 + (18*i)]*256 + data[11 + (18*i)]);
            var y0 = (data[12 + (18*i)]*256*256*256 + data[13 + (18*i)]*256*256 + data[14 + (18*i)]*256 + data[15 + (18*i)]);
            var y1 = (data[16 + (18*i)]*256*256*256 + data[17 + (18*i)]*256*256 + data[18 + (18*i)]*256 + data[19 + (18*i)]);
            var idx = (data[20 + (18*i)] + data[21 + (18*i)]*256);

            document.getElementById("prec-search-y0-" + (i + 1)).innerText = y0.toString(16);
            document.getElementById("prec-search-y1-" + (i + 1)).innerText = y1.toString(16);
            document.getElementById("prec-search-x0-" + (i + 1)).innerText = x0.toString(16);
            document.getElementById("prec-search-x1-" + (i + 1)).innerText = x1.toString(16);
            document.getElementById("prec-search-idx-" + (i + 1)).innerText = idx;

            if (gps_y > y0) {
                document.getElementById("prec-search-y0-" + (i + 1)).className = "ai-cell ai-cell-warning";
                document.getElementById("prec-search-y1-" + (i + 1)).className = "ai-cell ai-cell-warning";
            } else if (gps_y < y1) {
                document.getElementById("prec-search-y0-" + (i + 1)).className = "ai-cell ai-cell-danger";
                document.getElementById("prec-search-y1-" + (i + 1)).className = "ai-cell ai-cell-danger";
            } else {
                document.getElementById("prec-search-y0-" + (i + 1)).className = "ai-cell ai-cell-info-2";
                document.getElementById("prec-search-y1-" + (i + 1)).className = "ai-cell ai-cell-info-2";
            }
            if (gps_x < x0) {
                document.getElementById("prec-search-x0-" + (i + 1)).className = "ai-cell ai-cell-warning";
                document.getElementById("prec-search-x1-" + (i + 1)).className = "ai-cell ai-cell-warning";
            } else if (gps_x > x1) {
                document.getElementById("prec-search-x0-" + (i + 1)).className = "ai-cell ai-cell-danger";
                document.getElementById("prec-search-x1-" + (i + 1)).className = "ai-cell ai-cell-danger";
            } else {
                document.getElementById("prec-search-x0-" + (i + 1)).className = "ai-cell ai-cell-info-2";
                document.getElementById("prec-search-x1-" + (i + 1)).className = "ai-cell ai-cell-info-2";
            }
        }
    }
}

var updateMainUnit_7 = function(data)
{
    for (var i = 0; i < 4; i++) {
        if ((data[4 + (18*i)]*256*256*256 + data[5 + (18*i)]*256*256 + data[6 + (18*i)]*256 + data[7 + (18*i)]) == 0) {
            document.getElementById("prec-search-y0-" + (i + 5)).innerText = "--";
            document.getElementById("prec-search-y1-" + (i + 5)).innerText = "--";
            document.getElementById("prec-search-x0-" + (i + 5)).innerText = "--";
            document.getElementById("prec-search-x1-" + (i + 5)).innerText = "--";
            document.getElementById("prec-search-idx-" + (i + 5)).innerText = "--";
            
            document.getElementById("prec-search-y0-" + (i + 5)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-y1-" + (i + 5)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-x0-" + (i + 5)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-x1-" + (i + 5)).className = "ai-cell ai-cell-info-2";
        } else {
            var x0 = (data[4 + (18*i)]*256*256*256 + data[5 + (18*i)]*256*256 + data[6 + (18*i)]*256 + data[7 + (18*i)]);
            var x1 = (data[8 + (18*i)]*256*256*256 + data[9 + (18*i)]*256*256 + data[10 + (18*i)]*256 + data[11 + (18*i)]);
            var y0 = (data[12 + (18*i)]*256*256*256 + data[13 + (18*i)]*256*256 + data[14 + (18*i)]*256 + data[15 + (18*i)]);
            var y1 = (data[16 + (18*i)]*256*256*256 + data[17 + (18*i)]*256*256 + data[18 + (18*i)]*256 + data[19 + (18*i)]);
            var idx = (data[20 + (18*i)] + data[21 + (18*i)]*256);
            
            document.getElementById("prec-search-y0-" + (i + 5)).innerText = y0.toString(16);
            document.getElementById("prec-search-y1-" + (i + 5)).innerText = y1.toString(16);
            document.getElementById("prec-search-x0-" + (i + 5)).innerText = x0.toString(16);
            document.getElementById("prec-search-x1-" + (i + 5)).innerText = x1.toString(16);
            document.getElementById("prec-search-idx-" + (i + 5)).innerText = idx;

            if (gps_y < y0) {
                document.getElementById("prec-search-y0-" + (i + 5)).className = "ai-cell ai-cell-warning";
                document.getElementById("prec-search-y1-" + (i + 5)).className = "ai-cell ai-cell-warning";
            } else if (gps_y > y1) {
                document.getElementById("prec-search-y0-" + (i + 5)).className = "ai-cell ai-cell-danger";
                document.getElementById("prec-search-y1-" + (i + 5)).className = "ai-cell ai-cell-danger";
            } else {
                document.getElementById("prec-search-y0-" + (i + 5)).className = "ai-cell ai-cell-info-2";
                document.getElementById("prec-search-y1-" + (i + 5)).className = "ai-cell ai-cell-info-2";
            }
            if (gps_x < x0) {
                document.getElementById("prec-search-x0-" + (i + 5)).className = "ai-cell ai-cell-warning";
                document.getElementById("prec-search-x1-" + (i + 5)).className = "ai-cell ai-cell-warning";
            } else if (gps_x > x1) {
                document.getElementById("prec-search-x0-" + (i + 5)).className = "ai-cell ai-cell-danger";
                document.getElementById("prec-search-x1-" + (i + 5)).className = "ai-cell ai-cell-danger";
            } else {
                document.getElementById("prec-search-x0-" + (i + 5)).className = "ai-cell ai-cell-info-2";
                document.getElementById("prec-search-x1-" + (i + 5)).className = "ai-cell ai-cell-info-2";
            }
        }
    }
}

var updateMainUnit_8 = function(data)
{
    for (var i = 0; i < 4; i++) {
        if ((data[4 + (18*i)]*256*256*256 + data[5 + (18*i)]*256*256 + data[6 + (18*i)]*256 + data[7 + (18*i)]) == 0) {
            document.getElementById("prec-search-y0-" + (i + 9)).innerText = "--";
            document.getElementById("prec-search-y1-" + (i + 9)).innerText = "--";
            document.getElementById("prec-search-x0-" + (i + 9)).innerText = "--";
            document.getElementById("prec-search-x1-" + (i + 9)).innerText = "--";
            document.getElementById("prec-search-idx-" + (i + 9)).innerText = "--";
            
            document.getElementById("prec-search-y0-" + (i + 9)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-y1-" + (i + 9)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-x0-" + (i + 9)).className = "ai-cell ai-cell-info-2";
            document.getElementById("prec-search-x1-" + (i + 9)).className = "ai-cell ai-cell-info-2";
        } else {
            var x0 = (data[4 + (18*i)]*256*256*256 + data[5 + (18*i)]*256*256 + data[6 + (18*i)]*256 + data[7 + (18*i)]);
            var x1 = (data[8 + (18*i)]*256*256*256 + data[9 + (18*i)]*256*256 + data[10 + (18*i)]*256 + data[11 + (18*i)]);
            var y0 = (data[12 + (18*i)]*256*256*256 + data[13 + (18*i)]*256*256 + data[14 + (18*i)]*256 + data[15 + (18*i)]);
            var y1 = (data[16 + (18*i)]*256*256*256 + data[17 + (18*i)]*256*256 + data[18 + (18*i)]*256 + data[19 + (18*i)]);
            var idx = (data[20 + (18*i)] + data[21 + (18*i)]*256);
            
            document.getElementById("prec-search-y0-" + (i + 9)).innerText = y0.toString(16);
            document.getElementById("prec-search-y1-" + (i + 9)).innerText = y1.toString(16);
            document.getElementById("prec-search-x0-" + (i + 9)).innerText = x0.toString(16);
            document.getElementById("prec-search-x1-" + (i + 9)).innerText = x1.toString(16);
            document.getElementById("prec-search-idx-" + (i + 9)).innerText = idx;

            if (gps_y > y0) {
                document.getElementById("prec-search-y0-" + (i + 9)).className = "ai-cell ai-cell-warning";
                document.getElementById("prec-search-y1-" + (i + 9)).className = "ai-cell ai-cell-warning";
            } else if (gps_y < y1) {
                document.getElementById("prec-search-y0-" + (i + 9)).className = "ai-cell ai-cell-danger";
                document.getElementById("prec-search-y1-" + (i + 9)).className = "ai-cell ai-cell-danger";
            } else {
                document.getElementById("prec-search-y0-" + (i + 9)).className = "ai-cell ai-cell-info-2";
                document.getElementById("prec-search-y1-" + (i + 9)).className = "ai-cell ai-cell-info-2";
            }
            if (gps_x < x0) {
                document.getElementById("prec-search-x0-" + (i + 9)).className = "ai-cell ai-cell-warning";
                document.getElementById("prec-search-x1-" + (i + 9)).className = "ai-cell ai-cell-warning";
            } else if (gps_x > x1) {
                document.getElementById("prec-search-x0-" + (i + 9)).className = "ai-cell ai-cell-danger";
                document.getElementById("prec-search-x1-" + (i + 9)).className = "ai-cell ai-cell-danger";
            } else {
                document.getElementById("prec-search-x0-" + (i + 9)).className = "ai-cell ai-cell-info-2";
                document.getElementById("prec-search-x1-" + (i + 9)).className = "ai-cell ai-cell-info-2";
            }
        }
    }
}

var updateFlagObject = function(id, data, type)
{
    if (data == 1) {
        document.getElementById(id).className = "ai-cell ai-cell-" + type;
    } else {
        document.getElementById(id).className = "ai-cell ai-cell-info-1";
    }
}

var updateValueObject = function(id, data, text, type)
{
    if (data == 1) {
        document.getElementById(id).className = "ai-cell ai-cell-" + type;
        document.getElementById(id).innerText = text[0];
    } else {
        document.getElementById(id).className = "ai-cell ai-cell-info-1";
        document.getElementById(id).innerText = text[1];
    }
}

var updateMainUnit_Ping = function(data)
{
    document.getElementById('diag-main-usbcomms-1').className = "ai-cell ai-cell-success";
    diagnostic_main_count();
}

var updateMainUnit_GPS = function(data)
{
    ipcRenderer.send('main-unit-message', "GPSInput " + data + String.fromCharCode(13));
}

var updateMainUnit_Diagnostics = function(data)
{
    if (Math.floor(data[4])%2) {
        document.getElementById("diag-main-keypad-1").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[4]/2)%2) {
        document.getElementById("diag-main-keypad-2").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[4]/4)%2) {
        document.getElementById("diag-main-keypad-3").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[4]/8)%2) {
        document.getElementById("diag-main-keypad-4").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[4]/16)%2) {
        document.getElementById("diag-main-keypad-5").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[4]/32)%2) {
        document.getElementById("diag-main-keypad-6").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[4]/64)%2) {
        document.getElementById("diag-main-keypad-7").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[4]/128)%2) {
        document.getElementById("diag-main-keypad-8").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[5])%2) {
        document.getElementById("diag-main-keypad-9").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[5]/2)%2) {
        document.getElementById("diag-main-keypad-10").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[5]/4)%2) {
        document.getElementById("diag-main-flash-1").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[5]/8)%2) {
        document.getElementById("diag-main-flash-2").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[5]/16)%2) {
        document.getElementById("diag-main-flash-3").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[5]/32)%2) {
        document.getElementById("diag-main-flash-4").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[5]/64)%2) {
        document.getElementById("diag-main-flash-5").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[5]/128)%2) {
        document.getElementById("diag-main-flash-6").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[6])%2) {
        document.getElementById("diag-main-flash-7").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[6]/2)%2) {
        document.getElementById("diag-main-flash-8").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[6]/4)%2) {
        document.getElementById("diag-main-eeprom-1").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[6]/8)%2) {
        document.getElementById("diag-main-usb").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[6]/16)%2) {
        document.getElementById("diag-main-gps-int").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[6]/32)%2) {
        document.getElementById("diag-main-gps-ext").className = "ai-cell ai-cell-success";
    }
    if (Math.floor(data[6]/64)%2) {
        document.getElementById("diag-main-can-bus").className = "ai-cell ai-cell-success";
    }
    diagnostic_main_count();
    //devices_update_device_main(idx, data[6], data);
}