
ipcRenderer.on('data-message' , function(event , data){ 
    //Flow Device
    
    if ((data[2] == 0xA5) || (data[2] == 0xFC) || (data[2] == 0xC7)) {
        for (var i = 0; i < 40; i++) {
            if (document.getElementById("dev-cell-ID-" + i).innerText = data[4].toString()) {
                devices_update_device_seco(i, data[2], data);
                break;
            }
        }
    }
    if (data[2] == 0xA5) {
        updateFlowDevice(data);
    }
    //FC Device
    if (data[2] == 0xFC) {
        updateFCDevice(data);
    }
    //Hyd Device
    if (data[2] == 0xC7) {
        updateHYDDrive(data);
    }

    if (data[2] >= 0 && data[2] <= 23) {
        updateMainUnitS(data);
    }
});

var lastType = 0xFF;
var lastIDX = 0xFF;

var checkType = function(type, idx)
{
    if (lastType != type) {
        lastType = type;
        resetCharts(type);
        lastIDX = idx;
        changeDeviceIndex(idx);
    }
    else if (lastIDX != idx) {
        lastIDX = idx;
        changeDeviceIndex(idx);
    }
}

var updateFlowDevice = function(data)
{
    //Flow Device
    checkType(data[2], data[4]);
    var intTime3 = (data[6] + data[7]*256 + data[8]*256*256);

    var intMedian = [];
    var intAverage = [];
    for (var i = 0; i < 9; i++) {
        intMedian[i] = data[6 + (i*2)] + data[7 + (i*2)]*256;
    }
    for (var i = 0; i < 10; i++) {
        intAverage[i] = data[29 + (i*2)] + data[30 + (i*2)]*256;
    }
    var Device_ID = data[4];
    var pulseRange = data[47] + data[48]*256;
    var pulseProcess = data[49] + data[20]*256;
    var setPoint = data[51] + data[52]*256;
    var pulseAdjusted = data[53] + data[54]*256;
    var pulseFlags = data[55];
    var motorState = data[56];
    var deviceID_4 = data[57];
    var goldenNumber = (data[58] + data[59]*256 + data[60]*256*256 + data[61]*256*256*256 + data[62]*256*256*256*256);
    var error = setPoint - pulseAdjusted;
    var per = error*100/setPoint;
    var engage = pulseFlags%2;
    var KeyFlags = (data[63]);
    var amp_off = data[64];
    var amp_read = data[65];
    var vol_read = data[75];
    var MES_12C = data[71];
    var MES_12C_4 = data[72];
    var avg_min = intAverage[0];
    var avg_max = intAverage[0];
    var steps = data[76] + data[77]*256;
    var DIPReading = data[78];
    var deviceFlags = data[79];
    var configFlags = data[80];
    var diagnosticFlags = data[81];
    var motorError = data[82] + data[83]*256;
    if (motorError > 32767) motorError = 65536 - motorError;
    var motorErrorOut = data[84] + data[85]*256;
    if (motorErrorOut > 32767) motorErrorOut = 65536 - motorErrorOut;
    var portC = data[86];
    var latC = data[87];
    for (var i = 0 ; i < intAverage.length; i++) {
        if (intAverage[i] < avg_min) avg_min = intAverage[i];
        if (intAverage[i] > avg_max) avg_max = intAverage[i];
    }

    charts[0].updateLevel([intTime3]);
    charts[1].updateLevel([pulseAdjusted, setPoint, error]);
    charts[2].updateLevel([setPoint]);
    charts[3].updateLevel([pulseAdjusted]);
    charts[4].updateLevel([error]);
    charts[5].updateLevel([per]);
    charts[6].updateLevel([pulseRange]);
    charts[7].updateLevel([pulseProcess]);
    charts[8].updateLevel(intMedian);
    charts[9].updateLevel([intMedian[0], [intMedian[4]], [intMedian[8]]]);
    charts[10].updateLevel([goldenNumber]);
    charts[11].updateLevel(intAverage);
    //charts[12].updateLevel([MES_12C, MES_12C_4]);
    charts[12].updateLevel([avg_min, avg_max]);
    charts[13].updateLevel([pulseRange, pulseProcess]);
    charts[14].updateLevel([pulseProcess, pulseAdjusted]);
    charts[15].updateLevel([amp_read]);
    charts[17].updateLevel([motorState]);
    charts[16].updateLevel([vol_read, 100, 52, 85]);
    charts[18].updateLevel([motorError]);
    charts[19].updateLevel([motorErrorOut]);
    charts[20].updateLevel([latC]);
    charts[21].updateLevel([Device_ID]);

    if (document.getElementById("capture-data-1").className == 'ai-cell ai-cell-success') {
        pushLastInt(steps, intTime3);
    }
    if (document.getElementById("capture-data-2").className == 'ai-cell ai-cell-success') {
        pushLastFlow(steps, pulseRange);
    }
    dev_idx = -1;
    for (var i = 0; i < 40; i++) {
        if (document.getElementById("dev-cell-ID-" + i).innerText == data[4].toString()) {
            dev_idx = i;
            break;
        }
    }
    if (dev_idx != -1) {
        updateFlagObject("dev-cell-K_TL-" + dev_idx, Math.floor(KeyFlags/32)%2, "warning");
        updateFlagObject("dev-cell-K_BL-" + dev_idx, Math.floor(KeyFlags/16)%2, "warning");
        updateFlagObject("dev-cell-K_TR-" + dev_idx, Math.floor(KeyFlags/2)%2, "warning");
        updateFlagObject("dev-cell-K_BR-" + dev_idx, Math.floor(KeyFlags)%2, "warning");
        document.getElementById("device-s-f-" + dev_idx + "-amp-off").innerText = amp_off.toString(16);
        document.getElementById("device-s-f-" + dev_idx + "-amp-read").innerText = amp_read.toString(16);
        document.getElementById("device-s-f-" + dev_idx + "-vol-read").innerText = vol_read.toString();
        document.getElementById("device-s-f-" + dev_idx + "-trg-flow").innerText = setPoint;
        document.getElementById("device-s-f-" + dev_idx + "-gold").innerText = goldenNumber.toString(16);
    }

    //DIP REading
    //000   113
    //100   163
    //010   136
    //001   122

    //110   217
    //111   255
    //101   184
    //011   151

    if (document.getElementById("diag-flow-start").className == "ai-cell ai-cell-success") {
        if (Math.floor(KeyFlags/32)%2 == 1) {
            document.getElementById("diag-flow-keypad-1").className = "ai-cell ai-cell-success";
        } else if (Math.floor(KeyFlags/16)%2 == 1) {
            document.getElementById("diag-flow-keypad-2").className = "ai-cell ai-cell-success";
        } else if (Math.floor(KeyFlags/2)%2 == 1) {
            document.getElementById("diag-flow-keypad-3").className = "ai-cell ai-cell-success";
        } else if (Math.floor(KeyFlags)%2 == 1) {
            document.getElementById("diag-flow-keypad-4").className = "ai-cell ai-cell-success";
        }

        if (DIPReading >= 117 && DIPReading <= 173) {
            if (DIPReading >= 117 && DIPReading <= 127) {
                document.getElementById("diag-flow-dipswitch-3").className = "ai-cell ai-cell-success";
            } else if (DIPReading >= 128 && DIPReading <= 144) {
                document.getElementById("diag-flow-dipswitch-2").className = "ai-cell ai-cell-success";
            } else if (DIPReading >= 157 && DIPReading <= 173) {
                document.getElementById("diag-flow-dipswitch-1").className = "ai-cell ai-cell-success";
            }
        }

        if (Math.floor(diagnosticFlags)%2 == 1) {
            document.getElementById("diag-flow-can-bus").className = "ai-cell ai-cell-success";
        }
        if (Math.floor(diagnosticFlags/2)%2 == 1) {
            document.getElementById("diag-flow-pickup").className = "ai-cell ai-cell-success";
        }
        if (Math.floor(diagnosticFlags/4)%2 == 1) {
            document.getElementById("diag-flow-motor").className = "ai-cell ai-cell-success";
        }
        if (Math.floor(diagnosticFlags/8)%2 == 1) {
            document.getElementById("diag-flow-pressure").className = "ai-cell ai-cell-success";
        }
        if (Math.floor(diagnosticFlags/16)%2 == 1) {
            document.getElementById("diag-flow-engage").className = "ai-cell ai-cell-success";
        }

        diagnostic_flow_count();
    }
}

