<!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">
    <!-- <meta http-equiv="refresh" content="5" />  每 content 秒網頁自動更新-->

    <!-- 新 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">
        // 指定 秒 刷新網頁一次
        var ftn = '{{tid}}';
        var status = '{{status}}';
        console.log('ftn:' + ftn)

        $(function(){
            $("#ferment_output_page").text('發酵貨櫃出料儲豆槽 FO' + ftn + ' 攝影機畫面   ')
            $('#ferment_output_page').attr("href", "/camera_FO" + ftn)
            $("#ferment_container_output_title").text('發酵貨櫃出料儲豆槽 FO' + ftn + ' 操作介面')
            $("#coffee_title_pc").text('FO' + ftn + ' 發酵出料儲豆槽操作介面');
            $("#coffee_title_phone").text('FO' + ftn + ' 操作介面');

            if (status == 2) {
                $('input').prop('disabled', true);
                $('button').prop('disabled', true);
            } else if ( (status == 0) || (status == 1) ) {
                $('input').prop('disabled', false);
                $('button').prop('disabled', false);   
            }
        });

        WebUpdate_set = setInterval(function(){WebUpdate(ftn)} , 10 * 1000)
    </script>

    <script>
        // 發酵槽_制動器控制

        var tank_num = '{{tid}}';

        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": "FO" + 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(params){
            if (params == '1') {
                var data = { "tank_num": "DO" + tank_num, "command": "output_vacuum_status", "value": "on" };
            } else if (params == '0') {
                var data = { "tank_num": "DO" + tank_num, "command": "output_vacuum_status", "value": "off" };
            } else {
                var data = { "tank_num": "DO" + tank_num, "command": "output_vacuum_status", "value": params };
            }
            $.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 {
                    console.log('res error')
                };
            }, 'text')
        }

        function tankDiskValve(params) {
            if (params == '1') {
                var data = { "tank_num": "F" + tank_num, "command": "tank_diskvalve_status", "value": "on" };
            } else if (params == '0') {
                var data = { "tank_num": "F" + tank_num, "command": "tank_diskvalve_status", "value": "off" };
            } else {
                var data = { "tank_num": "F" + tank_num, "command": "tank_diskvalve_status", "value": params };
            }
            $.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')
        }

    </script>

    <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(ftn)} , webupdate_time * 1000);
        }

        // jQuery 更新感測器制動器狀態
        function WebUpdate(ftn) {

            $.get('/loading/FO' + ftn, '', function (res) {
                $("#LiDAR_t_status").text(res.LiDAR);
                
            }, 'json');
        }
    </script>

</head>

