
ipcRenderer.on('rain-message' , function(event , data){ 
    //Can Message
    updateRAINMessage(data);
});

var data_phase = 0; //0:start,1:type,2:data
var data_length = 0;
var data_idx = 0;
var data_array = [];
var internal_read_pointer = 0;
var log_write_pointer = 0;
var log_read_pointer = 0x40000;
var device_pointer = 0; //0: internal, 1: external
var download_phase = 0;
var escape = 0;

var updateRAINMessage = function(data)
{
    var length = data.length;
    for (var i = 2; i < length - 1; i++) {
        processRainByte(data[i]);
    }
    escape = 0;
    for (var i = 0; i < length; i++) {
        processAnotherByte(data[i]);
    }
}


var processRainByte = function(data)
{
    var table = document.getElementById("rain-data-table");
    //var table = document.getElementById("rain-message-table");
    if (escape == 1) {
        if (data == 0x52) data = 0x23;
        if (data == 0x53) data = 0x24;
        if (data == 0x5E) data = 0x2F;
        escape = 0;
    } else {
        if (data == 0x2F) {
            escape = 1;
            return;
        }
    }
    var rows = table.children;
    var columns = rows[rows.length - 1].children;
    if (columns.length >= 18) {
        var node = document.createElement("tr"); 
        var cell1 = document.createElement("td");
        var cell2 = document.createElement("td");
        if (device_pointer == 0) {
            cell1.className = "ai-cell ai-cell-warning";
            cell2.className = "ai-cell ai-cell-warning";
            var t = document.createTextNode(("00000" + (internal_read_pointer*16).toString(16)).substr(-5));
            cell1.appendChild(t);
        } else {
            cell1.className = "ai-cell";
            cell2.className = "ai-cell";
            var t = document.createTextNode(("00000" + (log_read_pointer - 16).toString(16)).substr(-5));
            cell1.appendChild(t);
        }
        node.appendChild(cell1);
        node.appendChild(cell2);
        table.appendChild(node);
    }

    var node = document.createElement("td");
    if (device_pointer == 0) {
        node.className = "ai-cell ai-cell-warning";
    }
    var t = document.createTextNode(("00" + data.toString(16)).substr(-2));
    node.appendChild(t);
    
    var r = columns[1].innerHTML += String.fromCharCode(data);
    table.children[rows.length - 1].appendChild(node);
}

var read_process_1 = function()
{
    //internal eeprom 0x00
    ipcRenderer.send('rain-message', "$" + String.fromCharCode(0x5F) + String.fromCharCode(0x00) + String.fromCharCode(0x00) + String.fromCharCode(0xA1));
}

var read_process_2 = function()
{
    //internal eeprom 0x10
    ipcRenderer.send('rain-message', "$" + String.fromCharCode(0x5F) + String.fromCharCode(0x00) + String.fromCharCode(0x10) + String.fromCharCode(0x91));
}

var read_process_3 = function()
{
    //internal eeprom 0x10
    ipcRenderer.send('rain-message', "$" + String.fromCharCode(0x7F) + String.fromCharCode(0x10) + String.fromCharCode(log_write_pointer%256) + String.fromCharCode(floor(log_write_pointer/256)%256));
}

var processAnotherByte = function(data) {
    if (escape == 1) {
        if (data == 0x52) data = 0x23;
        if (data == 0x53) data = 0x24;
        if (data == 0x5E) data = 0x2F;
        escape = 0;
    } else {
        if (data == 0x2F) {
            escape = 1;
            return;
        }
    }
    if (data_phase == 0) {
        if (data == 0x23) {
            //start byte received
            data_phase = 1;
            data_array = [0x23];
        }
    } else if (data_phase == 1) {
        data_phase = 2;
        if (data == 0x9F) data_length = 19;
        data_idx = 2;
        data_array[1] = data;
    } else {
        data_array[data_idx] = data;
        data_idx++;
        if (data_idx >= data_length) {
            push_rain_line();
            data_phase = 0;
        }
    }
}