var updateFCDevice = function(data)
{
    //FC Device
    checkType(data[2], data[4]);
    var intTime3 = (data[6] + data[7]*256 + data[8]*256*256);

    var intMedian = [];
    var intAverage = [];
    for (var i = 0; i < 9; i++) {
        intMedian[i] = data[6 + (i*2)] + data[7 + (i*2)]*256;
    }
    for (var i = 0; i < 10; i++) {
        intAverage[i] = data[29 + (i*2)] + data[30 + (i*2)]*256;
    }
    var pulseRange = data[47] + data[48]*256;
    var pulseProcess = data[49] + data[20]*256;
    var setPoint = data[51] + data[52]*256;
    var pulseAdjusted = data[53] + data[54]*256;
    var pulseFlags = data[55];
    var motorState = data[56];
    var deviceID_4 = data[57];
    var goldenNumber = (data[58] + data[59]*256 + data[60]*256*256 + data[61]*256*256*256 + data[62]*256*256*256*256);
    var error = setPoint - pulseAdjusted;
    var per = error*100/setPoint;
    var engage = pulseFlags%2;
    var KeyFlags = (data[63]);
    var amp_off = data[64];
    var amp_read = data[65];
    var vol_read = data[75];
    var pressT = data[66];
    var pressure = data[67] + data[68]*256;
    var pressureT = data[69] + data[70]*256;
    var avg_min = intAverage[0];
    var avg_max = intAverage[0];
    var MES_12C = data[71];
    var MES_12C_4 = data[72];
    var ComStat = data[73];
    var error_flag = Math.floor(data[73]/2)%2 + 2;

    for (var i = 0 ; i < intAverage.length; i++) {
        if (intAverage[i] < avg_min) avg_min = intAverage[i];
        if (intAverage[i] > avg_max) avg_max = intAverage[i];
    }

    charts[0].updateLevel([intTime3]);
    charts[1].updateLevel([pulseAdjusted, setPoint, error]);
    charts[2].updateLevel([setPoint]);
    charts[3].updateLevel([pulseAdjusted]);
    charts[4].updateLevel([error]);
    charts[5].updateLevel([per]);
    charts[6].updateLevel([pulseRange]);
    charts[7].updateLevel([pulseProcess]);
    charts[8].updateLevel(intMedian);
    charts[9].updateLevel([intMedian[0], [intMedian[4]], [intMedian[8]]]);
    charts[10].updateLevel([MES_12C_4, MES_12C]);
    charts[11].updateLevel(intAverage);
    charts[12].updateLevel([pressT]);
    charts[13].updateLevel([pressure]);
    charts[14].updateLevel([pressureT]);
    charts[15].updateLevel([motorState, engage]);
    charts[16].updateLevel([vol_read, 100, 52, 85]);

    dev_idx = -1;
    for (var i = 0; i < 40; i++) {
        if (document.getElementById("dev-cell-ID-" + i).innerText == data[4].toString()) {
            dev_idx = i;
            break;
        }
    }
    if (dev_idx != -1) {
        updateFlagObject("dev-cell-K_TL-" + dev_idx, Math.floor(KeyFlags/32)%2, "warning");
        updateFlagObject("dev-cell-K_BL-" + dev_idx, Math.floor(KeyFlags/16)%2, "warning");
        updateFlagObject("dev-cell-K_TR-" + dev_idx, Math.floor(KeyFlags/2)%2, "warning");
        updateFlagObject("dev-cell-K_BR-" + dev_idx, Math.floor(KeyFlags)%2, "warning");
        document.getElementById("device-s-c-" + dev_idx + "-amp-off").innerText = amp_off.toString(16);
        document.getElementById("device-s-c-" + dev_idx + "-amp-read").innerText = amp_read.toString(16);
        document.getElementById("device-s-c-" + dev_idx + "-vol-read").innerText = vol_read.toString();
        document.getElementById("device-s-c-" + dev_idx + "-gold").innerText = goldenNumber.toString(16);
    }
}

