<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- 新 Bootstrap4 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.1.0/css/bootstrap.min.css">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <!-- popper.min.js 用于弹窗、提示、下拉菜单 -->
    <script src="https://cdn.bootcss.com/popper.js/1.12.5/umd/popper.min.js"></script>
    <!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
    <script src="https://cdn.bootcss.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    <!--可用來建立使用者小圖示-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script language="JavaScript">
        // 指定 10秒 刷新網頁一次
        var dtn = '{{tid}}';
        console.log('dtn:' + dtn)

        WebUpdate_set = setInterval(function(){WebUpdate(dtn)} , 300 * 1000)
        var WebUpdate_set
    </script>

    <style>
        body {
            margin: 0;
        }

        .navbar-dark .navbar-nav .nav-link {
            color: white;
            cursor: pointer;
            text-decoration: none;
            width: 110px;
            height: 46px;
        }

        .nav-top {
            line-height: 40px;
            background-color: #C4C4C4;
        }

        .website_title {
            font-family: Roboto;
            font-style: normal;
            font-weight: normal;
            font-size: 30px;
            color: #000000;
        }

        .navbar-nav>li {
            float: none;
            display: inline-block;
            width: 100px;
            margin: 0 auto;
            text-align: center;
        }

        .navbar-nav>li a {
            font-size: 20px;
        }

        .main-page {
            margin-top: 200px;
        }

        .page-title {
            font-family: Roboto;
            font-style: normal;
            font-weight: bold;
            font-size: 36px;
        }


        .flex {
            display: flex;
            flex-direction: row;
            flex-wrap: wrap;
            justify-content: center;
        }

        .set-link {
            display: inline-block;
            width: 350px;
            height: 100px;
            line-height: 100px;
            background: #008CBA;
            border: 1px solid #CFCFCF;
            box-sizing: border-box;
            color: #FFFFFF;
            border-radius: 10px;
            font-size: 36px;
        }

        .cmn-toggle {
            position: absolute;
            margin-left: 0px;
            visibility: hidden;
        }

        .cmn-toggle+label {
            display: block;
            position: relative;
            cursor: pointer;
            outline: none;
            user-select: none;
        }

        input.cmn-toggle-round-flat+label {
            padding: 2px;
            width: 60px;
            height: 30px;
            background-color: #C0C0C0;
            border-radius: 60px;
            transition: background 0.4s;
        }

        input.cmn-toggle-round-flat+label:before,
        input.cmn-toggle-round-flat+label:after {
            display: block;
            position: absolute;
            content: "";
        }

        input.cmn-toggle-round-flat+label:before {
            top: 2px;
            left: 2px;
            bottom: 2px;
            right: 2px;
            background-color: #fff;
            border-radius: 60px;
            transition: background 0.4s;
        }

        input.cmn-toggle-round-flat+label:after {
            top: 4px;
            left: 4px;
            bottom: 4px;
            width: 24px;
            background-color: #dddddd;
            border-radius: 52px;
            transition: margin 0.4s, background 0.4s;
        }

        input.cmn-toggle-round-flat:checked+label {
            background-color: #C0C0C0;
        }

        input.cmn-toggle-round-flat:checked+label:after {
            margin-left: 27px;
            background-color: #008CBA;
        }

        @media(max-width:373px) {
            .card {
                margin-right: 0px;
            }

            .set-link {
                width: 250px;
            }
        }

        @media(max-width:577px) {}

        @media(min-width:576px) {}

        @media(min-width:768px) {
            .navbar-nav>li {
                margin-left: 0px;
            }

            .navbar-nav .li-block {
                display: none;
            }
        }

        @media(min-width:991px) {
            .navbar-nav>li {
                margin-left: 20px;
            }

            .navbar-nav .li-block {
                display: none;
            }
        }

        @media(min-width:1200px) {
            .navbar-nav>li {
                margin-left: 50px;
            }

            .navbar-nav .li-block {
                display: inline-block;
                width: 100px;
            }
        }

        @media(min-width:1400px) {
            .navbar-nav .li-block {
                display: inline-block;
                width: 200px;
            }
        }


        @media(min-width:1689px) {
            .navbar-nav>li {
                margin-left: 50px;
            }

            .navbar-nav .li-block {
                display: inline-block;
                width: 500px;
            }
        }
    </style>
    <script>
        // Rita 制動器運作

        var tank_num = '{{tid}}';

        // Benson 真空吸料機 (START)入料儲豆槽
        function inputVacuum() {
            var status = "off";
            var check = $("input[name=input_vacuum_status]:checked");  // 這裡面是 jQuery 撈取資料的方法, jQuery 常使用到 $ 錢字號
            console.log('check:', check);
            //大於0代表有被選中, 如果有多個可以呈現勾取的項目數量
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-02").prop('checked', false); // 設定為不要勾選
                if (!confirm("你確定要開啟入料儲豆槽真空吸料機嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-02").prop('checked', true); // 設定為勾選
                if (!confirm("你確定要關閉入料儲豆槽真空吸料機嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "input_vacuum_status", "value": status };
            // jquery 請求 '/mqtt/{{tid}}' 頁面
            $.post('/mqtt/{{tid}}', data, function (res) {      //res:HTTP response argument to the middleware function
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-02").prop('checked', true);
                    setTimeout("alert('入料儲豆槽真空吸料機_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-02").prop('checked', false);
                    setTimeout("alert('入料儲豆槽真空吸料機_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
        };

        function inputVacuum_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "input_vacuum_status", "value": "on" };

            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-02").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-02").prop('checked', false);
                } else {
                };
            }, 'text')
        }

        function inputVacuum_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "input_vacuum_status", "value": "off" };

            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-02").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-02").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        // Benson 真空吸料機 (END)入料儲豆槽
        // Benson 真空吸料機 (START)
        function tankVacuum() {
            var status = "off";
            var check = $("input[name=tank_vacuum_status]:checked");
            console.log('check:', check);
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-05").prop('checked', false);
                if (!confirm("你確定要開啟真空吸料機嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-05").prop('checked', true);
                if (!confirm("你確定要關閉真空吸料機嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-05").prop('checked', true);
                    setTimeout("alert('真空吸料機_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-05").prop('checked', false);
                    setTimeout("alert('真空吸料機_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_vacuum == 0) {
                    $("#cmn-toggle-05").prop('checked', false);
                } else if (res.tank_vacuum == 1) {
                    $("#cmn-toggle-05").prop('checked', true);
                }
            }, 'json');
        };

        function tankVacuum_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-05").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-05").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankVacuum_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-05").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-05").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        // Benson 真空吸料機 (END)
        function tankThreeWayValve() {
            //<!--setInterval(Relay,10000);-->
            var status = "off";
            var check = $("input[name=tank_threewayvalve_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-08").prop('checked', false);
                if (!confirm("你確定要開啟三通閥嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-08").prop('checked', true);
                if (!confirm("你確定要關閉三通閥嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_threewayvalve_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-08").prop('checked', true);
                    setTimeout("alert('三通閥入料_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-08").prop('checked', false);
                    setTimeout("alert('三通閥排氣_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_threewayvalve == 0) {
                    $("#cmn-toggle-08").prop('checked', false);
                } else if (res.tank_threewayvalve == 1) {
                    $("#cmn-toggle-08").prop('checked', true);
                }
            }, 'json');
        };
        function tankThreeWayValve_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_threewayvalve_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-08").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-08").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankThreeWayValve_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_threewayvalve_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-08").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-08").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        // Benson cargo2_actuator.html 控制蝴蝶閥函數 (START)
        function tankDiskValve() {
            //<!--setInterval(Relay,10000);-->
            var status = "off";
            var check = $("input[name=tank_diskvalve_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-11").prop('checked', false);
                if (!confirm("你確定要開啟蝴蝶閥嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-11").prop('checked', true);
                if (!confirm("你確定要關閉蝴蝶閥嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_diskvalve_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-11").prop('checked', true);
                    setTimeout("alert('蝴蝶閥_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-11").prop('checked', false);
                    setTimeout("alert('蝴蝶閥_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_diskvalve == 0) {
                    $("#cmn-toggle-11").prop('checked', false);
                } else if (res.tank_diskvalve == 1) {
                    $("#cmn-toggle-11").prop('checked', true);
                }
            }, 'json');
        };
        function tankDiskValve_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_diskvalve_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-11").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-11").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankDiskValve_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_diskvalve_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-11").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-11").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        // Benson cargo2_actuator.html 控制蝴蝶閥函數 (END)
        // Benson cargo2_actuator.html 閥 (START) 電磁閥消毒
        function tankSolenoidDisinfect() {
            var status = "off";
            var check = $("input[name=tank_solenoid_disinfect_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-14").prop('checked', false);
                if (!confirm("你確定要開啟電磁閥消毒嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-14").prop('checked', true);
                if (!confirm("你確定要關閉消毒電磁閥嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_solenoid_disinfect_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-14").prop('checked', true);
                    setTimeout("alert('電磁閥消毒_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-14").prop('checked', false);
                    setTimeout("alert('電磁閥消毒_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_solenoid_disinfect == 0) {
                    $("#cmn-toggle-14").prop('checked', false);
                } else if (res.tank_solenoid_disinfect == 1) {
                    $("#cmn-toggle-14").prop('checked', true);
                }
            }, 'json');
        };
        function tankSolenoidDisinfect_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_solenoid_disinfect_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-14").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-14").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankSolenoidDisinfect_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_solenoid_disinfect_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-14").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-14").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        // Benson cargo2_actuator.html 閥 (END) 電磁閥消毒
        // Benson cargo2_actuator.html 閥 (START) 電磁閥排水
        function tankSolenoidWater() {
            var status = "off";
            var check = $("input[name=tank_solenoid_water_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-17").prop('checked', false);
                if (!confirm("你確定要開啟電磁閥排水嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-17").prop('checked', true);
                if (!confirm("你確定要關閉排水電磁閥嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_solenoid_water_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-17").prop('checked', true);
                    setTimeout("alert('電磁閥排水_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-17").prop('checked', false);
                    setTimeout("alert('電磁閥排水_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_solenoid_water == 0) {
                    $("#cmn-toggle-17").prop('checked', false);
                } else if (res.tank_solenoid_water == 1) {
                    $("#cmn-toggle-17").prop('checked', true);
                }
            }, 'json');
        };
        function tankSolenoidWater_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_solenoid_water_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-17").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-17").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankSolenoidWater_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_solenoid_water_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-17").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-17").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        // Benson cargo2_actuator.html 閥 (END) 電磁閥排水
        /*
        function Motor() {
            $.get('/peeling', '', function (res) {
                //console.log(res.peeling);
                $("#motor_rpm_status").text(res.peeling + ' rpm(每1分鐘更新一次)');
            }, 'json');
        };
        function ChangeMotor() {
            var motor_data = $("input[name=tank_motor_status]").val();
            if (!confirm("你確定要更改轉速為" + motor_data + "嗎?")) {
                return false;
            };
            var data = { "tank_num": "D" + tank_num, "command": "tank_motor_status", "value": motor_data };
            console.log('data:', data)
            $.post('/mqtt/{{tid}}', data, function (res) {
                if (res == 'on') {
                    alert('更改成功');
                } else {
                    alert(res);
                };
            }, 'text');

            setTimeout(function(){ location.reload(); }, 500);
        };
        */
        // Benson cargo2_actuator.html 脫皮機馬達 (START) 馬達攪拌棒
        function ChangeMotor() {
            var motor_data = $("input[id=motor_rpm_data]").val();
            if (motor_data == '') {
                //$("#cmn-toggle-20").prop('checked', false);
                alert("請先輸入要運轉的值!");
                return false;

            } else if (motor_data == 0) {
                alert("轉速 0 為關閉馬達");

            } else if (Number(motor_data) < -50 || Number(motor_data) > 50 || !Number(motor_data)) {
                // 
                //$("#cmn-toggle-20").prop('checked', false);
                alert("您輸入的值已超過範圍,請重新輸入!" + !Number(motor_data));
                return false;
            };
            var value = "off";
            //var check = $("input[name=peeling-machine-on]:checked");
            if (motor_data != 0) {
                value = motor_data;
                //$("#cmn-toggle-20").prop('checked', false);
                if (!confirm("你確定要開啟攪拌馬達,運轉速度為 " + motor_data + " RPM 嗎?")) {
                    return false;
                };

            } else {
                //$("#cmn-toggle-20").prop('checked', true);
                if (!confirm("你確定要關閉攪拌馬達嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_motor_status", "value": motor_data };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    //$("#cmn-toggle-14").prop('checked', true); // prop 設置元素屬型與元素值, 設定 checked 屬性為 true
                    setTimeout("alert('攪拌馬達_開啟成功!')", 500); // 設定時間執行函式 delay(500), 只執行一次
                    var timer = setInterval(Rotate, 60000); // 啟動後會在 60000 毫秒(更新時間一分鐘)內不斷執行 (原因須配合更新時間?)

                } else if (res == 'off') {
                    //$("#cmn-toggle-14").prop('checked', false);
                    setTimeout("alert('攪拌馬達_關閉成功!')", 500);
                    clearInterval(timer);                   // 取消 timer 的不斷執行
                    $("#motor_rpm_status").text('');        // 設置 #motor_rpm_status 的文字為空, #井字號
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                $("#motor_rpm_data").attr("placeholder", res.tank_motor)
            }, 'json');
        };
        function ChangeMotor_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_motor_status", "value": "20" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    var timer = setInterval(Rotate, 60000);
                } else if (res == 'off') {
                    clearInterval(timer);
                    $("#motor_rpm_status").text('');
                } else {
                };
            }, 'text')
        }
        function ChangeMotor_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_motor_status", "value": "0" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    var timer = setInterval(Rotate, 60000);
                } else if (res == 'off') {
                    clearInterval(timer);
                    $("#motor_rpm_status").text('');
                } else {
                };
            }, 'text')
        }

        function Rotate() {
            $.get('/peeling', '', function (res) {
                //console.log(res.peeling);
                $("#motor_rpm_status-status").text(res.peeling + ' rpm(每1分鐘更新一次)');
            }, 'json');

            setTimeout(function () { location.reload(); }, 500);
        };

        // Benson cargo2_actuator.html 脫皮機馬達 (END) 馬達攪拌棒
        // Benson cargo2_actuator.html 機 (START)
        //鼓風機函數
        function tankBlower() {
            var status = "off";
            var check = $("input[name=tank_blower_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-23").prop('checked', false);
                if (!confirm("你確定要開啟鼓風機嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-23").prop('checked', true);
                if (!confirm("你確定要關閉鼓風機嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_blower_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-23").prop('checked', true);
                    setTimeout("alert('鼓風機_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-23").prop('checked', false);
                    setTimeout("alert('鼓風機_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_blower == 0) {
                    $("#cmn-toggle-23").prop('checked', false);
                } else if (res.tank_blower == 1) {
                    $("#cmn-toggle-23").prop('checked', true);
                }
            }, 'json');
        };
        function tankBlower_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_blower_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-23").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-23").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankBlower_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_blower_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-23").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-23").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        // Benson cargo2_actuator.html 鼓風機 (START)
        //電熱管1函數
        function tankHeater1() {
            var status = "off";
            var check = $("input[name=tank_heater1_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-26").prop('checked', false);
                if (!confirm("你確定要開啟電熱管1嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-26").prop('checked', true);
                if (!confirm("你確定要關閉電熱管1嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_heater1_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-26").prop('checked', true);
                    setTimeout("alert('電熱管1_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-26").prop('checked', false);
                    setTimeout("alert('電熱管1_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_heater1 == 0) {
                    $("#cmn-toggle-26").prop('checked', false);
                } else if (res.tank_heater1 == 1) {
                    $("#cmn-toggle-26").prop('checked', true);
                }
            }, 'json');
        };
        function tankHeater1_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_heater1_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-26").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-26").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankHeater1_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_heater1_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-26").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-26").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        //電熱管2函數
        function tankHeater2() {
            var status = "off";
            var check = $("input[name=tank_heater2_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-29").prop('checked', false);
                if (!confirm("你確定要開啟電熱管2嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-29").prop('checked', true);
                if (!confirm("你確定要關閉電熱管2嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "tank_heater2_status", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-29").prop('checked', true);
                    setTimeout("alert('電熱管2_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-29").prop('checked', false);
                    setTimeout("alert('電熱管2_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')

            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_heater2 == 0) {
                    $("#cmn-toggle-29").prop('checked', false);
                } else if (res.tank_heater2 == 1) {
                    $("#cmn-toggle-29").prop('checked', true);
                }
            }, 'json');
        };
        function tankHeater2_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_heater2_status", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-29").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-29").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankHeater2_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "tank_heater2_status", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-29").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-29").prop('checked', false);
                } else {
                };
            }, 'text')
        }

        // 溫度控制 
        function tankTemp1Enable() {
            var status = "off";
            var check = $("input[name=tank_temp1_enable_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-35").prop('checked', false);
                if (!confirm("你確定要開啟溫度控制嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-35").prop('checked', true);
                if (!confirm("你確定要關閉溫度控制嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "temp1_enable", "value": status };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-35").prop('checked', true);
                    setTimeout("alert('溫度控制_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-35").prop('checked', false);
                    setTimeout("alert('溫度控制_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')

            $.get('/loading/D{{tid}}', '', function (res) {
                if (res.tank_temp1_enable == 0) {
                    $("#cmn-toggle-35").prop('checked', false);
                } else if (res.tank_temp1_enable == 1) {
                    $("#cmn-toggle-35").prop('checked', true);
                }
            }, 'json');
        };
        function tankTemp1Enable_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "temp1_enable", "value": "on" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-35").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-35").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function tankTemp1Enable_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "temp1_enable", "value": "off" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    $("#cmn-toggle-35").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-35").prop('checked', false);
                } else {
                };
            }, 'text')
        }

        /* 判斷是否整數
        function isInteger(obj) {
            return obj % 1 === 0
        }
        isInteger(3) // true
        */

        // 設定溫度
        /*
        <h2>溫度控制設定(單位 ℃):(目前設定溫度) {{tank_temp1}}</h2>
            <input name="tank_temp1_data" type="text" placeholder="{{tank_temp1}}" style="width:70px;">℃ (限整數數值)
            <span id="tank_temp1_status" style="color:red;"></span>
            <input type="button" value="送出(設定溫度時請點擊)" name="tank_temp1_status" onclick="ChangeTemp1()">
        */
        function ChangeTemp1() {
            var temp_data = $("input[id=tank_temp1_data]").val();
            if (temp_data == '') {
                //$("#cmn-toggle-20").prop('checked', false);
                alert("請先輸入設定溫度!");
                return false;
            }
            var value = "0";
            //var check = $("input[name=peeling-machine-on]:checked");
            if ($("#cmn-toggle-35").prop('checked')) {
                value = temp_data;
                //$("#cmn-toggle-20").prop('checked', false);
                if (!confirm("你確定要設定內桶溫度為 " + temp_data + " ℃ 嗎?")) {
                    return false;
                };

            } else {
                //$("#cmn-toggle-20").prop('checked', true);
                //你確定要停止桶內溫度設定嗎
                if (!confirm("請先開啟溫控開關, 再設定桶內溫度")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "temp1", "value": temp_data };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
                if (res == 'on') {
                    //$("#cmn-toggle-14").prop('checked', true); // prop 設置元素屬型與元素值, 設定 checked 屬性為 true
                    setTimeout("alert('溫度設定_開啟成功!')", 500); // 設定時間執行函式 delay(500), 只執行一次
                    var timer = setInterval(Rotate, 60000); // 啟動後會在 60000 毫秒(更新時間一分鐘)內不斷執行 (原因須配合更新時間?)

                } else if (res == 'off') {
                    //$("#cmn-toggle-14").prop('checked', false);
                    setTimeout("alert('溫度設定_關閉成功!')", 500);
                    clearInterval(timer);                   // 取消 timer 的不斷執行
                    $("#motor_rpm_status").text('');        // 設置 #motor_rpm_status 的文字為空, #井字號
                } else {
                    alert(res);
                };
            }, 'text')

            $.get('/loading/D{{tid}}', '', function (res) {
                $("#tank_temp1_data").attr("placeholder", res.tank_temp1);
            }, 'json');
        };
        function ChangeTemp1_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "temp1", "value": "30" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
            }, 'text')
        }
        function ChangeTemp1_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "temp1", "value": "25" };
            $.post('/mqtt/{{tid}}', data, function (res) {
                console.log('data:', data)
            }, 'text')
        }

        // Benson 真空吸料機 (START) 出料儲豆槽
        function outputVacuum() {
            var status = "off";
            var check = $("input[name=output_vacuum_status]:checked");
            //大於0代表有被選中
            if (check.length > 0) {
                status = "on";
                $("#cmn-toggle-32").prop('checked', false);
                if (!confirm("你確定要開啟出料儲豆槽真空吸料機嗎?")) {
                    return false;
                };

            } else {
                $("#cmn-toggle-32").prop('checked', true);
                if (!confirm("你確定要關閉出料儲豆槽真空吸料機嗎?")) {
                    return false;
                };
            };

            var data = { "tank_num": "D" + tank_num, "command": "output_vacuum_status", "value": status };
            console.log('data:', data)
            $.post('/mqtt/{{tid}}', data, function (res) {
                if (res == 'on') {
                    $("#cmn-toggle-32").prop('checked', true);
                    setTimeout("alert('出料儲豆槽真空吸料機_開啟成功!')", 500);
                } else if (res == 'off') {
                    $("#cmn-toggle-32").prop('checked', false);
                    setTimeout("alert('出料儲豆槽真空吸料機_關閉成功!')", 500);
                } else {
                    alert(res);
                };
            }, 'text')
        };
        function outputVacuum_ON() {
            var data = { "tank_num": "D" + tank_num, "command": "output_vacuum_status", "value": "on" };
            console.log('data:', data)
            $.post('/mqtt/{{tid}}', data, function (res) {
                if (res == 'on') {
                    $("#cmn-toggle-32").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-32").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        function outputVacuum_OFF() {
            var data = { "tank_num": "D" + tank_num, "command": "output_vacuum_status", "value": "off" };
            console.log('data:', data)
            $.post('/mqtt/{{tid}}', data, function (res) {
                if (res == 'on') {
                    $("#cmn-toggle-32").prop('checked', true);
                } else if (res == 'off') {
                    $("#cmn-toggle-32").prop('checked', false);
                } else {
                };
            }, 'text')
        }
        // Benson 真空吸料機 (END) 出料儲豆槽
    </script>
</head>

<body>
    狀態更新時間(秒):
    <input name="webupdate_time" type="text" value="5" style="width:70px;">
    <input type="button" value="設定更新時間" onclick="changeUpdate()">
    設定溫度(與鼓風機連動)
    <input name="testing_tank_dry_temp" value="30" id="testing_tank_dry_temp" type="text" style="width:70px;">
    <script language="JavaScript">
        
        function changeUpdate() {
            clearInterval(WebUpdate_set);

            var webupdate_time = $("input[name=webupdate_time]").val()
            console.log('webupdate_time' + webupdate_time)
            WebUpdate_set = setInterval(function(){WebUpdate(dtn)} , webupdate_time * 1000);
        }

        // 到達 30 度時關閉加熱, 出豆
        function testing_end() {
            var testing_out_vacuum_in = $("input[name=testing_out_vacuum_in]").val();
            var testing_out_vacuum_out = $("input[name=testing_out_vacuum_out]").val();
            var testing_out_vacuum_loop = $("input[name=testing_out_vacuum_loop]").val();

            clearInterval(WebUpdate);
            console.log('達指定溫度,乾燥完成')

            var time = new Date();
            while ((new Date() - time) < 1000) { }

            tankBlower_OFF()
            console.log('鼓風機 OFF')
            var time = new Date();
            while ((new Date() - time) < 1000) { }

            tankHeater1_OFF()
            console.log('加熱棒 1 OFF')
            var time = new Date();
            while ((new Date() - time) < 1000) { }

            tankHeater2_OFF()
            console.log('加熱棒 2 OFF')
            var time = new Date();
            while ((new Date() - time) < 1000) { }

            ChangeMotor_OFF()
            console.log('馬達停止')
            var time = new Date();
            while ((new Date() - time) < 1000) { }

            for (step = 1; step <= testing_out_vacuum_loop; step++) {
                console.log('循環第 ' + step + ' 次');

                outputVacuum_ON()
                console.log('出料真空吸料機 ON')
                var time = new Date();
                while ((new Date() - time) < testing_out_vacuum_in * 1000) { }

                if (step == 1) {
                    tankDiskValve_ON()
                    console.log('蝴蝶閥 ON')
                    var time = new Date();
                    while ((new Date() - time) < testing_out_vacuum_in * 1000) { }
                }

                outputVacuum_OFF()
                console.log('出料真空吸料機 OFF')
                var time = new Date();
                while ((new Date() - time) < testing_out_vacuum_out * 1000) { }
            }

            tankDiskValve_OFF()
            console.log('蝴蝶閥 OFF')

            var time = new Date();
            while ((new Date() - time) < 1000) { }

            location.reload()
        }

        // jQuery 更新感測器制動器狀態
        function WebUpdate(dtn) {
            $.get('/loading/D' + dtn, '', function (res) {
                if (res.tank_vacuum == 0) {
                    $("#cmn-toggle-05").prop('checked', false);
                } else if (res.tank_vacuum == 1) {
                    $("#cmn-toggle-05").prop('checked', true);
                }

                if (res.tank_threewayvalve == 0) {
                    $("#cmn-toggle-08").prop('checked', false);
                } else if (res.tank_threewayvalve == 1) {
                    $("#cmn-toggle-08").prop('checked', true);
                }

                if (res.tank_diskvalve == 0) {
                    $("#cmn-toggle-11").prop('checked', false);
                } else if (res.tank_diskvalve == 1) {
                    $("#cmn-toggle-11").prop('checked', true);
                }

                if (res.tank_solenoid_disinfect == 0) {
                    $("#cmn-toggle-14").prop('checked', false);
                } else if (res.tank_solenoid_disinfect == 1) {
                    $("#cmn-toggle-14").prop('checked', true);
                }

                if (res.tank_solenoid_water == 0) {
                    $("#cmn-toggle-17").prop('checked', false);
                } else if (res.tank_solenoid_water == 1) {
                    $("#cmn-toggle-17").prop('checked', true);
                }

                if (res.tank_blower == 0) {
                    $("#cmn-toggle-23").prop('checked', false);
                } else if (res.tank_blower == 1) {
                    $("#cmn-toggle-23").prop('checked', true);
                }

                if (res.tank_heater1 == 0) {
                    $("#cmn-toggle-26").prop('checked', false);
                } else if (res.tank_heater1 == 1) {
                    $("#cmn-toggle-26").prop('checked', true);
                }

                if (res.tank_heater2 == 0) {
                    $("#cmn-toggle-29").prop('checked', false);
                } else if (res.tank_heater2 == 1) {
                    $("#cmn-toggle-29").prop('checked', true);
                }

                if (res.tank_temp1_enable == 0) {
                    $("#cmn-toggle-35").prop('checked', false);
                } else if (res.tank_temp1_enable == 1) {
                    $("#cmn-toggle-35").prop('checked', true);
                }
                
                $("#motor_rpm_data").attr("placeholder", res.tank_motor);
                $("#tank_temp1_data").attr("placeholder", res.tank_temp1);


                $("#UltraSonic_t_status").text(res.UltraSonic);
                $("#SHT11_Temp_t_status").text(res.SHT11_Temp);
                $("#SHT11_Humidity_t_status").text(res.SHT11_Humidity);
                $("#soil_Temp_t_status").text(res.soil_Temp);
                $("#soil_Humidity_t_status").text(res.soil_Humidity);
                $("#soil_EC_t_status").text(res.soil_EC);
                $("#PA_t_status").text(res.PA);

                var float_SHT11_Temp = parseFloat(res.SHT11_Temp);
                var testing_temp_set = $("input[name=testing_tank_dry_temp]").val();

                if (float_SHT11_Temp > testing_temp_set && res.tank_blower == '1'){
                    console.log('float_SHT11_Temp:' + typeof float_SHT11_Temp + float_SHT11_Temp)
                    console.log('tank_blower:' + typeof res.tank_blower + res.tank_blower)
                    testing_end()
                };
                
            }, 'json');
        }
    </script>



    <h4>制動器</h4>
    <table border="1px solid black" style="font-size:18px;" width=100%>
        <tr>
            <td style="color:red;">Valve</td>
            <td>入料<br>真空吸料機</td>
            <td>乾燥桶<br>真空吸料機</td>
            <td>三通閥</td>
            <td>桶內消毒<br>電磁閥</td>
            <td>馬達</td>
            <td>鼓風機</td>
            <td>加熱器 1</td>
            <td>加熱器 2</td>
            <td>溫控開關</td>
            <td>溫度設定</td>
            <td>排水<br>電磁閥</td>
            <td>蝴蝶閥</td>
            <td>出料<br>真空吸料機</td>
        </tr>
        <tr>
            <td>
                <span style="color:#C0C0C0;">off</span>
                <span style="color:#008CBA;">on</span>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-01" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-01"></label>
                    </div>
                    <div class="switch text-center">
                        {% if input_vacuum== 1 %}
                        <input id="cmn-toggle-02" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="input_vacuum_status" value="ON" onclick="inputVacuum()">
                        {% else %}
                        <input id="cmn-toggle-02" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="input_vacuum_status" value="OFF" onclick="inputVacuum()">
                        {% endif %}
                        <label for="cmn-toggle-02"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-03" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-03" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-04" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-04"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_vacuum== 1 %}
                        <input id="cmn-toggle-05" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_vacuum_status" value="ON" onclick="tankVacuum()">
                        {% else %}
                        <input id="cmn-toggle-05" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_vacuum_status" value="OFF" onclick="tankVacuum()">
                        {% endif %}
                        <label for="cmn-toggle-05"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-06" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-06" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-07" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-07"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_threewayvalve== 1 %}
                        <input id="cmn-toggle-08" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_threewayvalve_status" value="ON" onclick="tankThreeWayValve()">
                        {% else %}
                        <input id="cmn-toggle-08" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_threewayvalve_status" value="OFF" onclick="tankThreeWayValve()">
                        {% endif %}
                        <label for="cmn-toggle-08"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-09" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-09" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-13" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-13"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_solenoid_disinfect== 1 %}
                        <input id="cmn-toggle-14" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_solenoid_disinfect_status" value="ON" onclick="tankSolenoidDisinfect()">
                        {% else %}
                        <input id="cmn-toggle-14" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_solenoid_disinfect_status" value="OFF" onclick="tankSolenoidDisinfect()">
                        {% endif %}

                        <label for="cmn-toggle-14"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-15" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-15" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <input id="motor_rpm_data" type="text" placeholder="{{tank_motor}}" style="width:70px;">RPM (±50)
                <span id="motor_rpm_status" style="color:red;"></span>
                <br>
                <input type="button" value="送出" name="tank_motor_status" onclick="ChangeMotor()">
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-22" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-22"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_blower== 1 %}
                        <input id="cmn-toggle-23" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_blower_status" value="ON" onclick="tankBlower()">
                        {% else %}
                        <input id="cmn-toggle-23" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_blower_status" value="OFF" onclick="tankBlower()">
                        {% endif %}
                        <label for="cmn-toggle-23"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-24" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-24" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-25" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-25"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_heater1== 1 %}
                        <input id="cmn-toggle-26" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_heater1_status" value="ON" onclick="tankHeater1()">
                        {% else %}
                        <input id="cmn-toggle-26" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_heater1_status" value="OFF" onclick="tankHeater1()">
                        {% endif %}
                        <label for="cmn-toggle-26"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-27" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-27" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-28" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-28"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_heater2== 1 %}
                        <input id="cmn-toggle-29" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_heater2_status" value="ON" onclick="tankHeater2()">
                        {% else %}
                        <input id="cmn-toggle-29" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_heater2_status" value="OFF" onclick="tankHeater2()">
                        {% endif %}
                        <label for="cmn-toggle-29"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-30" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-30" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-34" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-34"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_temp1_enable== 1 %}
                        <input id="cmn-toggle-35" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_temp1_enable_status" value="ON" onclick="tankTemp1Enable()">
                        {% else %}
                        <input id="cmn-toggle-35" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_temp1_enable_status" value="OFF" onclick="tankTemp1Enable()">
                        {% endif %}
                        <label for="cmn-toggle-35"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-36" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-36" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <input id="tank_temp1_data" type="text" placeholder="{{tank_temp1}}" style="width:70px;">℃
                <span id="tank_temp1_status" style="color:red;"></span>
                <br>
                <input type="button" value="送出" name="tank_temp1_status" onclick="ChangeTemp1()">
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-16" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-16"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_solenoid_water== 1 %}
                        <input id="cmn-toggle-17" class="cmn-toggle cmn-toggle-round-flat" checked type="checkbox"
                            name="tank_solenoid_water_status" value="ON" onclick="tankSolenoidWater()">
                        {% else %}
                        <input id="cmn-toggle-17" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_solenoid_water_status" value="OFF" onclick="tankSolenoidWater()">
                        {% endif %}
                        <label for="cmn-toggle-17"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-18" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-18" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-10" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-10"></label>
                    </div>
                    <div class="switch text-center">
                        {% if tank_diskvalve== 1 %}
                        <input id="cmn-toggle-11" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="tank_diskvalve_status" value="ON" onclick="tankDiskValve()">
                        {% else %}
                        <input id="cmn-toggle-11" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="tank_diskvalve_status" value="OFF" onclick="tankDiskValve()">
                        {% endif %}
                        <label for="cmn-toggle-11"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-12" class="cmn-toggle cmn-toggle-on-off" type="checkbox">
                        <label for="cmn-toggle-12" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
            <td>
                <div class="switch_div">
                    <div class="switch">
                        <input id="cmn-toggle-31" class="cmn-toggle cmn-toggle-round" type="checkbox">
                        <label for="cmn-toggle-31"></label>
                    </div>
                    <div class="switch text-center">
                        {% if output_vacuum== 1 %}
                        <input id="cmn-toggle-32" class="cmn-toggle cmn-toggle-round-flat" type="checkbox" checked
                            name="output_vacuum_status" value="ON" onclick="outputVacuum()">
                        {% else %}
                        <input id="cmn-toggle-32" class="cmn-toggle cmn-toggle-round-flat" type="checkbox"
                            name="output_vacuum_status" value="OFF" onclick="outputVacuum()">
                        {% endif %}
                        <label for="cmn-toggle-32"></label>
                    </div>
                    <div class="switch">
                        <input id="cmn-toggle-33" class="cmn-toggle cmn-toggle-yes-no" type="checkbox">
                        <label for="cmn-toggle-33" data-on="Yes" data-off="No"></label>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td><span style="color:#008CBA;"><b>ON</b></span></td>
            <td><input type="button" value="ON" onclick="inputVacuum_ON()"></td>
            <td><input type="button" value="ON" onclick="tankVacuum_ON()"></td>
            <td><input type="button" value="ON" onclick="tankThreeWayValve_ON()"></td>
            <td><input type="button" value="ON" onclick="tankSolenoidDisinfect_ON()"></td>
            <td><input type="button" value="ON" onclick="ChangeMotor_ON()"></td>
            <td><input type="button" value="ON" onclick="tankBlower_ON()"></td>
            <td><input type="button" value="ON" onclick="tankHeater1_ON()"></td>
            <td><input type="button" value="ON" onclick="tankHeater2_ON()"></td>
            <td><input type="button" value="ON" onclick="tankTemp1Enable_ON()"></td>
            <td><input type="button" value="ON" onclick="ChangeTemp1_ON()"></td>
            <td><input type="button" value="ON" onclick="tankSolenoidWater_ON()"></td>
            <td><input type="button" value="ON" onclick="tankDiskValve_ON()"></td>
            <td><input type="button" value="ON" onclick="outputVacuum_ON()"></td>
        </tr>
        <tr>
            <td><span style="color:#C0C0C0;"><b>OFF</b></span></td>
            <td><input type="button" value="OFF" onclick="inputVacuum_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankVacuum_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankThreeWayValve_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankSolenoidDisinfect_OFF()"></td>
            <td><input type="button" value="OFF" onclick="ChangeMotor_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankBlower_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankHeater1_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankHeater2_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankTemp1Enable_OFF()"></td>
            <td><input type="button" value="OFF" onclick="ChangeTemp1_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankSolenoidWater_OFF()"></td>
            <td><input type="button" value="OFF" onclick="tankDiskValve_OFF()"></td>
            <td><input type="button" value="OFF" onclick="outputVacuum_OFF()"></td>
        </tr>
    </table>

    <br>

    <h4>感測器</h4>
    <table style="font-size:18px; border:2px #cccccc solid;" cellpadding="10" border='1' width=100%>
        <tr>
            <td colspan="6">[入料儲豆槽] 生豆高度:{{input_UltraSonic.UltraSonic}}</td>
        </tr>
        <tr>
            <td colspan="6">[桶內] 生豆高度:<span id="UltraSonic_t_status">{{tank_UltraSonic.UltraSonic}}</span></td>
        </tr>
        <tr>
            <td style="background-color:#f3f3f3">桶內溫度</td>
            <td><span id="SHT11_Temp_t_status">{{tank_SHT11.SHT11_Temp}}</span></td>
            <td style="background-color:#f3f3f3">桶內濕度</td>
            <td><span id="SHT11_Humidity_t_status">{{tank_SHT11.SHT11_Humidity}}</span></td>
            <td style="background-color:#f3f3f3">桶內壓力</td>
            <td><span id="PA_t_status">{{tank_PA.PA}}</span></td>
        </tr>
        <tr>
            <td style="background-color:#f3f3f3">咖啡生豆溫度</td>
            <td><span id="soil_Temp_t_status">{{tank_Soil.soil_Temp}}</span></td>
            <td style="background-color:#f3f3f3">咖啡生豆濕度</td>
            <td><span id="soil_Humidity_t_status">{{tank_Soil.soil_Humidity}}</span></td>
            <td style="background-color:#f3f3f3">咖啡生豆 EC</td>
            <td><span id="soil_EC_t_status">{{tank_Soil.soil_EC}}</span></td>
        </tr>
        <tr>
            <td colspan="6">[出料儲豆槽] 生豆高度:{{output_UltraSonic.UltraSonic}}</td>
        </tr>
    </table>

    <div id="input_UltraSonic" d={{input_UltraSonic.UltraSonic}} style="display:none"></div>
    <div id="tank_Temp" d={{tank_SHT11.SHT11_Temp}} style="display:none"></div>
    <div id="tank_EC" d={{tank_SHT11.SHT11_Humidity}} style="display:none"></div>
    <div id="tank_EC" d={{tank_Soil.soil_Temp}} style="display:none"></div>
    <div id="tank_EC" d={{tank_Soil.soil_Humidity}} style="display:none"></div>
    <div id="tank_EC" d={{tank_Soil.soil_EC}} style="display:none"></div>
    <div id="tank_PA" d={{tank_PA.PA}} style="display:none"></div>
    <div id="tank_UltraSonic" d={{tank_UltraSonic.UltraSonic}} style="display:none"></div>
    <div id="output_UltraSonic" d={{output_UltraSonic.UltraSonic}} style="display:none"></div>

    <br>

    <h4>排程設計</h4>乾燥 / 入豆 / 攪拌加熱 / 出豆 / 循環測試
    <table style="font-size:18px; border:2px #cccccc solid;" cellpadding="10" border='1' width=100%>
        <tr>
            <td>乾燥</td>
            <td>入料</td>
            <td>攪拌加熱</td>
            <td>出料</td>
            <td>循環測試</td>
        </tr>
        <tr>
            <td>
                -- 入料 --<br>
                吸料時間(秒)
                <input name="testing_tank_vacuum_in" id="testing_tank_vacuum_in" value="1" type="text"
                    style="width:70px;">
                <br>
                放料時間(>10秒)
                <input name="testing_tank_vacuum_out" id="testing_tank_vacuum_out" value="10" type="text"
                    style="width:70px;">
                <br>
                循環次數
                <input name="testing_tank_vacuum_loop" id="testing_tank_vacuum_loop" value="4" type="text"
                    style="width:70px;">
                <br>
                -- 乾燥 --<br>
                乾燥時間(秒)
                <input name="testing_tank_dry_time" value="" id="testing_tank_dry_time" type="text"
                    style="width:70px;">
                <br>
                馬達 rpm
                <input name="testing_motor_rpm" id="testing_motor_rpm" value="20" type="text" style="width:70px;">
                <br>
                -- 出料 --<br>
                出料吸料時間(秒)
                <input name="testing_out_vacuum_in" id="testing_out_vacuum_in" value="1" type="text"
                    style="width:70px;">
                <br>
                出料放料時間(>10秒)
                <input name="testing_out_vacuum_out" id="testing_out_vacuum_out" value="10" type="text"
                    style="width:70px;">
                <br>
                出料循環次數
                <input name="testing_out_vacuum_loop" id="testing_out_vacuum_loop" value="4" type="text"
                    style="width:70px;">
            </td>
            <td>
                <form action="/dry_container" method="post">
                    <div class="form">
                        <div class="form-inline">
                            <label for="schedule_input_height">指定儲豆槽生豆高度</label>
                            <br>
                            <input name="schedule_input_height" id="schedule_input_height" type="text"
                                placeholder="{{schedule_input_height}}" style="width:70px;"
                                value="{{schedule_input_height}}">
                        </div>
                        <div class="form-inline">
                            <label for="schedule_input_entertime">吸料時間</label>
                            <br>
                            <input name="schedule_input_entertime" type="text"
                                placeholder="{{schedule_input_entertime}}" style="width:70px;"
                                value="{{schedule_input_entertime}}">
                        </div>
                        <div class="form-inline">
                            <label for="schedule_input_exittime">放料時間</label>
                            <br>
                            <input name="schedule_input_exittime" type="text" placeholder="{{schedule_input_exittime}}"
                                style="width:70px;" value="{{schedule_input_exittime}}">
                        </div>
                        <div class="form-inline">
                            <label for="schedule_tank_height">指定桶內生豆高度</label>
                            <br>
                            <input name="schedule_tank_height" type="text" placeholder="{{schedule_tank_height}}"
                                style="width:70px;" value="{{schedule_tank_height}}">
                        </div>
                        <div>
                            ***循環次數***
                        </div>
                        <div class="form">
                            <!--
                            <button type="button" value="送出(修改入料排程請點擊)" name="dry_coffeebeans_input" onclick="DryInput()">
                            -->

                        </div>
                    </div>
                </form>
            </td>
            <td>
                ***馬達轉速<br>
                ***指定溫度<br>
                ***指定時間<br>
            </td>
            <td>
                指定桶內空桶高度
                <input name="schedule_output_height" type="text" placeholder="40" style="width:70px;"><br>
                吸料時間
                <input name="schedule_output_enter" type="text" placeholder="10" style="width:70px;"><br>
                放料時間
                <input name="schedule_output_exit" type="text" placeholder="3" style="width:70px;"><br>
                *** 循環次數
            </td>
            <td>
                ON (開) &nbsp;
                <input name="Testing_starttime" id="Testing_starttime" type="text" style="width:50px;">
                <br>
                OFF (關) &nbsp;
                <input name="Testing_endtime" id="Testing_endtime" type="text" style="width:50px;">
                <br>
                循環&nbsp;
                <input name="Testing_loop" id="Testing_loop" type="text" style="width:50px;">
                &nbsp;次
            </td>
        <tr>
            <td>
                <button type="submit" class="btn btn-primary" onclick="F_tankDiskValve_ON()">發酵槽蝴蝶閥 ON</button><br>
                <button type="submit" class="btn btn-primary" onclick="F_tankDiskValve_OFF()">發酵槽蝴蝶閥 OFF</button><br>
                <button type="submit" class="btn btn-primary" onclick="testing()">排程測試</button>
                <script>
                    function F_tankDiskValve_ON() {
                        var data = { "tank_num": "F" + tank_num, "command": "tank_diskvalve_status", "value": "on" };
                        $.post('/mqtt/{{tid}}', data, function (res) {
                            console.log('data:', data)
                            if (res == 'on') {
                                $("#cmn-toggle-11").prop('checked', true);
                            } else if (res == 'off') {
                                $("#cmn-toggle-11").prop('checked', false);
                            } else {
                            };
                        }, 'text')


                    };

                    function F_tankDiskValve_OFF() {
                        var data = { "tank_num": "F" + tank_num, "command": "tank_diskvalve_status", "value": "off" };
                        $.post('/mqtt/{{tid}}', data, function (res) {
                            console.log('data:', data)
                            if (res == 'on') {
                                $("#cmn-toggle-11").prop('checked', true);
                            } else if (res == 'off') {
                                $("#cmn-toggle-11").prop('checked', false);
                            } else {
                            };
                        }, 'text')


                    };


                    function testing() {
                        clearInterval(WebUpdate);
                        var testing_tank_vacuum_in = $("input[name=testing_tank_vacuum_in]").val();
                        var testing_tank_vacuum_out = $("input[name=testing_tank_vacuum_out]").val();
                        var testing_tank_vacuum_loop = $("input[name=testing_tank_vacuum_loop]").val();

                        var testing_tank_dry_time = $("input[name=testing_tank_dry_time]").val();
                        var testing_motor_rpm = $("input[name=testing_motor_rpm]").val();
                        var testing_tank_dry_temp = $("input[name=testing_tank_dry_temp]").val();

                        for (step = 1; step <= testing_tank_vacuum_loop; step++) {
                            console.log('循環第 ' + step + ' 次');

                            tankVacuum_ON()
                            console.log('真空吸料機 ON')

                            var time = new Date();
                            while ((new Date() - time) < testing_tank_vacuum_in * 1000) { }

                            if (step == 1) {
                                var data = { "tank_num": "F" + tank_num, "command": "tank_diskvalve_status", "value": "on" };
                                $.post('/mqtt/{{tid}}', data, function (res) {
                                    console.log('data:', data)
                                    if (res == 'on') {
                                        $("#cmn-toggle-11").prop('checked', true);
                                    } else if (res == 'off') {
                                        $("#cmn-toggle-11").prop('checked', false);
                                    } else {
                                    };
                                }, 'text')
                                console.log('發酵槽蝴蝶閥 ON')
                            }

                            var time = new Date();
                            while ((new Date() - time) < testing_tank_vacuum_in * 1000) { }

                            tankVacuum_OFF()
                            console.log('真空吸料機 OFF')

                            var time = new Date();
                            while ((new Date() - time) < testing_tank_vacuum_out * 1000) { }
                        }

                        tankBlower_ON()
                        console.log('鼓風機 ON')

                        var time = new Date();
                        while ((new Date() - time) < 1000) { }

                        tankHeater1_ON()
                        console.log('加熱棒 1 ON')

                        var time = new Date();
                        while ((new Date() - time) < 1000) { }

                        tankHeater2_ON()
                        console.log('加熱棒 2 ON')

                        var time = new Date();
                        while ((new Date() - time) < 1000) { }

                        var data = { "tank_num": "D" + tank_num, "command": "tank_motor_status", "value": testing_motor_rpm };
                        $.post('/mqtt/{{tid}}', data, function (res) {
                            console.log('data:', data)
                            if (res == 'on') {
                                var timer = setInterval(Rotate, 60000);
                            } else if (res == 'off') {
                                clearInterval(timer);
                                $("#motor_rpm_status").text('');
                            } else {
                            };
                        }, 'text')
                        console.log('馬達 ' + testing_motor_rpm + ' rpm')

                        var time = new Date();
                        while ((new Date() - time) < 1000) { }

                        console.log('乾燥 ' + testing_tank_dry_time + ' 秒')

                        var time = new Date();
                        while ((new Date() - time) < testing_tank_dry_time * 1000) { }

                        tankTemp1Enable_ON()

                        var data = { "tank_num": "D" + tank_num, "command": "temp1", "value": testing_tank_dry_temp };
                        $.post('/mqtt/{{tid}}', data, function (res) {
                            console.log('data:', data)
                        }, 'text')
                        console.log('指定乾燥溫度 ' + testing_tank_dry_temp + ' 開始乾燥')

                        $.get('/loading/D' + dtn, '', function (res) {
                            console.log("res.SHT11_Temp:" + res.SHT11_Temp)
                            console.log("res.tank_blower:" + res.tank_blower)
                        }, 'json');
                    }

                </script>