var push_rain_line = function() {
    if (data_array[1] == 0x9F && device_pointer == 0) {
        reply_internal();
    } else if (data_array[1] == 0x97) {
        reply_clock();
    } else if (data_array[1] == 0x9F && device_pointer == 1) {
        reply_external();
    }
    if (download_phase == 0) {
        var buffer = Buffer.from([0x24, 0x5F, 0x10, 0x91]);
        ipcRenderer.send('rain-message', buffer);
        download_phase++;
        internal_read_pointer++;
    } else if (download_phase == 1) {
        var buffer = Buffer.from([0x24, 0x5F, 0x20, 0x81]);
        ipcRenderer.send('rain-message', buffer);
        download_phase++;
        internal_read_pointer++;
    } else if (download_phase == 2) {
        if (log_read_pointer < log_write_pointer && log_read_pointer <= (0x41000)) {
            var low = log_read_pointer%256;
            var high = Math.floor(log_read_pointer/256)%256;
            var upper = Math.floor(log_read_pointer/256/256);
            var check = (((0x7F+upper+high+low)%0x100)^0xFF) + 1;
            var buffer = Buffer.from([0x24, 0x7F, upper, high, low, check]);
            ipcRenderer.send('rain-message', buffer);
            log_read_pointer += 16;
            device_pointer = 1;
        }
    }
}

var reply_clock = function()
{
    var table = document.getElementById("rain-message-table");
    var type = '<td><div class="ai-cell ai-cell-danger">UNKN</div></td>';
    var date_sec = data_array[2]%16 + Math.floor(data_array[2]/16);
    var date_min = data_array[3]%16 + Math.floor(data_array[3]/16);
    var date_hour = data_array[4]%16 + Math.floor(data_array[4]/16);
    var date_day = data_array[5]%16 + Math.floor(data_array[5]/16);
    var date_month = data_array[6]%16 + Math.floor(data_array[6]/16);
    var date_year = data_array[7]%16 + Math.floor(data_array[7]/16);
    var type = '<td><div class="ai-cell ai-cell-secondary-1">TIME</div></td>';
    var data = '<td>Date: ' + ("00" + date_sec).substr(-2) + ':' + ("00" + date_min).substr(-2) + ':' + ("00" + date_hour).substr(-2) + ' Time: ' + ("00" + date_day).substr(-2) + '/' + ("00" + date_month).substr(-2) + '/' + ("00" + date_year).substr(-2) + '</td>';
    var hex = '<td>' + data_array[2].toString(16) + data_array[3].toString(16) + data_array[4].toString(16) + data_array[5].toString(16) + data_array[6].toString(16) + data_array[7].toString(16) + '</td>';

    var row = '<tr>' + type + data + hex + '</tr>';
    table.innerHTML += row;
}