var updateHYDDrive = function(data)
{
    checkType(data[2], data[4]);
    var intTime = (data[5] + data[6]*256 + data[7]*256*256);
    var intWork = (data[8] + data[9]*256 + data[10]*256*256);
    var intCalcP = (data[11] + data[12]*256 + data[13]*256*256);
    var RPM_SP = (data[15] + data[16]*256);
    var RPM_PV = (data[20] + data[21]*256);
    var RPM_Error = (data[22] + data[23]*256);
    var PWM_OutR = (data[25] + data[26]*256);
    var PWM_Out = (data[27]);
    var PWM_SP = (data[28]);
    var PulseRange = (data[29] + data[30]*256);
    var PumpVolume = (data[32] + data[33]*256);
    var Golden = (data[35] + data[36]*256 + data[37]*256*256 + data[38]*256*256*256 + data[39]*256*256*256*256);
    var NewSample = (data[40] + data[41]*256);
    var TargetFlow = (data[46] + data[47]*256 + data[48]*256*256);
    var PulseFlags = (data[49]);
    var System0 = (data[51]);
    var System1 = (data[52]);
    var KeyFlags = (data[53]);
    var vol_read = data[54];
    var engage = Math.floor(PulseFlags)%2;
    var error_flag = Math.floor(System1/2)%2 + 2;
    var timeout = Math.floor(System1)%2 + 4;
    var manual_control = Math.floor(System0/128)%2 + 6;
    var actualFlow = RPM_PV*PumpVolume;
    var Error_ = TargetFlow - actualFlow;
    var per = Error_*100/TargetFlow;
    var diagnosticFlags = data[58];
    
    if (document.getElementById("capture-data-3").className == 'ai-cell ai-cell-success') {
        pushLastPWM(PWM_OutR, RPM_PV);
    }
    if (document.getElementById("capture-data-4").className == 'ai-cell ai-cell-success') {
        pushLastRPM(TargetFlow, RPM_PV);
    }
    medcharts[3].updateReference(PumpVolume);
    //pushLastRPM(flow, RPM_PV);
    //deviceValues[17].updateLevel(RPM_PV);
    //document.getElementById("device-s-18-tar-rpm").innerText = RPM_SP;
    //document.getElementById("device-s-18-act-rpm").innerText = RPM_PV;
    charts[0].updateLevel([intTime]);
    charts[1].updateLevel([intWork]);
    charts[2].updateLevel([intCalcP]);
    charts[3].updateLevel([RPM_SP, RPM_PV]);
    charts[4].updateLevel([Error_]);
    charts[5].updateLevel([per]);
    charts[6].updateLevel([intCalcP]);
    charts[7].updateLevel([PWM_OutR]);
    charts[8].updateLevel([PWM_Out]);
    charts[9].updateLevel([PulseRange]);
    charts[10].updateLevel([PumpVolume]);
    charts[11].updateLevel([RPM_PV*PumpVolume]);
    charts[12].updateLevel([TargetFlow]);
    charts[13].updateLevel([PWM_Out, PWM_SP]);
    charts[14].updateLevel([Golden]);
    charts[15].updateLevel([engage, error_flag, timeout, manual_control]);
    charts[16].updateLevel([vol_read, 100, 52, 85]);
    dev_idx = -1;
    for (var i = 0; i < 40; i++) {
        if (document.getElementById("dev-cell-ID-" + i).innerText == data[4].toString()) {
            dev_idx = i;
            break;
        }
    }
    if (dev_idx != -1) {
        updateFlagObject("dev-cell-K_TL-" + dev_idx, Math.floor(KeyFlags/32)%2, "warning");
        updateFlagObject("dev-cell-K_BL-" + dev_idx, Math.floor(KeyFlags/16)%2, "warning");
        updateFlagObject("dev-cell-K_TR-" + dev_idx, Math.floor(KeyFlags/2)%2, "warning");
        updateFlagObject("dev-cell-K_BR-" + dev_idx, Math.floor(KeyFlags)%2, "warning");
        document.getElementById("device-s-h-" + dev_idx + "-vol-read").innerText = vol_read.toString();
    }

    if (document.getElementById("diag-hydr-start").className == "ai-cell ai-cell-success") {
        if (Math.floor(KeyFlags/32)%2 == 1) {
            document.getElementById("diag-hydr-keypad-1").className = "ai-cell ai-cell-success";
        } else if (Math.floor(KeyFlags/16)%2 == 1) {
            document.getElementById("diag-hydr-keypad-2").className = "ai-cell ai-cell-success";
        } else if (Math.floor(KeyFlags/2)%2 == 1) {
            document.getElementById("diag-hydr-keypad-3").className = "ai-cell ai-cell-success";
        } else if (Math.floor(KeyFlags)%2 == 1) {
            document.getElementById("diag-hydr-keypad-4").className = "ai-cell ai-cell-success";
        }

        if (Math.floor(diagnosticFlags)%2 == 1) {
            document.getElementById("diag-hydr-can-bus").className = "ai-cell ai-cell-success";
        }
        if (Math.floor(diagnosticFlags/2)%2 == 1) {
            document.getElementById("diag-hydr-pickup").className = "ai-cell ai-cell-success";
        }
        if (Math.floor(diagnosticFlags/4)%2 == 1) {
            document.getElementById("diag-hydr-motor").className = "ai-cell ai-cell-success";
        }

        diagnostic_hydr_count();
    }
}

