<%@ page contentType="text/html;charset=UTF-8" %>
<script>

var defPrintOption = {
    // post ws
    linkDeviceType: 'post',
    // usb tcp
    protocol: 'usb'
}

const __io_lnk = function() {
    return 'localhost:8008'
}
const __io_url = function() {
    return 'http://' + __io_lnk() + '/thermal.io?data='
}
const __ws_url = function() {
    return 'ws://' + __io_lnk() + '/thermal.ws'
}

/**
 * 枚举设备  获取设备
 * @private
 */
function _thermal_enum_printer_ () {
    var onBack = function onBack (isOk, res) {
        if (!isOk) {
            output_control('通讯失败：' + JSON.stringify(res));
            return
        }
        if (res.code !== 0) {
            output_control(res.msg);
            return
        }
        let enum_list = document.getElementById('enum_list')
        if (enum_list.length > 0) { enum_list.clear() }

        if (res.device.length > 0) {
            // for (let i = 0; i < res.device.length; i++) {
            //     let opt = document.createElement('option')
            //     opt.text = res.device[i]
            //     enum_list.options.add(opt)
            // }
            console.log(res);
        } else {
            output_control(res.message)
        }
    }
    let pack = {
        command: '_thermal_enum_printer_',
        require: require_type(),
    }
    on_link_device(pack, onBack);
}

function require_type () {
    return defPrintOption.protocol;
}

function on_link_device (pack, onBack) {
    let radio = document.getElementsByName('req_model')
    if (radio != null) {
        if (radio.length > 0) {
            if (radio[0].checked) { jqpost(__io_url(), pack, onback) }
        }
        if (radio.length > 1) {
            if (radio[1].checked) { jqlink(__ws_url(), pack, onback) }
        }
    }
}

// 输出信息
function output_control (text) {
    console.log(text);
}

</script>