var reply_internal = function()
{
    var table = document.getElementById("rain-message-table");
    var type = '<td><div class="ai-cell ai-cell-danger">UNKN</div></td>';
    if (internal_read_pointer == 0) {
        var type = '<td><div class="ai-cell ai-cell-secondary-1">FIRM</div></td>';
        var data = '<td>Firmware version: ' + String.fromCharCode(data_array[4]) + String.fromCharCode(data_array[5]) + String.fromCharCode(data_array[6]) + String.fromCharCode(data_array[7]) + ' </td>';
        var hex = '<td>' + ("00" + data_array[4].toString(16)).substr(-2) + ("00" + data_array[5].toString(16)).substr(-2) + ("00" + data_array[6].toString(16)).substr(-2) + ("00" + data_array[7].toString(16)).substr(-2) + '</td>';
        var row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        type = '<td><div class="ai-cell ai-cell-secondary-1">POWE</div></td>';
        data = '<td>Power-ups: ' + (data_array[8] + data_array[9]*256).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[8].toString(16)).substr(-2) + ("00" + data_array[9].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        type = '<td><div class="ai-cell ai-cell-secondary-1">CONN</div></td>';
        data = '<td>Connections: ' + (data_array[10] + data_array[11]*256).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[10].toString(16)).substr(-2) + ("00" + data_array[11].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        type = '<td><div class="ai-cell ai-cell-secondary-1">BATT</div></td>';
        data = '<td>Battery Voltage: ' + (((data_array[12] + 180) * 0.0059) + 0.75).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[12].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        type = '<td><div class="ai-cell ai-cell-secondary-1">READ</div></td>';
        data = '<td>Log Read Pointer: ' + (data_array[13] + data_array[14]*256 + data_array[15]*256*256).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[13].toString(16)).substr(-2) + ("00" + data_array[14].toString(16)).substr(-2) + ("00" + data_array[15].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        log_write_pointer = log_write_pointer - log_write_pointer%(256*256) + (data_array[16] + data_array[17]*256);
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">WRIT</div></td>';
        var data = '<td>Log Write Pointer - part 1: ' + (data_array[16] + data_array[17]*256).toString(16) + ' </td>';
        hex = '<td>' + ("00" + data_array[16].toString(16)).substr(-2) + ("00" + data_array[17].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
    } else if (internal_read_pointer == 1) {
        log_write_pointer = log_write_pointer - (Math.floor(log_write_pointer/(256*256)) - data_array[2])*256*256;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">WRIT</div></td>';
        var data = '<td>Log Write Pointer - part 2: ' + log_write_pointer.toString(16) + ' </td>';
        hex = '<td>' + ("00" + data_array[2].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">RA_P</div></td>';
        var data = '<td>Rain Pulse: ' + (data_array[3] + data_array[4]*256 + data_array[5]*256*256).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[3].toString(16)).substr(-2) + ("00" + data_array[4].toString(16)).substr(-2) + ("00" + data_array[5].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">L_HU</div></td>';
        var data = '<td>Humidity Low: ' + (data_array[6]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[6].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">H_HU</div></td>';
        var data = '<td>Humidity High: ' + (data_array[7]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[7].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">L_TE</div></td>';
        var data = '<td>Temperature Low: ' + (((data_array[8] + data_array[9]*256) - 5000)/100).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[8].toString(16)).substr(-2) + ("00" + data_array[9].toString(16)).substr(-2)+ '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">H_TE</div></td>';
        var data = '<td>Temperature High: ' + (((data_array[10] + data_array[11]*256) - 5000)/100).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[10].toString(16)).substr(-2) + ("00" + data_array[11].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">DE_1</div></td>';
        var data = '<td>Debug 1: ' + (data_array[12]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[12].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">DE_2</div></td>';
        var data = '<td>Debug 2: ' + (data_array[13]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[13].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">DE_3</div></td>';
        var data = '<td>Debug 3: ' + (data_array[14]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[14].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">HU_0</div></td>';
        var data = '<td>Humidity 0: ' + (data_array[15]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[15].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;

        var type = '<td><div class="ai-cell ai-cell-secondary-1">TE_0</div></td>';
        var data = '<td>Temperature 0: ' + (((data_array[16] + data_array[17]*256) - 5000)/100).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[16].toString(16)).substr(-2) + ("00" + data_array[17].toString(16)).substr(-2)+ '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
    } else {
        var type = '<td><div class="ai-cell ai-cell-secondary-1">AH_S</div></td>';
        var data = '<td>AHT10_S_0: ' + (data_array[2]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[2].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">AHH1</div></td>';
        var data = '<td>AHT10_H1_0: ' + (data_array[3]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[3].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">AHH2</div></td>';
        var data = '<td>AHT10_H2_0: ' + (data_array[4]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[4].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">AHHT</div></td>';
        var data = '<td>AHT10_HT_0: ' + (data_array[5]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[5].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">AHT1</div></td>';
        var data = '<td>AHT10_T1_0: ' + (data_array[6]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[6].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">AHT2</div></td>';
        var data = '<td>AHT10_T2_0: ' + (data_array[7]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[7].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">EVQU</div></td>';
        var data = '<td>Event Queue Available: ' + (data_array[8]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[8].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">EEBU</div></td>';
        var data = '<td>EEPROM Buffered 0: ' + (data_array[9]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[9].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">SEOF</div></td>';
        var data = '<td>Seconds Offset 0: ' + (data_array[10] + data_array[11]*256 + data_array[12]*256).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[10].toString(16)).substr(-2) + ("00" + data_array[11].toString(16)).substr(-2) + ("00" + data_array[12].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
        
        var type = '<td><div class="ai-cell ai-cell-secondary-1">RATL</div></td>';
        var data = '<td>Rain Pulse Time Lost: ' + (data_array[13]).toString() + ' </td>';
        hex = '<td>' + ("00" + data_array[13].toString(16)).substr(-2) + '</td>';
        row = '<tr>' + type + data + hex + '</tr>';
        table.innerHTML += row;
    }
}

var external_last = [];
var external_type = 0;
var external_length = 0;
var external_count = 0;

var reply_external = function()
{
    for (var i = 2; i < data_array.length - 1; i++) {
        process_external(data_array[i]);
    }
}

var process_external = function(data)
{
    if (external_count == 0) {
        if (data == 0xA6) {
            external_length = 19;
            external_last[external_last.length] = data;
            external_count = 1;
        } else if (data == 0xC7 || data == 0xC6) {
            external_length = 3;
            external_last[external_last.length] = data;
            external_count = 1;
        } else if (data == 0xAE) {
            external_length = 7;
            external_last[external_last.length] = data;
            external_count = 1;
        } else {
            var table = document.getElementById("rain-message-table");
            
            var type = '<td><div class="ai-cell ai-cell-danger">UNKN</div></td>';
            var data_ = '<td>Unknown data</td>';
            var hex = '<td>' + data.toString(16) + '</td>';
            row = '<tr>' + type + data_ + hex + '</tr>';
            table.innerHTML += row;
        }
    } else {
        external_last[external_last.length] = data;
        external_count++;
        if (external_count >= external_length) {
            console.log(external_last);
            if (external_last[0] == 0xA6) {
                var table = document.getElementById("rain-message-table");
                
                type = '<td><div class="ai-cell ai-cell-success">TYPE</div></td>';
                data = '<td>Type: ' + external_last[1].toString(16) + external_last[2].toString(16) + '</td>';
                hex = '<td>' + ("00" + external_last[1].toString(16)).substr(-2) + ("00" + external_last[2].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;
                
                var type = '<td><div class="ai-cell ai-cell-primary-1">DATE</div></td>';
                var data = '<td>Date: ' + ("00" + (Math.floor(external_last[5]/16)*10 + external_last[5]%16)).substr(-2) + '/' + ("00" + (Math.floor(external_last[4]/16)*10 + external_last[4]%16)).substr(-2) + '/' + ("00" + (2000 + Math.floor(external_last[3]/16)*10 + external_last[3]%16)).substr(-2) + ', Time: ' + ("00" + (Math.floor(external_last[6]/16)*10 + external_last[6]%16)).substr(-2) + ':' + ("00" + (Math.floor(external_last[7]/16)*10 + external_last[7]%16)).substr(-2) + ':' + ("00" + (Math.floor(external_last[8]/16)*10 + external_last[8]%16)).substr(-2) + '</td>';
                hex = '<td>' + ("00" + external_last[3].toString(16)).substr(-2) + ("00" + external_last[4].toString(16)).substr(-2) + ("00" + external_last[5].toString(16)).substr(-2) + ("00" + external_last[6].toString(16)).substr(-2) + ("00" + external_last[7].toString(16)).substr(-2) + ("00" + external_last[8].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;
                
                type = '<td><div class="ai-cell ai-cell-primary-1">RAIN</div></td>';
                data = '<td>Total: ' + external_last[11]*256*256 + external_last[10]*256 + external_last[9] + '</td>';
                hex = '<td>' + ("00" + external_last[9].toString(16)).substr(-2) + ("00" + external_last[10].toString(16)).substr(-2) + ("00" + external_last[11].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;
                
                type = '<td><div class="ai-cell ai-cell-warning">L_HU</div></td>';
                data = '<td>Total: ' + external_last[12] + '</td>';
                hex = '<td>' + ("00" + external_last[12].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;
                
                type = '<td><div class="ai-cell ai-cell-warning">H_HU</div></td>';
                data = '<td>Total: ' + external_last[13] + '</td>';
                hex = '<td>' + ("00" + external_last[13].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;
                
                type = '<td><div class="ai-cell ai-cell-warning">L_TE</div></td>';
                data = '<td>Low Temperature: ' + ((external_last[15]*256 + external_last[14]) - 5000)/100 + '</td>';
                hex = '<td>' + ("00" + external_last[14].toString(16)).substr(-2) + ("00" + external_last[15].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;
                
                type = '<td><div class="ai-cell ai-cell-warning">H_TE</div></td>';
                data = '<td>High Temperature: ' + ((external_last[17]*256 + external_last[16]) - 5000)/100 + '</td>';
                hex = '<td>' + ("00" + external_last[16].toString(16)).substr(-2) + ("00" + external_last[17].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;

            } else if (external_last[0] == 0xC7 || external_last[0] == 0xC6) {
                var table = document.getElementById("rain-message-table");
                var type = '<td><div class="ai-cell ai-cell-info-2">TICK</div></td>';
                var total = 0;
                if (external_last[0] == 0xC7) total = 256*256;
                var data = '<td>Rain Tick - miliseconds: ' + (external_last[1]*256 + external_last[2] + total) + ' </td>';
                hex = '<td>' + ("00" + external_last[1].toString(16)).substr(-2) + ("00" + external_last[2].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;
            } else if (external_last[0] == 0xAE) {
                var table = document.getElementById("rain-message-table");
                var type = '<td><div class="ai-cell ai-cell-primary-1">DATE</div></td>';
                var data = '<td>Date: ' + (floor(external_last[4]/16)*10 + external_last[4]%16) + '/' + (floor(external_last[3]/16)*10 + external_last[3]%16) + '/' + (2000 + floor(external_last[2]/16)*10 + external_last[2]%16) + ', Time: ' + (floor(external_last[5]/16)*10 + external_last[5]%16) + ':' + (floor(external_last[6]/16)*10 + external_last[6]%16) + ':' + (floor(external_last[7]/16)*10 + external_last[7]%16) + '</td>';
                hex = '<td>' + ("00" + external_last[2].toString(16)).substr(-2) + ("00" + external_last[3].toString(16)).substr(-2) + ("00" + external_last[4].toString(16)).substr(-2) + ("00" + external_last[5].toString(16)).substr(-2) + ("00" + external_last[6].toString(16)).substr(-2) + ("00" + external_last[7].toString(16)).substr(-2) + '</td>';
                row = '<tr>' + type + data + hex + '</tr>';
                table.innerHTML += row;
            }
            external_last = [];
            external_count = 0;
            external_length = 0;
            external_type = 0;
        }
    }
}

function export_rain()
{
    var children = document.getElementById("rain-data-table").children;
    var output = "";
    for (var i = 1; i < children.length; i++) {
        var address = children[i].children[0].innerHTML;
        var type = address.substr(0,1);
        var checksum = 0x1F;
        if (type == "0") {
            output += " 0x24 0x1F";
            var add_int = parseInt("0x" + address.substr(3,2));
            output += " 0x" + address.substr(3,2);
            checksum += add_int;
        } else {
            output += " 0x24 0x3F";
            var add_int_1 = parseInt("0x0" + address.substr(0,1));
            var add_int_2 = parseInt("0x" + address.substr(1,2));
            var add_int_3 = parseInt("0x" + address.substr(3,2));
            output += " 0x0" + address.substr(0,1);
            output += " 0x" + address.substr(1,2);
            output += " 0x" + address.substr(3,2);
            checksum += add_int_1 + add_int_2 + add_int_3;
        }

        for (var j = 0; j < 16; j++) {
            var byte = children[i].children[j + 2].innerHTML;
            var byte_int = parseInt("0x" + byte);
            checksum += byte_int;
            output += " 0x" + children[i].children[j + 2].innerHTML;
        }
        checksum = checksum%0x100;
        checksum = (checksum^0xFF) + 0x01;
        output += " 0x" + ("00" + checksum.toString(16)).substr(-2) + "\n";
    }
    ipcRenderer.send('rain-export-message', output);
}

function download_rain()
{
    var html = '<tr><td>-----</td><td><div class="ai-cell ai-cell-info-1">ASCII</div><td><div class="ai-cell ai-cell-info-1">00</div></td><td><div class="ai-cell ai-cell-info-1">01</div></td><td><div class="ai-cell ai-cell-info-1">02</div></td><td><div class="ai-cell ai-cell-info-1">03</div></td><td><div class="ai-cell ai-cell-info-1">04</div></td><td><div class="ai-cell ai-cell-info-1">05</div></td><td><div class="ai-cell ai-cell-info-1">06</div></td><td><div class="ai-cell ai-cell-info-1">07</div></td><td><div class="ai-cell ai-cell-info-1">08</div></td><td><div class="ai-cell ai-cell-info-1">09</div></td><td><div class="ai-cell ai-cell-info-1">0A</div></td><td><div class="ai-cell ai-cell-info-1">0B</div></td><td><div class="ai-cell ai-cell-info-1">0C</div></td><td><div class="ai-cell ai-cell-info-1">0D</div></td><td><div class="ai-cell ai-cell-info-1">0E</div></td><td><div class="ai-cell ai-cell-info-1">0F</div></td></tr>';
    html += '<tr><td class="ai-cell ai-cell-warning">00000</td><td class="ai-cell ai-cell-warning"></td></tr>';
    document.getElementById("rain-data-table").innerHTML = html;
    document.getElementById("rain-message-table").innerHTML = "";

    var buffer = Buffer.from([0x24, 0x5F, 0x10, 0x91]);
    ipcRenderer.send('rain-message', buffer);

    data_phase = 0; //0:start,1:type,2:data
    data_length = 0;
    data_idx = 0;
    data_array = [];
    internal_read_pointer = 0;
    log_write_pointer = 0;
    log_read_pointer = 0x40000;
    device_pointer = 0; //0: internal, 1: external
    download_phase = 0;
}