var updateMainUnitS = function(data)
{
    switch(data[2]) {
        case 0:
            updateMainUnitS_0(data);
            break;
        case 1:
            updateMainUnitS_1(data);
            break;
        case 2:
            updateMainUnitS_2(data);
            break;
        case 3:
            updateMainUnitS_3(data);
            break;
        case 4:
            updateMainUnitS_4(data);
            break;
        case 5:
            updateMainUnitS_5(data);
            break;
        case 6:
            updateMainUnitS_6_21(data, 0);
            break;
        case 7:
            updateMainUnitS_6_21(data, 1);
            break;
        case 8:
            updateMainUnitS_6_21(data, 2);
            break;
        case 9:
            updateMainUnitS_6_21(data, 3);
            break;
        case 10:
            updateMainUnitS_6_21(data, 4);
            break;
        case 11:
            updateMainUnitS_6_21(data, 5);
            break;
        case 12:
            updateMainUnitS_6_21(data, 6);
            break;
        case 13:
            updateMainUnitS_6_21(data, 7);
            break;
        case 14:
            updateMainUnitS_6_21(data, 8);
            break;
        case 15:
            updateMainUnitS_6_21(data, 9);
            break;
        case 16:
            updateMainUnitS_6_21(data, 10);
            break;
        case 17:
            updateMainUnitS_6_21(data, 11);
            break;
        case 18:
            updateMainUnitS_6_21(data, 12);
            break;
        case 19:
            updateMainUnitS_6_21(data, 13);
            break;
        case 20:
            updateMainUnitS_6_21(data, 14);
            break;
        case 21:
            updateMainUnitS_6_21(data, 15);
            break;
        case 22:
            updateMainUnitS_22(data);
            break;
        case 23:
            updateMainUnitS_23(data);
            break;
    }
}