<body>
    <div id="wrapper">
        <div id="coffee_header">
            <!-- 匯入共同使用的 header.html 內容 -->
            {% include 'header.html' %}
        </div>
        
        <h4>制動器</h4>
        <table border="1" class="table-all" cellpadding="3" style="text-align: center;">
            <tr>
                <td style="color:red;">Valve</td>
                <td>真空吸料機</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 type="checkbox" class="cmn-toggle cmn-toggle-round" id="cmn-toggle-31">
                        </div>
                        <div class="switch text-center">
                            {% if output_vacuum == 1 %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-32" checked
                                name="output_vacuum_status" value="ON" onclick="outputVacuum()">
                            {% else %}
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round-flat" id="cmn-toggle-32"
                                name="output_vacuum_status" value="OFF" onclick="outputVacuum()">
                            {% endif %}
                            <label class="label_ActuatorStatus" for="cmn-toggle-32"></label>
                        </div>
                        <div class="switch">
                            <input type="checkbox" class="cmn-toggle cmn-toggle-round" id="cmn-toggle-33">
                        </div>
                    </div>
                </td>
            </tr>
            <tr>
                <td><span style="color:#008CBA;"><b>ON</b></span></td>
                <td><input type="button" class="input-button-on" value="ON" onclick="outputVacuum('1')"></td>
            </tr>
            <tr>
                <td><span style="color:#C0C0C0;"><b>OFF</b></span></td>
                <td><input type="button" class="input-button-off" value="OFF" onclick="outputVacuum('0')"></td>
            </tr>
        </table>

        <br>

        <h4>感測器</h4>
        <table border="1" class="table-all" cellpadding="3">
            <tr>
                <td>[出料儲豆槽] 生豆高度：<span id="UltraSonic_t_status">{{output_UltraSonic.UltraSonic}}</span> 公分</td>
            </tr>
        </table>

        <br>

        <h4>排程設計</h4>
        <table border="1" class="table-all" cellpadding="3">
            <tr>
                <td>入料</td>
                <td>循環測試</td>
            </tr>
            <tr>
                <td>
                    <!--
                    [桶內 F1] 生豆高度：&nbsp;<span id="LiDAR_t_status">{{tank_LiDAR.LiDAR}}</span>&nbsp;公分<br>
                    指定空桶高度
                    <input name="testing_tank_empty_height" id="testing_tank_empty_height" type="text" value="0" style="width: 40px;">公分<br>
                    蝴蝶閥 ON
                    <br>
                    -->
                    生豆高度
                    <input type="text" class="input-text" name="testing_BeanIn_Height" value="0">公分<br>
                    吸料                   
                    <input type="text" class="input-text" name="testing_out_vacuum_in" value="1">秒<br>
                    放料(>10)
                    <input type="text" class="input-text" name="testing_out_vacuum_out" value="10">秒<br>
                    循環次數
                    <input type="text" class="input-text" name="testing_out_vacuum_loop" value="3">次
                </td>
                <td>
                    ON (開)
                    <input type="text" class="input-text" name="Testing_starttime" value="3">秒<br>
                    OFF (關)
                    <input type="text" class="input-text" name="Testing_endtime" value="5">秒<br>
                    循環
                    <input type="text" class="input-text" name="Testing_loop" value="3">次
                </td>
            </tr>
            <tr>
                <td>
                    <button type="submit" class="btn btn-primary" onclick="CoffeeOut_testing()">排水出豆</button>
                    <script>
                        var CoffeeOut_process = 0;
                        function CoffeeOut_testing() {
                            var testing_BeanIn_Height = $("input[name=testing_BeanIn_Height]").val();
                            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();

                            if (testing_BeanIn_Height != 0){

                                console.log('指定高度 ' + testing_BeanIn_Height + ' 入豆')
                                
                                $.get('/ferment_LiDAR_' + tank_num, '', function (res) {
                                    if ( parseInt(res.LiDAR) > parseInt(testing_BeanIn_Height) ){
                                        tankDiskValve('1')
                                        console.log('tankDiskValve_ON')
                                    };
                                }, 'json');

                                var CoffeeOut_interval = setInterval(tankEmptyHeight, 5000);

                                function tankEmptyHeight(){
                                    if (CoffeeOut_process == 1) {
                                        return;
                                    }

                                    CoffeeOut_process = 1 ;
                                    $.ajax({
                                        type:"GET",
                                        url:"/ferment_LiDAR_" + tank_num,
                                        dataType:"json",
                                        success:function (response) {
                                            var present_Coffee_height = response.LiDAR
                                            console.log("目前桶內生豆高度: ", present_Coffee_height)
                                            console.log("指定空桶生豆高度: ", testing_BeanIn_Height)

                                            if( parseInt(present_Coffee_height) < parseInt(testing_BeanIn_Height)){
                                                clearInterval(CoffeeOut_interval);
                                                console.log("桶內已淨空！")
                                                CoffeeOut_process = 0;

                                            } else {
                                                outputVacuum('1');
                                                console.log('outputVacuum_ON')

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

                                                outputVacuum('0')
                                                console.log('outputVacuum_OFF')

                                                var time = new Date();
                                                while ((new Date() - time) < testing_out_vacuum_out * 1000) { };
                                                CoffeeOut_process = 0;
                                            }
    
                                        },
                                        error:function(thrownError){
                                            console.log("Error: " + thrownError)
                                            CoffeeIn_Process = 0;
                                        }
                                    })

                                }

                                tankDiskValve('0')
                                console.log('tankDiskValve_OFF')



                            } else if ( testing_BeanIn_Height == 0){

                                console.log('循環方式入豆')

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

                                    outputVacuum('1');
                                    console.log('outputVacuum_ON')

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

                                    if (step == 1) {
                                        tankDiskValve('1');
                                        console.log('tankDiskValve_ON')

                                        var time = new Date();
                                        while ((new Date() - time) < testing_out_vacuum_in * 1000) { };
                                    }
                                    outputVacuum('0');
                                    console.log('outputVacuum_OFF')

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

                                tankDiskValve('0');
                                console.log('tankDiskValve_OFF')

                            };

                        };
                    </script>
                </td>
                <td>
                    <button type="submit" class="btn btn-primary" onclick="tankVacuumTest()">真空吸料機測試(秒)</button><br>
                    <script>
                        function tankVacuumTest() {
                            clearInterval(WebUpdate_set);
                            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 + ' 次');

                                outputVacuum('1')
                                console.log('outputVacuum_ON')

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

                                outputVacuum('0')
                                console.log('outputVacuum_OFF')

                                var time = new Date();
                                while ((new Date() - time) < Testing_endtime * 1000) { }
                            }
                            WebUpdate_set = setInterval(function(){WebUpdate(ftn)} , 10 * 1000);
                        }
                    </script>
                </td>
            </tr>
        </table>

        <div id="coffee_footer">
            <!-- 匯入共同使用的 footer.html 內容 -->
            {% include 'footer.html' %}
        </div>
    </div>
    
</body>

</html>