<!--
                {% if num_SHT11_temp >= 9999 %}
                {% if tank_blower == 1 %}
                <script>
                    
                    testing_end()
                </script>
                {% endif %}
                {% endif %}
-->
            </td>
            <td>
                <button type="submit" class="btn btn-primary" onclick="DryInput()">開始入料</button>
                <script>
                    // 網頁一開始應該是 Waiting?
                    var DRY_STATE = 'Waiting'

                    function DryInput() {
                        clearInterval(WebUpdate);
                        console.log('開始入料按鈕按下')
                        /*
                        var schedule_input_height = $("input[name=schedule_input_height]").val();
                        var schedule_input_entertime = $("input[name=schedule_input_entertime]").val();
                        var schedule_input_exittime = $("input[name=schedule_input_exittime]").val();
                        var schedule_tank_height = $("input[name=schedule_tank_height]").val();
                        Q = "儲豆槽生豆高度 " + schedule_input_height + " 公分\n" +
                            "真空吸料機吸料時間 " + schedule_input_entertime + " 秒\n" +
                            "真空吸料機放料時間 " + schedule_input_exittime + " 秒\n" +
                            "乾燥桶生豆入料高度 " + schedule_tank_height + " 公分\n" +
                            "\n按下確定, 即開始乾燥入料"
            
                        if (confirm(Q)) {
                            var DRY_STATE = 'Operating';
                            var input_UltraSonic = document.getElementById('input_UltraSonic').getAttribute('d');
                            var tank_UltraSonic = document.getElementById('tank_UltraSonic').getAttribute('d');
                            var tank_EC = document.getElementById('tank_EC').getAttribute('d');
                            var tank_Temp = document.getElementById('tank_Temp').getAttribute('d');
                            var tank_PA = document.getElementById('tank_PA').getAttribute('d');
                            var output_UltraSonic = document.getElementById('output_UltraSonic').getAttribute('d');
            
                            // 如果 入料除豆槽高度 <= 指定高度  
                            if (input_UltraSonic >= schedule_input_height) {
                                if (DRY_STATE == 'Cleaning') {
                                    alert('乾燥桶目前 清洗中, 無法入料')
            
                                } else if (DRY_STATE == 'Calibrating') {
                                    alert('乾燥桶目前 校正中, 無法入料')
            
                                } else if (DRY_STATE == 'Operating' | DRY_STATE == 'Waiting') {
            
                                    // 設定狀態, 有 Operating/Cleaning/Calibrating/Waiting
                                    alert('乾燥貨櫃之入料儲豆槽達指定高度, 開始入料進乾燥桶')
            
                                    // 乾燥桶三通管舵機 ON
                                    var data = { "tank_num": "D" + tank_num, "command": "tank_threewayvalve_status", "value": "on" };
                                    console.log('data:' + data)
                                    $.post('/mqtt/{{tid}}', data, function (res) {
                                        console.log('data:', data)
                                        console.log('res:', res)
                                        if (res == 'on') {
                                            $("#cmn-toggle-08").prop('checked', true);
                                            setTimeout("alert('三通管舵機入料_開啟成功!')", 500);
                                        } else if (res == 'off') {
                                            $("#cmn-toggle-08").prop('checked', false);
                                            setTimeout("alert('三通管舵機排氣_關閉成功!')", 500);
                                        } else {
                                            alert(res);
                                        };
                                    }, 'text')
                                    
                                    setTimeout(function(){ location.reload(); }, 500); 
                                    console.log('乾燥桶三通管舵機 ON')
                                    
            
                                    // 暫停 5 秒
                                    var time = new Date();
                                    while ((new Date() - time) < 5000) { }
            
                                    // 乾燥桶蝴蝶閥 OFF
                                    var data = { "tank_num": "D" + tank_num, "command": "tank_diskvalve_status", "value": "off" };
                                    console.log('data:' + data)
                                    $.post('/mqtt/{{tid}}', data, function (res) {
                                        console.log('data:', data)
                                        console.log('res:', res)
                                        if (res == 'on') {
                                            $("#cmn-toggle-11").prop('checked', true);
                                            setTimeout("alert('乾燥桶蝴蝶閥_開啟成功!')", 500);
                                        } else if (res == 'off') {
                                            $("#cmn-toggle-11").prop('checked', false);
                                            setTimeout("alert('乾燥桶蝴蝶閥_關閉成功!')", 500);
                                        } else {
                                            alert(res);
                                        };
                                    }, 'text')
            
                                    setTimeout(function(){ location.reload(); }, 500);
                                    console.log('乾燥桶蝴蝶閥 OFF')
            
                                    // 暫停 5 秒
                                    var time = new Date();
                                    while ((new Date() - time) < 5000) { }
            
                                    // 馬達(set 6 RPM) ON
                                    var data = { "tank_num": "D" + tank_num, "command": "tank_motor_status", "value": "6" };
                                    console.log('data:', data)
                                    $.post('/mqtt/{{tid}}', data, function (res) {
                                        console.log('data:', data)
                                        console.log('res:', res)
                                        if (res == 'on') {
                                            //$("#cmn-toggle-14").prop('checked', true); // prop 設置元素屬型與元素值, 設定 checked 屬性為 true
                                            setTimeout("alert('開啟成功!')", 500); // 設定時間執行函式 delay(500), 只執行一次
                                            var timer = setInterval(Peeling, 60000); // 啟動後會在 60000 毫秒(更新時間一分鐘)內不斷執行 (原因須配合更新時間?)
            
                                        } else if (res == 'off') {
                                            //$("#cmn-toggle-14").prop('checked', false);
                                            setTimeout("alert('關閉成功!')", 500);
                                            clearInterval(timer);                   // 取消 timer 的不斷執行
                                            $("#motor_rpm_status").text('');        // 設置 #motor_rpm_status 的文字為空, #井字號
                                        } else {
                                            alert(res);
                                        };
                                    }, 'text')
            
                                    setTimeout(function(){ location.reload(); }, 500);
                                    console.log('馬達(set 6 RPM) ON')
            
                                    // 暫停 5 秒
                                    var time = new Date();
                                    while ((new Date() - time) < 5000) { }
            
                                    while (tank_UltraSonic > schedule_tank_height) {
                                        // 乾燥桶真空吸料機 OFF
                                        var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": "off" };
                                        console.log('data:', data)
                                        $.post('/mqtt/{{tid}}', data, function (res) {
                                            if (res == 'on') {
                                                $("#cmn-toggle-05").prop('checked', true);
                                                setTimeout("alert('真空吸料機_開啟成功!')", 500);
                                            } else if (res == 'off') {
                                                $("#cmn-toggle-05").prop('checked', false);
                                                setTimeout("alert('真空吸料機_關閉成功!')", 500);
                                            } else {
                                                alert(res);
                                            };
                                        }, 'text')
            
                                        setTimeout(function(){ location.reload(); }, 500);
                                        console.log('乾燥桶真空吸料機 OFF')
            
                                        // 指定放料秒數
                                        var time = new Date();
                                        while ((new Date() - time) < (schedule_input_exittime * 1000)) { }
            
                                        // 乾燥桶真空吸料機 ON
                                        var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": "on" };
                                        console.log('data:', data)
                                        $.post('/mqtt/{{tid}}', data, function (res) {
                                            if (res == 'on') {
                                                $("#cmn-toggle-05").prop('checked', true);
                                                setTimeout("alert('真空吸料機_開啟成功!')", 500);
                                            } else if (res == 'off') {
                                                $("#cmn-toggle-05").prop('checked', false);
                                                setTimeout("alert('真空吸料機_關閉成功!')", 500);
                                            } else {
                                                alert(res);
                                            };
                                        }, 'text')
            
                                        setTimeout(function(){ location.reload(); }, 500);
                                        console.log('乾燥桶真空吸料機 ON')
            
                                        // 指定吸料秒數
                                        var time = new Date();
                                        while ((new Date() - time) < (schedule_input_entertime * 1000)) { }
            
                                    }
            
            
                                }
            
                                // 乾燥桶真空吸料機 OFF
                                var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": "off" };
                                console.log('data:', data)
                                $.post('/mqtt/{{tid}}', data, function (res) {
                                    if (res == 'on') {
                                        $("#cmn-toggle-05").prop('checked', true);
                                        setTimeout("alert('真空吸料機_開啟成功!')", 500);
                                    } else if (res == 'off') {
                                        $("#cmn-toggle-05").prop('checked', false);
                                        setTimeout("alert('真空吸料機_關閉成功!')", 500);
                                    } else {
                                        alert(res);
                                    };
                                }, 'text')
            
                                setTimeout(function(){ location.reload(); }, 500);
                                console.log('乾燥桶真空吸料機 OFF')
                                alert('乾燥桶放料完成')
            
                            } else {
                                alert('儲豆槽未滿, 請在入料除豆槽放入咖啡豆')
                            }
            
                            var DRY_STATE = 'Waiting';
            
                        } else {
                            alert('已取消')
                        }
                        */
                    }
                </script>
            </td>
            <td>攪拌加熱</td>
            <td>
                <input type="button" value="送出(修改出料排程請點擊)" name="dry_coffeebeans_output" onclick="DryOutput()">
                <script>
                    // 網頁一開始應該是 Waiting?
                    var DRY_STATE = 'Waiting'

                    function DryOutput() {
                        clearInterval(WebUpdate);
                        var schedule_output_height = $("input[name=schedule_output_height]").val();
                        var schedule_output_enter = $("input[name=schedule_output_enter]").val();
                        var schedule_output_exit = $("input[name=schedule_output_exit]").val();
                        Q = "出料儲豆槽生豆高度 " + schedule_output_height + " 公分\n" +
                            "真空吸料機吸料時間 " + schedule_output_enter + " 秒\n" +
                            "真空吸料機放料時間 " + schedule_output_exit + " 秒\n" +
                            "\n按下確定, 即開始乾燥入料"

                        if (confirm(Q)) {
                            var DRY_STATE = 'Operating';
                            var input_UltraSonic = document.getElementById('input_UltraSonic').getAttribute('d');
                            var tank_UltraSonic = document.getElementById('tank_UltraSonic').getAttribute('d');
                            var tank_EC = document.getElementById('tank_EC').getAttribute('d');
                            var tank_Temp = document.getElementById('tank_Temp').getAttribute('d');
                            var tank_PA = document.getElementById('tank_PA').getAttribute('d');
                            var output_UltraSonic = document.getElementById('output_UltraSonic').getAttribute('d');

                            // 如果 出料除豆槽高度 >= 指定高度  
                            if (output_UltraSonic >= schedule_output_height) {
                                if (DRY_STATE == 'Cleaning') {
                                    alert('乾燥桶目前 清洗中, 無法出料')

                                } else if (DRY_STATE == 'Calibrating') {
                                    alert('乾燥桶目前 校正中, 無法出料')

                                } else if (DRY_STATE == 'Operating' | DRY_STATE == 'Waiting') {

                                    // 設定狀態, 有 Operating/Cleaning/Calibrating/Waiting
                                    alert('乾燥貨櫃之出料儲豆槽未達指定高度, 開始出料')

                                    // 乾燥桶三通管舵機 ON
                                    var data = { "tank_num": "D" + tank_num, "command": "tank_threewayvalve_status", "value": "on" };
                                    /*
                                    $.post('/mqtt/{{tid}}', data, function (res) {
                                        console.log('data:', data)
                                        console.log('res:', res)
                                        if (res == 'on') {
                                            $("#cmn-toggle-08").prop('checked', true);
                                            setTimeout("alert('三通管舵機入料_開啟成功!')", 500);
                                        } else if (res == 'off') {
                                            $("#cmn-toggle-08").prop('checked', false);
                                            setTimeout("alert('三通管舵機排氣_關閉成功!')", 500);
                                        } else {
                                            alert(res);
                                        };
                                    }, 'text')
            
                                    setTimeout(function(){ location.reload(); }, 500);
                                    */

                                    setTimeout('', 5000);  // 間隔 5 秒, 看看執行效果

                                    // 乾燥桶蝴蝶閥 OFF
                                    var data = { "tank_num": "D" + tank_num, "command": "tank_diskvalve_status", "value": "off" };
                                    /*
                                    $.post('/mqtt/{{tid}}', data, function (res) {
                                        console.log('data:', data)
                                        console.log('res:', res)
                                        if (res == 'on') {
                                            $("#cmn-toggle-11").prop('checked', true);
                                            setTimeout("alert('乾燥桶蝴蝶閥_開啟成功!')", 500);
                                        } else if (res == 'off') {
                                            $("#cmn-toggle-11").prop('checked', false);
                                            setTimeout("alert('乾燥桶蝴蝶閥_關閉成功!')", 500);
                                        } else {
                                            alert(res);
                                        };
                                    }, 'text')
            
                                    setTimeout(function(){ location.reload(); }, 500);
                                    */

                                    setTimeout('', 5000);  // 間隔 5 秒, 看看執行效果

                                    // 馬達(set 6 RPM) ON
                                    var data = { "tank_num": "D" + tank_num, "command": "tank_motor_status", "value": "on", "volume": "6" };
                                    /*
                                    $.post('/mqtt/{{tid}}', data, function (res) {
                                        console.log('data:', data)
                                        console.log('res:', res)
                                        if (res == 'on') {
                                            //$("#cmn-toggle-14").prop('checked', true); // prop 設置元素屬型與元素值, 設定 checked 屬性為 true
                                            setTimeout("alert('開啟成功!')", 500); // 設定時間執行函式 delay(500), 只執行一次
                                            var timer = setInterval(Peeling, 60000); // 啟動後會在 60000 毫秒(更新時間一分鐘)內不斷執行 (原因須配合更新時間?)
            
                                        } else if (res == 'off') {
                                            //$("#cmn-toggle-14").prop('checked', false);
                                            setTimeout("alert('關閉成功!')", 500);
                                            clearInterval(timer);                   // 取消 timer 的不斷執行
                                            $("#motor_rpm_status").text('');        // 設置 #motor_rpm_status 的文字為空, #井字號
                                        } else {
                                            alert(res);
                                        };
                                    }, 'text')
            
                                    setTimeout(function(){ location.reload(); }, 500);
                                    */

                                    setTimeout('', 5000);  // 間隔 5 秒, 看看執行效果

                                    /*
                                    while (output_UltraSonic >= schedule_output_height) {
            
                                        // 乾燥桶真空吸料機 OFF
                                        var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": "off" };
                                        console.log('data:', data)
                                        $.post('/mqtt/{{tid}}', data, function (res) {
                                            if (res == 'on') {
                                                $("#cmn-toggle-05").prop('checked', true);
                                                setTimeout("alert('真空吸料機_開啟成功!')", 500);
                                            } else if (res == 'off') {
                                                $("#cmn-toggle-05").prop('checked', false);
                                                setTimeout("alert('真空吸料機_關閉成功!')", 500);
                                            } else {
                                                alert(res);
                                            };
                                        }, 'text')
            
                                        setTimeout(function(){ location.reload(); }, 500);
            
                                        setTimeout('', schedule_output_exit * 1000);
            
                                        // 乾燥桶真空吸料機 ON
                                        var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": "on" };
                                        console.log('data:', data)
                                        $.post('/mqtt/{{tid}}', data, function (res) {
                                            if (res == 'on') {
                                                $("#cmn-toggle-05").prop('checked', true);
                                                setTimeout("alert('真空吸料機_開啟成功!')", 500);
                                            } else if (res == 'off') {
                                                $("#cmn-toggle-05").prop('checked', false);
                                                setTimeout("alert('真空吸料機_關閉成功!')", 500);
                                            } else {
                                                alert(res);
                                            };
                                        }, 'text')
            
                                        setTimeout(function(){ location.reload(); }, 500);
            
                                        setTimeout('', schedule_output_enter * 1000);
            
                                    }
            
                                    // 乾燥桶真空吸料機 OFF
                                    var data = { "tank_num": "D" + tank_num, "command": "tank_vacuum_status", "value": "off" };
                                    console.log('data:', data)
                                    $.post('/mqtt/{{tid}}', data, function (res) {
                                        if (res == 'on') {
                                            $("#cmn-toggle-05").prop('checked', true);
                                            setTimeout("alert('真空吸料機_開啟成功!')", 500);
                                        } else if (res == 'off') {
                                            $("#cmn-toggle-05").prop('checked', false);
                                            setTimeout("alert('真空吸料機_關閉成功!')", 500);
                                        } else {
                                            alert(res);
                                        };
                                    }, 'text')
            
                                    setTimeout(function(){ location.reload(); }, 500);
                                    */

                                    alert('乾燥桶放料完成')

                                } else {

                                }

                            } else {
                                alert('儲豆槽未滿, 請在入料除豆槽放入咖啡豆')
                            }

                            var DRY_STATE = 'Waiting';

                        } else {
                            alert('已取消')
                        }
                    }
                </script>
            </td>
            <td>
                <button type="submit" class="btn btn-primary" onclick="diskValveTest()">蝴蝶閥測試(分)</button><br>
                <button type="submit" class="btn btn-primary" onclick="tankVacuumTest()">真空吸料機測試(秒)</button><br>
                <button type="submit" class="btn btn-primary" onclick="threewayvalveTest()">三通閥測試(秒)</button>

                <script>
                    function threewayvalveTest() {
                        clearInterval(WebUpdate);
                        var Testing_starttime = $("input[name=Testing_starttime]").val();
                        var Testing_endtime = $("input[name=Testing_endtime]").val();
                        var Testing_loop = $("input[name=Testing_loop]").val();
                        alert('測試間隔' + Testing_starttime + ':' + Testing_endtime + ' 次數' + Testing_loop)
                        var step;
                        for (step = 1; step <= Testing_loop; step++) {
                            console.log('循環第 ' + step + ' 次');

                            tankVacuum_ON()
                            console.log('tankVacuum_ON')

                            var time = new Date();
                            while ((new Date() - time) < Testing_starttime * 1000 * 60) { }

                            tankVacuum_OFF()
                            console.log('tankVacuum_OFF')

                            var time = new Date();
                            while ((new Date() - time) < Testing_endtime * 1000 * 60) { }
                        }

                        location.reload()
                    }
                </script>

                <script>
                    function tankVacuumTest() {
                        clearInterval(WebUpdate);
                        var Testing_starttime = $("input[name=Testing_starttime]").val();
                        var Testing_endtime = $("input[name=Testing_endtime]").val();
                        var Testing_loop = $("input[name=Testing_loop]").val();
                        alert('測試間隔' + Testing_starttime + ':' + Testing_endtime + ' 次數' + Testing_loop)
                        var step;

                        for (step = 1; step <= Testing_loop; step++) {
                            console.log('循環第 ' + step + ' 次');

                            tankVacuum_ON()
                            console.log('tankVacuum_ON')

                            var time = new Date();
                            while ((new Date() - time) < Testing_starttime * 1000) { }

                            tankVacuum_OFF()
                            console.log('tankVacuum_OFF')

                            var time = new Date();
                            while ((new Date() - time) < Testing_endtime * 1000) { }
                        }
                    }
                </script>

                <script>
                    function diskValveTest() {
                        clearInterval(WebUpdate);
                        var Testing_starttime = $("input[name=Testing_starttime]").val();
                        var Testing_endtime = $("input[name=Testing_endtime]").val();
                        var Testing_loop = $("input[name=Testing_loop]").val();
                        alert('測試間隔' + Testing_starttime + ':' + Testing_endtime + ' 次數' + Testing_loop)
                        var step;
                        for (step = 1; step <= Testing_loop; step++) {
                            console.log('循環第 ' + step + ' 次');

                            tankDiskValve_ON()
                            console.log('tankDiskValve_ON')

                            var time = new Date();
                            while ((new Date() - time) < Testing_starttime * 1000 * 60) { }

                            tankDiskValve_OFF()
                            console.log('tankDiskValve_OFF')

                            var time = new Date();
                            while ((new Date() - time) < Testing_endtime * 1000 * 60) { }
                        }
                    }
                </script>
            </td>
        </tr>
    </table>

    <br>

    <h4>M5 攝影機畫面 img</h4>
    <div class="container-fluid">
        <div class="row" style="margin-top:100px;">
            <div class="col flex">
                <div class="col-md-12 row flex" style="text-align:center;margin-top:5px;">
                    <div class="col-md-12 row flex">
                        <img src="" id="four" width="70%" height="70%">
                    </div>
                </div>
                <script>
                    // M5 攝影機

                    //const img = document.querySelector('#one');
                    const img3 = document.querySelector('#four');
                    const WS_URL3 = 'ws:///60.250.156.230:8091';
                    const ws3 = new WebSocket(WS_URL3);
                    let urlObject3;
                    ws3.onopen = () => console.log(`Connected to ${WS_URL3}`);
                    ws3.onmessage = message3 => {
                        const arrayBuffer3 = message3.data;
                        if (urlObject3) {
                            URL.revokeObjectURL(urlObject3);
                        }
                        urlObject3 = URL.createObjectURL(new Blob([arrayBuffer3]));
                        img3.src = urlObject3;
                    }
                </script>
            </div>
        </div>
    </div>

</body>

</html>