var updateMainUnitS_0 = function(data)
{
    
}

var updateMainUnitS_1 = function(data)
{
    
}

var updateMainUnitS_2 = function(data)
{
    
}

var updateMainUnitS_3 = function(data)
{
    
}

var updateMainUnitS_4 = function(data)
{
    /*document.getElementById("device-s-1-env-tx").innerText = (256 + data[12]).toString(16);
    document.getElementById("device-s-2-env-tx").innerText = (256 + data[13]).toString(16);
    document.getElementById("device-s-3-env-tx").innerText = (256 + data[14]).toString(16);
    document.getElementById("device-s-4-env-tx").innerText = (256 + data[15]).toString(16);
    document.getElementById("device-s-5-env-tx").innerText = (256 + data[16]).toString(16);
    document.getElementById("device-s-6-env-tx").innerText = (256 + data[17]).toString(16);
    document.getElementById("device-s-7-env-tx").innerText = (256 + data[18]).toString(16);
    document.getElementById("device-s-8-env-tx").innerText = (256 + data[19]).toString(16);
    document.getElementById("device-s-9-env-tx").innerText = (256 + data[20]).toString(16);
    document.getElementById("device-s-1-env-rx").innerText = (256 + data[21]).toString(16);
    document.getElementById("device-s-2-env-rx").innerText = (256 + data[22]).toString(16);
    document.getElementById("device-s-3-env-rx").innerText = (256 + data[23]).toString(16);
    document.getElementById("device-s-4-env-rx").innerText = (256 + data[24]).toString(16);
    document.getElementById("device-s-5-env-rx").innerText = (256 + data[25]).toString(16);
    document.getElementById("device-s-6-env-rx").innerText = (256 + data[26]).toString(16);
    document.getElementById("device-s-7-env-rx").innerText = (256 + data[27]).toString(16);
    document.getElementById("device-s-8-env-rx").innerText = (256 + data[28]).toString(16);
    document.getElementById("device-s-9-env-rx").innerText = (256 + data[29]).toString(16);*/
}

var updateMainUnitS_5 = function(data)
{
    /*document.getElementById("device-s-10-env-tx").innerText = (256 + data[18]).toString(16);
    document.getElementById("device-s-11-env-tx").innerText = (256 + data[19]).toString(16);
    document.getElementById("device-s-12-env-tx").innerText = (256 + data[20]).toString(16);
    document.getElementById("device-s-13-env-tx").innerText = (256 + data[21]).toString(16);
    document.getElementById("device-s-14-env-tx").innerText = (256 + data[22]).toString(16);
    document.getElementById("device-s-15-env-tx").innerText = (256 + data[23]).toString(16);
    document.getElementById("device-s-16-env-tx").innerText = (256 + data[24]).toString(16);
    document.getElementById("device-s-17-env-tx").innerText = (256 + data[25]).toString(16);
    document.getElementById("device-s-18-env-tx").innerText = (256 + data[26]).toString(16);
    document.getElementById("device-s-10-env-rx").innerText = (256 + data[27]).toString(16);
    document.getElementById("device-s-11-env-rx").innerText = (256 + data[28]).toString(16);
    document.getElementById("device-s-12-env-rx").innerText = (256 + data[29]).toString(16);
    document.getElementById("device-s-13-env-rx").innerText = (256 + data[30]).toString(16);
    document.getElementById("device-s-14-env-rx").innerText = (256 + data[31]).toString(16);
    document.getElementById("device-s-15-env-rx").innerText = (256 + data[32]).toString(16);
    document.getElementById("device-s-16-env-rx").innerText = (256 + data[33]).toString(16);
    document.getElementById("device-s-17-env-rx").innerText = (256 + data[34]).toString(16);
    document.getElementById("device-s-18-env-rx").innerText = (256 + data[35]).toString(16);*/
}

var updateMainUnitS_6_21 = function(data, idx)
{
    var id = idx + 1;
    //deviceValues[idx].updateLevel(data[16] + data[17]*256);
    //document.getElementById("device-s-" + id + "-act-flow").innerText = (data[14] + data[15]*256);
    //document.getElementById("device-s-" + id + "-act-app").innerText = (data[16] + data[17]*256);
    //document.getElementById("device-s-" + id + "-tar-flow").innerText = (data[10] + data[11]*256);
    //document.getElementById("device-s-" + id + "-tar-app").innerText = (data[12] + data[13]*256);
    
    //updateValueObject("device-s-" + id + "-act", Math.floor(data[4]/16)%2, ["ON","OFF"], "warning");
    //updateValueObject("device-s-" + id + "-eng", Math.floor(data[18])%2, ["ON","OFF"], "warning");
    //updateValueObject("device-s-" + id + "-amp", Math.floor(data[18]/2)%2, ["ON","OFF"], "danger");
    //var volt = ((data[5])*144/92/10).toFixed(1);
    //updateFlagObject("device-s-" + id + "-volt", (volt < 10 || volt > 14), "danger");
    //document.getElementById("device-s-" + id + "-volt").innerText = volt;
    //updateValueObject("device-s-" + id + "-water", Math.floor(data[4]/8)%2, ["ON","OFF"], "danger");
    //updateValueObject("device-s-" + id + "-error", Math.floor(data[4]/4)%2, ["ON","OFF"], "danger");
    //updateValueObject("device-s-" + id + "-no-response", Math.floor(data[4]/2)%2, ["ON","OFF"], "danger");
    
    //document.getElementById("device-s-" + id + "-type").innerText = "FLOW";
    //document.getElementById("device-s-" + id + "-id").innerText = "0xA5";
    //document.getElementById("device-s-" + id + "-fidx").innerText = id;
    //document.getElementById("device-s-" + id + "-didx").innerText = id;
    //document.getElementById("device-s-" + id + "-soft").innerText = (data[6] + data[7]*256)/10;
    //document.getElementById("device-s-" + id + "-hard").innerText = (data[8] + data[9]*256)/10;
    //document.getElementById("device-s-" + id + "-calibration").innerText = (data[19]);
}

var updateMainUnitS_22 = function(data)
{
    //deviceValues[16].updateLevel(data[13] + data[14]*256);
    //document.getElementById("device-s-17-pulse").innerText = (data[11] + data[12]*256);
    //document.getElementById("device-s-17-speed").innerText = (data[13] + data[14]*256);
    //document.getElementById("device-s-17-tar-pres").innerText = (data[15] + data[16]*256);
    //document.getElementById("device-s-17-act-pres").innerText = (data[17] + data[18]*256);
    
    /*updateValueObject("device-s-17-act", Math.floor(data[4]/16)%2, ["ON","OFF"], "warning");
    updateValueObject("device-s-17-eng", Math.floor(data[18])%2, ["ON","OFF"], "warning");
    var volt = ((data[5])*144/92/10).toFixed(1);
    updateFlagObject("device-s-17-volt", (volt < 10 || volt > 14), "danger");
    document.getElementById("device-s-17-volt").innerText = volt;
    updateValueObject("device-s-17-water", Math.floor(data[4]/8)%2, ["ON","OFF"], "danger");
    updateValueObject("device-s-17-error", Math.floor(data[4]/4)%2, ["ON","OFF"], "danger");
    updateValueObject("device-s-17-no-response", Math.floor(data[4]/2)%2, ["ON","OFF"], "danger");
    
    document.getElementById("device-s-17-type").innerText = "FC PER";
    document.getElementById("device-s-17-id").innerText = "0xFC";
    document.getElementById("device-s-17-fidx").innerText = 1;
    document.getElementById("device-s-17-didx").innerText = 17;
    document.getElementById("device-s-17-soft").innerText = (data[6] + data[7]*256)/10;
    document.getElementById("device-s-17-hard").innerText = (data[8] + data[9]*256)/10;*/
}

var updateMainUnitS_23 = function(data)
{
    /*document.getElementById("device-s-18-tar-flow").innerText = (data[10] + data[11]*256 + data[12]*256*256);
    document.getElementById("device-s-18-tar-volume").innerText = (data[13] + data[14]*256);
    
    updateValueObject("device-s-18-act", Math.floor(data[4]/16)%2, ["ON","OFF"], "warning");
    updateValueObject("device-s-18-eng", Math.floor(data[15])%2, ["ON","OFF"], "warning");
    var volt = ((data[5])*144/92/10).toFixed(1);
    updateFlagObject("device-s-18-volt", (volt < 10 || volt > 14), "danger");
    document.getElementById("device-s-18-volt").innerText = volt;
    updateValueObject("device-s-18-water", Math.floor(data[4]/8)%2, ["ON","OFF"], "danger");
    updateValueObject("device-s-18-error", Math.floor(data[4]/4)%2, ["ON","OFF"], "danger");
    updateValueObject("device-s-18-no-response", Math.floor(data[4]/2)%2, ["ON","OFF"], "danger");
    
    document.getElementById("device-s-18-type").innerText = "HYDR";
    document.getElementById("device-s-18-id").innerText = "0xC7";
    document.getElementById("device-s-18-fidx").innerText = 1;
    document.getElementById("device-s-18-didx").innerText = 18;
    document.getElementById("device-s-18-soft").innerText = (data[6] + data[7]*256)/10;
    document.getElementById("device-s-18-hard").innerText = (data[8] + data[9]*256)/10;*/
}

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 last_pressure = 100;

var pushLastInt = function(steps, int)
{
    medcharts[0].updateLevel({x:steps, y:int, p:last_pressure});
}

var pushLastFlow = function(steps, flow)
{
    medcharts[1].updateLevel({x:steps, y:flow, p:last_pressure});
}

var pushLastPWM = function(PWM, RPM)
{
    medcharts[2].updateLevel({x:RPM, y:PWM, p:last_pressure});
}

var pushLastRPM = function(flow, RPM)
{
    medcharts[3].updateLevel({x:RPM, y:flow, p:last_pressure});
}

var pushLastPressure = function(pressure)
{
    last_pressure = pressure;
}